cmAddCustomCommandCommand.cxx 13 KB

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