cmAddCustomCommandCommand.cxx 14 KB

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