cmAddCustomCommandCommand.cxx 12 KB

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