cmTestGenerator.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "cmLocalGenerator.h"
  9. #include "cmOutputConverter.h"
  10. #include "cmProperty.h"
  11. #include "cmPropertyMap.h"
  12. #include "cmStateTypes.h"
  13. #include "cmSystemTools.h"
  14. #include "cmTest.h"
  15. cmTestGenerator::cmTestGenerator(
  16. cmTest* test, std::vector<std::string> const& configurations)
  17. : cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations)
  18. , Test(test)
  19. {
  20. this->ActionsPerConfig = !test->GetOldStyle();
  21. this->TestGenerated = false;
  22. this->LG = nullptr;
  23. }
  24. cmTestGenerator::~cmTestGenerator()
  25. {
  26. }
  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) {
  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::vector<std::string>::const_iterator ei =
  84. emulatorWithArgs.begin() + 1;
  85. ei != emulatorWithArgs.end(); ++ei) {
  86. os << cmOutputConverter::EscapeForCMake(*ei) << " ";
  87. }
  88. }
  89. } else {
  90. // Use the command name given.
  91. exe = ge.Parse(exe.c_str())->Evaluate(this->LG, config);
  92. cmSystemTools::ConvertToUnixSlashes(exe);
  93. }
  94. // Generate the command line with full escapes.
  95. os << cmOutputConverter::EscapeForCMake(exe);
  96. for (std::vector<std::string>::const_iterator ci = command.begin() + 1;
  97. ci != command.end(); ++ci) {
  98. os << " " << cmOutputConverter::EscapeForCMake(
  99. ge.Parse(*ci)->Evaluate(this->LG, config));
  100. }
  101. // Finish the test command.
  102. os << ")\n";
  103. // Output properties for the test.
  104. cmPropertyMap& pm = this->Test->GetProperties();
  105. if (!pm.empty()) {
  106. os << indent << "set_tests_properties(" << this->Test->GetName()
  107. << " PROPERTIES ";
  108. for (auto const& i : pm) {
  109. os << " " << i.first << " "
  110. << cmOutputConverter::EscapeForCMake(
  111. ge.Parse(i.second.GetValue())->Evaluate(this->LG, config));
  112. }
  113. os << ")" << std::endl;
  114. }
  115. }
  116. void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent)
  117. {
  118. os << indent << "add_test(" << this->Test->GetName() << " NOT_AVAILABLE)\n";
  119. }
  120. bool cmTestGenerator::NeedsScriptNoConfig() const
  121. {
  122. return (this->TestGenerated && // test generated for at least one config
  123. this->ActionsPerConfig && // test is config-aware
  124. this->Configurations.empty() && // test runs in all configs
  125. !this->ConfigurationTypes->empty()); // config-dependent command
  126. }
  127. void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent)
  128. {
  129. this->TestGenerated = true;
  130. // Get the test command line to be executed.
  131. std::vector<std::string> const& command = this->Test->GetCommand();
  132. std::string exe = command[0];
  133. cmSystemTools::ConvertToUnixSlashes(exe);
  134. fout << indent;
  135. fout << "add_test(";
  136. fout << this->Test->GetName() << " \"" << exe << "\"";
  137. for (std::vector<std::string>::const_iterator argit = command.begin() + 1;
  138. argit != command.end(); ++argit) {
  139. // Just double-quote all arguments so they are re-parsed
  140. // correctly by the test system.
  141. fout << " \"";
  142. for (char c : *argit) {
  143. // Escape quotes within arguments. We should escape
  144. // backslashes too but we cannot because it makes the result
  145. // inconsistent with previous behavior of this command.
  146. if (c == '"') {
  147. fout << '\\';
  148. }
  149. fout << c;
  150. }
  151. fout << "\"";
  152. }
  153. fout << ")" << std::endl;
  154. // Output properties for the test.
  155. cmPropertyMap& pm = this->Test->GetProperties();
  156. if (!pm.empty()) {
  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. fout << ")" << std::endl;
  164. }
  165. }