cmAddCustomTargetCommand.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, 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 = mf.GetCurrentBinaryDirectory();
  112. filename += "/";
  113. }
  114. filename += copy;
  115. cmSystemTools::ConvertToUnixSlashes(filename);
  116. byproducts.push_back(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. std::ostringstream msg;
  142. msg << "called with target name containing a \"" << targetName[pos]
  143. << "\". This character is not allowed.";
  144. status.SetError(msg.str());
  145. return false;
  146. }
  147. // Some requirements on custom target names already exist
  148. // and have been checked at this point.
  149. // The following restrictions overlap but depend on policy CMP0037.
  150. bool nameOk = cmGeneratorExpression::IsValidTargetName(targetName) &&
  151. !cmGlobalGenerator::IsReservedTarget(targetName);
  152. if (nameOk) {
  153. nameOk = targetName.find(':') == std::string::npos;
  154. }
  155. if (!nameOk && !mf.CheckCMP0037(targetName, cmStateEnums::UTILITY)) {
  156. return false;
  157. }
  158. // Store the last command line finished.
  159. if (!currentLine.empty()) {
  160. commandLines.push_back(currentLine);
  161. currentLine.clear();
  162. }
  163. // Enforce name uniqueness.
  164. {
  165. std::string msg;
  166. if (!mf.EnforceUniqueName(targetName, msg, true)) {
  167. status.SetError(msg);
  168. return false;
  169. }
  170. }
  171. if (commandLines.empty() && !byproducts.empty()) {
  172. mf.IssueMessage(MessageType::FATAL_ERROR,
  173. "BYPRODUCTS may not be specified without any COMMAND");
  174. return true;
  175. }
  176. if (commandLines.empty() && uses_terminal) {
  177. mf.IssueMessage(MessageType::FATAL_ERROR,
  178. "USES_TERMINAL may not be specified without any COMMAND");
  179. return true;
  180. }
  181. if (commandLines.empty() && command_expand_lists) {
  182. mf.IssueMessage(
  183. MessageType::FATAL_ERROR,
  184. "COMMAND_EXPAND_LISTS may not be specified without any COMMAND");
  185. return true;
  186. }
  187. if (uses_terminal && !job_pool.empty()) {
  188. status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
  189. return false;
  190. }
  191. // Add the utility target to the makefile.
  192. bool escapeOldStyle = !verbatim;
  193. cmTarget* target = mf.AddUtilityCommand(
  194. targetName, cmMakefile::TargetOrigin::Project, excludeFromAll,
  195. working_directory.c_str(), byproducts, depends, commandLines,
  196. escapeOldStyle, comment, uses_terminal, command_expand_lists, job_pool);
  197. // Add additional user-specified source files to the target.
  198. target->AddSources(sources);
  199. return true;
  200. }