cmAddCustomCommandCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. depends.push_back(copy);
  184. // Add the implicit dependency language and file.
  185. cmCustomCommand::ImplicitDependsPair
  186. entry(implicit_depends_lang, copy);
  187. implicit_depends.push_back(entry);
  188. // Switch back to looking for a language.
  189. doing = doing_implicit_depends_lang;
  190. }
  191. break;
  192. case doing_command:
  193. currentLine.push_back(copy);
  194. break;
  195. case doing_target:
  196. target = copy;
  197. break;
  198. case doing_depends:
  199. depends.push_back(copy);
  200. break;
  201. case doing_outputs:
  202. outputs.push_back(filename);
  203. break;
  204. case doing_comment:
  205. comment_buffer = copy;
  206. comment = comment_buffer.c_str();
  207. break;
  208. default:
  209. this->SetError("Wrong syntax. Unknown type of argument.");
  210. return false;
  211. }
  212. }
  213. }
  214. // Store the last command line finished.
  215. if(!currentLine.empty())
  216. {
  217. commandLines.push_back(currentLine);
  218. currentLine.clear();
  219. }
  220. // At this point we could complain about the lack of arguments. For
  221. // the moment, let's say that COMMAND, TARGET are always required.
  222. if(output.empty() && target.empty())
  223. {
  224. this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
  225. return false;
  226. }
  227. if(source.empty() && !target.empty() && !output.empty())
  228. {
  229. this->SetError(
  230. "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
  231. return false;
  232. }
  233. if(append && output.empty())
  234. {
  235. this->SetError("given APPEND option with no OUTPUT.");
  236. return false;
  237. }
  238. // Make sure the output names and locations are safe.
  239. if(!this->CheckOutputs(output) || !this->CheckOutputs(outputs))
  240. {
  241. return false;
  242. }
  243. // Check for an append request.
  244. if(append)
  245. {
  246. // Lookup an existing command.
  247. if(cmSourceFile* sf =
  248. this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
  249. {
  250. if(cmCustomCommand* cc = sf->GetCustomCommand())
  251. {
  252. cc->AppendCommands(commandLines);
  253. cc->AppendDepends(depends);
  254. cc->AppendImplicitDepends(implicit_depends);
  255. return true;
  256. }
  257. }
  258. // No command for this output exists.
  259. cmOStringStream e;
  260. e << "given APPEND option with output \"" << output[0].c_str()
  261. << "\" which is not already a custom command output.";
  262. this->SetError(e.str().c_str());
  263. return false;
  264. }
  265. // Choose which mode of the command to use.
  266. bool escapeOldStyle = !verbatim;
  267. if(source.empty() && output.empty())
  268. {
  269. // Source is empty, use the target.
  270. std::vector<std::string> no_depends;
  271. this->Makefile->AddCustomCommandToTarget(target.c_str(), no_depends,
  272. commandLines, cctype,
  273. comment, working.c_str(),
  274. escapeOldStyle);
  275. }
  276. else if(target.empty())
  277. {
  278. // Target is empty, use the output.
  279. this->Makefile->AddCustomCommandToOutput(output, depends,
  280. main_dependency.c_str(),
  281. commandLines, comment,
  282. working.c_str(), false,
  283. escapeOldStyle);
  284. // Add implicit dependency scanning requests if any were given.
  285. if(!implicit_depends.empty())
  286. {
  287. bool okay = false;
  288. if(cmSourceFile* sf =
  289. this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
  290. {
  291. if(cmCustomCommand* cc = sf->GetCustomCommand())
  292. {
  293. okay = true;
  294. cc->SetImplicitDepends(implicit_depends);
  295. }
  296. }
  297. if(!okay)
  298. {
  299. cmOStringStream e;
  300. e << "could not locate source file with a custom command producing \""
  301. << output[0] << "\" even though this command tried to create it!";
  302. this->SetError(e.str().c_str());
  303. return false;
  304. }
  305. }
  306. }
  307. else
  308. {
  309. // Use the old-style mode for backward compatibility.
  310. this->Makefile->AddCustomCommandOldStyle(target.c_str(), outputs, depends,
  311. source.c_str(), commandLines,
  312. comment);
  313. }
  314. return true;
  315. }
  316. //----------------------------------------------------------------------------
  317. bool
  318. cmAddCustomCommandCommand
  319. ::CheckOutputs(const std::vector<std::string>& outputs)
  320. {
  321. for(std::vector<std::string>::const_iterator o = outputs.begin();
  322. o != outputs.end(); ++o)
  323. {
  324. // Make sure the file will not be generated into the source
  325. // directory during an out of source build.
  326. if(!this->Makefile->CanIWriteThisFile(o->c_str()))
  327. {
  328. std::string e = "attempted to have a file \"" + *o +
  329. "\" in a source directory as an output of custom command.";
  330. this->SetError(e.c_str());
  331. cmSystemTools::SetFatalErrorOccured();
  332. return false;
  333. }
  334. // Make sure the output file name has no invalid characters.
  335. std::string::size_type pos = o->find_first_of("#<>");
  336. if(pos != o->npos)
  337. {
  338. cmOStringStream msg;
  339. msg << "called with OUTPUT containing a \"" << (*o)[pos]
  340. << "\". This character is not allowed.";
  341. this->SetError(msg.str().c_str());
  342. return false;
  343. }
  344. }
  345. return true;
  346. }