From 9a281e5e3a8979d530978a42be80ad232db67406 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 16 Jan 2020 06:58:40 -0800 Subject: [PATCH 1/4] Adds CMake support to wolfSSL: * Build wolfSSL as a library and builds all examples / tests. * Added instructions in the INSTALL file. * Fix for evp.c when being included directly due to improperly placed `WOLFSSL_EVP_INCLUDED`. --- CMakeLists.txt | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ INSTALL | 16 +++++++ 2 files changed, 138 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..d7cf8da64 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,122 @@ +# CMakeList.txt +# +# Copyright (C) 2006-2020 wolfSSL Inc. +# +# This file is part of wolfSSL. (formerly known as CyaSSL) +# +# + +cmake_minimum_required (VERSION 2.6) + +#################################################### +# Project +#################################################### +project(wolfssl) +find_package (Threads) + +#################################################### +# Compiler +#################################################### +set(CMAKE_C_COMPILER gcc) +set(CMAKE_CXX_COMPILER gcc) + +# Example for cross-compile +#set(CMAKE_SYSTEM_NAME Linux) +#set(CMAKE_SYSTEM_PROCESSOR arm) +#set(CMAKE_C_COMPILER "/opt/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-gcc") +#set(CMAKE_CXX_COMPILER "/opt/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-g++") +#set(CMAKE_SYSROOT "/opt/arm-linux-musleabihf-cross/arm-linux-musleabihf/") + +#################################################### +# Build Options +#################################################### +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h") + # Copy generated ./options.h + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) + file(RENAME ${CMAKE_CURRENT_BINARY_DIR}/options.h + ${CMAKE_CURRENT_BINARY_DIR}/user_settings.h) +else() + # Use template + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h.in + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) + file(RENAME ${CMAKE_CURRENT_BINARY_DIR}/options.h.in + ${CMAKE_CURRENT_BINARY_DIR}/user_settings.h) +endif() + +add_definitions(-DWOLFSSL_USER_SETTINGS) +add_definitions(-DWOLFSSL_IGNORE_FILE_WARN) +if(CMAKE_HAVE_PTHREAD_H) + add_definitions(-DHAVE_PTHREAD) +endif() + +#################################################### +# Build flags +#################################################### +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/.) +include_directories(${CMAKE_CURRENT_BINARY_DIR}/.) + +# Example for map file and custom linker script +set(CMAKE_C_FLAGS "-std=gnu89 ${CMAKE_C_FLAGS}") +#set(CMAKE_EXE_LINKER_FLAGS " -Xlinker -Map=output.map -T\"${CMAKE_CURRENT_SOURCE_DIR}/linker.ld\"") + +#################################################### +# Source Files +#################################################### +file(GLOB LIB_SOURCE_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/src/*.c) + +file(GLOB TEST_SOURCE_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/server/server.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/client/client.c) + +#################################################### +# Output Files +#################################################### +# Build library +add_library(wolfssl ${LIB_SOURCE_FILES}) + +# Build wolfCrypt test +add_executable(wolfcrypttest + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/test/test.c) +target_link_libraries(wolfcrypttest wolfssl) + +# Build wolfCrypt benchmark +add_executable(wolfcryptbench + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/benchmark/benchmark.c) +target_link_libraries(wolfcryptbench wolfssl) + +# Build wolfSSL Client example +add_executable(client + ${CMAKE_CURRENT_SOURCE_DIR}/examples/client/client.c) +target_link_libraries(client wolfssl) + +# Build wolfSSL Server example +add_executable(server + ${CMAKE_CURRENT_SOURCE_DIR}/examples/server/server.c) +target_link_libraries(server wolfssl) + +# Build Echo Client Example +add_executable(echoclient + ${CMAKE_CURRENT_SOURCE_DIR}/examples/echoclient/echoclient.c) +target_link_libraries(echoclient wolfssl) + +# Build Echo Server Example +add_executable(echoserver + ${CMAKE_CURRENT_SOURCE_DIR}/examples/echoserver/echoserver.c) +target_link_libraries(echoserver wolfssl) + +# Build TLS benchmark example +add_executable(tls_bench + ${CMAKE_CURRENT_SOURCE_DIR}/examples/benchmark/tls_bench.c) +target_link_libraries(tls_bench wolfssl) +target_link_libraries(tls_bench Threads::Threads) + +# Build Unit Tests +add_executable(unit_test + ${TEST_SOURCE_FILES}) +set_target_properties( unit_test PROPERTIES COMPILE_FLAGS "-DNO_MAIN_DRIVER" ) +target_link_libraries(unit_test wolfssl) +target_link_libraries(unit_test Threads::Threads) diff --git a/INSTALL b/INSTALL index ad8cfdd74..0a54465d7 100644 --- a/INSTALL +++ b/INSTALL @@ -73,3 +73,19 @@ Please see section 2.4 in the manual: http://www.wolfssl.com/yaSSL/Docs-cyassl-manual-2-building-cyassl.html +13. Building with CMake + Note: Primary development uses automake (./configure). The support for CMake is minimal. + + Internally cmake is setup to do the following: + 1. Uses the ./configure generated wolfssl/options.h as the build options by coping it to the build directory as user_settings.h. + 2. Builds wolfSSL as library. + 3. Builds the examples. + + Build Steps: + $ mkdir build + $ cd build + $ cmake .. + $ make + $ sudo make install + + Make sure and run the built examples and test from the wolfssl-root to properly find the ./certs directory. From b273ba771e491d457a3a71872a619bc9511cb809 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 16 Jan 2020 09:08:57 -0800 Subject: [PATCH 2/4] Add new file to automake. --- Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile.am b/Makefile.am index 37533fa23..cf7498b94 100644 --- a/Makefile.am +++ b/Makefile.am @@ -145,6 +145,7 @@ EXTRA_DIST+= IPP EXTRA_DIST+= LPCExpresso.cproject EXTRA_DIST+= LPCExpresso.project EXTRA_DIST+= resource.h wolfssl.rc +EXTRA_DIST+= CMakeLists.txt include wrapper/include.am include cyassl/include.am From 8b25b4862102c0792d191bbbfdce029dca6cc4a1 Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 27 Jul 2020 12:13:08 -0700 Subject: [PATCH 3/4] Improvements to CMake support based on feedback from users. --- CMakeLists.txt | 132 ++++++++++++++++++++++++++++--------------------- INSTALL | 6 +++ 2 files changed, 82 insertions(+), 56 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d7cf8da64..e4c093cef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,18 @@ # # This file is part of wolfSSL. (formerly known as CyaSSL) # -# +# Usage: +# $ mkdir build +# $ cd build +# $ cmake .. +# $ make + +# To build library only and not build examples and test apps use: +# $ cmake .. -DBUILD_TESTS=NO + +# To build with debugging use: +# $ cmake .. -DCMAKE_BUILD_TYPE=Debug + cmake_minimum_required (VERSION 2.6) @@ -17,31 +28,41 @@ find_package (Threads) #################################################### # Compiler #################################################### -set(CMAKE_C_COMPILER gcc) -set(CMAKE_CXX_COMPILER gcc) +# Let CMake choose default compiler + +# Silence ranlib warning "has no symbols" +SET(CMAKE_C_ARCHIVE_CREATE " Scr ") +SET(CMAKE_CXX_ARCHIVE_CREATE " Scr ") +SET(CMAKE_C_ARCHIVE_FINISH " -no_warning_for_no_symbols -c ") +SET(CMAKE_CXX_ARCHIVE_FINISH " -no_warning_for_no_symbols -c ") + +#################################################### +# Cross Compile Example +#################################################### -# Example for cross-compile #set(CMAKE_SYSTEM_NAME Linux) #set(CMAKE_SYSTEM_PROCESSOR arm) #set(CMAKE_C_COMPILER "/opt/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-gcc") #set(CMAKE_CXX_COMPILER "/opt/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-g++") #set(CMAKE_SYSROOT "/opt/arm-linux-musleabihf-cross/arm-linux-musleabihf/") +# Example for setting CFLAGS +#set(CMAKE_C_FLAGS "-std=gnu89 ${CMAKE_C_FLAGS}") +# Example for map file and custom linker script +#set(CMAKE_EXE_LINKER_FLAGS " -Xlinker -Map=output.map -T\"${CMAKE_CURRENT_SOURCE_DIR}/linker.ld\"") #################################################### # Build Options #################################################### +SET(BUILD_TESTS YES CACHE BOOL "Build test applications") + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h") # Copy generated ./options.h - file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) - file(RENAME ${CMAKE_CURRENT_BINARY_DIR}/options.h - ${CMAKE_CURRENT_BINARY_DIR}/user_settings.h) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h + ${CMAKE_CURRENT_BINARY_DIR}/user_settings.h) else() # Use template - file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h.in - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) - file(RENAME ${CMAKE_CURRENT_BINARY_DIR}/options.h.in - ${CMAKE_CURRENT_BINARY_DIR}/user_settings.h) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h.in + ${CMAKE_CURRENT_BINARY_DIR}/user_settings.h) endif() add_definitions(-DWOLFSSL_USER_SETTINGS) @@ -51,18 +72,11 @@ if(CMAKE_HAVE_PTHREAD_H) endif() #################################################### -# Build flags +# Source Files #################################################### include_directories(${CMAKE_CURRENT_SOURCE_DIR}/.) include_directories(${CMAKE_CURRENT_BINARY_DIR}/.) -# Example for map file and custom linker script -set(CMAKE_C_FLAGS "-std=gnu89 ${CMAKE_C_FLAGS}") -#set(CMAKE_EXE_LINKER_FLAGS " -Xlinker -Map=output.map -T\"${CMAKE_CURRENT_SOURCE_DIR}/linker.ld\"") - -#################################################### -# Source Files -#################################################### file(GLOB LIB_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/src/*.c) @@ -75,48 +89,54 @@ file(GLOB TEST_SOURCE_FILES #################################################### # Output Files #################################################### -# Build library +# DH requires math (m) library +link_libraries(m) + +# Build wolfssl library add_library(wolfssl ${LIB_SOURCE_FILES}) -# Build wolfCrypt test -add_executable(wolfcrypttest - ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/test/test.c) -target_link_libraries(wolfcrypttest wolfssl) +# Optionally build example and test applications +if(BUILD_TESTS) + # Build wolfCrypt test + add_executable(wolfcrypttest + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/test/test.c) + target_link_libraries(wolfcrypttest wolfssl) -# Build wolfCrypt benchmark -add_executable(wolfcryptbench - ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/benchmark/benchmark.c) -target_link_libraries(wolfcryptbench wolfssl) + # Build wolfCrypt benchmark + add_executable(wolfcryptbench + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/benchmark/benchmark.c) + target_link_libraries(wolfcryptbench wolfssl) -# Build wolfSSL Client example -add_executable(client - ${CMAKE_CURRENT_SOURCE_DIR}/examples/client/client.c) -target_link_libraries(client wolfssl) + # Build wolfSSL Client example + add_executable(client + ${CMAKE_CURRENT_SOURCE_DIR}/examples/client/client.c) + target_link_libraries(client wolfssl) -# Build wolfSSL Server example -add_executable(server - ${CMAKE_CURRENT_SOURCE_DIR}/examples/server/server.c) -target_link_libraries(server wolfssl) + # Build wolfSSL Server example + add_executable(server + ${CMAKE_CURRENT_SOURCE_DIR}/examples/server/server.c) + target_link_libraries(server wolfssl) -# Build Echo Client Example -add_executable(echoclient - ${CMAKE_CURRENT_SOURCE_DIR}/examples/echoclient/echoclient.c) -target_link_libraries(echoclient wolfssl) + # Build Echo Client Example + add_executable(echoclient + ${CMAKE_CURRENT_SOURCE_DIR}/examples/echoclient/echoclient.c) + target_link_libraries(echoclient wolfssl) -# Build Echo Server Example -add_executable(echoserver - ${CMAKE_CURRENT_SOURCE_DIR}/examples/echoserver/echoserver.c) -target_link_libraries(echoserver wolfssl) + # Build Echo Server Example + add_executable(echoserver + ${CMAKE_CURRENT_SOURCE_DIR}/examples/echoserver/echoserver.c) + target_link_libraries(echoserver wolfssl) -# Build TLS benchmark example -add_executable(tls_bench - ${CMAKE_CURRENT_SOURCE_DIR}/examples/benchmark/tls_bench.c) -target_link_libraries(tls_bench wolfssl) -target_link_libraries(tls_bench Threads::Threads) + # Build TLS benchmark example + add_executable(tls_bench + ${CMAKE_CURRENT_SOURCE_DIR}/examples/benchmark/tls_bench.c) + target_link_libraries(tls_bench wolfssl) + target_link_libraries(tls_bench Threads::Threads) -# Build Unit Tests -add_executable(unit_test - ${TEST_SOURCE_FILES}) -set_target_properties( unit_test PROPERTIES COMPILE_FLAGS "-DNO_MAIN_DRIVER" ) -target_link_libraries(unit_test wolfssl) -target_link_libraries(unit_test Threads::Threads) + # Build Unit Tests + add_executable(unit_test + ${TEST_SOURCE_FILES}) + set_target_properties( unit_test PROPERTIES COMPILE_FLAGS "-DNO_MAIN_DRIVER" ) + target_link_libraries(unit_test wolfssl) + target_link_libraries(unit_test Threads::Threads) +endif() diff --git a/INSTALL b/INSTALL index 0a54465d7..1aa12c69b 100644 --- a/INSTALL +++ b/INSTALL @@ -88,4 +88,10 @@ $ make $ sudo make install + To build library only and not build examples and test apps use: + $ cmake .. -DBUILD_TESTS=NO + + To build with debugging use: + $ cmake .. -DCMAKE_BUILD_TYPE=Debug + Make sure and run the built examples and test from the wolfssl-root to properly find the ./certs directory. From db20fb6ca175a72da5e0c4faefdba78b8641c268 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 30 Jul 2020 16:44:36 -0700 Subject: [PATCH 4/4] Fixes for using CMake with Visual Studio. Improvements to documentation for portability. --- CMakeLists.txt | 14 +++++++++++--- INSTALL | 9 +++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e4c093cef..b35c774c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ # $ mkdir build # $ cd build # $ cmake .. -# $ make +# $ cmake --build . # To build library only and not build examples and test apps use: # $ cmake .. -DBUILD_TESTS=NO @@ -89,12 +89,18 @@ file(GLOB TEST_SOURCE_FILES #################################################### # Output Files #################################################### -# DH requires math (m) library -link_libraries(m) # Build wolfssl library add_library(wolfssl ${LIB_SOURCE_FILES}) +if(WIN32) + # For Windows link ws2_32 + target_link_libraries(wolfssl PUBLIC $<$:ws2_32>) +else() + # DH requires math (m) library + target_link_libraries(wolfssl PUBLIC m) +endif() + # Optionally build example and test applications if(BUILD_TESTS) # Build wolfCrypt test @@ -140,3 +146,5 @@ if(BUILD_TESTS) target_link_libraries(unit_test wolfssl) target_link_libraries(unit_test Threads::Threads) endif() + +# TODO: Add install() for library, headers and test applications diff --git a/INSTALL b/INSTALL index 1aa12c69b..33a983ed7 100644 --- a/INSTALL +++ b/INSTALL @@ -85,8 +85,8 @@ $ mkdir build $ cd build $ cmake .. - $ make - $ sudo make install + $ cmake --build . + $ cmake --install . To build library only and not build examples and test apps use: $ cmake .. -DBUILD_TESTS=NO @@ -95,3 +95,8 @@ $ cmake .. -DCMAKE_BUILD_TYPE=Debug Make sure and run the built examples and test from the wolfssl-root to properly find the ./certs directory. + + CMake on Windows with Visual Studio + 1. Open Command Prompt + 2. Run the Visual Studio batch to setup command line variables: Example: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat + 3. Then use steps above