cmAddCustomCommandCommand.cxx 7.7 KB

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