cmAddCustomCommandCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. if (cmSystemTools::FileIsFullPath(filename.c_str()))
  162. {
  163. filename = cmSystemTools::CollapseFullPath(filename);
  164. }
  165. switch (doing)
  166. {
  167. case doing_working_directory:
  168. working = copy;
  169. break;
  170. case doing_source:
  171. source = copy;
  172. break;
  173. case doing_output:
  174. output.push_back(filename);
  175. break;
  176. case doing_main_dependency:
  177. main_dependency = copy;
  178. break;
  179. case doing_implicit_depends_lang:
  180. implicit_depends_lang = copy;
  181. doing = doing_implicit_depends_file;
  182. break;
  183. case doing_implicit_depends_file:
  184. {
  185. // An implicit dependency starting point is also an
  186. // explicit dependency.
  187. std::string dep = copy;
  188. cmSystemTools::ConvertToUnixSlashes(dep);
  189. depends.push_back(dep);
  190. // Add the implicit dependency language and file.
  191. cmCustomCommand::ImplicitDependsPair
  192. entry(implicit_depends_lang, dep);
  193. implicit_depends.push_back(entry);
  194. // Switch back to looking for a language.
  195. doing = doing_implicit_depends_lang;
  196. }
  197. break;
  198. case doing_command:
  199. currentLine.push_back(copy);
  200. break;
  201. case doing_target:
  202. target = copy;
  203. break;
  204. case doing_depends:
  205. {
  206. std::string dep = copy;
  207. cmSystemTools::ConvertToUnixSlashes(dep);
  208. depends.push_back(dep);
  209. }
  210. break;
  211. case doing_outputs:
  212. outputs.push_back(filename);
  213. break;
  214. case doing_comment:
  215. comment_buffer = copy;
  216. comment = comment_buffer.c_str();
  217. break;
  218. default:
  219. this->SetError("Wrong syntax. Unknown type of argument.");
  220. return false;
  221. }
  222. }
  223. }
  224. // Store the last command line finished.
  225. if(!currentLine.empty())
  226. {
  227. commandLines.push_back(currentLine);
  228. currentLine.clear();
  229. }
  230. // At this point we could complain about the lack of arguments. For
  231. // the moment, let's say that COMMAND, TARGET are always required.
  232. if(output.empty() && target.empty())
  233. {
  234. this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
  235. return false;
  236. }
  237. if(source.empty() && !target.empty() && !output.empty())
  238. {
  239. this->SetError(
  240. "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
  241. return false;
  242. }
  243. if(append && output.empty())
  244. {
  245. this->SetError("given APPEND option with no OUTPUT.");
  246. return false;
  247. }
  248. // Make sure the output names and locations are safe.
  249. if(!this->CheckOutputs(output) || !this->CheckOutputs(outputs))
  250. {
  251. return false;
  252. }
  253. // Check for an append request.
  254. if(append)
  255. {
  256. // Lookup an existing command.
  257. if(cmSourceFile* sf =
  258. this->Makefile->GetSourceFileWithOutput(output[0]))
  259. {
  260. if(cmCustomCommand* cc = sf->GetCustomCommand())
  261. {
  262. cc->AppendCommands(commandLines);
  263. cc->AppendDepends(depends);
  264. cc->AppendImplicitDepends(implicit_depends);
  265. return true;
  266. }
  267. }
  268. // No command for this output exists.
  269. cmOStringStream e;
  270. e << "given APPEND option with output \"" << output[0]
  271. << "\" which is not already a custom command output.";
  272. this->SetError(e.str());
  273. return false;
  274. }
  275. // Convert working directory to a full path.
  276. if(!working.empty())
  277. {
  278. const char* build_dir = this->Makefile->GetCurrentOutputDirectory();
  279. working = cmSystemTools::CollapseFullPath(working.c_str(), build_dir);
  280. }
  281. // Choose which mode of the command to use.
  282. bool escapeOldStyle = !verbatim;
  283. if(source.empty() && output.empty())
  284. {
  285. // Source is empty, use the target.
  286. std::vector<std::string> no_depends;
  287. this->Makefile->AddCustomCommandToTarget(target, no_depends,
  288. commandLines, cctype,
  289. comment, working.c_str(),
  290. escapeOldStyle);
  291. }
  292. else if(target.empty())
  293. {
  294. // Target is empty, use the output.
  295. this->Makefile->AddCustomCommandToOutput(output, depends,
  296. main_dependency,
  297. commandLines, comment,
  298. working.c_str(), false,
  299. escapeOldStyle);
  300. // Add implicit dependency scanning requests if any were given.
  301. if(!implicit_depends.empty())
  302. {
  303. bool okay = false;
  304. if(cmSourceFile* sf =
  305. this->Makefile->GetSourceFileWithOutput(output[0]))
  306. {
  307. if(cmCustomCommand* cc = sf->GetCustomCommand())
  308. {
  309. okay = true;
  310. cc->SetImplicitDepends(implicit_depends);
  311. }
  312. }
  313. if(!okay)
  314. {
  315. cmOStringStream e;
  316. e << "could not locate source file with a custom command producing \""
  317. << output[0] << "\" even though this command tried to create it!";
  318. this->SetError(e.str());
  319. return false;
  320. }
  321. }
  322. }
  323. else
  324. {
  325. bool issueMessage = true;
  326. cmOStringStream e;
  327. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  328. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0050))
  329. {
  330. case cmPolicies::WARN:
  331. e << (this->Makefile->GetPolicies()
  332. ->GetPolicyWarning(cmPolicies::CMP0050)) << "\n";
  333. break;
  334. case cmPolicies::OLD:
  335. issueMessage = false;
  336. break;
  337. case cmPolicies::REQUIRED_ALWAYS:
  338. case cmPolicies::REQUIRED_IF_USED:
  339. case cmPolicies::NEW:
  340. messageType = cmake::FATAL_ERROR;
  341. break;
  342. }
  343. if (issueMessage)
  344. {
  345. e << "The SOURCE signatures of add_custom_command are no longer "
  346. "supported.";
  347. this->Makefile->IssueMessage(messageType, e.str());
  348. if (messageType == cmake::FATAL_ERROR)
  349. {
  350. return false;
  351. }
  352. }
  353. // Use the old-style mode for backward compatibility.
  354. this->Makefile->AddCustomCommandOldStyle(target, outputs, depends,
  355. source, commandLines,
  356. comment);
  357. }
  358. return true;
  359. }
  360. //----------------------------------------------------------------------------
  361. bool
  362. cmAddCustomCommandCommand
  363. ::CheckOutputs(const std::vector<std::string>& outputs)
  364. {
  365. for(std::vector<std::string>::const_iterator o = outputs.begin();
  366. o != outputs.end(); ++o)
  367. {
  368. // Make sure the file will not be generated into the source
  369. // directory during an out of source build.
  370. if(!this->Makefile->CanIWriteThisFile(o->c_str()))
  371. {
  372. std::string e = "attempted to have a file \"" + *o +
  373. "\" in a source directory as an output of custom command.";
  374. this->SetError(e);
  375. cmSystemTools::SetFatalErrorOccured();
  376. return false;
  377. }
  378. // Make sure the output file name has no invalid characters.
  379. std::string::size_type pos = o->find_first_of("#<>");
  380. if(pos != o->npos)
  381. {
  382. cmOStringStream msg;
  383. msg << "called with OUTPUT containing a \"" << (*o)[pos]
  384. << "\". This character is not allowed.";
  385. this->SetError(msg.str());
  386. return false;
  387. }
  388. }
  389. return true;
  390. }