cmAddCustomCommandCommand.cxx 14 KB

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