Modernize cmake_parse_arguments
cmake_parse_arguments
is a really nice command to write functiions/macros which are more readable as they would be without. Nevertheless: Using cmake_parse_arguments
itselfs hurts, as currently it's always looks like this:
function(magic_method)
set(options)
set(oneValueArgs LIBRARY PROJECT_NAME)
set(multiValueArgs FILES)
cmake_parse_arguments(PARAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT PARAM_LIBRARY)
message(FATAL_ERROR "Parameter LIBRARY is required.")
endif()
if(NOT PARAM_PROJECT_NAME)
set(PARAM_PROJECT_NAME ${PARAM_LIBRARY})
endif()
if(PARAM_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown parameter: ${PARAM_UNPARSED_ARGUMENTS}.")
endif()
...
endfunction()
Better would be if you could write this like this:
function(magic_method)
cmake_parse_arguments2(
PREFIX myPrefix
REQUIRED_ONE_VALUE_OPTIONS LIBRARY PROJECT_NAME
ARGUMENTS ${ARGN}
)
if(NOT myPrefix_PROJECT_NAME)
set(myPrefix_PROJECT_NAME ${myPrefix_LIBRARY})
endif()
...
endfunction()
So the signature would be:
cmake_parse_arguments2(PREFIX <prefix>
[NO_VALUE_OPTIONS <option> [<option> ...]]
[ONE_VALUE_OPTIONS <option> [<option> ...]]
[MULTIPLE_VALUE_OPTIONS <option> [<option> ...]]
[REQUIRED_NO_VALUE_OPTIONS <option> [<option> ...]]
[REQUIRED_ONE_VALUE_OPTIONS <option> [<option> ...]]
[REQUIRED_MULTIPLE_VALUE_OPTIONS <option> [<option> ...]]
[ALLOW_UNPARSED_ARGUMENTS]
ARGUMENTS <argument> [<argument> ...]
)
So in case ALLOW_UNPARSED_ARGUMENTS
is not set, a FATAL_ERROR
will occur if it could not be resolved. FATAL_ERROR
will also thrown in case an option of REQUIRED_..._OPTIONS
is not present.
Alternate signature could be:
cmake_parse_arguments2(PREFIX <prefix>
[NO_VALUE_OPTIONS <option> [<option> ...]]
[ONE_VALUE_OPTIONS <option> [<option> ...]]
[MULTIPLE_VALUE_OPTIONS <option> [<option> ...]]
[REQUIRED_OPTIONS <option> [<option> ...]]
[MULTIPLE_OPTIONS <option> [<option> ...]]
[ALLOW_UNPARSED_ARGUMENTS]
ARGUMENTS <argument_variable>
)
Changes:
-
REQUIRED_OPTIONS
is an reference to a previous defined option -
ARGUMENTS
accepts a variable name instead of the list of real arguments -
MULTIPLE_OPTIONS
to allow some option to occure more than one time (see #18932)
This modernization would be really helpful, as I have this issue in multiple projects and it would increase readability.