cmAddCustomTargetCommand.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "cmAddCustomTargetCommand.h"
  4. #include <utility>
  5. #include <cm/memory>
  6. #include "cmCustomCommand.h"
  7. #include "cmCustomCommandLines.h"
  8. #include "cmExecutionStatus.h"
  9. #include "cmGeneratorExpression.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmMessageType.h"
  13. #include "cmStateTypes.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include "cmValue.h"
  18. bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
  19. cmExecutionStatus& status)
  20. {
  21. if (args.empty()) {
  22. status.SetError("called with incorrect number of arguments");
  23. return false;
  24. }
  25. cmMakefile& mf = status.GetMakefile();
  26. std::string const& targetName = args[0];
  27. // Check the target name.
  28. if (targetName.find_first_of("/\\") != std::string::npos) {
  29. status.SetError(cmStrCat("called with invalid target name \"", targetName,
  30. "\". Target names may not contain a slash. "
  31. "Use ADD_CUSTOM_COMMAND to generate files."));
  32. return false;
  33. }
  34. // Accumulate one command line at a time.
  35. cmCustomCommandLine currentLine;
  36. // Save all command lines.
  37. cmCustomCommandLines commandLines;
  38. // Accumulate dependencies.
  39. std::vector<std::string> depends;
  40. std::vector<std::string> byproducts;
  41. std::string working_directory;
  42. bool verbatim = false;
  43. bool uses_terminal = false;
  44. bool command_expand_lists = false;
  45. std::string comment_buffer;
  46. const char* comment = nullptr;
  47. std::vector<std::string> sources;
  48. std::string job_pool;
  49. std::string job_server_aware;
  50. // Keep track of parser state.
  51. enum tdoing
  52. {
  53. doing_command,
  54. doing_depends,
  55. doing_byproducts,
  56. doing_working_directory,
  57. doing_comment,
  58. doing_source,
  59. doing_job_pool,
  60. doing_job_server_aware,
  61. doing_nothing
  62. };
  63. tdoing doing = doing_command;
  64. // Look for the ALL option.
  65. bool excludeFromAll = true;
  66. unsigned int start = 1;
  67. if (args.size() > 1) {
  68. if (args[1] == "ALL") {
  69. excludeFromAll = false;
  70. start = 2;
  71. }
  72. }
  73. // Parse the rest of the arguments.
  74. for (unsigned int j = start; j < args.size(); ++j) {
  75. std::string const& copy = args[j];
  76. if (copy == "DEPENDS") {
  77. doing = doing_depends;
  78. } else if (copy == "BYPRODUCTS") {
  79. doing = doing_byproducts;
  80. } else if (copy == "WORKING_DIRECTORY") {
  81. doing = doing_working_directory;
  82. } else if (copy == "VERBATIM") {
  83. doing = doing_nothing;
  84. verbatim = true;
  85. } else if (copy == "USES_TERMINAL") {
  86. doing = doing_nothing;
  87. uses_terminal = true;
  88. } else if (copy == "COMMAND_EXPAND_LISTS") {
  89. doing = doing_nothing;
  90. command_expand_lists = true;
  91. } else if (copy == "COMMENT") {
  92. doing = doing_comment;
  93. } else if (copy == "JOB_POOL") {
  94. doing = doing_job_pool;
  95. } else if (copy == "JOB_SERVER_AWARE") {
  96. doing = doing_job_server_aware;
  97. } else if (copy == "COMMAND") {
  98. doing = doing_command;
  99. // Save the current command before starting the next command.
  100. if (!currentLine.empty()) {
  101. commandLines.push_back(currentLine);
  102. currentLine.clear();
  103. }
  104. } else if (copy == "SOURCES") {
  105. doing = doing_source;
  106. } else {
  107. switch (doing) {
  108. case doing_working_directory:
  109. working_directory = copy;
  110. break;
  111. case doing_command:
  112. currentLine.push_back(copy);
  113. break;
  114. case doing_byproducts: {
  115. std::string filename;
  116. if (!cmSystemTools::FileIsFullPath(copy) &&
  117. cmGeneratorExpression::Find(copy) != 0) {
  118. filename = cmStrCat(mf.GetCurrentBinaryDirectory(), '/');
  119. }
  120. filename += copy;
  121. cmSystemTools::ConvertToUnixSlashes(filename);
  122. if (cmSystemTools::FileIsFullPath(filename)) {
  123. filename = cmSystemTools::CollapseFullPath(filename);
  124. }
  125. byproducts.push_back(filename);
  126. } break;
  127. case doing_depends: {
  128. std::string dep = copy;
  129. cmSystemTools::ConvertToUnixSlashes(dep);
  130. depends.push_back(std::move(dep));
  131. } break;
  132. case doing_comment:
  133. comment_buffer = copy;
  134. comment = comment_buffer.c_str();
  135. break;
  136. case doing_source:
  137. sources.push_back(copy);
  138. break;
  139. case doing_job_pool:
  140. job_pool = copy;
  141. break;
  142. case doing_job_server_aware:
  143. job_server_aware = copy;
  144. break;
  145. default:
  146. status.SetError("Wrong syntax. Unknown type of argument.");
  147. return false;
  148. }
  149. }
  150. }
  151. std::string::size_type pos = targetName.find_first_of("#<>");
  152. if (pos != std::string::npos) {
  153. status.SetError(cmStrCat("called with target name containing a \"",
  154. targetName[pos],
  155. "\". This character is not allowed."));
  156. return false;
  157. }
  158. // Some requirements on custom target names already exist
  159. // and have been checked at this point.
  160. // The following restrictions overlap but depend on policy CMP0037.
  161. bool nameOk = cmGeneratorExpression::IsValidTargetName(targetName) &&
  162. !cmGlobalGenerator::IsReservedTarget(targetName);
  163. if (nameOk) {
  164. nameOk = targetName.find(':') == std::string::npos;
  165. }
  166. if (!nameOk && !mf.CheckCMP0037(targetName, cmStateEnums::UTILITY)) {
  167. return false;
  168. }
  169. // Store the last command line finished.
  170. if (!currentLine.empty()) {
  171. commandLines.push_back(currentLine);
  172. currentLine.clear();
  173. }
  174. // Enforce name uniqueness.
  175. {
  176. std::string msg;
  177. if (!mf.EnforceUniqueName(targetName, msg, true)) {
  178. status.SetError(msg);
  179. return false;
  180. }
  181. }
  182. if (commandLines.empty() && !byproducts.empty()) {
  183. mf.IssueMessage(MessageType::FATAL_ERROR,
  184. "BYPRODUCTS may not be specified without any COMMAND");
  185. return true;
  186. }
  187. if (commandLines.empty() && uses_terminal) {
  188. mf.IssueMessage(MessageType::FATAL_ERROR,
  189. "USES_TERMINAL may not be specified without any COMMAND");
  190. return true;
  191. }
  192. if (commandLines.empty() && command_expand_lists) {
  193. mf.IssueMessage(
  194. MessageType::FATAL_ERROR,
  195. "COMMAND_EXPAND_LISTS may not be specified without any COMMAND");
  196. return true;
  197. }
  198. if (uses_terminal && !job_pool.empty()) {
  199. status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
  200. return false;
  201. }
  202. // Add the utility target to the makefile.
  203. auto cc = cm::make_unique<cmCustomCommand>();
  204. cc->SetWorkingDirectory(working_directory.c_str());
  205. cc->SetByproducts(byproducts);
  206. cc->SetDepends(depends);
  207. cc->SetCommandLines(commandLines);
  208. cc->SetEscapeOldStyle(!verbatim);
  209. cc->SetComment(comment);
  210. cc->SetUsesTerminal(uses_terminal);
  211. cc->SetCommandExpandLists(command_expand_lists);
  212. cc->SetJobPool(job_pool);
  213. cc->SetJobserverAware(cmIsOn(job_server_aware));
  214. cmTarget* target =
  215. mf.AddUtilityCommand(targetName, excludeFromAll, std::move(cc));
  216. // Add additional user-specified source files to the target.
  217. target->AddSources(sources);
  218. return true;
  219. }