cmAddCustomCommandCommand.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. // 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_main_dependency,
  46. doing_output,
  47. doing_outputs,
  48. doing_comment,
  49. doing_working_directory,
  50. doing_nothing
  51. };
  52. tdoing doing = doing_nothing;
  53. for (unsigned int j = 0; j < args.size(); ++j)
  54. {
  55. std::string const& copy = args[j];
  56. if(copy == "SOURCE")
  57. {
  58. doing = doing_source;
  59. }
  60. else if(copy == "COMMAND")
  61. {
  62. doing = doing_command;
  63. // Save the current command before starting the next command.
  64. if(!currentLine.empty())
  65. {
  66. commandLines.push_back(currentLine);
  67. currentLine.clear();
  68. }
  69. }
  70. else if(copy == "PRE_BUILD")
  71. {
  72. cctype = cmTarget::PRE_BUILD;
  73. }
  74. else if(copy == "PRE_LINK")
  75. {
  76. cctype = cmTarget::PRE_LINK;
  77. }
  78. else if(copy == "POST_BUILD")
  79. {
  80. cctype = cmTarget::POST_BUILD;
  81. }
  82. else if(copy == "VERBATIM")
  83. {
  84. verbatim = true;
  85. }
  86. else if(copy == "APPEND")
  87. {
  88. append = true;
  89. }
  90. else if(copy == "TARGET")
  91. {
  92. doing = doing_target;
  93. }
  94. else if(copy == "ARGS")
  95. {
  96. // Ignore this old keyword.
  97. }
  98. else if (copy == "DEPENDS")
  99. {
  100. doing = doing_depends;
  101. }
  102. else if (copy == "OUTPUTS")
  103. {
  104. doing = doing_outputs;
  105. }
  106. else if (copy == "OUTPUT")
  107. {
  108. doing = doing_output;
  109. }
  110. else if (copy == "WORKING_DIRECTORY")
  111. {
  112. doing = doing_working_directory;
  113. }
  114. else if (copy == "MAIN_DEPENDENCY")
  115. {
  116. doing = doing_main_dependency;
  117. }
  118. else if (copy == "COMMENT")
  119. {
  120. doing = doing_comment;
  121. }
  122. else
  123. {
  124. std::string filename;
  125. switch (doing)
  126. {
  127. case doing_output:
  128. case doing_outputs:
  129. if (!cmSystemTools::FileIsFullPath(copy.c_str()))
  130. {
  131. filename = this->Makefile->GetStartDirectory();
  132. filename += "/";
  133. }
  134. filename += copy;
  135. break;
  136. case doing_source:
  137. // We do not want to convert the argument to SOURCE because
  138. // that option is only available for backward compatibility.
  139. // Old-style use of this command may use the SOURCE==TARGET
  140. // trick which we must preserve. If we convert the source
  141. // to a full path then it will no longer equal the target.
  142. default:
  143. break;
  144. }
  145. switch (doing)
  146. {
  147. case doing_working_directory:
  148. working = copy;
  149. break;
  150. case doing_source:
  151. source = copy;
  152. break;
  153. case doing_output:
  154. output.push_back(filename);
  155. break;
  156. case doing_main_dependency:
  157. main_dependency = copy;
  158. break;
  159. case doing_command:
  160. currentLine.push_back(copy);
  161. break;
  162. case doing_target:
  163. target = copy;
  164. break;
  165. case doing_depends:
  166. depends.push_back(copy);
  167. break;
  168. case doing_outputs:
  169. outputs.push_back(filename);
  170. break;
  171. case doing_comment:
  172. comment_buffer = copy;
  173. comment = comment_buffer.c_str();
  174. break;
  175. default:
  176. this->SetError("Wrong syntax. Unknown type of argument.");
  177. return false;
  178. }
  179. }
  180. }
  181. // Store the last command line finished.
  182. if(!currentLine.empty())
  183. {
  184. commandLines.push_back(currentLine);
  185. currentLine.clear();
  186. }
  187. // At this point we could complain about the lack of arguments. For
  188. // the moment, let's say that COMMAND, TARGET are always required.
  189. if(output.empty() && target.empty())
  190. {
  191. this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
  192. return false;
  193. }
  194. if(source.empty() && !target.empty() && !output.empty())
  195. {
  196. this->SetError(
  197. "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
  198. return false;
  199. }
  200. if(append && output.empty())
  201. {
  202. this->SetError("given APPEND option with no OUTPUT.");
  203. return false;
  204. }
  205. // Make sure the output names and locations are safe.
  206. if(!this->CheckOutputs(output) || !this->CheckOutputs(outputs))
  207. {
  208. return false;
  209. }
  210. // Check for an append request.
  211. if(append)
  212. {
  213. // Lookup an existing command.
  214. if(cmSourceFile* sf =
  215. this->Makefile->GetSourceFileWithOutput(output[0].c_str()))
  216. {
  217. if(cmCustomCommand* cc = sf->GetCustomCommand())
  218. {
  219. cc->AppendCommands(commandLines);
  220. cc->AppendDepends(depends);
  221. return true;
  222. }
  223. }
  224. // No command for this output exists.
  225. cmOStringStream e;
  226. e << "given APPEND option with output \"" << output[0].c_str()
  227. << "\" which is not already a custom command output.";
  228. this->SetError(e.str().c_str());
  229. return false;
  230. }
  231. // Choose which mode of the command to use.
  232. bool escapeOldStyle = !verbatim;
  233. if(source.empty() && output.empty())
  234. {
  235. // Source is empty, use the target.
  236. std::vector<std::string> no_depends;
  237. this->Makefile->AddCustomCommandToTarget(target.c_str(), no_depends,
  238. commandLines, cctype,
  239. comment, working.c_str(),
  240. escapeOldStyle);
  241. }
  242. else if(target.empty())
  243. {
  244. // Target is empty, use the output.
  245. this->Makefile->AddCustomCommandToOutput(output, depends,
  246. main_dependency.c_str(),
  247. commandLines, comment,
  248. working.c_str(), false,
  249. escapeOldStyle);
  250. }
  251. else
  252. {
  253. // Use the old-style mode for backward compatibility.
  254. this->Makefile->AddCustomCommandOldStyle(target.c_str(), outputs, depends,
  255. source.c_str(), commandLines,
  256. comment);
  257. }
  258. return true;
  259. }
  260. //----------------------------------------------------------------------------
  261. bool
  262. cmAddCustomCommandCommand
  263. ::CheckOutputs(const std::vector<std::string>& outputs)
  264. {
  265. for(std::vector<std::string>::const_iterator o = outputs.begin();
  266. o != outputs.end(); ++o)
  267. {
  268. // Make sure the file will not be generated into the source
  269. // directory during an out of source build.
  270. if(!this->Makefile->CanIWriteThisFile(o->c_str()))
  271. {
  272. std::string e = "attempted to have a file \"" + *o +
  273. "\" in a source directory as an output of custom command.";
  274. this->SetError(e.c_str());
  275. cmSystemTools::SetFatalErrorOccured();
  276. return false;
  277. }
  278. // Make sure the output file name has no invalid characters.
  279. std::string::size_type pos = o->find_first_of("#<>");
  280. if(pos != o->npos)
  281. {
  282. cmOStringStream msg;
  283. msg << "called with OUTPUT containing a \"" << (*o)[pos]
  284. << "\". This character is not allowed.";
  285. this->SetError(msg.str().c_str());
  286. return false;
  287. }
  288. }
  289. return true;
  290. }