From ec857f6f628fda6b296e977e815e8529f3f203cf Mon Sep 17 00:00:00 2001 From: Hayden Roche Date: Wed, 22 Sep 2021 16:54:35 -0700 Subject: [PATCH 1/4] Add a CMake option to build wolfcrypt test and bench code as static libs. Application code can use the resulting CMake targets or the static library artifacts directly (e.g. libwolfcrypttest.a on *nix). --- CMakeLists.txt | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 811ad0947..b8454acb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1234,9 +1234,12 @@ else() set(CRYPT_TESTS_DEFAULT "yes") endif() -set(WOLFSSL_CRYPT_TESTS_HELP_STRING "Enable Crypt Bench/Test (default: enabled)") +set(WOLFSSL_CRYPT_TESTS_HELP_STRING "Enable Crypt Bench/Test (default: enabled)") add_option("WOLFSSL_CRYPT_TESTS" ${WOLFSSL_CRYPT_TESTS_HELP_STRING} ${CRYPT_TESTS_DEFAULT} "yes;no") +set(WOLFSSL_CRYPT_TESTS_LIBS_HELP_STRING "Build static libraries from the wolfCrypt test and benchmark sources (default: disabled)") +add_option("WOLFSSL_CRYPT_TESTS_LIBS" ${WOLFSSL_CRYPT_TESTS_LIBS_HELP_STRING} "no" "yes;no") + # TODO: - LIBZ # - PKCS#11 # - PKCS#12 @@ -1510,7 +1513,27 @@ if(WOLFSSL_EXAMPLES) endif() if(WOLFSSL_CRYPT_TESTS) - # Build wolfCrypt test + if(WOLFSSL_CRYPT_TESTS_LIBS) + # Build wolfCrypt test as a static library. This will compile test.c + # and make its functions available as a CMake target that other CMake + # targets can pull in, in addition to producing the static library + # itself. Note that this feature is not enabled by default, and the API + # of this library and wofcryptbench_lib should NOT be treated as stable. + add_library(wolfcrypttest_lib + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/test/test.c) + set_target_properties(wolfcrypttest_lib PROPERTIES OUTPUT_NAME "wolfcrypttest") + target_link_libraries(wolfcrypttest_lib wolfssl) + target_compile_options(wolfcrypttest_lib PRIVATE "-DNO_MAIN_DRIVER") + + # Make another static library for the wolfCrypt benchmark code. + add_library(wolfcryptbench_lib + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/benchmark/benchmark.c) + set_target_properties(wolfcryptbench_lib PROPERTIES OUTPUT_NAME "wolfcryptbench") + target_link_libraries(wolfcryptbench_lib wolfssl) + target_compile_options(wolfcryptbench_lib PRIVATE "-DNO_MAIN_DRIVER") + endif() + + # Build wolfCrypt test executable. add_executable(wolfcrypttest ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/test/test.c) target_link_libraries(wolfcrypttest wolfssl) @@ -1521,7 +1544,7 @@ if(WOLFSSL_CRYPT_TESTS) PROPERTY RUNTIME_OUTPUT_NAME testwolfcrypt) - # Build wolfCrypt benchmark + # Build wolfCrypt benchmark executable. add_executable(wolfcryptbench ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/benchmark/benchmark.c) target_include_directories(wolfcryptbench PRIVATE From 9634a54b8ffd85b1e21d2d0d1dea3d33790487cd Mon Sep 17 00:00:00 2001 From: Hayden Roche Date: Fri, 24 Sep 2021 13:49:30 -0700 Subject: [PATCH 2/4] 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) From 302938d3c6652da831f5d8da26c25746165dd864 Mon Sep 17 00:00:00 2001 From: Hayden Roche Date: Sat, 25 Sep 2021 10:30:01 -0700 Subject: [PATCH 3/4] Improve wolfcrypt test/bench library comments. These can be built as shared libraries, too, so the comments shouldn't be specific about static libraries. --- CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8454acb3..76902c003 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1514,18 +1514,18 @@ endif() if(WOLFSSL_CRYPT_TESTS) if(WOLFSSL_CRYPT_TESTS_LIBS) - # Build wolfCrypt test as a static library. This will compile test.c - # and make its functions available as a CMake target that other CMake - # targets can pull in, in addition to producing the static library - # itself. Note that this feature is not enabled by default, and the API - # of this library and wofcryptbench_lib should NOT be treated as stable. + # Build wolfCrypt test as a library. This will compile test.c and make + # its functions available as a CMake target that other CMake targets can + # pull in, in addition to producing the library itself. Note that this + # feature is not enabled by default, and the API of this library and + # wofcryptbench_lib should NOT be treated as stable. add_library(wolfcrypttest_lib ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/test/test.c) set_target_properties(wolfcrypttest_lib PROPERTIES OUTPUT_NAME "wolfcrypttest") target_link_libraries(wolfcrypttest_lib wolfssl) target_compile_options(wolfcrypttest_lib PRIVATE "-DNO_MAIN_DRIVER") - # Make another static library for the wolfCrypt benchmark code. + # Make another library for the wolfCrypt benchmark code. add_library(wolfcryptbench_lib ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/benchmark/benchmark.c) set_target_properties(wolfcryptbench_lib PROPERTIES OUTPUT_NAME "wolfcryptbench") From 709a84f8b5fc5a799fd6e4744385721a46d377fa Mon Sep 17 00:00:00 2001 From: Hayden Roche Date: Sat, 25 Sep 2021 10:31:06 -0700 Subject: [PATCH 4/4] Add support for libwolfcrypttest and libwolfcryptbench to autotools flow. These can be built by configuring with `--enable-crypttests-libs`. --- configure.ac | 10 ++++++++++ wolfcrypt/benchmark/include.am | 8 ++++++++ wolfcrypt/test/include.am | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/configure.ac b/configure.ac index b417a3b43..ad73fb6bd 100644 --- a/configure.ac +++ b/configure.ac @@ -5072,6 +5072,15 @@ AC_ARG_ENABLE([crypttests], ) AC_SUBST([ENABLED_CRYPT_TESTS]) +# Build wolfCrypt test and benchmark as libraries. This will compile test.c and +# benchmark.c and make their functions available via libraries, libwolfcrypttest +# and libwolfcryptbench, respectively. Note that this feature is not enabled by +# default, and the API of these libraries should NOT be treated as stable. +AC_ARG_ENABLE([crypttests-libs], + [AS_HELP_STRING([--enable-crypttests-libs],[Enable wolfcrypt test and benchmark libraries (default: disabled)])], + [ ENABLED_CRYPT_TESTS_LIBS=$enableval ], + [ ENABLED_CRYPT_TESTS_LIBS=no ] + ) # LIBZ ENABLED_LIBZ="no" @@ -6889,6 +6898,7 @@ AM_CONDITIONAL([BUILD_EXAMPLE_CLIENTS],[test "x$ENABLED_EXAMPLES" = "xyes"]) AM_CONDITIONAL([BUILD_TESTS],[test "x$ENABLED_EXAMPLES" = "xyes"]) AM_CONDITIONAL([BUILD_THREADED_EXAMPLES],[test "x$ENABLED_SINGLETHREADED" = "xno" && test "x$ENABLED_EXAMPLES" = "xyes" && test "x$ENABLED_LEANTLS" = "xno"]) AM_CONDITIONAL([BUILD_WOLFCRYPT_TESTS],[test "x$ENABLED_CRYPT_TESTS" = "xyes"]) +AM_CONDITIONAL([BUILD_WOLFCRYPT_TESTS_LIBS],[test "x$ENABLED_CRYPT_TESTS_LIBS" = "xyes"]) AM_CONDITIONAL([BUILD_LIBZ],[test "x$ENABLED_LIBZ" = "xyes"]) AM_CONDITIONAL([BUILD_PKCS11],[test "x$ENABLED_PKCS11" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"]) AM_CONDITIONAL([BUILD_PKCS12],[test "x$ENABLED_PKCS12" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"]) diff --git a/wolfcrypt/benchmark/include.am b/wolfcrypt/benchmark/include.am index aabd7a654..b493bde38 100644 --- a/wolfcrypt/benchmark/include.am +++ b/wolfcrypt/benchmark/include.am @@ -13,6 +13,14 @@ noinst_HEADERS += wolfcrypt/benchmark/benchmark.h endif endif +if BUILD_WOLFCRYPT_TESTS_LIBS +lib_LTLIBRARIES += wolfcrypt/benchmark/libwolfcryptbench.la +wolfcrypt_benchmark_libwolfcryptbench_la_SOURCES = wolfcrypt/benchmark/benchmark.c +wolfcrypt_benchmark_libwolfcryptbench_la_CPPFLAGS = -DNO_MAIN_DRIVER +wolfcrypt_benchmark_libwolfcryptbench_la_LIBADD = src/libwolfssl.la +wolfcrypt_benchmark_libwolfcryptbench_la_DEPENDENCIES = src/libwolfssl.la +endif + EXTRA_DIST += wolfcrypt/benchmark/benchmark.sln EXTRA_DIST += wolfcrypt/benchmark/benchmark.vcproj EXTRA_DIST += wolfcrypt/benchmark/README.md diff --git a/wolfcrypt/test/include.am b/wolfcrypt/test/include.am index 905333ee8..667cd087d 100644 --- a/wolfcrypt/test/include.am +++ b/wolfcrypt/test/include.am @@ -16,6 +16,14 @@ noinst_HEADERS += wolfcrypt/test/test.h wolfcrypt/test/test_paths.h.in endif endif +if BUILD_WOLFCRYPT_TESTS_LIBS +lib_LTLIBRARIES += wolfcrypt/test/libwolfcrypttest.la +wolfcrypt_test_libwolfcrypttest_la_SOURCES = wolfcrypt/test/test.c +wolfcrypt_test_libwolfcrypttest_la_CPPFLAGS = -DNO_MAIN_DRIVER +wolfcrypt_test_libwolfcrypttest_la_LIBADD = src/libwolfssl.la +wolfcrypt_test_libwolfcrypttest_la_DEPENDENCIES = src/libwolfssl.la +endif + EXTRA_DIST += wolfcrypt/test/test.sln EXTRA_DIST += wolfcrypt/test/test.vcproj EXTRA_DIST += wolfcrypt/test/README.md