cmAddCustomCommandCommand.cxx 12 KB

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