cmAddCustomCommandCommand.cxx 12 KB

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