cmCMakeLanguageCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. std::vector<cmListFileArgument> funcArgs;
  70. funcArgs.reserve(args.size() - startArg);
  71. // The rest of the arguments are passed to the function call above
  72. for (size_t i = startArg; i < args.size(); ++i) {
  73. funcArgs.emplace_back(args[i].Value, args[i].Delim, context.Line);
  74. }
  75. cmListFileFunction func{ callCommand, context.Line, std::move(funcArgs) };
  76. if (defer) {
  77. if (defer->Id.empty()) {
  78. defer->Id = makefile.NewDeferId();
  79. }
  80. if (!defer->IdVar.empty()) {
  81. makefile.AddDefinition(defer->IdVar, defer->Id);
  82. }
  83. cmMakefile* deferMakefile =
  84. defer->Directory ? defer->Directory : &makefile;
  85. if (!deferMakefile->DeferCall(defer->Id, context.FilePath, func)) {
  86. return FatalError(
  87. status,
  88. cmStrCat("DEFER CALL may not be scheduled in directory:\n "_s,
  89. deferMakefile->GetCurrentBinaryDirectory(),
  90. "\nat this time."_s));
  91. }
  92. return true;
  93. }
  94. return makefile.ExecuteCommand(func, status);
  95. }
  96. bool cmCMakeLanguageCommandDEFER(Defer const& defer,
  97. std::vector<std::string> const& args,
  98. size_t arg, cmExecutionStatus& status)
  99. {
  100. cmMakefile* deferMakefile =
  101. defer.Directory ? defer.Directory : &status.GetMakefile();
  102. if (args[arg] == "CANCEL_CALL"_s) {
  103. ++arg; // Consume CANCEL_CALL.
  104. auto ids = cmMakeRange(args).advance(arg);
  105. for (std::string const& id : ids) {
  106. if (id[0] >= 'A' && id[0] <= 'Z') {
  107. return FatalError(
  108. status, cmStrCat("DEFER CANCEL_CALL unknown argument:\n "_s, id));
  109. }
  110. if (!deferMakefile->DeferCancelCall(id)) {
  111. return FatalError(
  112. status,
  113. cmStrCat("DEFER CANCEL_CALL may not update directory:\n "_s,
  114. deferMakefile->GetCurrentBinaryDirectory(),
  115. "\nat this time."_s));
  116. }
  117. }
  118. return true;
  119. }
  120. if (args[arg] == "GET_CALL_IDS"_s) {
  121. ++arg; // Consume GET_CALL_IDS.
  122. if (arg == args.size()) {
  123. return FatalError(status, "DEFER GET_CALL_IDS missing output variable");
  124. }
  125. std::string const& var = args[arg++];
  126. if (arg != args.size()) {
  127. return FatalError(status, "DEFER GET_CALL_IDS given too many arguments");
  128. }
  129. cm::optional<std::string> ids = deferMakefile->DeferGetCallIds();
  130. if (!ids) {
  131. return FatalError(
  132. status,
  133. cmStrCat("DEFER GET_CALL_IDS may not access directory:\n "_s,
  134. deferMakefile->GetCurrentBinaryDirectory(),
  135. "\nat this time."_s));
  136. }
  137. status.GetMakefile().AddDefinition(var, *ids);
  138. return true;
  139. }
  140. if (args[arg] == "GET_CALL"_s) {
  141. ++arg; // Consume GET_CALL.
  142. if (arg == args.size()) {
  143. return FatalError(status, "DEFER GET_CALL missing id");
  144. }
  145. std::string const& id = args[arg++];
  146. if (arg == args.size()) {
  147. return FatalError(status, "DEFER GET_CALL missing output variable");
  148. }
  149. std::string const& var = args[arg++];
  150. if (arg != args.size()) {
  151. return FatalError(status, "DEFER GET_CALL given too many arguments");
  152. }
  153. if (id.empty()) {
  154. return FatalError(status, "DEFER GET_CALL id may not be empty");
  155. }
  156. if (id[0] >= 'A' && id[0] <= 'Z') {
  157. return FatalError(status,
  158. cmStrCat("DEFER GET_CALL unknown argument:\n "_s, id));
  159. }
  160. cm::optional<std::string> call = deferMakefile->DeferGetCall(id);
  161. if (!call) {
  162. return FatalError(
  163. status,
  164. cmStrCat("DEFER GET_CALL may not access directory:\n "_s,
  165. deferMakefile->GetCurrentBinaryDirectory(),
  166. "\nat this time."_s));
  167. }
  168. status.GetMakefile().AddDefinition(var, *call);
  169. return true;
  170. }
  171. return FatalError(status,
  172. cmStrCat("DEFER operation unknown: "_s, args[arg]));
  173. }
  174. bool cmCMakeLanguageCommandEVAL(std::vector<cmListFileArgument> const& args,
  175. cmExecutionStatus& status)
  176. {
  177. cmMakefile& makefile = status.GetMakefile();
  178. cmListFileContext context = makefile.GetBacktrace().Top();
  179. std::vector<std::string> expandedArgs;
  180. makefile.ExpandArguments(args, expandedArgs);
  181. if (expandedArgs.size() < 2) {
  182. return FatalError(status, "called with incorrect number of arguments");
  183. }
  184. if (expandedArgs[1] != "CODE") {
  185. auto code_iter =
  186. std::find(expandedArgs.begin() + 2, expandedArgs.end(), "CODE");
  187. if (code_iter == expandedArgs.end()) {
  188. return FatalError(status, "called without CODE argument");
  189. }
  190. return FatalError(
  191. status,
  192. "called with unsupported arguments between EVAL and CODE arguments");
  193. }
  194. const std::string code =
  195. cmJoin(cmMakeRange(expandedArgs.begin() + 2, expandedArgs.end()), " ");
  196. return makefile.ReadListFileAsString(
  197. code, cmStrCat(context.FilePath, ":", context.Line, ":EVAL"));
  198. }
  199. }
  200. bool cmCMakeLanguageCommand(std::vector<cmListFileArgument> const& args,
  201. cmExecutionStatus& status)
  202. {
  203. std::vector<std::string> expArgs;
  204. size_t rawArg = 0;
  205. size_t expArg = 0;
  206. // Helper to consume and expand one raw argument at a time.
  207. auto moreArgs = [&]() -> bool {
  208. while (expArg >= expArgs.size()) {
  209. if (rawArg >= args.size()) {
  210. return false;
  211. }
  212. std::vector<cmListFileArgument> tmpArg;
  213. tmpArg.emplace_back(args[rawArg++]);
  214. status.GetMakefile().ExpandArguments(tmpArg, expArgs);
  215. }
  216. return true;
  217. };
  218. auto finishArgs = [&]() {
  219. std::vector<cmListFileArgument> tmpArgs(args.begin() + rawArg, args.end());
  220. status.GetMakefile().ExpandArguments(tmpArgs, expArgs);
  221. rawArg = args.size();
  222. };
  223. if (!moreArgs()) {
  224. return FatalError(status, "called with incorrect number of arguments");
  225. }
  226. cm::optional<Defer> maybeDefer;
  227. if (expArgs[expArg] == "DEFER"_s) {
  228. ++expArg; // Consume "DEFER".
  229. if (!moreArgs()) {
  230. return FatalError(status, "DEFER requires at least one argument");
  231. }
  232. Defer defer;
  233. // Process optional arguments.
  234. while (moreArgs()) {
  235. if (expArgs[expArg] == "CALL"_s) {
  236. break;
  237. }
  238. if (expArgs[expArg] == "CANCEL_CALL"_s ||
  239. expArgs[expArg] == "GET_CALL_IDS"_s ||
  240. expArgs[expArg] == "GET_CALL"_s) {
  241. if (!defer.Id.empty() || !defer.IdVar.empty()) {
  242. return FatalError(status,
  243. cmStrCat("DEFER "_s, expArgs[expArg],
  244. " does not accept ID or ID_VAR."_s));
  245. }
  246. finishArgs();
  247. return cmCMakeLanguageCommandDEFER(defer, expArgs, expArg, status);
  248. }
  249. if (expArgs[expArg] == "DIRECTORY"_s) {
  250. ++expArg; // Consume "DIRECTORY".
  251. if (defer.Directory) {
  252. return FatalError(status,
  253. "DEFER given multiple DIRECTORY arguments");
  254. }
  255. if (!moreArgs()) {
  256. return FatalError(status, "DEFER DIRECTORY missing value");
  257. }
  258. std::string dir = expArgs[expArg++];
  259. if (dir.empty()) {
  260. return FatalError(status, "DEFER DIRECTORY may not be empty");
  261. }
  262. dir = cmSystemTools::CollapseFullPath(
  263. dir, status.GetMakefile().GetCurrentSourceDirectory());
  264. defer.Directory =
  265. status.GetMakefile().GetGlobalGenerator()->FindMakefile(dir);
  266. if (!defer.Directory) {
  267. return FatalError(status,
  268. cmStrCat("DEFER DIRECTORY:\n "_s, dir,
  269. "\nis not known. "_s,
  270. "It may not have been processed yet."_s));
  271. }
  272. } else if (expArgs[expArg] == "ID"_s) {
  273. ++expArg; // Consume "ID".
  274. if (!defer.Id.empty()) {
  275. return FatalError(status, "DEFER given multiple ID arguments");
  276. }
  277. if (!moreArgs()) {
  278. return FatalError(status, "DEFER ID missing value");
  279. }
  280. defer.Id = expArgs[expArg++];
  281. if (defer.Id.empty()) {
  282. return FatalError(status, "DEFER ID may not be empty");
  283. }
  284. if (defer.Id[0] >= 'A' && defer.Id[0] <= 'Z') {
  285. return FatalError(status, "DEFER ID may not start in A-Z.");
  286. }
  287. } else if (expArgs[expArg] == "ID_VAR"_s) {
  288. ++expArg; // Consume "ID_VAR".
  289. if (!defer.IdVar.empty()) {
  290. return FatalError(status, "DEFER given multiple ID_VAR arguments");
  291. }
  292. if (!moreArgs()) {
  293. return FatalError(status, "DEFER ID_VAR missing variable name");
  294. }
  295. defer.IdVar = expArgs[expArg++];
  296. if (defer.IdVar.empty()) {
  297. return FatalError(status, "DEFER ID_VAR may not be empty");
  298. }
  299. } else {
  300. return FatalError(
  301. status, cmStrCat("DEFER unknown option:\n "_s, expArgs[expArg]));
  302. }
  303. }
  304. if (!(moreArgs() && expArgs[expArg] == "CALL"_s)) {
  305. return FatalError(status, "DEFER must be followed by a CALL argument");
  306. }
  307. maybeDefer = std::move(defer);
  308. }
  309. if (expArgs[expArg] == "CALL") {
  310. ++expArg; // Consume "CALL".
  311. // CALL requires a command name.
  312. if (!moreArgs()) {
  313. return FatalError(status, "CALL missing command name");
  314. }
  315. std::string const& callCommand = expArgs[expArg++];
  316. // CALL accepts no further expanded arguments.
  317. if (expArg != expArgs.size()) {
  318. return FatalError(status, "CALL command's arguments must be literal");
  319. }
  320. // Run the CALL.
  321. return cmCMakeLanguageCommandCALL(args, callCommand, rawArg,
  322. std::move(maybeDefer), status);
  323. }
  324. if (expArgs[expArg] == "EVAL") {
  325. return cmCMakeLanguageCommandEVAL(args, status);
  326. }
  327. return FatalError(status, "called with unknown meta-operation");
  328. }