cmCMakeLanguageCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <cm/string_view>
  10. #include <cmext/string_view>
  11. #include "cmExecutionStatus.h"
  12. #include "cmListFileCache.h"
  13. #include "cmMakefile.h"
  14. #include "cmRange.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. namespace {
  18. std::array<cm::static_string_view, 12> InvalidCommands{
  19. { // clang-format off
  20. "function"_s, "endfunction"_s,
  21. "macro"_s, "endmacro"_s,
  22. "if"_s, "elseif"_s, "else"_s, "endif"_s,
  23. "while"_s, "endwhile"_s,
  24. "foreach"_s, "endforeach"_s
  25. } // clang-format on
  26. };
  27. }
  28. bool cmCMakeLanguageCommand(std::vector<cmListFileArgument> const& args,
  29. cmExecutionStatus& status)
  30. {
  31. if (args.empty()) {
  32. status.SetError("called with incorrect number of arguments");
  33. return false;
  34. }
  35. cmMakefile& makefile = status.GetMakefile();
  36. cmListFileContext context = makefile.GetBacktrace().Top();
  37. bool result = false;
  38. std::vector<std::string> dispatchExpandedArgs;
  39. std::vector<cmListFileArgument> dispatchArgs;
  40. dispatchArgs.emplace_back(args[0]);
  41. makefile.ExpandArguments(dispatchArgs, dispatchExpandedArgs);
  42. if (dispatchExpandedArgs.empty()) {
  43. status.SetError("called with incorrect number of arguments");
  44. return false;
  45. }
  46. if (dispatchExpandedArgs[0] == "CALL") {
  47. if ((args.size() == 1 && dispatchExpandedArgs.size() != 2) ||
  48. dispatchExpandedArgs.size() > 2) {
  49. status.SetError("called with incorrect number of arguments");
  50. return false;
  51. }
  52. // First argument is the name of the function to call
  53. std::string callCommand;
  54. size_t startArg;
  55. if (dispatchExpandedArgs.size() == 1) {
  56. std::vector<std::string> functionExpandedArg;
  57. std::vector<cmListFileArgument> functionArg;
  58. functionArg.emplace_back(args[1]);
  59. makefile.ExpandArguments(functionArg, functionExpandedArg);
  60. if (functionExpandedArg.size() != 1) {
  61. status.SetError("called with incorrect number of arguments");
  62. return false;
  63. }
  64. callCommand = functionExpandedArg[0];
  65. startArg = 2;
  66. } else {
  67. callCommand = dispatchExpandedArgs[1];
  68. startArg = 1;
  69. }
  70. // ensure specified command is valid
  71. // start/end flow control commands are not allowed
  72. auto cmd = cmSystemTools::LowerCase(callCommand);
  73. if (std::find(InvalidCommands.cbegin(), InvalidCommands.cend(), cmd) !=
  74. InvalidCommands.cend()) {
  75. status.SetError(cmStrCat("invalid command specified: "_s, callCommand));
  76. return false;
  77. }
  78. cmListFileFunction func;
  79. func.Name = callCommand;
  80. func.Line = context.Line;
  81. // The rest of the arguments are passed to the function call above
  82. for (size_t i = startArg; i < args.size(); ++i) {
  83. cmListFileArgument lfarg;
  84. lfarg.Delim = args[i].Delim;
  85. lfarg.Line = context.Line;
  86. lfarg.Value = args[i].Value;
  87. func.Arguments.emplace_back(lfarg);
  88. }
  89. result = makefile.ExecuteCommand(func, status);
  90. } else if (dispatchExpandedArgs[0] == "EVAL") {
  91. std::vector<std::string> expandedArgs;
  92. makefile.ExpandArguments(args, expandedArgs);
  93. if (expandedArgs.size() < 2) {
  94. status.SetError("called with incorrect number of arguments");
  95. return false;
  96. }
  97. if (expandedArgs[1] != "CODE") {
  98. auto code_iter =
  99. std::find(expandedArgs.begin() + 2, expandedArgs.end(), "CODE");
  100. if (code_iter == expandedArgs.end()) {
  101. status.SetError("called without CODE argument");
  102. } else {
  103. status.SetError(
  104. "called with unsupported arguments between EVAL and CODE arguments");
  105. }
  106. return false;
  107. }
  108. const std::string code =
  109. cmJoin(cmMakeRange(expandedArgs.begin() + 2, expandedArgs.end()), " ");
  110. result = makefile.ReadListFileAsString(
  111. code, cmStrCat(context.FilePath, ":", context.Line, ":EVAL"));
  112. } else {
  113. status.SetError("called with unknown meta-operation");
  114. }
  115. return result;
  116. }