You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
10 KiB

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