cmAddCustomTargetCommand.cxx 7.4 KB

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