cmTestGenerator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. cmList::EmptyElements::Yes
  139. };
  140. // Expanding lists on an empty command may have left it empty
  141. if (argv.empty()) {
  142. argv.emplace_back();
  143. }
  144. // Check whether the command executable is a target whose name is to
  145. // be translated.
  146. std::string exe = argv[0];
  147. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe);
  148. if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
  149. // Use the target file on disk.
  150. exe = target->GetFullPath(config);
  151. auto addLauncher = [this, &config, &ge, &os,
  152. target](std::string const& propertyName) {
  153. cmValue launcher = target->GetProperty(propertyName);
  154. if (!cmNonempty(launcher)) {
  155. return;
  156. }
  157. const auto propVal = ge.Parse(*launcher)->Evaluate(this->LG, config);
  158. cmList launcherWithArgs(propVal, cmList::ExpandElements::Yes,
  159. this->Test->GetCMP0178() == cmPolicies::NEW
  160. ? cmList::EmptyElements::Yes
  161. : cmList::EmptyElements::No);
  162. if (!launcherWithArgs.empty() && !launcherWithArgs[0].empty()) {
  163. if (this->Test->GetCMP0178() == cmPolicies::WARN) {
  164. cmList argsWithEmptyValuesPreserved(
  165. propVal, cmList::ExpandElements::Yes, cmList::EmptyElements::Yes);
  166. if (launcherWithArgs != argsWithEmptyValuesPreserved) {
  167. this->Test->GetMakefile()->IssueMessage(
  168. MessageType::AUTHOR_WARNING,
  169. cmStrCat("The ", propertyName, " property of target '",
  170. target->GetName(),
  171. "' contains empty list items. Those empty items are "
  172. "being silently discarded to preserve backward "
  173. "compatibility.\n",
  174. cmPolicies::GetPolicyWarning(cmPolicies::CMP0178)));
  175. }
  176. }
  177. std::string launcherExe(launcherWithArgs[0]);
  178. cmSystemTools::ConvertToUnixSlashes(launcherExe);
  179. os << cmOutputConverter::EscapeForCMake(launcherExe) << " ";
  180. for (std::string const& arg :
  181. cmMakeRange(launcherWithArgs).advance(1)) {
  182. if (arg.empty()) {
  183. os << "\"\" ";
  184. } else {
  185. os << cmOutputConverter::EscapeForCMake(arg) << " ";
  186. }
  187. }
  188. }
  189. };
  190. // Prepend with the test launcher if specified.
  191. addLauncher("TEST_LAUNCHER");
  192. // Prepend with the emulator when cross compiling if required.
  193. if (!this->GetTest()->GetCMP0158IsNew() ||
  194. this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) {
  195. addLauncher("CROSSCOMPILING_EMULATOR");
  196. }
  197. } else {
  198. // Use the command name given.
  199. cmSystemTools::ConvertToUnixSlashes(exe);
  200. }
  201. // Generate the command line with full escapes.
  202. os << cmOutputConverter::EscapeForCMake(exe);
  203. for (auto const& arg : cmMakeRange(argv).advance(1)) {
  204. os << " " << cmOutputConverter::EscapeForCMake(arg);
  205. }
  206. // Finish the test command.
  207. os << ")\n";
  208. // Output properties for the test.
  209. if (quote_test_name) {
  210. os << indent << "set_tests_properties([" << equalSigns << "["
  211. << this->Test->GetName() << "]" << equalSigns << "] PROPERTIES ";
  212. } else {
  213. os << indent << "set_tests_properties(" << this->Test->GetName()
  214. << " PROPERTIES ";
  215. }
  216. for (auto const& i : this->Test->GetProperties().GetList()) {
  217. os << " " << i.first << " "
  218. << cmOutputConverter::EscapeForCMake(
  219. ge.Parse(i.second)->Evaluate(this->LG, config));
  220. }
  221. this->GenerateInternalProperties(os);
  222. os << ")\n";
  223. }
  224. void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent)
  225. {
  226. // Determine if policy CMP0110 is set to NEW.
  227. const bool quote_test_name =
  228. needToQuoteTestName(*this->Test->GetMakefile(), this->Test->GetName());
  229. // Determine the number of equal-signs needed for quoting test name with
  230. // [==[...]==] syntax.
  231. const std::string equalSigns(
  232. 1 + countMaxConsecutiveEqualSigns(this->Test->GetName()), '=');
  233. if (quote_test_name) {
  234. os << indent << "add_test([" << equalSigns << "[" << this->Test->GetName()
  235. << "]" << equalSigns << "] NOT_AVAILABLE)\n";
  236. } else {
  237. os << indent << "add_test(" << this->Test->GetName()
  238. << " NOT_AVAILABLE)\n";
  239. }
  240. }
  241. bool cmTestGenerator::NeedsScriptNoConfig() const
  242. {
  243. return (this->TestGenerated && // test generated for at least one config
  244. this->ActionsPerConfig && // test is config-aware
  245. this->Configurations.empty() && // test runs in all configs
  246. !this->ConfigurationTypes->empty()); // config-dependent command
  247. }
  248. void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent)
  249. {
  250. this->TestGenerated = true;
  251. // Determine if policy CMP0110 is set to NEW.
  252. const bool quote_test_name =
  253. needToQuoteTestName(*this->Test->GetMakefile(), this->Test->GetName());
  254. // Determine the number of equal-signs needed for quoting test name with
  255. // [==[...]==] syntax.
  256. const std::string equalSigns(
  257. 1 + countMaxConsecutiveEqualSigns(this->Test->GetName()), '=');
  258. // Get the test command line to be executed.
  259. std::vector<std::string> const& command = this->Test->GetCommand();
  260. std::string exe = command[0];
  261. cmSystemTools::ConvertToUnixSlashes(exe);
  262. if (quote_test_name) {
  263. fout << indent << "add_test([" << equalSigns << "["
  264. << this->Test->GetName() << "]" << equalSigns << "] \"" << exe
  265. << "\"";
  266. } else {
  267. fout << indent << "add_test(" << this->Test->GetName() << " \"" << exe
  268. << "\"";
  269. }
  270. for (std::string const& arg : cmMakeRange(command).advance(1)) {
  271. // Just double-quote all arguments so they are re-parsed
  272. // correctly by the test system.
  273. fout << " \"";
  274. for (char c : arg) {
  275. // Escape quotes within arguments. We should escape
  276. // backslashes too but we cannot because it makes the result
  277. // inconsistent with previous behavior of this command.
  278. if (c == '"') {
  279. fout << '\\';
  280. }
  281. fout << c;
  282. }
  283. fout << '"';
  284. }
  285. fout << ")\n";
  286. // Output properties for the test.
  287. if (quote_test_name) {
  288. fout << indent << "set_tests_properties([" << equalSigns << "["
  289. << this->Test->GetName() << "]" << equalSigns << "] PROPERTIES ";
  290. } else {
  291. fout << indent << "set_tests_properties(" << this->Test->GetName()
  292. << " PROPERTIES ";
  293. }
  294. for (auto const& i : this->Test->GetProperties().GetList()) {
  295. fout << " " << i.first << " "
  296. << cmOutputConverter::EscapeForCMake(i.second);
  297. }
  298. this->GenerateInternalProperties(fout);
  299. fout << ")\n";
  300. }
  301. void cmTestGenerator::GenerateInternalProperties(std::ostream& os)
  302. {
  303. cmListFileBacktrace bt = this->Test->GetBacktrace();
  304. if (bt.Empty()) {
  305. return;
  306. }
  307. os << " "
  308. << "_BACKTRACE_TRIPLES"
  309. << " \"";
  310. bool prependTripleSeparator = false;
  311. while (!bt.Empty()) {
  312. const auto& entry = bt.Top();
  313. if (prependTripleSeparator) {
  314. os << ";";
  315. }
  316. os << entry.FilePath << ";" << entry.Line << ";" << entry.Name;
  317. bt = bt.Pop();
  318. prependTripleSeparator = true;
  319. }
  320. os << '"';
  321. }
  322. std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments(
  323. const std::vector<std::string>& argv, cmGeneratorExpression& ge,
  324. const std::string& config) const
  325. {
  326. // Evaluate executable name and arguments
  327. auto evaluatedRange =
  328. cmMakeRange(argv).transform([&](const std::string& arg) {
  329. return ge.Parse(arg)->Evaluate(this->LG, config);
  330. });
  331. return { evaluatedRange.begin(), evaluatedRange.end() };
  332. }