in cmake , how to define a function which can be called with multiple keyvalue…
- User
in cmake , how to define a function which can be called with multiple keyvalue arguments. and the arguments has same name like add_custom_command can be called like add_custom_command( command xx command yy).
- Assistant
In CMake, you can define a function that accepts multiple key-value arguments by using the
setcommand withARGVand a loop to process the arguments. Here's an example function that demonstrates this:function(my_custom_command) set(options) set(oneValueArgs COMMAND) set(multiValueArgs ARGS) cmake_parse_arguments(MY_CMD "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) # Process the command argument if (MY_CMD_COMMAND) message("Command: ${MY_CMD_COMMAND}") endif() # Process the arguments foreach(arg ${MY_CMD_ARGS}) message("Arg: ${arg}") endforeach() endfunction()You can then call this function with multiple key-value arguments, similar to
add_custom_command:my_custom_command(COMMAND "xx" ARGS "arg1" "arg2" ARGS "arg3" "arg4")This will display the following output:
Command: xx Arg: arg1 Arg: arg2 Arg: arg3 Arg: arg4Note that the
cmake_parse_argumentsmacro is used to handle the parsing of the arguments and store them in variables. You can modify theoneValueArgsandmultiValueArgslists to define the expected argument types.