cmAddCustomCommandCommand.cxx 11 KB

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