Skip to content
Snippets Groups Projects
Commit 3c5d5257 authored by Brad King's avatar Brad King Committed by Kitware Robot
Browse files

Merge topic 'cmake_command_invoke_expand_function_name'


aa555870 cmake_command: Expand INVOKE function name argument

Acked-by: default avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !4750
parents 8526a8cd aa555870
No related branches found
No related tags found
No related merge requests found
Showing
with 105 additions and 21 deletions
......@@ -4,7 +4,6 @@
#include <algorithm>
#include <cstddef>
#include <iosfwd>
#include <memory>
#include <string>
......@@ -14,13 +13,6 @@
#include "cmRange.h"
#include "cmStringAlgorithms.h"
inline std::ostream& operator<<(std::ostream& os,
cmListFileArgument const& arg)
{
os << arg.Value;
return os;
}
bool cmCMakeCommand(std::vector<cmListFileArgument> const& args,
cmExecutionStatus& status)
{
......@@ -34,20 +26,50 @@ bool cmCMakeCommand(std::vector<cmListFileArgument> const& args,
bool result = false;
if (args[0].Value == "INVOKE") {
if (args.size() == 1) {
std::vector<std::string> dispatchExpandedArgs;
std::vector<cmListFileArgument> dispatchArgs;
dispatchArgs.emplace_back(args[0]);
makefile.ExpandArguments(dispatchArgs, dispatchExpandedArgs);
if (dispatchExpandedArgs.empty()) {
status.SetError("called with incorrect number of arguments");
return false;
}
if (dispatchExpandedArgs[0] == "INVOKE") {
if ((args.size() == 1 && dispatchExpandedArgs.size() != 2) ||
dispatchExpandedArgs.size() > 2) {
status.SetError("called with incorrect number of arguments");
return false;
}
// First argument is the name of the function to call
std::string invokeCommand;
size_t startArg;
if (dispatchExpandedArgs.size() == 1) {
std::vector<std::string> functionExpandedArg;
std::vector<cmListFileArgument> functionArg;
functionArg.emplace_back(args[1]);
makefile.ExpandArguments(functionArg, functionExpandedArg);
if (functionExpandedArg.size() != 1) {
status.SetError("called with incorrect number of arguments");
return false;
}
invokeCommand = functionExpandedArg[0];
startArg = 2;
} else {
invokeCommand = dispatchExpandedArgs[1];
startArg = 1;
}
cmListFileFunction func;
func.Name = args[1].Value;
func.Name = invokeCommand;
func.Line = context.Line;
// The rest of the arguments are passed to the function call above
func.Arguments.resize(args.size() - 1);
for (size_t i = 2; i < args.size(); ++i) {
for (size_t i = startArg; i < args.size(); ++i) {
cmListFileArgument lfarg;
lfarg.Delim = args[i].Delim;
lfarg.Line = context.Line;
......@@ -56,21 +78,29 @@ bool cmCMakeCommand(std::vector<cmListFileArgument> const& args,
}
result = makefile.ExecuteCommand(func, status);
} else if (args[0].Value == "EVAL") {
if (args.size() < 2) {
} else if (dispatchExpandedArgs[0] == "EVAL") {
std::vector<std::string> expandedArgs;
makefile.ExpandArguments(args, expandedArgs);
if (expandedArgs.size() < 2) {
status.SetError("called with incorrect number of arguments");
return false;
}
auto code_iter = std::find_if(
args.begin(), args.end(),
[](cmListFileArgument const& arg) { return arg.Value == "CODE"; });
if (code_iter == args.end()) {
status.SetError("called without CODE argument");
if (expandedArgs[1] != "CODE") {
auto code_iter =
std::find(expandedArgs.begin() + 2, expandedArgs.end(), "CODE");
if (code_iter == expandedArgs.end()) {
status.SetError("called without CODE argument");
} else {
status.SetError(
"called with unsupported arguments between EVAL and CODE arguments");
}
return false;
}
const std::string code = cmJoin(cmMakeRange(++code_iter, args.end()), " ");
const std::string code =
cmJoin(cmMakeRange(expandedArgs.begin() + 2, expandedArgs.end()), " ");
result = makefile.ReadListFileAsString(
code, cmStrCat(context.FilePath, ":", context.Line, ":EVAL"));
} else {
......
......@@ -2,11 +2,20 @@ include(RunCMake)
run_cmake(cmake_command_no_parameters)
run_cmake(cmake_command_unknown_meta_operation)
run_cmake(cmake_command_invoke_double_evaluation)
run_cmake(cmake_command_invoke_expanded_command)
run_cmake(cmake_command_invoke_expanded_command_and_arguments)
run_cmake(cmake_command_invoke_expanded_command_with_explicit_argument)
run_cmake(cmake_command_invoke_expand_command_name)
run_cmake(cmake_command_invoke_expand_function_name)
run_cmake(cmake_command_invoke_message)
run_cmake(cmake_command_invoke_message_fatal_error)
run_cmake(cmake_command_invoke_no_parameters)
run_cmake(cmake_command_invoke_preserve_arguments)
run_cmake(cmake_command_invoke_unknown_function)
run_cmake(cmake_command_eval_expand_command_name)
run_cmake(cmake_command_eval_expanded_command_and_arguments)
run_cmake(cmake_command_eval_extra_parameters_between_eval_and_code)
run_cmake(cmake_command_eval_message)
run_cmake(cmake_command_eval_message_fatal_error)
run_cmake(cmake_command_eval_no_code)
......
OK!
set (my_eval "EVAL")
cmake_command (${my_eval} CODE message("OK!"))
set(cmd EVAL CODE [[message("OK!")]])
cmake_command(${cmd})
CMake Error at cmake_command_eval_extra_parameters_between_eval_and_code.cmake:1 \(cmake_command\):
cmake_command called with unsupported arguments between EVAL and CODE
arguments
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
cmake_command(EVAL BAD CODE "message(BAD CODE)")
var='\${foo}'
set(var [[${foo}]])
cmake_command(INVOKE cmake_command INVOKE message "var='${var}'")
set (my_invoke "INVOKE")
cmake_command (${my_invoke} message "OK!")
function(some_function_1)
message(1)
endfunction()
function(some_function_2)
message(2)
endfunction()
set(function_version 1)
cmake_command(INVOKE some_function_${function_version})
OK!
function (itsok)
message(OK!)
endfunction()
set (cmd INVOKE itsok)
cmake_command (${cmd})
CMake Error at cmake_command_invoke_expanded_command_and_arguments.cmake:2 \(cmake_command\):
cmake_command called with incorrect number of arguments
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
set(invoke_message INVOKE message a b)
cmake_command(${invoke_message})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment