cmAddCustomCommandCommand.cxx 13 KB

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