cmAddCustomTargetCommand.cxx 6.7 KB

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