cmAddCustomTargetCommand.cxx 7.0 KB

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