cmAddCustomTargetCommand.cxx 6.4 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 <sstream>
  5. #include <utility>
  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 "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. std::ostringstream e;
  27. e << "called with invalid target name \"" << targetName
  28. << "\". Target names may not contain a slash. "
  29. << "Use ADD_CUSTOM_COMMAND to generate files.";
  30. status.SetError(e.str());
  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 = mf.GetCurrentBinaryDirectory();
  113. filename += "/";
  114. }
  115. filename += copy;
  116. cmSystemTools::ConvertToUnixSlashes(filename);
  117. byproducts.push_back(filename);
  118. } break;
  119. case doing_depends: {
  120. std::string dep = copy;
  121. cmSystemTools::ConvertToUnixSlashes(dep);
  122. depends.push_back(std::move(dep));
  123. } break;
  124. case doing_comment:
  125. comment_buffer = copy;
  126. comment = comment_buffer.c_str();
  127. break;
  128. case doing_source:
  129. sources.push_back(copy);
  130. break;
  131. case doing_job_pool:
  132. job_pool = copy;
  133. break;
  134. default:
  135. status.SetError("Wrong syntax. Unknown type of argument.");
  136. return false;
  137. }
  138. }
  139. }
  140. std::string::size_type pos = targetName.find_first_of("#<>");
  141. if (pos != std::string::npos) {
  142. std::ostringstream msg;
  143. msg << "called with target name containing a \"" << targetName[pos]
  144. << "\". This character is not allowed.";
  145. status.SetError(msg.str());
  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, cmMakefile::TargetOrigin::Project, excludeFromAll,
  196. working_directory.c_str(), byproducts, depends, commandLines,
  197. escapeOldStyle, comment, uses_terminal, command_expand_lists, job_pool);
  198. // Add additional user-specified source files to the target.
  199. target->AddSources(sources);
  200. return true;
  201. }