cmAddCustomCommandCommand.cxx 12 KB

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