cmAddCustomCommandCommand.cxx 14 KB

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