cmAddCustomCommandCommand.cxx 12 KB

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