cmScriptGenerator.cxx 5.5 KB

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