cmAddCustomTargetCommand.cxx 6.6 KB

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