1
0

cmCMakeLanguageCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "cmCMakeLanguageCommand.h"
  4. #include <algorithm>
  5. #include <array>
  6. #include <cstddef>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <cm/optional>
  11. #include <cm/string_view>
  12. #include <cmext/string_view>
  13. #include "cmExecutionStatus.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmListFileCache.h"
  16. #include "cmMakefile.h"
  17. #include "cmRange.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmSystemTools.h"
  20. namespace {
  21. bool FatalError(cmExecutionStatus& status, std::string const& error)
  22. {
  23. status.SetError(error);
  24. cmSystemTools::SetFatalErrorOccured();
  25. return false;
  26. }
  27. std::array<cm::static_string_view, 12> InvalidCommands{
  28. { // clang-format off
  29. "function"_s, "endfunction"_s,
  30. "macro"_s, "endmacro"_s,
  31. "if"_s, "elseif"_s, "else"_s, "endif"_s,
  32. "while"_s, "endwhile"_s,
  33. "foreach"_s, "endforeach"_s
  34. } // clang-format on
  35. };
  36. std::array<cm::static_string_view, 1> InvalidDeferCommands{
  37. {
  38. // clang-format off
  39. "return"_s,
  40. } // clang-format on
  41. };
  42. struct Defer
  43. {
  44. std::string Id;
  45. std::string IdVar;
  46. cmMakefile* Directory = nullptr;
  47. };
  48. bool cmCMakeLanguageCommandCALL(std::vector<cmListFileArgument> const& args,
  49. std::string const& callCommand,
  50. size_t startArg, cm::optional<Defer> defer,
  51. cmExecutionStatus& status)
  52. {
  53. // ensure specified command is valid
  54. // start/end flow control commands are not allowed
  55. auto cmd = cmSystemTools::LowerCase(callCommand);
  56. if (std::find(InvalidCommands.cbegin(), InvalidCommands.cend(), cmd) !=
  57. InvalidCommands.cend()) {
  58. return FatalError(status,
  59. cmStrCat("invalid command specified: "_s, callCommand));
  60. }
  61. if (defer &&
  62. std::find(InvalidDeferCommands.cbegin(), InvalidDeferCommands.cend(),
  63. cmd) != InvalidDeferCommands.cend()) {
  64. return FatalError(status,
  65. cmStrCat("invalid command specified: "_s, callCommand));
  66. }
  67. cmMakefile& makefile = status.GetMakefile();
  68. cmListFileContext context = makefile.GetBacktrace().Top();
  69. cmListFileFunction func;
  70. func.Name = callCommand;
  71. func.Line = context.Line;
  72. // The rest of the arguments are passed to the function call above
  73. for (size_t i = startArg; i < args.size(); ++i) {
  74. cmListFileArgument lfarg;
  75. lfarg.Delim = args[i].Delim;
  76. lfarg.Line = context.Line;
  77. lfarg.Value = args[i].Value;
  78. func.Arguments.emplace_back(lfarg);
  79. }
  80. if (defer) {
  81. if (defer->Id.empty()) {
  82. defer->Id = makefile.NewDeferId();
  83. }
  84. if (!defer->IdVar.empty()) {
  85. makefile.AddDefinition(defer->IdVar, defer->Id);
  86. }
  87. cmMakefile* deferMakefile =
  88. defer->Directory ? defer->Directory : &makefile;
  89. if (!deferMakefile->DeferCall(defer->Id, context.FilePath, func)) {
  90. return FatalError(
  91. status,
  92. cmStrCat("DEFER CALL may not be scheduled in directory:\n "_s,
  93. deferMakefile->GetCurrentBinaryDirectory(),
  94. "\nat this time."_s));
  95. }
  96. return true;
  97. }
  98. return makefile.ExecuteCommand(func, status);
  99. }
  100. bool cmCMakeLanguageCommandDEFER(Defer const& defer,
  101. std::vector<std::string> const& args,
  102. size_t arg, cmExecutionStatus& status)
  103. {
  104. cmMakefile* deferMakefile =
  105. defer.Directory ? defer.Directory : &status.GetMakefile();
  106. if (args[arg] == "CANCEL_CALL"_s) {
  107. ++arg; // Consume CANCEL_CALL.
  108. auto ids = cmMakeRange(args).advance(arg);
  109. for (std::string const& id : ids) {
  110. if (id[0] >= 'A' && id[0] <= 'Z') {
  111. return FatalError(
  112. status, cmStrCat("DEFER CANCEL_CALL unknown argument:\n "_s, id));
  113. }
  114. if (!deferMakefile->DeferCancelCall(id)) {
  115. return FatalError(
  116. status,
  117. cmStrCat("DEFER CANCEL_CALL may not update directory:\n "_s,
  118. deferMakefile->GetCurrentBinaryDirectory(),
  119. "\nat this time."_s));
  120. }
  121. }
  122. return true;
  123. }
  124. if (args[arg] == "GET_CALL_IDS"_s) {
  125. ++arg; // Consume GET_CALL_IDS.
  126. if (arg == args.size()) {
  127. return FatalError(status, "DEFER GET_CALL_IDS missing output variable");
  128. }
  129. std::string const& var = args[arg++];
  130. if (arg != args.size()) {
  131. return FatalError(status, "DEFER GET_CALL_IDS given too many arguments");
  132. }
  133. cm::optional<std::string> ids = deferMakefile->DeferGetCallIds();
  134. if (!ids) {
  135. return FatalError(
  136. status,
  137. cmStrCat("DEFER GET_CALL_IDS may not access directory:\n "_s,
  138. deferMakefile->GetCurrentBinaryDirectory(),
  139. "\nat this time."_s));
  140. }
  141. status.GetMakefile().AddDefinition(var, *ids);
  142. return true;
  143. }
  144. if (args[arg] == "GET_CALL"_s) {
  145. ++arg; // Consume GET_CALL.
  146. if (arg == args.size()) {
  147. return FatalError(status, "DEFER GET_CALL missing id");
  148. }
  149. std::string const& id = args[arg++];
  150. if (arg == args.size()) {
  151. return FatalError(status, "DEFER GET_CALL missing output variable");
  152. }
  153. std::string const& var = args[arg++];
  154. if (arg != args.size()) {
  155. return FatalError(status, "DEFER GET_CALL given too many arguments");
  156. }
  157. if (id.empty()) {
  158. return FatalError(status, "DEFER GET_CALL id may not be empty");
  159. }
  160. if (id[0] >= 'A' && id[0] <= 'Z') {
  161. return FatalError(status,
  162. cmStrCat("DEFER GET_CALL unknown argument:\n "_s, id));
  163. }
  164. cm::optional<std::string> call = deferMakefile->DeferGetCall(id);
  165. if (!call) {
  166. return FatalError(
  167. status,
  168. cmStrCat("DEFER GET_CALL may not access directory:\n "_s,
  169. deferMakefile->GetCurrentBinaryDirectory(),
  170. "\nat this time."_s));
  171. }
  172. status.GetMakefile().AddDefinition(var, *call);
  173. return true;
  174. }
  175. return FatalError(status,
  176. cmStrCat("DEFER operation unknown: "_s, args[arg]));
  177. }
  178. bool cmCMakeLanguageCommandEVAL(std::vector<cmListFileArgument> const& args,
  179. cmExecutionStatus& status)
  180. {
  181. cmMakefile& makefile = status.GetMakefile();
  182. cmListFileContext context = makefile.GetBacktrace().Top();
  183. std::vector<std::string> expandedArgs;
  184. makefile.ExpandArguments(args, expandedArgs);
  185. if (expandedArgs.size() < 2) {
  186. return FatalError(status, "called with incorrect number of arguments");
  187. }
  188. if (expandedArgs[1] != "CODE") {
  189. auto code_iter =
  190. std::find(expandedArgs.begin() + 2, expandedArgs.end(), "CODE");
  191. if (code_iter == expandedArgs.end()) {
  192. return FatalError(status, "called without CODE argument");
  193. }
  194. return FatalError(
  195. status,
  196. "called with unsupported arguments between EVAL and CODE arguments");
  197. }
  198. const std::string code =
  199. cmJoin(cmMakeRange(expandedArgs.begin() + 2, expandedArgs.end()), " ");
  200. return makefile.ReadListFileAsString(
  201. code, cmStrCat(context.FilePath, ":", context.Line, ":EVAL"));
  202. }
  203. }
  204. bool cmCMakeLanguageCommand(std::vector<cmListFileArgument> const& args,
  205. cmExecutionStatus& status)
  206. {
  207. std::vector<std::string> expArgs;
  208. size_t rawArg = 0;
  209. size_t expArg = 0;
  210. // Helper to consume and expand one raw argument at a time.
  211. auto moreArgs = [&]() -> bool {
  212. while (expArg >= expArgs.size()) {
  213. if (rawArg >= args.size()) {
  214. return false;
  215. }
  216. std::vector<cmListFileArgument> tmpArg;
  217. tmpArg.emplace_back(args[rawArg++]);
  218. status.GetMakefile().ExpandArguments(tmpArg, expArgs);
  219. }
  220. return true;
  221. };
  222. auto finishArgs = [&]() {
  223. std::vector<cmListFileArgument> tmpArgs(args.begin() + rawArg, args.end());
  224. status.GetMakefile().ExpandArguments(tmpArgs, expArgs);
  225. rawArg = args.size();
  226. };
  227. if (!moreArgs()) {
  228. return FatalError(status, "called with incorrect number of arguments");
  229. }
  230. cm::optional<Defer> maybeDefer;
  231. if (expArgs[expArg] == "DEFER"_s) {
  232. ++expArg; // Consume "DEFER".
  233. if (!moreArgs()) {
  234. return FatalError(status, "DEFER requires at least one argument");
  235. }
  236. Defer defer;
  237. // Process optional arguments.
  238. while (moreArgs()) {
  239. if (expArgs[expArg] == "CALL"_s) {
  240. break;
  241. }
  242. if (expArgs[expArg] == "CANCEL_CALL"_s ||
  243. expArgs[expArg] == "GET_CALL_IDS"_s ||
  244. expArgs[expArg] == "GET_CALL"_s) {
  245. if (!defer.Id.empty() || !defer.IdVar.empty()) {
  246. return FatalError(status,
  247. cmStrCat("DEFER "_s, expArgs[expArg],
  248. " does not accept ID or ID_VAR."_s));
  249. }
  250. finishArgs();
  251. return cmCMakeLanguageCommandDEFER(defer, expArgs, expArg, status);
  252. }
  253. if (expArgs[expArg] == "DIRECTORY"_s) {
  254. ++expArg; // Consume "DIRECTORY".
  255. if (defer.Directory) {
  256. return FatalError(status,
  257. "DEFER given multiple DIRECTORY arguments");
  258. }
  259. if (!moreArgs()) {
  260. return FatalError(status, "DEFER DIRECTORY missing value");
  261. }
  262. std::string dir = expArgs[expArg++];
  263. if (dir.empty()) {
  264. return FatalError(status, "DEFER DIRECTORY may not be empty");
  265. }
  266. dir = cmSystemTools::CollapseFullPath(
  267. dir, status.GetMakefile().GetCurrentSourceDirectory());
  268. defer.Directory =
  269. status.GetMakefile().GetGlobalGenerator()->FindMakefile(dir);
  270. if (!defer.Directory) {
  271. return FatalError(status,
  272. cmStrCat("DEFER DIRECTORY:\n "_s, dir,
  273. "\nis not known. "_s,
  274. "It may not have been processed yet."_s));
  275. }
  276. } else if (expArgs[expArg] == "ID"_s) {
  277. ++expArg; // Consume "ID".
  278. if (!defer.Id.empty()) {
  279. return FatalError(status, "DEFER given multiple ID arguments");
  280. }
  281. if (!moreArgs()) {
  282. return FatalError(status, "DEFER ID missing value");
  283. }
  284. defer.Id = expArgs[expArg++];
  285. if (defer.Id.empty()) {
  286. return FatalError(status, "DEFER ID may not be empty");
  287. }
  288. if (defer.Id[0] >= 'A' && defer.Id[0] <= 'Z') {
  289. return FatalError(status, "DEFER ID may not start in A-Z.");
  290. }
  291. } else if (expArgs[expArg] == "ID_VAR"_s) {
  292. ++expArg; // Consume "ID_VAR".
  293. if (!defer.IdVar.empty()) {
  294. return FatalError(status, "DEFER given multiple ID_VAR arguments");
  295. }
  296. if (!moreArgs()) {
  297. return FatalError(status, "DEFER ID_VAR missing variable name");
  298. }
  299. defer.IdVar = expArgs[expArg++];
  300. if (defer.IdVar.empty()) {
  301. return FatalError(status, "DEFER ID_VAR may not be empty");
  302. }
  303. } else {
  304. return FatalError(
  305. status, cmStrCat("DEFER unknown option:\n "_s, expArgs[expArg]));
  306. }
  307. }
  308. if (!(moreArgs() && expArgs[expArg] == "CALL"_s)) {
  309. return FatalError(status, "DEFER must be followed by a CALL argument");
  310. }
  311. maybeDefer = std::move(defer);
  312. }
  313. if (expArgs[expArg] == "CALL") {
  314. ++expArg; // Consume "CALL".
  315. // CALL requires a command name.
  316. if (!moreArgs()) {
  317. return FatalError(status, "CALL missing command name");
  318. }
  319. std::string const& callCommand = expArgs[expArg++];
  320. // CALL accepts no further expanded arguments.
  321. if (expArg != expArgs.size()) {
  322. return FatalError(status, "CALL command's arguments must be literal");
  323. }
  324. // Run the CALL.
  325. return cmCMakeLanguageCommandCALL(args, callCommand, rawArg,
  326. std::move(maybeDefer), status);
  327. }
  328. if (expArgs[expArg] == "EVAL") {
  329. return cmCMakeLanguageCommandEVAL(args, status);
  330. }
  331. return FatalError(status, "called with unknown meta-operation");
  332. }