For example, I want write a my_add_lib
function with the following syntax:
my_add_lib(NAME <name>
SRCS [src1 [src2 ...]]
DEPS [dependency1 [dependancy2 ...]]])
How to do implement these "parameter group"?
If you create the cache variable in the CMakeLists. txt file and then pass the argument via calling cmake, won't the CMakeList. txt file keep overwriting the argument value? No, the cache is populated on the first run with either the default value, or the value supplied on the command line if it is provided.
CMakeParseArguments. CMake has a predefined command to parse function and macro arguments. This command is for use in macros or functions. It processes the arguments given to that macro or function, and defines a set of variables which hold the values of the respective options.
Return values from functions There is no built-in notion of a return value from a function. To get values out of a function, write to one of the arguments. Another issue is the variable name for the output value needs to be dereferenced before being set.
CMake provides CMakeParseArguments module which can do arguments parsing for you. Example:
include(CMakeParseArguments)
function(my_add_lib)
cmake_parse_arguments(
PARSED_ARGS # prefix of output variables
"" # list of names of the boolean arguments (only defined ones will be true)
"NAME" # list of names of mono-valued arguments
"SRCS;DEPS" # list of names of multi-valued arguments (output variables are lists)
${ARGN} # arguments of the function to parse, here we take the all original ones
)
# note: if it remains unparsed arguments, here, they can be found in variable PARSED_ARGS_UNPARSED_ARGUMENTS
if(NOT PARSED_ARGS_NAME)
message(FATAL_ERROR "You must provide a name")
endif(NOT PARSED_ARGS_NAME)
message("Provided sources are:")
foreach(src ${PARSED_ARGS_SRCS})
message("- ${src}")
endforeach(src)
endfunction(my_add_lib)
As of CMake 3.5, cmake_parse_arguments
becomes a builtin command (written in C++ instead of CMake): include(CMakeParseArguments)
is no longer required but, for now, the file CMakeParseArguments.cmake is kept empty for compatibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With