1
0

cmAddCustomCommandCommand.cxx 14 KB

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