cmAddCustomTargetCommand.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "cmStateTypes.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. !this->Makefile->CheckCMP0037(targetName, cmStateEnums::UTILITY)) {
  149. return false;
  150. }
  151. // Store the last command line finished.
  152. if (!currentLine.empty()) {
  153. commandLines.push_back(currentLine);
  154. currentLine.clear();
  155. }
  156. // Enforce name uniqueness.
  157. {
  158. std::string msg;
  159. if (!this->Makefile->EnforceUniqueName(targetName, msg, true)) {
  160. this->SetError(msg);
  161. return false;
  162. }
  163. }
  164. // Convert working directory to a full path.
  165. if (!working_directory.empty()) {
  166. const char* build_dir = this->Makefile->GetCurrentBinaryDirectory();
  167. working_directory =
  168. cmSystemTools::CollapseFullPath(working_directory, build_dir);
  169. }
  170. if (commandLines.empty() && !byproducts.empty()) {
  171. this->Makefile->IssueMessage(
  172. cmake::FATAL_ERROR,
  173. "BYPRODUCTS may not be specified without any COMMAND");
  174. return true;
  175. }
  176. if (commandLines.empty() && uses_terminal) {
  177. this->Makefile->IssueMessage(
  178. cmake::FATAL_ERROR,
  179. "USES_TERMINAL may not be specified without any COMMAND");
  180. return true;
  181. }
  182. if (commandLines.empty() && command_expand_lists) {
  183. this->Makefile->IssueMessage(
  184. cmake::FATAL_ERROR,
  185. "COMMAND_EXPAND_LISTS may not be specified without any COMMAND");
  186. return true;
  187. }
  188. // Add the utility target to the makefile.
  189. bool escapeOldStyle = !verbatim;
  190. cmTarget* target = this->Makefile->AddUtilityCommand(
  191. targetName, excludeFromAll, working_directory.c_str(), byproducts, depends,
  192. commandLines, escapeOldStyle, comment, uses_terminal,
  193. command_expand_lists);
  194. // Add additional user-specified source files to the target.
  195. target->AddSources(sources);
  196. return true;
  197. }