cmAddCustomTargetCommand.cxx 6.4 KB

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