cmAddCustomCommandCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. std::string implicit_depends_lang;
  48. cmImplicitDependsList implicit_depends;
  49. // Accumulate one command line at a time.
  50. cmCustomCommandLine currentLine;
  51. // Save all command lines.
  52. cmCustomCommandLines commandLines;
  53. cmCustomCommandType cctype = cmCustomCommandType::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 = cmCustomCommandType::PRE_BUILD;
  133. } else if (copy == keyPRE_LINK) {
  134. cctype = cmCustomCommandType::PRE_LINK;
  135. } else if (copy == keyPOST_BUILD) {
  136. cctype = cmCustomCommandType::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()->SupportsCustomCommandDepfile()) {
  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. cmGeneratorExpression::Find(copy) != 0) {
  183. // This is an output to be generated, so it should be
  184. // under the build tree.
  185. filename = cmStrCat(mf.GetCurrentBinaryDirectory(), '/');
  186. }
  187. filename += copy;
  188. cmSystemTools::ConvertToUnixSlashes(filename);
  189. break;
  190. case doing_source:
  191. // We do not want to convert the argument to SOURCE because
  192. // that option is only available for backward compatibility.
  193. // Old-style use of this command may use the SOURCE==TARGET
  194. // trick which we must preserve. If we convert the source
  195. // to a full path then it will no longer equal the target.
  196. default:
  197. break;
  198. }
  199. if (cmSystemTools::FileIsFullPath(filename)) {
  200. filename = cmSystemTools::CollapseFullPath(filename);
  201. }
  202. switch (doing) {
  203. case doing_depfile:
  204. depfile = copy;
  205. break;
  206. case doing_job_pool:
  207. job_pool = copy;
  208. break;
  209. case doing_working_directory:
  210. working = copy;
  211. break;
  212. case doing_source:
  213. source = copy;
  214. break;
  215. case doing_output:
  216. output.push_back(filename);
  217. break;
  218. case doing_main_dependency:
  219. main_dependency = copy;
  220. break;
  221. case doing_implicit_depends_lang:
  222. implicit_depends_lang = copy;
  223. doing = doing_implicit_depends_file;
  224. break;
  225. case doing_implicit_depends_file: {
  226. // An implicit dependency starting point is also an
  227. // explicit dependency.
  228. std::string dep = copy;
  229. // Upfront path conversion is correct because Genex
  230. // are not supported.
  231. cmSystemTools::ConvertToUnixSlashes(dep);
  232. depends.push_back(dep);
  233. // Add the implicit dependency language and file.
  234. implicit_depends.emplace_back(implicit_depends_lang, dep);
  235. // Switch back to looking for a language.
  236. doing = doing_implicit_depends_lang;
  237. } break;
  238. case doing_command:
  239. currentLine.push_back(copy);
  240. break;
  241. case doing_target:
  242. target = copy;
  243. break;
  244. case doing_depends:
  245. depends.push_back(copy);
  246. break;
  247. case doing_outputs:
  248. outputs.push_back(filename);
  249. break;
  250. case doing_byproducts:
  251. byproducts.push_back(filename);
  252. break;
  253. case doing_comment:
  254. comment_buffer = copy;
  255. comment = comment_buffer.c_str();
  256. break;
  257. default:
  258. status.SetError("Wrong syntax. Unknown type of argument.");
  259. return false;
  260. }
  261. }
  262. }
  263. // Store the last command line finished.
  264. if (!currentLine.empty()) {
  265. commandLines.push_back(currentLine);
  266. currentLine.clear();
  267. }
  268. // At this point we could complain about the lack of arguments. For
  269. // the moment, let's say that COMMAND, TARGET are always required.
  270. if (output.empty() && target.empty()) {
  271. status.SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
  272. return false;
  273. }
  274. if (source.empty() && !target.empty() && !output.empty()) {
  275. status.SetError(
  276. "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
  277. return false;
  278. }
  279. if (append && output.empty()) {
  280. status.SetError("given APPEND option with no OUTPUT.");
  281. return false;
  282. }
  283. if (!implicit_depends.empty() && !depfile.empty() &&
  284. mf.GetGlobalGenerator()->GetName() != "Ninja") {
  285. // Makefiles generators does not support both at the same time
  286. status.SetError("IMPLICIT_DEPENDS and DEPFILE can not both be specified.");
  287. return false;
  288. }
  289. // Check for an append request.
  290. if (append) {
  291. mf.AppendCustomCommandToOutput(output[0], depends, implicit_depends,
  292. commandLines);
  293. return true;
  294. }
  295. if (uses_terminal && !job_pool.empty()) {
  296. status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
  297. return false;
  298. }
  299. // Choose which mode of the command to use.
  300. auto cc = cm::make_unique<cmCustomCommand>();
  301. cc->SetByproducts(byproducts);
  302. cc->SetCommandLines(commandLines);
  303. cc->SetComment(comment);
  304. cc->SetWorkingDirectory(working.c_str());
  305. cc->SetEscapeOldStyle(!verbatim);
  306. cc->SetUsesTerminal(uses_terminal);
  307. cc->SetDepfile(depfile);
  308. cc->SetJobPool(job_pool);
  309. cc->SetCommandExpandLists(command_expand_lists);
  310. if (source.empty() && output.empty()) {
  311. // Source is empty, use the target.
  312. mf.AddCustomCommandToTarget(target, cctype, std::move(cc));
  313. } else if (target.empty()) {
  314. // Target is empty, use the output.
  315. cc->SetOutputs(output);
  316. cc->SetMainDependency(main_dependency);
  317. cc->SetDepends(depends);
  318. cc->SetImplicitDepends(implicit_depends);
  319. mf.AddCustomCommandToOutput(std::move(cc));
  320. } else if (!byproducts.empty()) {
  321. status.SetError("BYPRODUCTS may not be specified with SOURCE signatures");
  322. return false;
  323. } else if (uses_terminal) {
  324. status.SetError("USES_TERMINAL may not be used with SOURCE signatures");
  325. return false;
  326. } else {
  327. bool issueMessage = true;
  328. std::ostringstream e;
  329. MessageType messageType = MessageType::AUTHOR_WARNING;
  330. switch (mf.GetPolicyStatus(cmPolicies::CMP0050)) {
  331. case cmPolicies::WARN:
  332. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0050) << "\n";
  333. break;
  334. case cmPolicies::OLD:
  335. issueMessage = false;
  336. break;
  337. case cmPolicies::REQUIRED_ALWAYS:
  338. case cmPolicies::REQUIRED_IF_USED:
  339. case cmPolicies::NEW:
  340. messageType = MessageType::FATAL_ERROR;
  341. break;
  342. }
  343. if (issueMessage) {
  344. e << "The SOURCE signatures of add_custom_command are no longer "
  345. "supported.";
  346. mf.IssueMessage(messageType, e.str());
  347. if (messageType == MessageType::FATAL_ERROR) {
  348. return false;
  349. }
  350. }
  351. // Use the old-style mode for backward compatibility.
  352. mf.AddCustomCommandOldStyle(target, outputs, depends, source, commandLines,
  353. comment);
  354. }
  355. return true;
  356. }