cmAddCustomCommandCommand.cxx 12 KB

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