cmAddCustomCommandCommand.cxx 12 KB

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