From 9634a54b8ffd85b1e21d2d0d1dea3d33790487cd Mon Sep 17 00:00:00 2001 From: Hayden Roche Date: Fri, 24 Sep 2021 13:49:30 -0700 Subject: [PATCH] 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. --- cmake/functions.cmake | 45 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/cmake/functions.cmake b/cmake/functions.cmake index 2e96fcf50..6aeb1dbcb 100644 --- a/cmake/functions.cmake +++ b/cmake/functions.cmake @@ -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)