cmAddCustomTargetCommand.cxx 6.8 KB

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