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