cmTestGenerator.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. os << indent << "set_tests_properties(" << this->Test->GetName()
  103. << " PROPERTIES ";
  104. for (auto const& i : this->Test->GetProperties().GetList()) {
  105. os << " " << i.first << " "
  106. << cmOutputConverter::EscapeForCMake(
  107. ge.Parse(i.second)->Evaluate(this->LG, config));
  108. }
  109. this->GenerateInternalProperties(os);
  110. os << ")" << std::endl;
  111. }
  112. void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent)
  113. {
  114. os << indent << "add_test(" << this->Test->GetName() << " NOT_AVAILABLE)\n";
  115. }
  116. bool cmTestGenerator::NeedsScriptNoConfig() const
  117. {
  118. return (this->TestGenerated && // test generated for at least one config
  119. this->ActionsPerConfig && // test is config-aware
  120. this->Configurations.empty() && // test runs in all configs
  121. !this->ConfigurationTypes->empty()); // config-dependent command
  122. }
  123. void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent)
  124. {
  125. this->TestGenerated = true;
  126. // Get the test command line to be executed.
  127. std::vector<std::string> const& command = this->Test->GetCommand();
  128. std::string exe = command[0];
  129. cmSystemTools::ConvertToUnixSlashes(exe);
  130. fout << indent;
  131. fout << "add_test(";
  132. fout << this->Test->GetName() << " \"" << exe << "\"";
  133. for (std::string const& arg : cmMakeRange(command).advance(1)) {
  134. // Just double-quote all arguments so they are re-parsed
  135. // correctly by the test system.
  136. fout << " \"";
  137. for (char c : arg) {
  138. // Escape quotes within arguments. We should escape
  139. // backslashes too but we cannot because it makes the result
  140. // inconsistent with previous behavior of this command.
  141. if (c == '"') {
  142. fout << '\\';
  143. }
  144. fout << c;
  145. }
  146. fout << "\"";
  147. }
  148. fout << ")" << std::endl;
  149. // Output properties for the test.
  150. fout << indent << "set_tests_properties(" << this->Test->GetName()
  151. << " PROPERTIES ";
  152. for (auto const& i : this->Test->GetProperties().GetList()) {
  153. fout << " " << i.first << " "
  154. << cmOutputConverter::EscapeForCMake(i.second);
  155. }
  156. this->GenerateInternalProperties(fout);
  157. fout << ")" << std::endl;
  158. }
  159. void cmTestGenerator::GenerateInternalProperties(std::ostream& os)
  160. {
  161. cmListFileBacktrace bt = this->Test->GetBacktrace();
  162. if (bt.Empty()) {
  163. return;
  164. }
  165. os << " "
  166. << "_BACKTRACE_TRIPLES"
  167. << " \"";
  168. bool prependTripleSeparator = false;
  169. while (!bt.Empty()) {
  170. const auto& entry = bt.Top();
  171. if (prependTripleSeparator) {
  172. os << ";";
  173. }
  174. os << entry.FilePath << ";" << entry.Line << ";" << entry.Name;
  175. bt = bt.Pop();
  176. prependTripleSeparator = true;
  177. }
  178. os << "\"";
  179. }