cmTestGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <algorithm>
  5. #include <cstddef> // IWYU pragma: keep
  6. #include <iterator>
  7. #include <memory>
  8. #include <ostream>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "cmGeneratorExpression.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmList.h"
  15. #include "cmListFileCache.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmMessageType.h"
  19. #include "cmOutputConverter.h"
  20. #include "cmPolicies.h"
  21. #include "cmPropertyMap.h"
  22. #include "cmRange.h"
  23. #include "cmStateTypes.h"
  24. #include "cmStringAlgorithms.h"
  25. #include "cmSystemTools.h"
  26. #include "cmTest.h"
  27. #include "cmValue.h"
  28. namespace /* anonymous */
  29. {
  30. bool needToQuoteTestName(const cmMakefile& mf, const std::string& name)
  31. {
  32. // Determine if policy CMP0110 is set to NEW.
  33. switch (mf.GetPolicyStatus(cmPolicies::CMP0110)) {
  34. case cmPolicies::WARN:
  35. // Only warn if a forbidden character is used in the name.
  36. if (name.find_first_of("$[] #;\t\n\"\\") != std::string::npos) {
  37. mf.IssueMessage(
  38. MessageType::AUTHOR_WARNING,
  39. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0110),
  40. "\nThe following name given to add_test() is invalid if "
  41. "CMP0110 is not set or set to OLD:\n `",
  42. name, "´\n"));
  43. }
  44. CM_FALLTHROUGH;
  45. case cmPolicies::OLD:
  46. // OLD behavior is to not quote the test's name.
  47. return false;
  48. case cmPolicies::REQUIRED_IF_USED:
  49. case cmPolicies::REQUIRED_ALWAYS:
  50. case cmPolicies::NEW:
  51. default:
  52. // NEW behavior is to quote the test's name.
  53. return true;
  54. }
  55. }
  56. std::size_t countMaxConsecutiveEqualSigns(const std::string& name)
  57. {
  58. std::size_t max = 0;
  59. auto startIt = find(name.begin(), name.end(), '=');
  60. auto endIt = startIt;
  61. for (; startIt != name.end(); startIt = find(endIt, name.end(), '=')) {
  62. endIt =
  63. find_if_not(startIt + 1, name.end(), [](char c) { return c == '='; });
  64. max =
  65. std::max(max, static_cast<std::size_t>(std::distance(startIt, endIt)));
  66. }
  67. return max;
  68. }
  69. } // End: anonymous namespace
  70. cmTestGenerator::cmTestGenerator(
  71. cmTest* test, std::vector<std::string> const& configurations)
  72. : cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations)
  73. , Test(test)
  74. {
  75. this->ActionsPerConfig = !test->GetOldStyle();
  76. this->TestGenerated = false;
  77. this->LG = nullptr;
  78. }
  79. cmTestGenerator::~cmTestGenerator() = default;
  80. void cmTestGenerator::Compute(cmLocalGenerator* lg)
  81. {
  82. this->LG = lg;
  83. }
  84. bool cmTestGenerator::TestsForConfig(const std::string& config)
  85. {
  86. return this->GeneratesForConfig(config);
  87. }
  88. cmTest* cmTestGenerator::GetTest() const
  89. {
  90. return this->Test;
  91. }
  92. void cmTestGenerator::GenerateScriptConfigs(std::ostream& os, Indent indent)
  93. {
  94. // Create the tests.
  95. this->cmScriptGenerator::GenerateScriptConfigs(os, indent);
  96. }
  97. void cmTestGenerator::GenerateScriptActions(std::ostream& os, Indent indent)
  98. {
  99. if (this->ActionsPerConfig) {
  100. // This is the per-config generation in a single-configuration
  101. // build generator case. The superclass will call our per-config
  102. // method.
  103. this->cmScriptGenerator::GenerateScriptActions(os, indent);
  104. } else {
  105. // This is an old-style test, so there is only one config.
  106. // assert(this->Test->GetOldStyle());
  107. this->GenerateOldStyle(os, indent);
  108. }
  109. }
  110. void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
  111. const std::string& config,
  112. Indent indent)
  113. {
  114. this->TestGenerated = true;
  115. // Set up generator expression evaluation context.
  116. cmGeneratorExpression ge(*this->Test->GetMakefile()->GetCMakeInstance(),
  117. this->Test->GetBacktrace());
  118. // Determine if policy CMP0110 is set to NEW.
  119. const bool quote_test_name =
  120. needToQuoteTestName(*this->Test->GetMakefile(), this->Test->GetName());
  121. // Determine the number of equal-signs needed for quoting test name with
  122. // [==[...]==] syntax.
  123. const std::string equalSigns(
  124. 1 + countMaxConsecutiveEqualSigns(this->Test->GetName()), '=');
  125. // Start the test command.
  126. if (quote_test_name) {
  127. os << indent << "add_test([" << equalSigns << "[" << this->Test->GetName()
  128. << "]" << equalSigns << "] ";
  129. } else {
  130. os << indent << "add_test(" << this->Test->GetName() << " ";
  131. }
  132. // Evaluate command line arguments
  133. cmList argv{
  134. this->EvaluateCommandLineArguments(this->Test->GetCommand(), ge, config),
  135. // Expand arguments if COMMAND_EXPAND_LISTS is set
  136. this->Test->GetCommandExpandLists() ? cmList::ExpandElements::Yes
  137. : cmList::ExpandElements::No
  138. };
  139. // Expanding lists on an empty command may have left it empty
  140. if (argv.empty()) {
  141. argv.emplace_back();
  142. }
  143. // Check whether the command executable is a target whose name is to
  144. // be translated.
  145. std::string exe = argv[0];
  146. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe);
  147. if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
  148. // Use the target file on disk.
  149. exe = target->GetFullPath(config);
  150. // Prepend with the emulator when cross compiling if required.
  151. cmValue emulator = target->GetProperty("CROSSCOMPILING_EMULATOR");
  152. if (cmNonempty(emulator)) {
  153. cmList emulatorWithArgs{ *emulator };
  154. std::string emulatorExe(emulatorWithArgs[0]);
  155. cmSystemTools::ConvertToUnixSlashes(emulatorExe);
  156. os << cmOutputConverter::EscapeForCMake(emulatorExe) << " ";
  157. for (std::string const& arg : cmMakeRange(emulatorWithArgs).advance(1)) {
  158. os << cmOutputConverter::EscapeForCMake(arg) << " ";
  159. }
  160. }
  161. } else {
  162. // Use the command name given.
  163. cmSystemTools::ConvertToUnixSlashes(exe);
  164. }
  165. // Generate the command line with full escapes.
  166. os << cmOutputConverter::EscapeForCMake(exe);
  167. for (auto const& arg : cmMakeRange(argv).advance(1)) {
  168. os << " " << cmOutputConverter::EscapeForCMake(arg);
  169. }
  170. // Finish the test command.
  171. os << ")\n";
  172. // Output properties for the test.
  173. if (quote_test_name) {
  174. os << indent << "set_tests_properties([" << equalSigns << "["
  175. << this->Test->GetName() << "]" << equalSigns << "] PROPERTIES ";
  176. } else {
  177. os << indent << "set_tests_properties(" << this->Test->GetName()
  178. << " PROPERTIES ";
  179. }
  180. for (auto const& i : this->Test->GetProperties().GetList()) {
  181. os << " " << i.first << " "
  182. << cmOutputConverter::EscapeForCMake(
  183. ge.Parse(i.second)->Evaluate(this->LG, config));
  184. }
  185. this->GenerateInternalProperties(os);
  186. os << ")\n";
  187. }
  188. void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent)
  189. {
  190. // Determine if policy CMP0110 is set to NEW.
  191. const bool quote_test_name =
  192. needToQuoteTestName(*this->Test->GetMakefile(), this->Test->GetName());
  193. // Determine the number of equal-signs needed for quoting test name with
  194. // [==[...]==] syntax.
  195. const std::string equalSigns(
  196. 1 + countMaxConsecutiveEqualSigns(this->Test->GetName()), '=');
  197. if (quote_test_name) {
  198. os << indent << "add_test([" << equalSigns << "[" << this->Test->GetName()
  199. << "]" << equalSigns << "] NOT_AVAILABLE)\n";
  200. } else {
  201. os << indent << "add_test(" << this->Test->GetName()
  202. << " NOT_AVAILABLE)\n";
  203. }
  204. }
  205. bool cmTestGenerator::NeedsScriptNoConfig() const
  206. {
  207. return (this->TestGenerated && // test generated for at least one config
  208. this->ActionsPerConfig && // test is config-aware
  209. this->Configurations.empty() && // test runs in all configs
  210. !this->ConfigurationTypes->empty()); // config-dependent command
  211. }
  212. void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent)
  213. {
  214. this->TestGenerated = true;
  215. // Determine if policy CMP0110 is set to NEW.
  216. const bool quote_test_name =
  217. needToQuoteTestName(*this->Test->GetMakefile(), this->Test->GetName());
  218. // Determine the number of equal-signs needed for quoting test name with
  219. // [==[...]==] syntax.
  220. const std::string equalSigns(
  221. 1 + countMaxConsecutiveEqualSigns(this->Test->GetName()), '=');
  222. // Get the test command line to be executed.
  223. std::vector<std::string> const& command = this->Test->GetCommand();
  224. std::string exe = command[0];
  225. cmSystemTools::ConvertToUnixSlashes(exe);
  226. if (quote_test_name) {
  227. fout << indent << "add_test([" << equalSigns << "["
  228. << this->Test->GetName() << "]" << equalSigns << "] \"" << exe
  229. << "\"";
  230. } else {
  231. fout << indent << "add_test(" << this->Test->GetName() << " \"" << exe
  232. << "\"";
  233. }
  234. for (std::string const& arg : cmMakeRange(command).advance(1)) {
  235. // Just double-quote all arguments so they are re-parsed
  236. // correctly by the test system.
  237. fout << " \"";
  238. for (char c : arg) {
  239. // Escape quotes within arguments. We should escape
  240. // backslashes too but we cannot because it makes the result
  241. // inconsistent with previous behavior of this command.
  242. if (c == '"') {
  243. fout << '\\';
  244. }
  245. fout << c;
  246. }
  247. fout << '"';
  248. }
  249. fout << ")\n";
  250. // Output properties for the test.
  251. if (quote_test_name) {
  252. fout << indent << "set_tests_properties([" << equalSigns << "["
  253. << this->Test->GetName() << "]" << equalSigns << "] PROPERTIES ";
  254. } else {
  255. fout << indent << "set_tests_properties(" << this->Test->GetName()
  256. << " PROPERTIES ";
  257. }
  258. for (auto const& i : this->Test->GetProperties().GetList()) {
  259. fout << " " << i.first << " "
  260. << cmOutputConverter::EscapeForCMake(i.second);
  261. }
  262. this->GenerateInternalProperties(fout);
  263. fout << ")\n";
  264. }
  265. void cmTestGenerator::GenerateInternalProperties(std::ostream& os)
  266. {
  267. cmListFileBacktrace bt = this->Test->GetBacktrace();
  268. if (bt.Empty()) {
  269. return;
  270. }
  271. os << " "
  272. << "_BACKTRACE_TRIPLES"
  273. << " \"";
  274. bool prependTripleSeparator = false;
  275. while (!bt.Empty()) {
  276. const auto& entry = bt.Top();
  277. if (prependTripleSeparator) {
  278. os << ";";
  279. }
  280. os << entry.FilePath << ";" << entry.Line << ";" << entry.Name;
  281. bt = bt.Pop();
  282. prependTripleSeparator = true;
  283. }
  284. os << '"';
  285. }
  286. std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments(
  287. const std::vector<std::string>& argv, cmGeneratorExpression& ge,
  288. const std::string& config) const
  289. {
  290. // Evaluate executable name and arguments
  291. auto evaluatedRange =
  292. cmMakeRange(argv).transform([&](const std::string& arg) {
  293. return ge.Parse(arg)->Evaluate(this->LG, config);
  294. });
  295. return { evaluatedRange.begin(), evaluatedRange.end() };
  296. }