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 "cmCheckCustomOutputs.h"
  7. #include "cmCustomCommand.h"
  8. #include "cmCustomCommandLines.h"
  9. #include "cmExecutionStatus.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmMessageType.h"
  13. #include "cmPolicies.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.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. cmTarget::CustomCommandType cctype = cmTarget::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 = cmTarget::PRE_BUILD;
  131. } else if (copy == keyPRE_LINK) {
  132. cctype = cmTarget::PRE_LINK;
  133. } else if (copy == keyPOST_BUILD) {
  134. cctype = cmTarget::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()->GetName() != "Ninja") {
  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. // This is an output to be generated, so it should be
  181. // under the build tree. CMake 2.4 placed this under the
  182. // source tree. However the only case that this change
  183. // will break is when someone writes
  184. //
  185. // add_custom_command(OUTPUT out.txt ...)
  186. //
  187. // and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
  188. // This is fairly obscure so we can wait for someone to
  189. // complain.
  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. // Make sure the output names and locations are safe.
  289. if (!cmCheckCustomOutputs(output, "OUTPUT", status) ||
  290. !cmCheckCustomOutputs(outputs, "OUTPUTS", status) ||
  291. !cmCheckCustomOutputs(byproducts, "BYPRODUCTS", status)) {
  292. return false;
  293. }
  294. // Check for an append request.
  295. if (append) {
  296. if (mf.AppendCustomCommandToOutput(output[0], depends, implicit_depends,
  297. commandLines)) {
  298. return true;
  299. }
  300. // No command for this output exists.
  301. status.SetError(
  302. cmStrCat("given APPEND option with output\n ", output[0],
  303. "\nwhich is not already a custom command output."));
  304. return false;
  305. }
  306. if (uses_terminal && !job_pool.empty()) {
  307. status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
  308. return false;
  309. }
  310. // Choose which mode of the command to use.
  311. bool escapeOldStyle = !verbatim;
  312. if (source.empty() && output.empty()) {
  313. // Source is empty, use the target.
  314. std::vector<std::string> no_depends;
  315. mf.AddCustomCommandToTarget(target, byproducts, no_depends, commandLines,
  316. cctype, comment, working.c_str(),
  317. escapeOldStyle, uses_terminal, depfile,
  318. job_pool, command_expand_lists);
  319. } else if (target.empty()) {
  320. // Target is empty, use the output.
  321. mf.AddCustomCommandToOutput(
  322. output, byproducts, depends, main_dependency, implicit_depends,
  323. commandLines, comment, working.c_str(), false, escapeOldStyle,
  324. uses_terminal, command_expand_lists, depfile, job_pool);
  325. } else if (!byproducts.empty()) {
  326. status.SetError("BYPRODUCTS may not be specified with SOURCE signatures");
  327. return false;
  328. } else if (uses_terminal) {
  329. status.SetError("USES_TERMINAL may not be used with SOURCE signatures");
  330. return false;
  331. } else {
  332. bool issueMessage = true;
  333. std::ostringstream e;
  334. MessageType messageType = MessageType::AUTHOR_WARNING;
  335. switch (mf.GetPolicyStatus(cmPolicies::CMP0050)) {
  336. case cmPolicies::WARN:
  337. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0050) << "\n";
  338. break;
  339. case cmPolicies::OLD:
  340. issueMessage = false;
  341. break;
  342. case cmPolicies::REQUIRED_ALWAYS:
  343. case cmPolicies::REQUIRED_IF_USED:
  344. case cmPolicies::NEW:
  345. messageType = MessageType::FATAL_ERROR;
  346. break;
  347. }
  348. if (issueMessage) {
  349. e << "The SOURCE signatures of add_custom_command are no longer "
  350. "supported.";
  351. mf.IssueMessage(messageType, e.str());
  352. if (messageType == MessageType::FATAL_ERROR) {
  353. return false;
  354. }
  355. }
  356. // Use the old-style mode for backward compatibility.
  357. mf.AddCustomCommandOldStyle(target, outputs, depends, source, commandLines,
  358. comment);
  359. }
  360. return true;
  361. }