From a8fda5f4350f4eb1e4fbac501e1eb4c60fd9641c Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Mon, 11 Nov 2024 21:07:21 +0000 Subject: [PATCH] cmake: new add_option_defs helper, add KEYLOG_EXPORT build param Add add_option_defs helper which cover simple case of setting compile definitions when build option is selected. New helper uses named arguments and adds following QoL improvements: - optionally infer default values from list of possible values. First value of allowed values list becomes default. - optionally append default value to helper string. Helper string always reflects accurate default value. - support adding defines if build option is set. It covers many simple cases like new WOLFSSL_KEYLOG_EXPORT. --- CMakeLists.txt | 7 ++++++- cmake/functions.cmake | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 72e6550b5c..6ff96d59c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -873,7 +873,7 @@ endif() # - SEP add_option("WOLFSSL_KEYGEN" - "Enable key generation (default: disabled)])" + "Enable key generation (default: disabled)" "no" "yes;no") add_option("WOLFSSL_CERTGEN" @@ -2279,6 +2279,11 @@ if (ENABLE_SCCACHE AND (NOT WOLFSSL_SCCACHE_ALREADY_SET_FLAG)) endif() endif() +add_option_defs(NAME WOLFSSL_KEYLOG_EXPORT + HELP_STRING "Enable insecure export of TLS secrets to an NSS keylog file" + VALUES no yes + DEFINITIONS SHOW_SECRETS HAVE_SECRET_CALLBACK WOLFSSL_SSLKEYLOGFILE WOLFSSL_KEYLOG_EXPORT_WARNED) + file(REMOVE ${OPTION_FILE}) diff --git a/cmake/functions.cmake b/cmake/functions.cmake index 3c8832c2c3..9ab4aba9b9 100644 --- a/cmake/functions.cmake +++ b/cmake/functions.cmake @@ -9,19 +9,27 @@ function(override_cache VAR VAL) set_property(CACHE ${VAR} PROPERTY VALUE ${VAL}) endfunction() -function(add_option NAME HELP_STRING DEFAULT VALUES) - if(VALUES STREQUAL "yes;no") +function(add_option_defs) + cmake_parse_arguments(PARSE_ARGV 0 arg "" "NAME;HELP_STRING;DEFAULT" "VALUES;DEFINITIONS") + if(NOT DEFINED arg_DEFAULT) + # Pick first value from list of values as default + list(GET arg_VALUES 0 arg_DEFAULT) + endif() + if(NOT arg_HELP_STRING MATCHES "default:") + string(APPEND arg_HELP_STRING ". (default: " "${arg_DEFAULT}" ")") + endif() + if(arg_VALUES STREQUAL "yes;no" OR arg_VALUES STREQUAL "no;yes") # Set the default value for the option. - set(${NAME} ${DEFAULT} CACHE BOOL ${HELP_STRING}) + set(${arg_NAME} ${arg_DEFAULT} CACHE BOOL ${arg_HELP_STRING}) else() # Set the default value for the option. - set(${NAME} ${DEFAULT} CACHE STRING ${HELP_STRING}) + set(${arg_NAME} ${arg_DEFAULT} CACHE STRING ${arg_HELP_STRING}) # Set the list of allowed values for the option. - set_property(CACHE ${NAME} PROPERTY STRINGS ${VALUES}) + set_property(CACHE ${arg_NAME} PROPERTY STRINGS ${arg_VALUES}) endif() - if(DEFINED ${NAME}) - list(FIND VALUES ${${NAME}} IDX) + if(DEFINED ${arg_NAME}) + list(FIND arg_VALUES ${${arg_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: @@ -31,15 +39,25 @@ function(add_option NAME HELP_STRING DEFAULT VALUES) # CMakeCache.txt and cmake-gui easier to read. # if (${IDX} EQUAL -1) - if(${${NAME}}) - override_cache(${NAME} "yes") + if(${${arg_NAME}}) + override_cache(${arg_NAME} "yes") else() - override_cache(${NAME} "no") + override_cache(${arg_NAME} "no") endif() endif() endif() + if(DEFINED arg_DEFINITIONS) + list(APPEND WOLFSSL_DEFINITIONS ${arg_DEFINITIONS}) + set(WOLFSSL_DEFINITIONS ${WOLFSSL_DEFINITIONS} PARENT_SCOPE) + endif() endfunction() +function(add_option NAME HELP_STRING DEFAULT VALUES) + add_option_defs(NAME ${NAME} HELP_STRING ${HELP_STRING} DEFAULT ${DEFAULT} VALUES ${VALUES}) + set(WOLFSSL_DEFINITIONS ${WOLFSSL_DEFINITIONS} PARENT_SCOPE) +endfunction() + + function(generate_build_flags) set(BUILD_DISTRO ${WOLFSSL_DISTRO} PARENT_SCOPE) set(BUILD_ALL ${WOLFSSL_ALL} PARENT_SCOPE)