cmAddCustomCommandCommand.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "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 "cmSourceFile.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. static bool cmAddCustomCommandCommandCheckOutputs(
  19. const std::vector<std::string>& outputs, cmExecutionStatus& status);
  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 comment_buffer;
  39. const char* comment = nullptr;
  40. std::vector<std::string> depends;
  41. std::vector<std::string> outputs;
  42. std::vector<std::string> output;
  43. std::vector<std::string> byproducts;
  44. bool verbatim = false;
  45. bool append = false;
  46. bool uses_terminal = false;
  47. bool command_expand_lists = false;
  48. std::string implicit_depends_lang;
  49. cmImplicitDependsList implicit_depends;
  50. // Accumulate one command line at a time.
  51. cmCustomCommandLine currentLine;
  52. // Save all command lines.
  53. cmCustomCommandLines commandLines;
  54. cmTarget::CustomCommandType cctype = cmTarget::POST_BUILD;
  55. enum tdoing
  56. {
  57. doing_source,
  58. doing_command,
  59. doing_target,
  60. doing_depends,
  61. doing_implicit_depends_lang,
  62. doing_implicit_depends_file,
  63. doing_main_dependency,
  64. doing_output,
  65. doing_outputs,
  66. doing_byproducts,
  67. doing_comment,
  68. doing_working_directory,
  69. doing_depfile,
  70. doing_job_pool,
  71. doing_nothing
  72. };
  73. tdoing doing = doing_nothing;
  74. #define MAKE_STATIC_KEYWORD(KEYWORD) \
  75. static const std::string key##KEYWORD = #KEYWORD
  76. MAKE_STATIC_KEYWORD(APPEND);
  77. MAKE_STATIC_KEYWORD(ARGS);
  78. MAKE_STATIC_KEYWORD(BYPRODUCTS);
  79. MAKE_STATIC_KEYWORD(COMMAND);
  80. MAKE_STATIC_KEYWORD(COMMAND_EXPAND_LISTS);
  81. MAKE_STATIC_KEYWORD(COMMENT);
  82. MAKE_STATIC_KEYWORD(DEPENDS);
  83. MAKE_STATIC_KEYWORD(DEPFILE);
  84. MAKE_STATIC_KEYWORD(IMPLICIT_DEPENDS);
  85. MAKE_STATIC_KEYWORD(JOB_POOL);
  86. MAKE_STATIC_KEYWORD(MAIN_DEPENDENCY);
  87. MAKE_STATIC_KEYWORD(OUTPUT);
  88. MAKE_STATIC_KEYWORD(OUTPUTS);
  89. MAKE_STATIC_KEYWORD(POST_BUILD);
  90. MAKE_STATIC_KEYWORD(PRE_BUILD);
  91. MAKE_STATIC_KEYWORD(PRE_LINK);
  92. MAKE_STATIC_KEYWORD(SOURCE);
  93. MAKE_STATIC_KEYWORD(TARGET);
  94. MAKE_STATIC_KEYWORD(USES_TERMINAL);
  95. MAKE_STATIC_KEYWORD(VERBATIM);
  96. MAKE_STATIC_KEYWORD(WORKING_DIRECTORY);
  97. #undef MAKE_STATIC_KEYWORD
  98. static std::unordered_set<std::string> const keywords{
  99. keyAPPEND,
  100. keyARGS,
  101. keyBYPRODUCTS,
  102. keyCOMMAND,
  103. keyCOMMAND_EXPAND_LISTS,
  104. keyCOMMENT,
  105. keyDEPENDS,
  106. keyDEPFILE,
  107. keyIMPLICIT_DEPENDS,
  108. keyJOB_POOL,
  109. keyMAIN_DEPENDENCY,
  110. keyOUTPUT,
  111. keyOUTPUTS,
  112. keyPOST_BUILD,
  113. keyPRE_BUILD,
  114. keyPRE_LINK,
  115. keySOURCE,
  116. keyTARGET,
  117. keyUSES_TERMINAL,
  118. keyVERBATIM,
  119. keyWORKING_DIRECTORY
  120. };
  121. for (std::string const& copy : args) {
  122. if (keywords.count(copy)) {
  123. if (copy == keySOURCE) {
  124. doing = doing_source;
  125. } else if (copy == keyCOMMAND) {
  126. doing = doing_command;
  127. // Save the current command before starting the next command.
  128. if (!currentLine.empty()) {
  129. commandLines.push_back(currentLine);
  130. currentLine.clear();
  131. }
  132. } else if (copy == keyPRE_BUILD) {
  133. cctype = cmTarget::PRE_BUILD;
  134. } else if (copy == keyPRE_LINK) {
  135. cctype = cmTarget::PRE_LINK;
  136. } else if (copy == keyPOST_BUILD) {
  137. cctype = cmTarget::POST_BUILD;
  138. } else if (copy == keyVERBATIM) {
  139. verbatim = true;
  140. } else if (copy == keyAPPEND) {
  141. append = true;
  142. } else if (copy == keyUSES_TERMINAL) {
  143. uses_terminal = true;
  144. } else if (copy == keyCOMMAND_EXPAND_LISTS) {
  145. command_expand_lists = true;
  146. } else if (copy == keyTARGET) {
  147. doing = doing_target;
  148. } else if (copy == keyARGS) {
  149. // Ignore this old keyword.
  150. } else if (copy == keyDEPENDS) {
  151. doing = doing_depends;
  152. } else if (copy == keyOUTPUTS) {
  153. doing = doing_outputs;
  154. } else if (copy == keyOUTPUT) {
  155. doing = doing_output;
  156. } else if (copy == keyBYPRODUCTS) {
  157. doing = doing_byproducts;
  158. } else if (copy == keyWORKING_DIRECTORY) {
  159. doing = doing_working_directory;
  160. } else if (copy == keyMAIN_DEPENDENCY) {
  161. doing = doing_main_dependency;
  162. } else if (copy == keyIMPLICIT_DEPENDS) {
  163. doing = doing_implicit_depends_lang;
  164. } else if (copy == keyCOMMENT) {
  165. doing = doing_comment;
  166. } else if (copy == keyDEPFILE) {
  167. doing = doing_depfile;
  168. if (mf.GetGlobalGenerator()->GetName() != "Ninja") {
  169. status.SetError("Option DEPFILE not supported by " +
  170. mf.GetGlobalGenerator()->GetName());
  171. return false;
  172. }
  173. } else if (copy == keyJOB_POOL) {
  174. doing = doing_job_pool;
  175. }
  176. } else {
  177. std::string filename;
  178. switch (doing) {
  179. case doing_output:
  180. case doing_outputs:
  181. case doing_byproducts:
  182. if (!cmSystemTools::FileIsFullPath(copy)) {
  183. // This is an output to be generated, so it should be
  184. // under the build tree. CMake 2.4 placed this under the
  185. // source tree. However the only case that this change
  186. // will break is when someone writes
  187. //
  188. // add_custom_command(OUTPUT out.txt ...)
  189. //
  190. // and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
  191. // This is fairly obscure so we can wait for someone to
  192. // complain.
  193. filename = cmStrCat(mf.GetCurrentBinaryDirectory(), '/');
  194. }
  195. filename += copy;
  196. cmSystemTools::ConvertToUnixSlashes(filename);
  197. break;
  198. case doing_source:
  199. // We do not want to convert the argument to SOURCE because
  200. // that option is only available for backward compatibility.
  201. // Old-style use of this command may use the SOURCE==TARGET
  202. // trick which we must preserve. If we convert the source
  203. // to a full path then it will no longer equal the target.
  204. default:
  205. break;
  206. }
  207. if (cmSystemTools::FileIsFullPath(filename)) {
  208. filename = cmSystemTools::CollapseFullPath(filename);
  209. }
  210. switch (doing) {
  211. case doing_depfile:
  212. depfile = copy;
  213. break;
  214. case doing_job_pool:
  215. job_pool = copy;
  216. break;
  217. case doing_working_directory:
  218. working = copy;
  219. break;
  220. case doing_source:
  221. source = copy;
  222. break;
  223. case doing_output:
  224. output.push_back(filename);
  225. break;
  226. case doing_main_dependency:
  227. main_dependency = copy;
  228. break;
  229. case doing_implicit_depends_lang:
  230. implicit_depends_lang = copy;
  231. doing = doing_implicit_depends_file;
  232. break;
  233. case doing_implicit_depends_file: {
  234. // An implicit dependency starting point is also an
  235. // explicit dependency.
  236. std::string dep = copy;
  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. std::string dep = copy;
  252. cmSystemTools::ConvertToUnixSlashes(dep);
  253. depends.push_back(std::move(dep));
  254. } break;
  255. case doing_outputs:
  256. outputs.push_back(filename);
  257. break;
  258. case doing_byproducts:
  259. byproducts.push_back(filename);
  260. break;
  261. case doing_comment:
  262. comment_buffer = copy;
  263. comment = comment_buffer.c_str();
  264. break;
  265. default:
  266. status.SetError("Wrong syntax. Unknown type of argument.");
  267. return false;
  268. }
  269. }
  270. }
  271. // Store the last command line finished.
  272. if (!currentLine.empty()) {
  273. commandLines.push_back(currentLine);
  274. currentLine.clear();
  275. }
  276. // At this point we could complain about the lack of arguments. For
  277. // the moment, let's say that COMMAND, TARGET are always required.
  278. if (output.empty() && target.empty()) {
  279. status.SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
  280. return false;
  281. }
  282. if (source.empty() && !target.empty() && !output.empty()) {
  283. status.SetError(
  284. "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
  285. return false;
  286. }
  287. if (append && output.empty()) {
  288. status.SetError("given APPEND option with no OUTPUT.");
  289. return false;
  290. }
  291. // Make sure the output names and locations are safe.
  292. if (!cmAddCustomCommandCommandCheckOutputs(output, status) ||
  293. !cmAddCustomCommandCommandCheckOutputs(outputs, status) ||
  294. !cmAddCustomCommandCommandCheckOutputs(byproducts, status)) {
  295. return false;
  296. }
  297. // Check for an append request.
  298. if (append) {
  299. // Lookup an existing command.
  300. if (cmSourceFile* sf = mf.GetSourceFileWithOutput(output[0])) {
  301. if (cmCustomCommand* cc = sf->GetCustomCommand()) {
  302. cc->AppendCommands(commandLines);
  303. cc->AppendDepends(depends);
  304. cc->AppendImplicitDepends(implicit_depends);
  305. return true;
  306. }
  307. }
  308. // No command for this output exists.
  309. std::ostringstream e;
  310. e << "given APPEND option with output\n\"" << output[0]
  311. << "\"\nwhich is not already a custom command output.";
  312. status.SetError(e.str());
  313. return false;
  314. }
  315. if (uses_terminal && !job_pool.empty()) {
  316. status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
  317. return false;
  318. }
  319. // Choose which mode of the command to use.
  320. bool escapeOldStyle = !verbatim;
  321. if (source.empty() && output.empty()) {
  322. // Source is empty, use the target.
  323. std::vector<std::string> no_depends;
  324. mf.AddCustomCommandToTarget(target, byproducts, no_depends, commandLines,
  325. cctype, comment, working.c_str(),
  326. escapeOldStyle, uses_terminal, depfile,
  327. job_pool, command_expand_lists);
  328. } else if (target.empty()) {
  329. // Target is empty, use the output.
  330. mf.AddCustomCommandToOutput(
  331. output, byproducts, depends, main_dependency, implicit_depends,
  332. commandLines, comment, working.c_str(), false, escapeOldStyle,
  333. uses_terminal, command_expand_lists, depfile, job_pool);
  334. } else if (!byproducts.empty()) {
  335. status.SetError("BYPRODUCTS may not be specified with SOURCE signatures");
  336. return false;
  337. } else if (uses_terminal) {
  338. status.SetError("USES_TERMINAL may not be used with SOURCE signatures");
  339. return false;
  340. } else {
  341. bool issueMessage = true;
  342. std::ostringstream e;
  343. MessageType messageType = MessageType::AUTHOR_WARNING;
  344. switch (mf.GetPolicyStatus(cmPolicies::CMP0050)) {
  345. case cmPolicies::WARN:
  346. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0050) << "\n";
  347. break;
  348. case cmPolicies::OLD:
  349. issueMessage = false;
  350. break;
  351. case cmPolicies::REQUIRED_ALWAYS:
  352. case cmPolicies::REQUIRED_IF_USED:
  353. case cmPolicies::NEW:
  354. messageType = MessageType::FATAL_ERROR;
  355. break;
  356. }
  357. if (issueMessage) {
  358. e << "The SOURCE signatures of add_custom_command are no longer "
  359. "supported.";
  360. mf.IssueMessage(messageType, e.str());
  361. if (messageType == MessageType::FATAL_ERROR) {
  362. return false;
  363. }
  364. }
  365. // Use the old-style mode for backward compatibility.
  366. mf.AddCustomCommandOldStyle(target, outputs, depends, source, commandLines,
  367. comment);
  368. }
  369. return true;
  370. }
  371. bool cmAddCustomCommandCommandCheckOutputs(
  372. const std::vector<std::string>& outputs, cmExecutionStatus& status)
  373. {
  374. cmMakefile& mf = status.GetMakefile();
  375. for (std::string const& o : outputs) {
  376. // Make sure the file will not be generated into the source
  377. // directory during an out of source build.
  378. if (!mf.CanIWriteThisFile(o)) {
  379. std::string e = "attempted to have a file \"" + o +
  380. "\" in a source directory as an output of custom command.";
  381. status.SetError(e);
  382. cmSystemTools::SetFatalErrorOccured();
  383. return false;
  384. }
  385. // Make sure the output file name has no invalid characters.
  386. std::string::size_type pos = o.find_first_of("#<>");
  387. if (pos != std::string::npos) {
  388. std::ostringstream msg;
  389. msg << "called with OUTPUT containing a \"" << o[pos]
  390. << "\". This character is not allowed.";
  391. status.SetError(msg.str());
  392. return false;
  393. }
  394. }
  395. return true;
  396. }