Improve CMake build option handling.

Prior to this commit, we only allowed CMake options to be specified according to
a finite set of values. For example if an option "WOLFSSL_FEATURE" was permitted
to take only the values "yes" and "no" and a user ran
`cmake -DWOLFSSL_FEATURE=ON`, that would fail because ON isn't in `[yes, no]`.
However, this behavior runs counter to CMake's way of evaluating boolean values,
which permits a variety of values that evaluate to true/false (see
https://cmake.org/cmake/help/latest/command/if.html#basic-expressions). This
commit will allow the user to specify any value for a build option. If it's not
in the predefined set of values, we use CMake's "if" logic to reduce the value
to yes or no.
This commit is contained in:
Hayden Roche 2021-09-24 13:49:30 -07:00
parent ec857f6f62
commit 9634a54b8f
1 changed files with 26 additions and 19 deletions

View File

@ -1,27 +1,34 @@
function(add_option NAME HELP_STRING DEFAULT VALUES)
list(FIND VALUES ${DEFAULT} IDX)
if (${IDX} EQUAL -1)
message(FATAL_ERROR "Failed to add option ${NAME}. Default value "
"${DEFAULT} is not in list of possible values: ${VALUES}.")
endif()
if(DEFINED ${NAME})
list(FIND VALUES ${${NAME}} IDX)
if (${IDX} EQUAL -1)
message(FATAL_ERROR "Failed to set option ${NAME}. Value "
"${${NAME}} is not in list of possible values: ${VALUES}.")
endif()
endif()
set(${NAME} ${DEFAULT} CACHE STRING ${HELP_STRING})
set_property(CACHE ${NAME} PROPERTY STRINGS ${VALUES})
endfunction()
function(override_cache VAR VAL)
get_property(VAR_TYPE CACHE ${VAR} PROPERTY TYPE)
set(${VAR} ${VAL} CACHE ${VAR_TYPE} ${${VAR}_HELP_STRING} FORCE)
endfunction()
function(add_option NAME HELP_STRING DEFAULT VALUES)
# Set the default value for the option.
set(${NAME} ${DEFAULT} CACHE STRING ${HELP_STRING})
# Set the list of allowed values for the option.
set_property(CACHE ${NAME} PROPERTY STRINGS ${VALUES})
if(DEFINED ${NAME})
list(FIND VALUES ${${NAME}} IDX)
#
# If the given value isn't in the list of allowed values for the option,
# reduce it to yes/no according to CMake's "if" logic:
# https://cmake.org/cmake/help/latest/command/if.html#basic-expressions
#
# This has no functional impact; it just makes the settings in
# CMakeCache.txt and cmake-gui easier to read.
#
if (${IDX} EQUAL -1)
if(${${NAME}})
override_cache(${NAME} "yes")
else()
override_cache(${NAME} "no")
endif()
endif()
endif()
endfunction()
function(generate_build_flags)
set(BUILD_DISTRO ${WOLFSSL_DISTRO} PARENT_SCOPE)
set(BUILD_ALL ${WOLFSSL_ALL} PARENT_SCOPE)