cmTestGenerator.cxx 6.5 KB

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