cmAddCustomCommandCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 "cmAddCustomCommandCommand.h"
  11. #include "cmTarget.h"
  12. #include "cmSourceFile.h"
  13. // cmAddCustomCommandCommand
  14. bool cmAddCustomCommandCommand
  15. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  16. {
  17. /* Let's complain at the end of this function about the lack of a particular
  18. arg. For the moment, let's say that COMMAND, and either TARGET or SOURCE
  19. are required.
  20. */
  21. if (args.size() < 4)
  22. {
  23. this->SetError("called with wrong number of arguments.");
  24. return false;
  25. }
  26. std::string source, target, main_dependency, working;
  27. std::string comment_buffer;
  28. const char* comment = 0;
  29. std::vector<std::string> depends, outputs, output;
  30. bool verbatim = false;
  31. bool append = false;
  32. std::string implicit_depends_lang;
  33. cmCustomCommand::ImplicitDependsList implicit_depends;
  34. // Accumulate one command line at a time.
  35. cmCustomCommandLine currentLine;
  36. // Save all command lines.
  37. cmCustomCommandLines commandLines;
  38. cmTarget::CustomCommandType cctype = cmTarget::POST_BUILD;
  39. enum tdoing {
  40. doing_source,
  41. doing_command,
  42. doing_target,
  43. doing_depends,
  44. doing_implicit_depends_lang,
  45. doing_implicit_depends_file,
  46. doing_main_dependency,
  47. doing_output,
  48. doing_outputs,
  49. doing_comment,
  50. doing_working_directory,
  51. doing_nothing
  52. };
  53. tdoing doing = doing_nothing;
  54. for (unsigned int j = 0; j < args.size(); ++j)
  55. {
  56. std::string const& copy = args[j];
  57. if(copy == "SOURCE")
  58. {
  59. doing = doing_source;
  60. }
  61. else if(copy == "COMMAND")
  62. {
  63. doing = doing_command;
  64. // Save the current command before starting the next command.
  65. if(!currentLine.empty())
  66. {
  67. commandLines.push_back(currentLine);
  68. currentLine.clear();
  69. }
  70. }
  71. else if(copy == "PRE_BUILD")
  72. {
  73. cctype = cmTarget::PRE_BUILD;
  74. }
  75. else if(copy == "PRE_LINK")
  76. {
  77. cctype = cmTarget::PRE_LINK;
  78. }
  79. else if(copy == "POST_BUILD")
  80. {
  81. cctype = cmTarget::POST_BUILD;
  82. }
  83. else if(copy == "VERBATIM")
  84. {
  85. verbatim = true;
  86. }
  87. else if(copy == "APPEND")
  88. {
  89. append = true;
  90. }
  91. else if(copy == "TARGET")
  92. {
  93. doing = doing_target;
  94. }
  95. else if(copy == "ARGS")
  96. {
  97. // Ignore this old keyword.
  98. }
  99. else if (copy == "DEPENDS")
  100. {
  101. doing = doing_depends;
  102. }
  103. else if (copy == "OUTPUTS")
  104. {
  105. doing = doing_outputs;
  106. }
  107. else if (copy == "OUTPUT")
  108. {
  109. doing = doing_output;
  110. }
  111. else if (copy == "WORKING_DIRECTORY")
  112. {
  113. doing = doing_working_directory;
  114. }
  115. else if (copy == "MAIN_DEPENDENCY")
  116. {
  117. doing = doing_main_dependency;
  118. }
  119. else if (copy == "IMPLICIT_DEPENDS")
  120. {
  121. doing = doing_implicit_depends_lang;
  122. }
  123. else if (copy == "COMMENT")
  124. {
  125. doing = doing_comment;
  126. }
  127. else
  128. {
  129. std::string filename;
  130. switch (doing)
  131. {
  132. case doing_output:
  133. case doing_outputs:
  134. if (!cmSystemTools::FileIsFullPath(copy.c_str()))
  135. {
  136. // This is an output to be generated, so it should be
  137. // under the build tree. CMake 2.4 placed this under the
  138. // source tree. However the only case that this change
  139. // will break is when someone writes
  140. //
  141. // add_custom_command(OUTPUT out.txt ...)
  142. //
  143. // and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
  144. // This is fairly obscure so we can wait for someone to
  145. // complain.
  146. filename = this->Makefile->GetCurrentOutputDirectory();
  147. filename += "/";
  148. }
  149. filename += copy;
  150. cmSystemTools::ConvertToUnixSlashes(filename);
  151. break;
  152. case doing_source:
  153. // We do not want to convert the argument to SOURCE because
  154. // that option is only available for backward compatibility.
  155. // Old-style use of this command may use the SOURCE==TARGET
  156. // trick which we must preserve. If we convert the source
  157. // to a full path then it will no longer equal the target.
  158. default:
  159. break;
  160. }
  161. switch (doing)
  162. {
  163. case doing_working_directory:
  164. working = copy;
  165. break;
  166. case doing_source:
  167. source = copy;
  168. break;
  169. case doing_output:
  170. output.push_back(filename);
  171. break;
  172. case doing_main_dependency:
  173. main_dependency = copy;
  174. break;
  175. case doing_implicit_depends_lang:
  176. implicit_depends_lang = copy;
  177. doing = doing_implicit_depends_file;
  178. break;
  179. case doing_implicit_depends_file:
  180. {
  181. // An implicit dependency starting point is also an
  182. // explicit dependency.
  183. std::string dep = copy;
  184. cmSystemTools::ConvertToUnixSlashes(dep);
  185. depends.push_back(dep);
  186. // Add the implicit dependency language and file.
  187. cmCustomCommand::ImplicitDependsPair
  188. entry(implicit_depends_lang, dep);
  189. implicit_depends.push_back(entry);
  190. // Switch back to looking for a language.
  191. doing = doing_implicit_depends_lang;
  192. }
  193. break;
  194. case doing_command:
  195. currentLine.push_back(copy);
  196. break;
  197. case doing_target:
  198. target = copy;
  199. break;
  200. case doing_depends:
  201. {
  202. std::string dep = copy;
  203. cmSystemTools::ConvertToUnixSlashes(dep);
  204. depends.push_back(dep);
  205. }
  206. break;
  207. case doing_outputs:
  208. outputs.push_back(filename);
  209. break;
  210. case doing_comment:
  211. comment_buffer = copy;
  212. comment = comment_buffer.c_str();
  213. break;
  214. default:
  215. this->SetError("Wrong syntax. Unknown type of argument.");
  216. return false;
  217. }
  218. }
  219. }
  220. // Store the last command line finished.
  221. if(!currentLine.empty())
  222. {
  223. commandLines.push_back(currentLine);
  224. currentLine.clear();
  225. }
  226. // At this point we could complain about the lack of arguments. For
  227. // the moment, let's say that COMMAND, TARGET are always required.
  228. if(output.empty() && target.empty())
  229. {
  230. this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
  231. return false;
  232. }
  233. if(source.empty() && !target.empty() && !output.empty())
  234. {
  235. this->SetError(
  236. "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
  237. return false;
  238. }
  239. if(append && output.empty())
  240. {
  241. this->SetError("given APPEND option with no OUTPUT.");
  242. return false;
  243. }
  244. // Make sure the output names and locations are safe.
  245. if(!this->CheckOutputs(output) || !this->CheckOutputs(outputs))
  246. {
  247. return false;
  248. }
  249. // Check for an append request.
  250. if(append)
  251. {
  252. // Lookup an existing command.
  253. if(cmSourceFile* sf =
  254. this->Makefile->GetSourceFileWithOutput(output[0]))
  255. {
  256. if(cmCustomCommand* cc = sf->GetCustomCommand())
  257. {
  258. cc->AppendCommands(commandLines);
  259. cc->AppendDepends(depends);
  260. cc->AppendImplicitDepends(implicit_depends);
  261. return true;
  262. }
  263. }
  264. // No command for this output exists.
  265. cmOStringStream e;
  266. e << "given APPEND option with output \"" << output[0].c_str()
  267. << "\" which is not already a custom command output.";
  268. this->SetError(e.str());
  269. return false;
  270. }
  271. // Convert working directory to a full path.
  272. if(!working.empty())
  273. {
  274. const char* build_dir = this->Makefile->GetCurrentOutputDirectory();
  275. working = cmSystemTools::CollapseFullPath(working.c_str(), build_dir);
  276. }
  277. // Choose which mode of the command to use.
  278. bool escapeOldStyle = !verbatim;
  279. if(source.empty() && output.empty())
  280. {
  281. // Source is empty, use the target.
  282. std::vector<std::string> no_depends;
  283. this->Makefile->AddCustomCommandToTarget(target, no_depends,
  284. commandLines, cctype,
  285. comment, working.c_str(),
  286. escapeOldStyle);
  287. }
  288. else if(target.empty())
  289. {
  290. // Target is empty, use the output.
  291. this->Makefile->AddCustomCommandToOutput(output, depends,
  292. main_dependency,
  293. commandLines, comment,
  294. working.c_str(), false,
  295. escapeOldStyle);
  296. // Add implicit dependency scanning requests if any were given.
  297. if(!implicit_depends.empty())
  298. {
  299. bool okay = false;
  300. if(cmSourceFile* sf =
  301. this->Makefile->GetSourceFileWithOutput(output[0]))
  302. {
  303. if(cmCustomCommand* cc = sf->GetCustomCommand())
  304. {
  305. okay = true;
  306. cc->SetImplicitDepends(implicit_depends);
  307. }
  308. }
  309. if(!okay)
  310. {
  311. cmOStringStream e;
  312. e << "could not locate source file with a custom command producing \""
  313. << output[0] << "\" even though this command tried to create it!";
  314. this->SetError(e.str());
  315. return false;
  316. }
  317. }
  318. }
  319. else
  320. {
  321. bool issueMessage = true;
  322. cmOStringStream e;
  323. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  324. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0050))
  325. {
  326. case cmPolicies::WARN:
  327. e << (this->Makefile->GetPolicies()
  328. ->GetPolicyWarning(cmPolicies::CMP0050)) << "\n";
  329. break;
  330. case cmPolicies::OLD:
  331. issueMessage = false;
  332. break;
  333. case cmPolicies::REQUIRED_ALWAYS:
  334. case cmPolicies::REQUIRED_IF_USED:
  335. case cmPolicies::NEW:
  336. messageType = cmake::FATAL_ERROR;
  337. break;
  338. }
  339. if (issueMessage)
  340. {
  341. e << "The SOURCE signatures of add_custom_command are no longer "
  342. "supported.";
  343. this->Makefile->IssueMessage(messageType, e.str());
  344. if (messageType == cmake::FATAL_ERROR)
  345. {
  346. return false;
  347. }
  348. }
  349. // Use the old-style mode for backward compatibility.
  350. this->Makefile->AddCustomCommandOldStyle(target, outputs, depends,
  351. source, commandLines,
  352. comment);
  353. }
  354. return true;
  355. }
  356. //----------------------------------------------------------------------------
  357. bool
  358. cmAddCustomCommandCommand
  359. ::CheckOutputs(const std::vector<std::string>& outputs)
  360. {
  361. for(std::vector<std::string>::const_iterator o = outputs.begin();
  362. o != outputs.end(); ++o)
  363. {
  364. // Make sure the file will not be generated into the source
  365. // directory during an out of source build.
  366. if(!this->Makefile->CanIWriteThisFile(o->c_str()))
  367. {
  368. std::string e = "attempted to have a file \"" + *o +
  369. "\" in a source directory as an output of custom command.";
  370. this->SetError(e);
  371. cmSystemTools::SetFatalErrorOccured();
  372. return false;
  373. }
  374. // Make sure the output file name has no invalid characters.
  375. std::string::size_type pos = o->find_first_of("#<>");
  376. if(pos != o->npos)
  377. {
  378. cmOStringStream msg;
  379. msg << "called with OUTPUT containing a \"" << (*o)[pos]
  380. << "\". This character is not allowed.";
  381. this->SetError(msg.str());
  382. return false;
  383. }
  384. }
  385. return true;
  386. }