cmScriptGenerator.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmScriptGenerator.h"
  4. #include "cmSystemTools.h"
  5. cmScriptGenerator::cmScriptGenerator(
  6. const std::string& config_var,
  7. std::vector<std::string> const& configurations)
  8. : RuntimeConfigVariable(config_var)
  9. , Configurations(configurations)
  10. , ConfigurationName("")
  11. , ConfigurationTypes(nullptr)
  12. , ActionsPerConfig(false)
  13. {
  14. }
  15. cmScriptGenerator::~cmScriptGenerator()
  16. {
  17. }
  18. void cmScriptGenerator::Generate(
  19. std::ostream& os, const std::string& config,
  20. std::vector<std::string> const& configurationTypes)
  21. {
  22. this->ConfigurationName = config;
  23. this->ConfigurationTypes = &configurationTypes;
  24. this->GenerateScript(os);
  25. this->ConfigurationName.clear();
  26. this->ConfigurationTypes = nullptr;
  27. }
  28. static void cmScriptGeneratorEncodeConfig(const std::string& config,
  29. std::string& result)
  30. {
  31. for (const char* c = config.c_str(); *c; ++c) {
  32. if (*c >= 'a' && *c <= 'z') {
  33. result += "[";
  34. result += static_cast<char>(*c + 'A' - 'a');
  35. result += *c;
  36. result += "]";
  37. } else if (*c >= 'A' && *c <= 'Z') {
  38. result += "[";
  39. result += *c;
  40. result += static_cast<char>(*c + 'a' - 'A');
  41. result += "]";
  42. } else {
  43. result += *c;
  44. }
  45. }
  46. }
  47. std::string cmScriptGenerator::CreateConfigTest(const std::string& config)
  48. {
  49. std::string result = "\"${";
  50. result += this->RuntimeConfigVariable;
  51. result += "}\" MATCHES \"^(";
  52. if (!config.empty()) {
  53. cmScriptGeneratorEncodeConfig(config, result);
  54. }
  55. result += ")$\"";
  56. return result;
  57. }
  58. std::string cmScriptGenerator::CreateConfigTest(
  59. std::vector<std::string> const& configs)
  60. {
  61. std::string result = "\"${";
  62. result += this->RuntimeConfigVariable;
  63. result += "}\" MATCHES \"^(";
  64. const char* sep = "";
  65. for (std::string const& config : configs) {
  66. result += sep;
  67. sep = "|";
  68. cmScriptGeneratorEncodeConfig(config, result);
  69. }
  70. result += ")$\"";
  71. return result;
  72. }
  73. void cmScriptGenerator::GenerateScript(std::ostream& os)
  74. {
  75. // Track indentation.
  76. Indent indent;
  77. // Generate the script possibly with per-configuration code.
  78. this->GenerateScriptConfigs(os, indent);
  79. }
  80. void cmScriptGenerator::GenerateScriptConfigs(std::ostream& os, Indent indent)
  81. {
  82. if (this->ActionsPerConfig) {
  83. this->GenerateScriptActionsPerConfig(os, indent);
  84. } else {
  85. this->GenerateScriptActionsOnce(os, indent);
  86. }
  87. }
  88. void cmScriptGenerator::GenerateScriptActions(std::ostream& os, Indent indent)
  89. {
  90. if (this->ActionsPerConfig) {
  91. // This is reached for single-configuration build generators in a
  92. // per-config script generator.
  93. this->GenerateScriptForConfig(os, this->ConfigurationName, indent);
  94. }
  95. }
  96. void cmScriptGenerator::GenerateScriptForConfig(std::ostream& /*unused*/,
  97. const std::string& /*unused*/,
  98. Indent /*unused*/)
  99. {
  100. // No actions for this generator.
  101. }
  102. bool cmScriptGenerator::GeneratesForConfig(const std::string& config)
  103. {
  104. // If this is not a configuration-specific rule then we install.
  105. if (this->Configurations.empty()) {
  106. return true;
  107. }
  108. // This is a configuration-specific rule. Check if the config
  109. // matches this rule.
  110. std::string config_upper = cmSystemTools::UpperCase(config);
  111. for (std::string const& cfg : this->Configurations) {
  112. if (cmSystemTools::UpperCase(cfg) == config_upper) {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. void cmScriptGenerator::GenerateScriptActionsOnce(std::ostream& os,
  119. Indent indent)
  120. {
  121. if (this->Configurations.empty()) {
  122. // This rule is for all configurations.
  123. this->GenerateScriptActions(os, indent);
  124. } else {
  125. // Generate a per-configuration block.
  126. std::string config_test = this->CreateConfigTest(this->Configurations);
  127. os << indent << "if(" << config_test << ")\n";
  128. this->GenerateScriptActions(os, indent.Next());
  129. os << indent << "endif(" << config_test << ")\n";
  130. }
  131. }
  132. void cmScriptGenerator::GenerateScriptActionsPerConfig(std::ostream& os,
  133. Indent indent)
  134. {
  135. if (this->ConfigurationTypes->empty()) {
  136. // In a single-configuration generator there is only one action
  137. // and it applies if the runtime-requested configuration is among
  138. // the rule's allowed configurations. The configuration built in
  139. // the tree does not matter for this decision but will be used to
  140. // generate proper target file names into the code.
  141. this->GenerateScriptActionsOnce(os, indent);
  142. } else {
  143. // In a multi-configuration generator we produce a separate rule
  144. // in a block for each configuration that is built. We restrict
  145. // the list of configurations to those to which this rule applies.
  146. bool first = true;
  147. for (std::string const& cfgType : *this->ConfigurationTypes) {
  148. if (this->GeneratesForConfig(cfgType)) {
  149. // Generate a per-configuration block.
  150. std::string config_test = this->CreateConfigTest(cfgType);
  151. os << indent << (first ? "if(" : "elseif(") << config_test << ")\n";
  152. this->GenerateScriptForConfig(os, cfgType, indent.Next());
  153. first = false;
  154. }
  155. }
  156. if (!first) {
  157. if (this->NeedsScriptNoConfig()) {
  158. os << indent << "else()\n";
  159. this->GenerateScriptNoConfig(os, indent.Next());
  160. }
  161. os << indent << "endif()\n";
  162. }
  163. }
  164. }