cmAddCustomCommandCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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> keywords;
  88. if (keywords.empty()) {
  89. keywords.insert(keyAPPEND);
  90. keywords.insert(keyARGS);
  91. keywords.insert(keyBYPRODUCTS);
  92. keywords.insert(keyCOMMAND);
  93. keywords.insert(keyCOMMAND_EXPAND_LISTS);
  94. keywords.insert(keyCOMMENT);
  95. keywords.insert(keyDEPENDS);
  96. keywords.insert(keyDEPFILE);
  97. keywords.insert(keyIMPLICIT_DEPENDS);
  98. keywords.insert(keyJOB_POOL);
  99. keywords.insert(keyMAIN_DEPENDENCY);
  100. keywords.insert(keyOUTPUT);
  101. keywords.insert(keyOUTPUTS);
  102. keywords.insert(keyPOST_BUILD);
  103. keywords.insert(keyPRE_BUILD);
  104. keywords.insert(keyPRE_LINK);
  105. keywords.insert(keySOURCE);
  106. keywords.insert(keyTARGET);
  107. keywords.insert(keyUSES_TERMINAL);
  108. keywords.insert(keyVERBATIM);
  109. keywords.insert(keyWORKING_DIRECTORY);
  110. }
  111. for (std::string const& copy : args) {
  112. if (keywords.count(copy)) {
  113. if (copy == keySOURCE) {
  114. doing = doing_source;
  115. } else if (copy == keyCOMMAND) {
  116. doing = doing_command;
  117. // Save the current command before starting the next command.
  118. if (!currentLine.empty()) {
  119. commandLines.push_back(currentLine);
  120. currentLine.clear();
  121. }
  122. } else if (copy == keyPRE_BUILD) {
  123. cctype = cmTarget::PRE_BUILD;
  124. } else if (copy == keyPRE_LINK) {
  125. cctype = cmTarget::PRE_LINK;
  126. } else if (copy == keyPOST_BUILD) {
  127. cctype = cmTarget::POST_BUILD;
  128. } else if (copy == keyVERBATIM) {
  129. verbatim = true;
  130. } else if (copy == keyAPPEND) {
  131. append = true;
  132. } else if (copy == keyUSES_TERMINAL) {
  133. uses_terminal = true;
  134. } else if (copy == keyCOMMAND_EXPAND_LISTS) {
  135. command_expand_lists = true;
  136. } else if (copy == keyTARGET) {
  137. doing = doing_target;
  138. } else if (copy == keyARGS) {
  139. // Ignore this old keyword.
  140. } else if (copy == keyDEPENDS) {
  141. doing = doing_depends;
  142. } else if (copy == keyOUTPUTS) {
  143. doing = doing_outputs;
  144. } else if (copy == keyOUTPUT) {
  145. doing = doing_output;
  146. } else if (copy == keyBYPRODUCTS) {
  147. doing = doing_byproducts;
  148. } else if (copy == keyWORKING_DIRECTORY) {
  149. doing = doing_working_directory;
  150. } else if (copy == keyMAIN_DEPENDENCY) {
  151. doing = doing_main_dependency;
  152. } else if (copy == keyIMPLICIT_DEPENDS) {
  153. doing = doing_implicit_depends_lang;
  154. } else if (copy == keyCOMMENT) {
  155. doing = doing_comment;
  156. } else if (copy == keyDEPFILE) {
  157. doing = doing_depfile;
  158. if (this->Makefile->GetGlobalGenerator()->GetName() != "Ninja") {
  159. this->SetError("Option DEPFILE not supported by " +
  160. this->Makefile->GetGlobalGenerator()->GetName());
  161. return false;
  162. }
  163. } else if (copy == keyJOB_POOL) {
  164. doing = doing_job_pool;
  165. }
  166. } else {
  167. std::string filename;
  168. switch (doing) {
  169. case doing_output:
  170. case doing_outputs:
  171. case doing_byproducts:
  172. if (!cmSystemTools::FileIsFullPath(copy)) {
  173. // This is an output to be generated, so it should be
  174. // under the build tree. CMake 2.4 placed this under the
  175. // source tree. However the only case that this change
  176. // will break is when someone writes
  177. //
  178. // add_custom_command(OUTPUT out.txt ...)
  179. //
  180. // and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
  181. // This is fairly obscure so we can wait for someone to
  182. // complain.
  183. filename = this->Makefile->GetCurrentBinaryDirectory();
  184. filename += "/";
  185. }
  186. filename += copy;
  187. cmSystemTools::ConvertToUnixSlashes(filename);
  188. break;
  189. case doing_source:
  190. // We do not want to convert the argument to SOURCE because
  191. // that option is only available for backward compatibility.
  192. // Old-style use of this command may use the SOURCE==TARGET
  193. // trick which we must preserve. If we convert the source
  194. // to a full path then it will no longer equal the target.
  195. default:
  196. break;
  197. }
  198. if (cmSystemTools::FileIsFullPath(filename)) {
  199. filename = cmSystemTools::CollapseFullPath(filename);
  200. }
  201. switch (doing) {
  202. case doing_depfile:
  203. depfile = copy;
  204. break;
  205. case doing_job_pool:
  206. job_pool = copy;
  207. break;
  208. case doing_working_directory:
  209. working = copy;
  210. break;
  211. case doing_source:
  212. source = copy;
  213. break;
  214. case doing_output:
  215. output.push_back(filename);
  216. break;
  217. case doing_main_dependency:
  218. main_dependency = copy;
  219. break;
  220. case doing_implicit_depends_lang:
  221. implicit_depends_lang = copy;
  222. doing = doing_implicit_depends_file;
  223. break;
  224. case doing_implicit_depends_file: {
  225. // An implicit dependency starting point is also an
  226. // explicit dependency.
  227. std::string dep = copy;
  228. cmSystemTools::ConvertToUnixSlashes(dep);
  229. depends.push_back(dep);
  230. // Add the implicit dependency language and file.
  231. implicit_depends.emplace_back(implicit_depends_lang, dep);
  232. // Switch back to looking for a language.
  233. doing = doing_implicit_depends_lang;
  234. } break;
  235. case doing_command:
  236. currentLine.push_back(copy);
  237. break;
  238. case doing_target:
  239. target = copy;
  240. break;
  241. case doing_depends: {
  242. std::string dep = copy;
  243. cmSystemTools::ConvertToUnixSlashes(dep);
  244. depends.push_back(std::move(dep));
  245. } break;
  246. case doing_outputs:
  247. outputs.push_back(filename);
  248. break;
  249. case doing_byproducts:
  250. byproducts.push_back(filename);
  251. break;
  252. case doing_comment:
  253. comment_buffer = copy;
  254. comment = comment_buffer.c_str();
  255. break;
  256. default:
  257. this->SetError("Wrong syntax. Unknown type of argument.");
  258. return false;
  259. }
  260. }
  261. }
  262. // Store the last command line finished.
  263. if (!currentLine.empty()) {
  264. commandLines.push_back(currentLine);
  265. currentLine.clear();
  266. }
  267. // At this point we could complain about the lack of arguments. For
  268. // the moment, let's say that COMMAND, TARGET are always required.
  269. if (output.empty() && target.empty()) {
  270. this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
  271. return false;
  272. }
  273. if (source.empty() && !target.empty() && !output.empty()) {
  274. this->SetError(
  275. "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
  276. return false;
  277. }
  278. if (append && output.empty()) {
  279. this->SetError("given APPEND option with no OUTPUT.");
  280. return false;
  281. }
  282. // Make sure the output names and locations are safe.
  283. if (!this->CheckOutputs(output) || !this->CheckOutputs(outputs) ||
  284. !this->CheckOutputs(byproducts)) {
  285. return false;
  286. }
  287. // Check for an append request.
  288. if (append) {
  289. // Lookup an existing command.
  290. if (cmSourceFile* sf =
  291. this->Makefile->GetSourceFileWithOutput(output[0])) {
  292. if (cmCustomCommand* cc = sf->GetCustomCommand()) {
  293. cc->AppendCommands(commandLines);
  294. cc->AppendDepends(depends);
  295. cc->AppendImplicitDepends(implicit_depends);
  296. return true;
  297. }
  298. }
  299. // No command for this output exists.
  300. std::ostringstream e;
  301. e << "given APPEND option with output\n\"" << output[0]
  302. << "\"\nwhich is not already a custom command output.";
  303. this->SetError(e.str());
  304. return false;
  305. }
  306. if (uses_terminal && !job_pool.empty()) {
  307. this->SetError("JOB_POOL is shadowed by USES_TERMINAL.");
  308. return false;
  309. }
  310. // Choose which mode of the command to use.
  311. bool escapeOldStyle = !verbatim;
  312. if (source.empty() && output.empty()) {
  313. // Source is empty, use the target.
  314. std::vector<std::string> no_depends;
  315. this->Makefile->AddCustomCommandToTarget(
  316. target, byproducts, no_depends, commandLines, cctype, comment,
  317. working.c_str(), escapeOldStyle, uses_terminal, depfile, job_pool,
  318. command_expand_lists);
  319. } else if (target.empty()) {
  320. // Target is empty, use the output.
  321. this->Makefile->AddCustomCommandToOutput(
  322. output, byproducts, depends, main_dependency, commandLines, comment,
  323. working.c_str(), false, escapeOldStyle, uses_terminal,
  324. command_expand_lists, depfile, job_pool);
  325. // Add implicit dependency scanning requests if any were given.
  326. if (!implicit_depends.empty()) {
  327. bool okay = false;
  328. if (cmSourceFile* sf =
  329. this->Makefile->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. this->SetError(e.str());
  340. return false;
  341. }
  342. }
  343. } else if (!byproducts.empty()) {
  344. this->SetError("BYPRODUCTS may not be specified with SOURCE signatures");
  345. return false;
  346. } else if (uses_terminal) {
  347. this->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 (this->Makefile->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. this->Makefile->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. this->Makefile->AddCustomCommandOldStyle(target, outputs, depends, source,
  376. commandLines, comment);
  377. }
  378. return true;
  379. }
  380. bool cmAddCustomCommandCommand::CheckOutputs(
  381. const std::vector<std::string>& outputs)
  382. {
  383. for (std::string const& o : outputs) {
  384. // Make sure the file will not be generated into the source
  385. // directory during an out of source build.
  386. if (!this->Makefile->CanIWriteThisFile(o)) {
  387. std::string e = "attempted to have a file \"" + o +
  388. "\" in a source directory as an output of custom command.";
  389. this->SetError(e);
  390. cmSystemTools::SetFatalErrorOccured();
  391. return false;
  392. }
  393. // Make sure the output file name has no invalid characters.
  394. std::string::size_type pos = o.find_first_of("#<>");
  395. if (pos != std::string::npos) {
  396. std::ostringstream msg;
  397. msg << "called with OUTPUT containing a \"" << o[pos]
  398. << "\". This character is not allowed.";
  399. this->SetError(msg.str());
  400. return false;
  401. }
  402. }
  403. return true;
  404. }