cmAddCustomCommandCommand.cxx 12 KB

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