cmAddCustomCommandCommand.cxx 7.8 KB

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