cmAddCustomTargetCommand.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "cmCustomCommandLines.h"
  6. #include "cmGeneratorExpression.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmMakefile.h"
  9. #include "cmPolicies.h"
  10. #include "cmSystemTools.h"
  11. #include "cmTarget.h"
  12. #include "cmake.h"
  13. class cmExecutionStatus;
  14. // cmAddCustomTargetCommand
  15. bool cmAddCustomTargetCommand::InitialPass(
  16. std::vector<std::string> const& args, cmExecutionStatus&)
  17. {
  18. if (args.empty()) {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. std::string const& targetName = args[0];
  23. // Check the target name.
  24. if (targetName.find_first_of("/\\") != std::string::npos) {
  25. std::ostringstream e;
  26. e << "called with invalid target name \"" << targetName
  27. << "\". Target names may not contain a slash. "
  28. << "Use ADD_CUSTOM_COMMAND to generate files.";
  29. this->SetError(e.str());
  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, 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. // Keep track of parser state.
  46. enum tdoing
  47. {
  48. doing_command,
  49. doing_depends,
  50. doing_byproducts,
  51. doing_working_directory,
  52. doing_comment,
  53. doing_source,
  54. doing_nothing
  55. };
  56. tdoing doing = doing_command;
  57. // Look for the ALL option.
  58. bool excludeFromAll = true;
  59. unsigned int start = 1;
  60. if (args.size() > 1) {
  61. if (args[1] == "ALL") {
  62. excludeFromAll = false;
  63. start = 2;
  64. }
  65. }
  66. // Parse the rest of the arguments.
  67. for (unsigned int j = start; j < args.size(); ++j) {
  68. std::string const& copy = args[j];
  69. if (copy == "DEPENDS") {
  70. doing = doing_depends;
  71. } else if (copy == "BYPRODUCTS") {
  72. doing = doing_byproducts;
  73. } else if (copy == "WORKING_DIRECTORY") {
  74. doing = doing_working_directory;
  75. } else if (copy == "VERBATIM") {
  76. doing = doing_nothing;
  77. verbatim = true;
  78. } else if (copy == "USES_TERMINAL") {
  79. doing = doing_nothing;
  80. uses_terminal = true;
  81. } else if (copy == "COMMAND_EXPAND_LISTS") {
  82. doing = doing_nothing;
  83. command_expand_lists = true;
  84. } else if (copy == "COMMENT") {
  85. doing = doing_comment;
  86. } else if (copy == "COMMAND") {
  87. doing = doing_command;
  88. // Save the current command before starting the next command.
  89. if (!currentLine.empty()) {
  90. commandLines.push_back(currentLine);
  91. currentLine.clear();
  92. }
  93. } else if (copy == "SOURCES") {
  94. doing = doing_source;
  95. } else {
  96. switch (doing) {
  97. case doing_working_directory:
  98. working_directory = copy;
  99. break;
  100. case doing_command:
  101. currentLine.push_back(copy);
  102. break;
  103. case doing_byproducts: {
  104. std::string filename;
  105. if (!cmSystemTools::FileIsFullPath(copy.c_str())) {
  106. filename = this->Makefile->GetCurrentBinaryDirectory();
  107. filename += "/";
  108. }
  109. filename += copy;
  110. cmSystemTools::ConvertToUnixSlashes(filename);
  111. byproducts.push_back(filename);
  112. } break;
  113. case doing_depends: {
  114. std::string dep = copy;
  115. cmSystemTools::ConvertToUnixSlashes(dep);
  116. depends.push_back(dep);
  117. } break;
  118. case doing_comment:
  119. comment_buffer = copy;
  120. comment = comment_buffer.c_str();
  121. break;
  122. case doing_source:
  123. sources.push_back(copy);
  124. break;
  125. default:
  126. this->SetError("Wrong syntax. Unknown type of argument.");
  127. return false;
  128. }
  129. }
  130. }
  131. std::string::size_type pos = targetName.find_first_of("#<>");
  132. if (pos != std::string::npos) {
  133. std::ostringstream msg;
  134. msg << "called with target name containing a \"" << targetName[pos]
  135. << "\". This character is not allowed.";
  136. this->SetError(msg.str());
  137. return false;
  138. }
  139. // Some requirements on custom target names already exist
  140. // and have been checked at this point.
  141. // The following restrictions overlap but depend on policy CMP0037.
  142. bool nameOk = cmGeneratorExpression::IsValidTargetName(targetName) &&
  143. !cmGlobalGenerator::IsReservedTarget(targetName);
  144. if (nameOk) {
  145. nameOk = targetName.find(':') == std::string::npos;
  146. }
  147. if (!nameOk) {
  148. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  149. std::ostringstream e;
  150. bool issueMessage = false;
  151. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0037)) {
  152. case cmPolicies::WARN:
  153. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0037) << "\n";
  154. issueMessage = true;
  155. case cmPolicies::OLD:
  156. break;
  157. case cmPolicies::NEW:
  158. case cmPolicies::REQUIRED_IF_USED:
  159. case cmPolicies::REQUIRED_ALWAYS:
  160. issueMessage = true;
  161. messageType = cmake::FATAL_ERROR;
  162. }
  163. if (issueMessage) {
  164. /* clang-format off */
  165. e << "The target name \"" << targetName <<
  166. "\" is reserved or not valid for certain "
  167. "CMake features, such as generator expressions, and may result "
  168. "in undefined behavior.";
  169. /* clang-format on */
  170. this->Makefile->IssueMessage(messageType, e.str());
  171. if (messageType == cmake::FATAL_ERROR) {
  172. return false;
  173. }
  174. }
  175. }
  176. // Store the last command line finished.
  177. if (!currentLine.empty()) {
  178. commandLines.push_back(currentLine);
  179. currentLine.clear();
  180. }
  181. // Enforce name uniqueness.
  182. {
  183. std::string msg;
  184. if (!this->Makefile->EnforceUniqueName(targetName, msg, true)) {
  185. this->SetError(msg);
  186. return false;
  187. }
  188. }
  189. // Convert working directory to a full path.
  190. if (!working_directory.empty()) {
  191. const char* build_dir = this->Makefile->GetCurrentBinaryDirectory();
  192. working_directory =
  193. cmSystemTools::CollapseFullPath(working_directory, build_dir);
  194. }
  195. if (commandLines.empty() && !byproducts.empty()) {
  196. this->Makefile->IssueMessage(
  197. cmake::FATAL_ERROR,
  198. "BYPRODUCTS may not be specified without any COMMAND");
  199. return true;
  200. }
  201. if (commandLines.empty() && uses_terminal) {
  202. this->Makefile->IssueMessage(
  203. cmake::FATAL_ERROR,
  204. "USES_TERMINAL may not be specified without any COMMAND");
  205. return true;
  206. }
  207. if (commandLines.empty() && command_expand_lists) {
  208. this->Makefile->IssueMessage(
  209. cmake::FATAL_ERROR,
  210. "COMMAND_EXPAND_LISTS may not be specified without any COMMAND");
  211. return true;
  212. }
  213. // Add the utility target to the makefile.
  214. bool escapeOldStyle = !verbatim;
  215. cmTarget* target = this->Makefile->AddUtilityCommand(
  216. targetName, excludeFromAll, working_directory.c_str(), byproducts, depends,
  217. commandLines, escapeOldStyle, comment, uses_terminal,
  218. command_expand_lists);
  219. // Add additional user-specified source files to the target.
  220. target->AddSources(sources);
  221. return true;
  222. }