cmAddCustomCommandCommand.cxx 13 KB

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