cmAddCustomTargetCommand.cxx 6.9 KB

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