cmAddCustomCommandCommand.cxx 12 KB

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