cmAddTestCommand.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmAddTestCommand.h"
  4. #include <algorithm>
  5. #include <cm/memory>
  6. #include "cmExecutionStatus.h"
  7. #include "cmMakefile.h"
  8. #include "cmPolicies.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmTest.h"
  11. #include "cmTestGenerator.h"
  12. static std::string const keywordCMP0178 = "__CMP0178";
  13. static bool cmAddTestCommandHandleNameMode(
  14. std::vector<std::string> const& args, cmExecutionStatus& status);
  15. bool cmAddTestCommand(std::vector<std::string> const& args,
  16. cmExecutionStatus& status)
  17. {
  18. if (!args.empty() && args[0] == "NAME") {
  19. return cmAddTestCommandHandleNameMode(args, status);
  20. }
  21. // First argument is the name of the test Second argument is the name of
  22. // the executable to run (a target or external program) Remaining arguments
  23. // are the arguments to pass to the executable
  24. if (args.size() < 2) {
  25. status.SetError("called with incorrect number of arguments");
  26. return false;
  27. }
  28. cmMakefile& mf = status.GetMakefile();
  29. cmPolicies::PolicyStatus cmp0178;
  30. // If the __CMP0178 keyword is present, it is always at the end
  31. auto endOfCommandIter =
  32. std::find(args.begin() + 2, args.end(), keywordCMP0178);
  33. if (endOfCommandIter != args.end()) {
  34. auto cmp0178Iter = endOfCommandIter + 1;
  35. if (cmp0178Iter == args.end()) {
  36. status.SetError(cmStrCat(keywordCMP0178, " keyword missing value"));
  37. return false;
  38. }
  39. if (*cmp0178Iter == "NEW") {
  40. cmp0178 = cmPolicies::PolicyStatus::NEW;
  41. } else if (*cmp0178Iter == "OLD") {
  42. cmp0178 = cmPolicies::PolicyStatus::OLD;
  43. } else {
  44. cmp0178 = cmPolicies::PolicyStatus::WARN;
  45. }
  46. } else {
  47. cmp0178 = mf.GetPolicyStatus(cmPolicies::CMP0178);
  48. }
  49. // Collect the command with arguments.
  50. std::vector<std::string> command(args.begin() + 1, endOfCommandIter);
  51. // Create the test but add a generator only the first time it is
  52. // seen. This preserves behavior from before test generators.
  53. cmTest* test = mf.GetTest(args[0]);
  54. if (test) {
  55. // If the test was already added by a new-style signature do not
  56. // allow it to be duplicated.
  57. if (!test->GetOldStyle()) {
  58. status.SetError(cmStrCat(" given test name \"", args[0],
  59. "\" which already exists in this directory."));
  60. return false;
  61. }
  62. } else {
  63. test = mf.CreateTest(args[0]);
  64. test->SetOldStyle(true);
  65. test->SetCMP0178(cmp0178);
  66. mf.AddTestGenerator(cm::make_unique<cmTestGenerator>(test));
  67. }
  68. test->SetCommand(command);
  69. return true;
  70. }
  71. bool cmAddTestCommandHandleNameMode(std::vector<std::string> const& args,
  72. cmExecutionStatus& status)
  73. {
  74. cmMakefile& mf = status.GetMakefile();
  75. std::string name;
  76. std::vector<std::string> configurations;
  77. std::string working_directory;
  78. std::vector<std::string> command;
  79. bool command_expand_lists = false;
  80. cmPolicies::PolicyStatus cmp0178 = mf.GetPolicyStatus(cmPolicies::CMP0178);
  81. // Read the arguments.
  82. enum Doing
  83. {
  84. DoingName,
  85. DoingCommand,
  86. DoingConfigs,
  87. DoingWorkingDirectory,
  88. DoingCmp0178,
  89. DoingNone
  90. };
  91. Doing doing = DoingName;
  92. for (unsigned int i = 1; i < args.size(); ++i) {
  93. if (args[i] == "COMMAND") {
  94. if (!command.empty()) {
  95. status.SetError(" may be given at most one COMMAND.");
  96. return false;
  97. }
  98. doing = DoingCommand;
  99. } else if (args[i] == "CONFIGURATIONS") {
  100. if (!configurations.empty()) {
  101. status.SetError(" may be given at most one set of CONFIGURATIONS.");
  102. return false;
  103. }
  104. doing = DoingConfigs;
  105. } else if (args[i] == "WORKING_DIRECTORY") {
  106. if (!working_directory.empty()) {
  107. status.SetError(" may be given at most one WORKING_DIRECTORY.");
  108. return false;
  109. }
  110. doing = DoingWorkingDirectory;
  111. } else if (args[i] == keywordCMP0178) {
  112. doing = DoingCmp0178;
  113. } else if (args[i] == "COMMAND_EXPAND_LISTS") {
  114. if (command_expand_lists) {
  115. status.SetError(" may be given at most one COMMAND_EXPAND_LISTS.");
  116. return false;
  117. }
  118. command_expand_lists = true;
  119. doing = DoingNone;
  120. } else if (doing == DoingName) {
  121. name = args[i];
  122. doing = DoingNone;
  123. } else if (doing == DoingCommand) {
  124. command.push_back(args[i]);
  125. } else if (doing == DoingConfigs) {
  126. configurations.push_back(args[i]);
  127. } else if (doing == DoingWorkingDirectory) {
  128. working_directory = args[i];
  129. doing = DoingNone;
  130. } else if (doing == DoingCmp0178) {
  131. if (args[i] == "NEW") {
  132. cmp0178 = cmPolicies::PolicyStatus::NEW;
  133. } else if (args[i] == "OLD") {
  134. cmp0178 = cmPolicies::PolicyStatus::OLD;
  135. } else {
  136. cmp0178 = cmPolicies::PolicyStatus::WARN;
  137. }
  138. doing = DoingNone;
  139. } else {
  140. status.SetError(cmStrCat(" given unknown argument:\n ", args[i], '\n'));
  141. return false;
  142. }
  143. }
  144. // Require a test name.
  145. if (name.empty()) {
  146. status.SetError(" must be given non-empty NAME.");
  147. return false;
  148. }
  149. // Require a command.
  150. if (command.empty()) {
  151. status.SetError(" must be given non-empty COMMAND.");
  152. return false;
  153. }
  154. // Require a unique test name within the directory.
  155. if (mf.GetTest(name)) {
  156. status.SetError(cmStrCat(" given test NAME \"", name,
  157. "\" which already exists in this directory."));
  158. return false;
  159. }
  160. // Add the test.
  161. cmTest* test = mf.CreateTest(name);
  162. test->SetOldStyle(false);
  163. test->SetCMP0178(cmp0178);
  164. test->SetCommand(command);
  165. if (!working_directory.empty()) {
  166. test->SetProperty("WORKING_DIRECTORY", working_directory);
  167. }
  168. test->SetCommandExpandLists(command_expand_lists);
  169. mf.AddTestGenerator(cm::make_unique<cmTestGenerator>(test, configurations));
  170. return true;
  171. }