cmAddCustomTargetCommand.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "cmCheckCustomOutputs.h"
  6. #include "cmCustomCommandLines.h"
  7. #include "cmCustomCommandTypes.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. filename = cmStrCat(mf.GetCurrentBinaryDirectory(), '/');
  113. }
  114. filename += copy;
  115. cmSystemTools::ConvertToUnixSlashes(filename);
  116. byproducts.push_back(cmSystemTools::CollapseFullPath(filename));
  117. } break;
  118. case doing_depends: {
  119. std::string dep = copy;
  120. cmSystemTools::ConvertToUnixSlashes(dep);
  121. depends.push_back(std::move(dep));
  122. } break;
  123. case doing_comment:
  124. comment_buffer = copy;
  125. comment = comment_buffer.c_str();
  126. break;
  127. case doing_source:
  128. sources.push_back(copy);
  129. break;
  130. case doing_job_pool:
  131. job_pool = copy;
  132. break;
  133. default:
  134. status.SetError("Wrong syntax. Unknown type of argument.");
  135. return false;
  136. }
  137. }
  138. }
  139. std::string::size_type pos = targetName.find_first_of("#<>");
  140. if (pos != std::string::npos) {
  141. status.SetError(cmStrCat("called with target name containing a \"",
  142. targetName[pos],
  143. "\". This character is not allowed."));
  144. return false;
  145. }
  146. // Some requirements on custom target names already exist
  147. // and have been checked at this point.
  148. // The following restrictions overlap but depend on policy CMP0037.
  149. bool nameOk = cmGeneratorExpression::IsValidTargetName(targetName) &&
  150. !cmGlobalGenerator::IsReservedTarget(targetName);
  151. if (nameOk) {
  152. nameOk = targetName.find(':') == std::string::npos;
  153. }
  154. if (!nameOk && !mf.CheckCMP0037(targetName, cmStateEnums::UTILITY)) {
  155. return false;
  156. }
  157. // Store the last command line finished.
  158. if (!currentLine.empty()) {
  159. commandLines.push_back(currentLine);
  160. currentLine.clear();
  161. }
  162. // Enforce name uniqueness.
  163. {
  164. std::string msg;
  165. if (!mf.EnforceUniqueName(targetName, msg, true)) {
  166. status.SetError(msg);
  167. return false;
  168. }
  169. }
  170. if (commandLines.empty() && !byproducts.empty()) {
  171. mf.IssueMessage(MessageType::FATAL_ERROR,
  172. "BYPRODUCTS may not be specified without any COMMAND");
  173. return true;
  174. }
  175. if (commandLines.empty() && uses_terminal) {
  176. mf.IssueMessage(MessageType::FATAL_ERROR,
  177. "USES_TERMINAL may not be specified without any COMMAND");
  178. return true;
  179. }
  180. if (commandLines.empty() && command_expand_lists) {
  181. mf.IssueMessage(
  182. MessageType::FATAL_ERROR,
  183. "COMMAND_EXPAND_LISTS may not be specified without any COMMAND");
  184. return true;
  185. }
  186. if (uses_terminal && !job_pool.empty()) {
  187. status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
  188. return false;
  189. }
  190. // Make sure the byproduct names and locations are safe.
  191. if (!cmCheckCustomOutputs(byproducts, "BYPRODUCTS", status)) {
  192. return false;
  193. }
  194. // Add the utility target to the makefile.
  195. bool escapeOldStyle = !verbatim;
  196. cmTarget* target = mf.AddUtilityCommand(
  197. targetName, cmCommandOrigin::Project, excludeFromAll,
  198. working_directory.c_str(), byproducts, depends, commandLines,
  199. escapeOldStyle, comment, uses_terminal, command_expand_lists, job_pool);
  200. // Add additional user-specified source files to the target.
  201. target->AddSources(sources);
  202. return true;
  203. }