cmTestGenerator.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "cmGeneratorExpression.h"
  5. #include "cmGeneratorTarget.h"
  6. #include "cmLocalGenerator.h"
  7. #include "cmOutputConverter.h"
  8. #include "cmProperty.h"
  9. #include "cmPropertyMap.h"
  10. #include "cmState.h"
  11. #include "cmSystemTools.h"
  12. #include "cmTest.h"
  13. #include "cm_auto_ptr.hxx"
  14. #include <map>
  15. #include <ostream>
  16. #include <utility>
  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 = CM_NULLPTR;
  25. }
  26. cmTestGenerator::~cmTestGenerator()
  27. {
  28. }
  29. void cmTestGenerator::Compute(cmLocalGenerator* lg)
  30. {
  31. this->LG = lg;
  32. }
  33. void cmTestGenerator::GenerateScriptConfigs(std::ostream& os,
  34. Indent const& indent)
  35. {
  36. // Create the tests.
  37. this->cmScriptGenerator::GenerateScriptConfigs(os, indent);
  38. }
  39. void cmTestGenerator::GenerateScriptActions(std::ostream& os,
  40. Indent const& indent)
  41. {
  42. if (this->ActionsPerConfig) {
  43. // This is the per-config generation in a single-configuration
  44. // build generator case. The superclass will call our per-config
  45. // method.
  46. this->cmScriptGenerator::GenerateScriptActions(os, indent);
  47. } else {
  48. // This is an old-style test, so there is only one config.
  49. // assert(this->Test->GetOldStyle());
  50. this->GenerateOldStyle(os, indent);
  51. }
  52. }
  53. void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
  54. const std::string& config,
  55. Indent const& indent)
  56. {
  57. this->TestGenerated = true;
  58. // Set up generator expression evaluation context.
  59. cmGeneratorExpression ge(this->Test->GetBacktrace());
  60. // Start the test command.
  61. os << indent << "add_test(" << this->Test->GetName() << " ";
  62. // Get the test command line to be executed.
  63. std::vector<std::string> const& command = this->Test->GetCommand();
  64. // Check whether the command executable is a target whose name is to
  65. // be translated.
  66. std::string exe = command[0];
  67. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe);
  68. if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
  69. // Use the target file on disk.
  70. exe = target->GetFullPath(config);
  71. // Prepend with the emulator when cross compiling if required.
  72. const char* emulator = target->GetProperty("CROSSCOMPILING_EMULATOR");
  73. if (emulator != CM_NULLPTR) {
  74. std::vector<std::string> emulatorWithArgs;
  75. cmSystemTools::ExpandListArgument(emulator, emulatorWithArgs);
  76. std::string emulatorExe(emulatorWithArgs[0]);
  77. cmSystemTools::ConvertToUnixSlashes(emulatorExe);
  78. os << cmOutputConverter::EscapeForCMake(emulatorExe) << " ";
  79. for (std::vector<std::string>::const_iterator ei =
  80. emulatorWithArgs.begin() + 1;
  81. ei != emulatorWithArgs.end(); ++ei) {
  82. os << cmOutputConverter::EscapeForCMake(*ei) << " ";
  83. }
  84. }
  85. } else {
  86. // Use the command name given.
  87. exe = ge.Parse(exe.c_str())->Evaluate(this->LG, config);
  88. cmSystemTools::ConvertToUnixSlashes(exe);
  89. }
  90. // Generate the command line with full escapes.
  91. os << cmOutputConverter::EscapeForCMake(exe);
  92. for (std::vector<std::string>::const_iterator ci = command.begin() + 1;
  93. ci != command.end(); ++ci) {
  94. os << " " << cmOutputConverter::EscapeForCMake(
  95. ge.Parse(*ci)->Evaluate(this->LG, config));
  96. }
  97. // Finish the test command.
  98. os << ")\n";
  99. // Output properties for the test.
  100. cmPropertyMap& pm = this->Test->GetProperties();
  101. if (!pm.empty()) {
  102. os << indent << "set_tests_properties(" << this->Test->GetName()
  103. << " PROPERTIES ";
  104. for (cmPropertyMap::const_iterator i = pm.begin(); i != pm.end(); ++i) {
  105. os << " " << i->first << " "
  106. << cmOutputConverter::EscapeForCMake(
  107. ge.Parse(i->second.GetValue())->Evaluate(this->LG, config));
  108. }
  109. os << ")" << std::endl;
  110. }
  111. }
  112. void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os,
  113. Indent const& 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,
  125. Indent const& indent)
  126. {
  127. this->TestGenerated = true;
  128. // Get the test command line to be executed.
  129. std::vector<std::string> const& command = this->Test->GetCommand();
  130. std::string exe = command[0];
  131. cmSystemTools::ConvertToUnixSlashes(exe);
  132. fout << indent;
  133. fout << "add_test(";
  134. fout << this->Test->GetName() << " \"" << exe << "\"";
  135. for (std::vector<std::string>::const_iterator argit = command.begin() + 1;
  136. argit != command.end(); ++argit) {
  137. // Just double-quote all arguments so they are re-parsed
  138. // correctly by the test system.
  139. fout << " \"";
  140. for (std::string::const_iterator c = argit->begin(); c != argit->end();
  141. ++c) {
  142. // Escape quotes within arguments. We should escape
  143. // backslashes too but we cannot because it makes the result
  144. // inconsistent with previous behavior of this command.
  145. if ((*c == '"')) {
  146. fout << '\\';
  147. }
  148. fout << *c;
  149. }
  150. fout << "\"";
  151. }
  152. fout << ")" << std::endl;
  153. // Output properties for the test.
  154. cmPropertyMap& pm = this->Test->GetProperties();
  155. if (!pm.empty()) {
  156. fout << indent << "set_tests_properties(" << this->Test->GetName()
  157. << " PROPERTIES ";
  158. for (cmPropertyMap::const_iterator i = pm.begin(); i != pm.end(); ++i) {
  159. fout << " " << i->first << " "
  160. << cmOutputConverter::EscapeForCMake(i->second.GetValue());
  161. }
  162. fout << ")" << std::endl;
  163. }
  164. }