cmAddCustomTargetCommand.cxx 6.6 KB

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