cmTestGenerator.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "cmTestGenerator.h"
  4. #include <memory>
  5. #include <ostream>
  6. #include <utility>
  7. #include <vector>
  8. #include "cmGeneratorExpression.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmListFileCache.h"
  11. #include "cmLocalGenerator.h"
  12. #include "cmOutputConverter.h"
  13. #include "cmProperty.h"
  14. #include "cmPropertyMap.h"
  15. #include "cmRange.h"
  16. #include "cmStateTypes.h"
  17. #include "cmStringAlgorithms.h"
  18. #include "cmSystemTools.h"
  19. #include "cmTest.h"
  20. cmTestGenerator::cmTestGenerator(
  21. cmTest* test, std::vector<std::string> const& configurations)
  22. : cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations)
  23. , Test(test)
  24. {
  25. this->ActionsPerConfig = !test->GetOldStyle();
  26. this->TestGenerated = false;
  27. this->LG = nullptr;
  28. }
  29. cmTestGenerator::~cmTestGenerator() = default;
  30. void cmTestGenerator::Compute(cmLocalGenerator* lg)
  31. {
  32. this->LG = lg;
  33. }
  34. bool cmTestGenerator::TestsForConfig(const std::string& config)
  35. {
  36. return this->GeneratesForConfig(config);
  37. }
  38. cmTest* cmTestGenerator::GetTest() const
  39. {
  40. return this->Test;
  41. }
  42. void cmTestGenerator::GenerateScriptConfigs(std::ostream& os, Indent indent)
  43. {
  44. // Create the tests.
  45. this->cmScriptGenerator::GenerateScriptConfigs(os, indent);
  46. }
  47. void cmTestGenerator::GenerateScriptActions(std::ostream& os, Indent indent)
  48. {
  49. if (this->ActionsPerConfig) {
  50. // This is the per-config generation in a single-configuration
  51. // build generator case. The superclass will call our per-config
  52. // method.
  53. this->cmScriptGenerator::GenerateScriptActions(os, indent);
  54. } else {
  55. // This is an old-style test, so there is only one config.
  56. // assert(this->Test->GetOldStyle());
  57. this->GenerateOldStyle(os, indent);
  58. }
  59. }
  60. void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
  61. const std::string& config,
  62. Indent indent)
  63. {
  64. this->TestGenerated = true;
  65. // Set up generator expression evaluation context.
  66. cmGeneratorExpression ge(this->Test->GetBacktrace());
  67. // Start the test command.
  68. os << indent << "add_test(\"" << this->Test->GetName() << "\" ";
  69. // Evaluate command line arguments
  70. std::vector<std::string> argv =
  71. EvaluateCommandLineArguments(this->Test->GetCommand(), ge, config);
  72. // Expand arguments if COMMAND_EXPAND_LISTS is set
  73. if (this->Test->GetCommandExpandLists()) {
  74. argv = cmExpandedLists(argv.begin(), argv.end());
  75. // Expanding lists on an empty command may have left it empty
  76. if (argv.empty()) {
  77. argv.emplace_back();
  78. }
  79. }
  80. // Check whether the command executable is a target whose name is to
  81. // be translated.
  82. std::string exe = argv[0];
  83. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe);
  84. if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
  85. // Use the target file on disk.
  86. exe = target->GetFullPath(config);
  87. // Prepend with the emulator when cross compiling if required.
  88. cmProp emulator = target->GetProperty("CROSSCOMPILING_EMULATOR");
  89. if (cmNonempty(emulator)) {
  90. std::vector<std::string> emulatorWithArgs = cmExpandedList(*emulator);
  91. std::string emulatorExe(emulatorWithArgs[0]);
  92. cmSystemTools::ConvertToUnixSlashes(emulatorExe);
  93. os << cmOutputConverter::EscapeForCMake(emulatorExe) << " ";
  94. for (std::string const& arg : cmMakeRange(emulatorWithArgs).advance(1)) {
  95. os << cmOutputConverter::EscapeForCMake(arg) << " ";
  96. }
  97. }
  98. } else {
  99. // Use the command name given.
  100. cmSystemTools::ConvertToUnixSlashes(exe);
  101. }
  102. // Generate the command line with full escapes.
  103. os << cmOutputConverter::EscapeForCMake(exe);
  104. for (auto const& arg : cmMakeRange(argv).advance(1)) {
  105. os << " " << cmOutputConverter::EscapeForCMake(arg);
  106. }
  107. // Finish the test command.
  108. os << ")\n";
  109. // Output properties for the test.
  110. os << indent << "set_tests_properties(\"" << this->Test->GetName()
  111. << "\" PROPERTIES ";
  112. for (auto const& i : this->Test->GetProperties().GetList()) {
  113. os << " " << i.first << " "
  114. << cmOutputConverter::EscapeForCMake(
  115. ge.Parse(i.second)->Evaluate(this->LG, config));
  116. }
  117. this->GenerateInternalProperties(os);
  118. os << ")\n";
  119. }
  120. void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent)
  121. {
  122. os << indent << "add_test(\"" << this->Test->GetName()
  123. << "\" NOT_AVAILABLE)\n";
  124. }
  125. bool cmTestGenerator::NeedsScriptNoConfig() const
  126. {
  127. return (this->TestGenerated && // test generated for at least one config
  128. this->ActionsPerConfig && // test is config-aware
  129. this->Configurations.empty() && // test runs in all configs
  130. !this->ConfigurationTypes->empty()); // config-dependent command
  131. }
  132. void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent)
  133. {
  134. this->TestGenerated = true;
  135. // Get the test command line to be executed.
  136. std::vector<std::string> const& command = this->Test->GetCommand();
  137. std::string exe = command[0];
  138. cmSystemTools::ConvertToUnixSlashes(exe);
  139. fout << indent << "add_test(\"" << this->Test->GetName() << "\" \"" << exe
  140. << "\"";
  141. for (std::string const& arg : cmMakeRange(command).advance(1)) {
  142. // Just double-quote all arguments so they are re-parsed
  143. // correctly by the test system.
  144. fout << " \"";
  145. for (char c : arg) {
  146. // Escape quotes within arguments. We should escape
  147. // backslashes too but we cannot because it makes the result
  148. // inconsistent with previous behavior of this command.
  149. if (c == '"') {
  150. fout << '\\';
  151. }
  152. fout << c;
  153. }
  154. fout << '"';
  155. }
  156. fout << ")\n";
  157. // Output properties for the test.
  158. fout << indent << "set_tests_properties(" << this->Test->GetName()
  159. << " PROPERTIES ";
  160. for (auto const& i : this->Test->GetProperties().GetList()) {
  161. fout << " " << i.first << " "
  162. << cmOutputConverter::EscapeForCMake(i.second);
  163. }
  164. this->GenerateInternalProperties(fout);
  165. fout << ")\n";
  166. }
  167. void cmTestGenerator::GenerateInternalProperties(std::ostream& os)
  168. {
  169. cmListFileBacktrace bt = this->Test->GetBacktrace();
  170. if (bt.Empty()) {
  171. return;
  172. }
  173. os << " "
  174. << "_BACKTRACE_TRIPLES"
  175. << " \"";
  176. bool prependTripleSeparator = false;
  177. while (!bt.Empty()) {
  178. const auto& entry = bt.Top();
  179. if (prependTripleSeparator) {
  180. os << ";";
  181. }
  182. os << entry.FilePath << ";" << entry.Line << ";" << entry.Name;
  183. bt = bt.Pop();
  184. prependTripleSeparator = true;
  185. }
  186. os << '"';
  187. }
  188. std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments(
  189. const std::vector<std::string>& argv, cmGeneratorExpression& ge,
  190. const std::string& config) const
  191. {
  192. // Evaluate executable name and arguments
  193. auto evaluatedRange =
  194. cmMakeRange(argv).transform([&](const std::string& arg) {
  195. return ge.Parse(arg)->Evaluate(this->LG, config);
  196. });
  197. return { evaluatedRange.begin(), evaluatedRange.end() };
  198. }