cmTestGenerator.cxx 6.4 KB

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