Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: new add_option_defs helper, add KEYLOG_EXPORT build param #8174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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})

Expand Down
38 changes: 28 additions & 10 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Copy link
Contributor Author

@redbaron redbaron Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand purpose this if() in original function. I transformed it mechanically to new args so it should work as before.

when building there is no difference in CMakeCache.txt before and after, so this branch either never triggered or mechanical transformation works as before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think I get it. It triggers when build options is passed via command line with value which is not among the list of supported values, like we pass "ON" for values like "yes|no" This works with booleans, but doesn't it break build options where string input is expected?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redbaron sorry accidentally deleted my previous response.

You are correct, this looks like it squashes non-boolean values inappropriately. Could you add a check for this case, to ensure this doesn't happen? Something like this to fall back to defaults maybe?

if(DEFINED ${NAME})
    list(FIND VALUES ${${NAME}} IDX)

    # Apply override only if VALUES is yes/no
    if (VALUES STREQUAL "yes;no" OR VALUES STREQUAL "no;yes")
        if (${IDX} EQUAL -1)
            # Interpret CMake truthiness for boolean-like values
            if(${${NAME}})
                override_cache(${NAME} "yes")
            else()
                override_cache(${NAME} "no")
            endif()
        endif()
    else()
        # For non-boolean VALUES, reset to default or error on invalid input
        if (${IDX} EQUAL -1)
            message(WARNING "Invalid value for ${NAME}. Resetting to default: ${DEFAULT}")
            set(${NAME} ${DEFAULT} CACHE STRING "${HELP_STRING}")
        endif()
    endif()
endif()

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:
Expand All @@ -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)
Expand Down
Loading