From 31b6c507f96223e7c7302782fcf0c589190610c3 Mon Sep 17 00:00:00 2001 From: Hayden Roche Date: Mon, 19 Oct 2020 16:07:48 -0500 Subject: [PATCH] Tweak CMakeLists.txt - Add generated CMake files/directories to .gitignore. - Use lowercase for CMake commands, UPPERCASE for variables. - Favor the CMake "option" command over SET(... CACHE BOOL ...). - Use CMAKE_CURRENT_SOURCE_DIR in place of CMAKE_CURRENT_BINARY_DIR. - Use CMAKE_USE_PTHREADS_INIT instead of CMAKE_HAVE_PTHREAD_H. - Use target_include_directories on the wolfssl library target instead of include_directories. --- .gitignore | 5 ++++ CMakeLists.txt | 67 +++++++++++++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 48c7de11a..b749fa970 100644 --- a/.gitignore +++ b/.gitignore @@ -352,3 +352,8 @@ IDE/XCODE/Index # Emacs *~ + +# CMake +CMakeFiles/ +CMakeCache.txt +cmake_install.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 45fbea28f..c380e2efe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,14 +16,18 @@ # To build with debugging use: # $ cmake .. -DCMAKE_BUILD_TYPE=Debug - -cmake_minimum_required (VERSION 2.6) - #################################################### # Project #################################################### + +cmake_minimum_required(VERSION 2.6) project(wolfssl) -find_package (Threads) + +#################################################### +# Dependencies +#################################################### + +find_package(Threads) #################################################### # Compiler @@ -32,10 +36,10 @@ find_package (Threads) if(APPLE) # 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 ") + 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 ") endif() #################################################### @@ -55,46 +59,50 @@ endif() #################################################### # Build Options #################################################### -SET(BUILD_TESTS YES CACHE BOOL "Build test applications") + +option(BUILD_TESTS "Build test applications" YES) if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h") # Copy generated ./options.h configure_file(${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h - ${CMAKE_CURRENT_BINARY_DIR}/user_settings.h) + ${CMAKE_CURRENT_SOURCE_DIR}/user_settings.h) else() # Use template configure_file(${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/options.h.in - ${CMAKE_CURRENT_BINARY_DIR}/user_settings.h) + ${CMAKE_CURRENT_SOURCE_DIR}/user_settings.h) endif() add_definitions(-DWOLFSSL_USER_SETTINGS) add_definitions(-DWOLFSSL_IGNORE_FILE_WARN) -if(CMAKE_HAVE_PTHREAD_H) +if(CMAKE_USE_PTHREADS_INIT) add_definitions(-DHAVE_PTHREAD) endif() #################################################### -# Source Files +# Library Target and Source Files #################################################### -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/.) -include_directories(${CMAKE_CURRENT_BINARY_DIR}/.) 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 wolfssl library add_library(wolfssl ${LIB_SOURCE_FILES}) +#################################################### +# Include Directories +#################################################### + +target_include_directories(wolfssl + PUBLIC + $ + $ + ${CMAKE_CURRENT_SOURCE_DIR}/. # Needed for user_settings.h to be visible +) + +#################################################### +# Link Libraries +#################################################### + if(WIN32) # For Windows link ws2_32 target_link_libraries(wolfssl PUBLIC $<$:ws2_32>) @@ -103,6 +111,10 @@ else() target_link_libraries(wolfssl PUBLIC m) endif() +#################################################### +# Tests and Examples +#################################################### + # Optionally build example and test applications if(BUILD_TESTS) # Build wolfCrypt test @@ -141,6 +153,11 @@ if(BUILD_TESTS) target_link_libraries(tls_bench wolfssl) target_link_libraries(tls_bench Threads::Threads) + 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) + # Build Unit Tests add_executable(unit_test ${TEST_SOURCE_FILES})