cmAddCustomTargetCommand.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. std::string comment_buffer;
  43. const char* comment = 0;
  44. std::vector<std::string> sources;
  45. // Keep track of parser state.
  46. enum tdoing {
  47. doing_command,
  48. doing_depends,
  49. doing_working_directory,
  50. doing_comment,
  51. doing_source,
  52. doing_verbatim
  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. {
  60. if(args[1] == "ALL")
  61. {
  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. {
  69. std::string const& copy = args[j];
  70. if(copy == "DEPENDS")
  71. {
  72. doing = doing_depends;
  73. }
  74. else if(copy == "WORKING_DIRECTORY")
  75. {
  76. doing = doing_working_directory;
  77. }
  78. else if(copy == "VERBATIM")
  79. {
  80. doing = doing_verbatim;
  81. verbatim = true;
  82. }
  83. else if (copy == "COMMENT")
  84. {
  85. doing = doing_comment;
  86. }
  87. else if(copy == "COMMAND")
  88. {
  89. doing = doing_command;
  90. // Save the current command before starting the next command.
  91. if(!currentLine.empty())
  92. {
  93. commandLines.push_back(currentLine);
  94. currentLine.clear();
  95. }
  96. }
  97. else if(copy == "SOURCES")
  98. {
  99. doing = doing_source;
  100. }
  101. else
  102. {
  103. switch (doing)
  104. {
  105. case doing_working_directory:
  106. working_directory = copy;
  107. break;
  108. case doing_command:
  109. currentLine.push_back(copy);
  110. break;
  111. case doing_depends:
  112. {
  113. std::string dep = copy;
  114. cmSystemTools::ConvertToUnixSlashes(dep);
  115. depends.push_back(dep);
  116. }
  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 != targetName.npos)
  133. {
  134. cmOStringStream 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. {
  147. nameOk = targetName.find(":") == std::string::npos;
  148. }
  149. if (!nameOk)
  150. {
  151. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  152. bool issueMessage = false;
  153. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0037))
  154. {
  155. case cmPolicies::WARN:
  156. issueMessage = true;
  157. case cmPolicies::OLD:
  158. break;
  159. case cmPolicies::NEW:
  160. case cmPolicies::REQUIRED_IF_USED:
  161. case cmPolicies::REQUIRED_ALWAYS:
  162. issueMessage = true;
  163. messageType = cmake::FATAL_ERROR;
  164. }
  165. if (issueMessage)
  166. {
  167. cmOStringStream e;
  168. e << (this->Makefile->GetPolicies()
  169. ->GetPolicyWarning(cmPolicies::CMP0037)) << "\n";
  170. e << "The target name \"" << targetName <<
  171. "\" is reserved or not valid for certain "
  172. "CMake features, such as generator expressions, and may result "
  173. "in undefined behavior.";
  174. this->Makefile->IssueMessage(messageType, e.str());
  175. if (messageType == cmake::FATAL_ERROR)
  176. {
  177. return false;
  178. }
  179. }
  180. }
  181. // Store the last command line finished.
  182. if(!currentLine.empty())
  183. {
  184. commandLines.push_back(currentLine);
  185. currentLine.clear();
  186. }
  187. // Enforce name uniqueness.
  188. {
  189. std::string msg;
  190. if(!this->Makefile->EnforceUniqueName(targetName, msg, true))
  191. {
  192. this->SetError(msg);
  193. return false;
  194. }
  195. }
  196. // Convert working directory to a full path.
  197. if(!working_directory.empty())
  198. {
  199. const char* build_dir = this->Makefile->GetCurrentOutputDirectory();
  200. working_directory =
  201. cmSystemTools::CollapseFullPath(working_directory.c_str(), build_dir);
  202. }
  203. // Add the utility target to the makefile.
  204. bool escapeOldStyle = !verbatim;
  205. cmTarget* target =
  206. this->Makefile->AddUtilityCommand(targetName, excludeFromAll,
  207. working_directory.c_str(), depends,
  208. commandLines, escapeOldStyle, comment);
  209. // Add additional user-specified source files to the target.
  210. target->AddSources(sources);
  211. return true;
  212. }