cmAddCustomCommandCommand.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 "cmExecutionStatus.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmMessageType.h"
  12. #include "cmPolicies.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. #include "cmTarget.h"
  16. static bool cmAddCustomCommandCommandCheckOutputs(
  17. const std::vector<std::string>& outputs, cmExecutionStatus& status);
  18. bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
  19. cmExecutionStatus& status)
  20. {
  21. /* Let's complain at the end of this function about the lack of a particular
  22. arg. For the moment, let's say that COMMAND, and either TARGET or SOURCE
  23. are required.
  24. */
  25. if (args.size() < 4) {
  26. status.SetError("called with wrong number of arguments.");
  27. return false;
  28. }
  29. cmMakefile& mf = status.GetMakefile();
  30. std::string source;
  31. std::string target;
  32. std::string main_dependency;
  33. std::string working;
  34. std::string depfile;
  35. std::string job_pool;
  36. std::string comment_buffer;
  37. const char* comment = nullptr;
  38. std::vector<std::string> depends;
  39. std::vector<std::string> outputs;
  40. std::vector<std::string> output;
  41. std::vector<std::string> byproducts;
  42. bool verbatim = false;
  43. bool append = false;
  44. bool uses_terminal = false;
  45. bool command_expand_lists = false;
  46. std::string implicit_depends_lang;
  47. cmImplicitDependsList implicit_depends;
  48. // Accumulate one command line at a time.
  49. cmCustomCommandLine currentLine;
  50. // Save all command lines.
  51. cmCustomCommandLines commandLines;
  52. cmTarget::CustomCommandType cctype = cmTarget::POST_BUILD;
  53. enum tdoing
  54. {
  55. doing_source,
  56. doing_command,
  57. doing_target,
  58. doing_depends,
  59. doing_implicit_depends_lang,
  60. doing_implicit_depends_file,
  61. doing_main_dependency,
  62. doing_output,
  63. doing_outputs,
  64. doing_byproducts,
  65. doing_comment,
  66. doing_working_directory,
  67. doing_depfile,
  68. doing_job_pool,
  69. doing_nothing
  70. };
  71. tdoing doing = doing_nothing;
  72. #define MAKE_STATIC_KEYWORD(KEYWORD) \
  73. static const std::string key##KEYWORD = #KEYWORD
  74. MAKE_STATIC_KEYWORD(APPEND);
  75. MAKE_STATIC_KEYWORD(ARGS);
  76. MAKE_STATIC_KEYWORD(BYPRODUCTS);
  77. MAKE_STATIC_KEYWORD(COMMAND);
  78. MAKE_STATIC_KEYWORD(COMMAND_EXPAND_LISTS);
  79. MAKE_STATIC_KEYWORD(COMMENT);
  80. MAKE_STATIC_KEYWORD(DEPENDS);
  81. MAKE_STATIC_KEYWORD(DEPFILE);
  82. MAKE_STATIC_KEYWORD(IMPLICIT_DEPENDS);
  83. MAKE_STATIC_KEYWORD(JOB_POOL);
  84. MAKE_STATIC_KEYWORD(MAIN_DEPENDENCY);
  85. MAKE_STATIC_KEYWORD(OUTPUT);
  86. MAKE_STATIC_KEYWORD(OUTPUTS);
  87. MAKE_STATIC_KEYWORD(POST_BUILD);
  88. MAKE_STATIC_KEYWORD(PRE_BUILD);
  89. MAKE_STATIC_KEYWORD(PRE_LINK);
  90. MAKE_STATIC_KEYWORD(SOURCE);
  91. MAKE_STATIC_KEYWORD(TARGET);
  92. MAKE_STATIC_KEYWORD(USES_TERMINAL);
  93. MAKE_STATIC_KEYWORD(VERBATIM);
  94. MAKE_STATIC_KEYWORD(WORKING_DIRECTORY);
  95. #undef MAKE_STATIC_KEYWORD
  96. static std::unordered_set<std::string> const keywords{
  97. keyAPPEND,
  98. keyARGS,
  99. keyBYPRODUCTS,
  100. keyCOMMAND,
  101. keyCOMMAND_EXPAND_LISTS,
  102. keyCOMMENT,
  103. keyDEPENDS,
  104. keyDEPFILE,
  105. keyIMPLICIT_DEPENDS,
  106. keyJOB_POOL,
  107. keyMAIN_DEPENDENCY,
  108. keyOUTPUT,
  109. keyOUTPUTS,
  110. keyPOST_BUILD,
  111. keyPRE_BUILD,
  112. keyPRE_LINK,
  113. keySOURCE,
  114. keyTARGET,
  115. keyUSES_TERMINAL,
  116. keyVERBATIM,
  117. keyWORKING_DIRECTORY
  118. };
  119. for (std::string const& copy : args) {
  120. if (keywords.count(copy)) {
  121. if (copy == keySOURCE) {
  122. doing = doing_source;
  123. } else if (copy == keyCOMMAND) {
  124. doing = doing_command;
  125. // Save the current command before starting the next command.
  126. if (!currentLine.empty()) {
  127. commandLines.push_back(currentLine);
  128. currentLine.clear();
  129. }
  130. } else if (copy == keyPRE_BUILD) {
  131. cctype = cmTarget::PRE_BUILD;
  132. } else if (copy == keyPRE_LINK) {
  133. cctype = cmTarget::PRE_LINK;
  134. } else if (copy == keyPOST_BUILD) {
  135. cctype = cmTarget::POST_BUILD;
  136. } else if (copy == keyVERBATIM) {
  137. verbatim = true;
  138. } else if (copy == keyAPPEND) {
  139. append = true;
  140. } else if (copy == keyUSES_TERMINAL) {
  141. uses_terminal = true;
  142. } else if (copy == keyCOMMAND_EXPAND_LISTS) {
  143. command_expand_lists = true;
  144. } else if (copy == keyTARGET) {
  145. doing = doing_target;
  146. } else if (copy == keyARGS) {
  147. // Ignore this old keyword.
  148. } else if (copy == keyDEPENDS) {
  149. doing = doing_depends;
  150. } else if (copy == keyOUTPUTS) {
  151. doing = doing_outputs;
  152. } else if (copy == keyOUTPUT) {
  153. doing = doing_output;
  154. } else if (copy == keyBYPRODUCTS) {
  155. doing = doing_byproducts;
  156. } else if (copy == keyWORKING_DIRECTORY) {
  157. doing = doing_working_directory;
  158. } else if (copy == keyMAIN_DEPENDENCY) {
  159. doing = doing_main_dependency;
  160. } else if (copy == keyIMPLICIT_DEPENDS) {
  161. doing = doing_implicit_depends_lang;
  162. } else if (copy == keyCOMMENT) {
  163. doing = doing_comment;
  164. } else if (copy == keyDEPFILE) {
  165. doing = doing_depfile;
  166. if (mf.GetGlobalGenerator()->GetName() != "Ninja") {
  167. status.SetError("Option DEPFILE not supported by " +
  168. mf.GetGlobalGenerator()->GetName());
  169. return false;
  170. }
  171. } else if (copy == keyJOB_POOL) {
  172. doing = doing_job_pool;
  173. }
  174. } else {
  175. std::string filename;
  176. switch (doing) {
  177. case doing_output:
  178. case doing_outputs:
  179. case doing_byproducts:
  180. if (!cmSystemTools::FileIsFullPath(copy)) {
  181. // This is an output to be generated, so it should be
  182. // under the build tree. CMake 2.4 placed this under the
  183. // source tree. However the only case that this change
  184. // will break is when someone writes
  185. //
  186. // add_custom_command(OUTPUT out.txt ...)
  187. //
  188. // and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
  189. // This is fairly obscure so we can wait for someone to
  190. // complain.
  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. // Make sure the output names and locations are safe.
  290. if (!cmAddCustomCommandCommandCheckOutputs(output, status) ||
  291. !cmAddCustomCommandCommandCheckOutputs(outputs, status) ||
  292. !cmAddCustomCommandCommandCheckOutputs(byproducts, status)) {
  293. return false;
  294. }
  295. // Check for an append request.
  296. if (append) {
  297. if (mf.AppendCustomCommandToOutput(output[0], depends, implicit_depends,
  298. commandLines)) {
  299. return true;
  300. }
  301. // No command for this output exists.
  302. std::ostringstream e;
  303. e << "given APPEND option with output\n\"" << output[0]
  304. << "\"\nwhich is not already a custom command output.";
  305. status.SetError(e.str());
  306. return false;
  307. }
  308. if (uses_terminal && !job_pool.empty()) {
  309. status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
  310. return false;
  311. }
  312. // Choose which mode of the command to use.
  313. bool escapeOldStyle = !verbatim;
  314. if (source.empty() && output.empty()) {
  315. // Source is empty, use the target.
  316. std::vector<std::string> no_depends;
  317. mf.AddCustomCommandToTarget(target, byproducts, no_depends, commandLines,
  318. cctype, comment, working.c_str(),
  319. escapeOldStyle, uses_terminal, depfile,
  320. job_pool, command_expand_lists);
  321. } else if (target.empty()) {
  322. // Target is empty, use the output.
  323. mf.AddCustomCommandToOutput(
  324. output, byproducts, depends, main_dependency, implicit_depends,
  325. commandLines, comment, working.c_str(), false, escapeOldStyle,
  326. uses_terminal, command_expand_lists, depfile, job_pool);
  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. }
  364. bool cmAddCustomCommandCommandCheckOutputs(
  365. const std::vector<std::string>& outputs, cmExecutionStatus& status)
  366. {
  367. cmMakefile& mf = status.GetMakefile();
  368. for (std::string const& o : outputs) {
  369. // Make sure the file will not be generated into the source
  370. // directory during an out of source build.
  371. if (!mf.CanIWriteThisFile(o)) {
  372. std::string e = "attempted to have a file \"" + o +
  373. "\" in a source directory as an output of custom command.";
  374. status.SetError(e);
  375. cmSystemTools::SetFatalErrorOccured();
  376. return false;
  377. }
  378. // Make sure the output file name has no invalid characters.
  379. std::string::size_type pos = o.find_first_of("#<>");
  380. if (pos != std::string::npos) {
  381. std::ostringstream msg;
  382. msg << "called with OUTPUT containing a \"" << o[pos]
  383. << "\". This character is not allowed.";
  384. status.SetError(msg.str());
  385. return false;
  386. }
  387. }
  388. return true;
  389. }