From f06f294a060f14728a595689594cc8ca0d4bf6ce Mon Sep 17 00:00:00 2001 From: Luca Schlecker Date: Mon, 23 Aug 2021 22:30:39 +0200 Subject: [PATCH] rework cmake configs. See #160 for more info. --- CMakeLists.txt | 82 +++++++++++--------- cmake/FindTcmalloc.cmake | 38 --------- cmake/compiler_options.cmake | 42 +++++----- cmake/dependencies.cmake | 35 --------- examples/CMakeLists.txt | 142 ++++++++++++++++------------------ tests/CMakeLists.txt | 8 +- tests/template/CMakeLists.txt | 7 +- 7 files changed, 144 insertions(+), 210 deletions(-) delete mode 100644 cmake/FindTcmalloc.cmake delete mode 100644 cmake/dependencies.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index ec229c0b7..0835951c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,17 +3,18 @@ ##################################### cmake_minimum_required(VERSION 3.15.0 FATAL_ERROR) -# Define the Project Name and Description -project (crow_all LANGUAGES CXX) - -# Define the module path -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/") +# Define the Project Name and Version +project(crow + VERSION 3.0 + LANGUAGES CXX +) # Set required C++ Standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) -if (NOT CMAKE_BUILD_TYPE) +# Default to build type "Release" +if(NOT CMAKE_BUILD_TYPE) message(STATUS "No build type selected, default to Release") set(CMAKE_BUILD_TYPE "Release") endif() @@ -21,50 +22,55 @@ endif() ##################################### # Define Options ##################################### -option(BUILD_EXAMPLES "Builds the examples in the project" ON) -option(BUILD_TESTING "Builds the tests in the project" ON) +option(CROW_BUILD_EXAMPLES "Build the examples in the project" ON ) +option(CROW_BUILD_TESTS "Build the tests in the project" ON ) +option(CROW_AMALGAMATE "Combine all headers into one" OFF) +option(CROW_INSTALL "Add install step for crow" ON ) ##################################### -# Define CMake Module Imports +# Define Targets ##################################### -include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies.cmake) -include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/compiler_options.cmake) +add_library(crow INTERFACE) +target_include_directories(crow INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) -##################################### -# Define project-wide imports -##################################### -# this can be alternatively (and as recommended way) done with target_include_directories() -if(BUILD_EXAMPLES OR BUILD_TESTING) - set(PROJECT_INCLUDE_DIR - ${CMAKE_CURRENT_SOURCE_DIR}/include - ) +find_package(Boost 1.64 COMPONENTS system date_time REQUIRED) +find_package(Threads REQUIRED) +find_package(ZLIB) +find_package(OpenSSL) - include_directories("${PROJECT_INCLUDE_DIR}") - include_directories("${CMAKE_CURRENT_SOURCE_DIR}") - include_directories("${CMAKE_CURRENT_BINARY_DIR}") # To include crow_all.h +target_link_libraries(crow + INTERFACE + Boost::boost Boost::system Boost::date_time + Threads::Threads +) + +if(OPENSSL_FOUND) + target_link_libraries(crow INTERFACE OpenSSL::SSL) endif() -##################################### -# Define Targets -##################################### -add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h - COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/merge_all.py - ${CMAKE_CURRENT_SOURCE_DIR}/include - ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/crow/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/crow/middlewares/*.h -) +if(ZLIB_FOUND) + target_link_libraries(crow INTERFACE ZLIB::ZLIB) +endif() -# Amalgamation -add_custom_target(amalgamation ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h) +if(CROW_AMALGAMATE) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h + COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/merge_all.py + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/crow/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/crow/middlewares/*.h + ) + + add_custom_target(crow_amalgamated ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h) +endif() # Examples -if(BUILD_EXAMPLES) +if(CROW_BUILD_EXAMPLES) add_subdirectory(examples) endif() # Tests -if (NOT MSVC AND BUILD_TESTING) +if(NOT MSVC AND CROW_BUILD_TESTS) add_subdirectory(tests) enable_testing() add_test(NAME crow_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/unittest) @@ -74,7 +80,9 @@ endif() ##################################### # Install Files ##################################### -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h DESTINATION include) +if(CROW_INSTALL) + install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION include) +endif() set(CPACK_GENERATOR "DEB") set(CPACK_DEBIAN_PACKAGE_MAINTAINER "CrowCpp") diff --git a/cmake/FindTcmalloc.cmake b/cmake/FindTcmalloc.cmake deleted file mode 100644 index 6ec479d9f..000000000 --- a/cmake/FindTcmalloc.cmake +++ /dev/null @@ -1,38 +0,0 @@ -# - Find Tcmalloc -# Find the native Tcmalloc library -# -# Tcmalloc_LIBRARIES - List of libraries when using Tcmalloc. -# Tcmalloc_FOUND - True if Tcmalloc found. - -if (USE_TCMALLOC) - set(Tcmalloc_NAMES tcmalloc) -else () - set(Tcmalloc_NAMES tcmalloc_minimal tcmalloc) -endif () - -find_library(Tcmalloc_LIBRARY NO_DEFAULT_PATH - NAMES ${Tcmalloc_NAMES} - PATHS ${HT_DEPENDENCY_LIB_DIR} /lib /usr/lib /usr/local/lib /opt/local/lib -) - -if (Tcmalloc_LIBRARY) - set(Tcmalloc_FOUND TRUE) - set( Tcmalloc_LIBRARIES ${Tcmalloc_LIBRARY} ) -else () - set(Tcmalloc_FOUND FALSE) - set( Tcmalloc_LIBRARIES ) -endif () - -if (Tcmalloc_FOUND) - message(STATUS "Found Tcmalloc: ${Tcmalloc_LIBRARY}") -else () - message(STATUS "Not Found Tcmalloc: ${Tcmalloc_LIBRARY}") - if (Tcmalloc_FIND_REQUIRED) - message(STATUS "Looked for Tcmalloc libraries named ${Tcmalloc_NAMES}.") - message(FATAL_ERROR "Could NOT find Tcmalloc library") - endif () -endif () - -mark_as_advanced( - Tcmalloc_LIBRARY - ) diff --git a/cmake/compiler_options.cmake b/cmake/compiler_options.cmake index 1d8c6ab7c..ffe1d017c 100644 --- a/cmake/compiler_options.cmake +++ b/cmake/compiler_options.cmake @@ -1,22 +1,24 @@ # Compiler options with hardening flags -if(MSVC) - - list(APPEND compiler_options - /W4 - /permissive- - $<$:/O2 /Ob2> - $<$:/O1 /Ob1> - $<$:/Zi /O2 /Ob1> - $<$:/Zi /Ob0 /Od /RTC1>) - -else(MSVC) - - list(APPEND compiler_options - -Wall - -Wextra - -Wpedantic - $<$:-O2> - $<$:-O0 -g -p -pg>) - -endif() +function(add_warnings_optimizations target_name) + if(MSVC) + target_compile_options(${target_name} + PRIVATE + /W4 + /permissive- + $<$:/O2 /Ob2> + $<$:/O1 /Ob1> + $<$:/Zi /O2 /Ob1> + $<$:/Zi /Ob0 /Od /RTC1> + ) + else() + target_compile_options(${target_name} + PRIVATE + -Wall + -Wextra + -Wpedantic + $<$:-O2> + $<$:-O0 -g -p -pg> + ) + endif() +endfunction() diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake deleted file mode 100644 index 7a31f5468..000000000 --- a/cmake/dependencies.cmake +++ /dev/null @@ -1,35 +0,0 @@ -# Dependencies - -if(BUILD_EXAMPLES OR BUILD_TESTING) - find_program(CCACHE_FOUND ccache) - if(CCACHE_FOUND) - message("Found ccache ${CCACHE_FOUND}") - message("Using ccache to speed up compilation") - set(ENV{CCACHE_CPP2} "yes") - set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) - set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) - endif(CCACHE_FOUND) - - find_package(Tcmalloc) - find_package(Threads) - - if (MSVC) - set(Boost_USE_STATIC_LIBS ON) - find_package( Boost 1.64.0 COMPONENTS system thread regex REQUIRED ) - else() - find_package( Boost 1.64.0 COMPONENTS system thread REQUIRED ) - endif() - - if(Boost_FOUND) - include_directories(${Boost_INCLUDE_DIR}) - endif() -endif() - -if(BUILD_EXAMPLES) - # OpenSSL is needed at runtime dynamically by some examples - # if it isn't installed, the examples won't be built - find_package(OpenSSL) - if(OPENSSL_FOUND) - include_directories(${OPENSSL_INCLUDE_DIR}) - endif() -endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4841e37ff..f9d84f310 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,91 +1,85 @@ cmake_minimum_required(VERSION 3.15) -project (crow_examples) +project(crow_examples) -# Define Required libraries -list(APPEND REQUIRED_LIBRARIES - ${Boost_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} - z -) +include(${CMAKE_SOURCE_DIR}/cmake/compiler_options.cmake) -if (MSVC) - add_executable(example_vs example_vs.cpp) - target_compile_options(example_vs PRIVATE "${compiler_options}") - target_link_libraries(example_vs ) -else () - add_executable(helloworld helloworld.cpp) - target_compile_options(helloworld PRIVATE "${compiler_options}") - target_link_libraries(helloworld PUBLIC ${REQUIRED_LIBRARIES}) +add_executable(helloworld helloworld.cpp) +add_warnings_optimizations(helloworld) +target_link_libraries(helloworld PUBLIC crow) +# If ZLIB is not found, the example won't be built +if(ZLIB_FOUND) add_executable(example_compression example_compression.cpp) - target_compile_options(example_compression PRIVATE "${compiler_options}") - target_link_libraries(example_compression ${REQUIRED_LIBRARIES}) - - # If OpenSSL is not found, the example won't be built - if (OPENSSL_FOUND) - add_executable(example_ssl ssl/example_ssl.cpp) - target_compile_options(example_ssl PRIVATE "${compiler_options}") - target_link_libraries(example_ssl PUBLIC ${REQUIRED_LIBRARIES} ${OPENSSL_LIBRARIES}) - else() - message(STATUS "example_ssl Example deactivated - OpenSSL was not found") - endif() + add_warnings_optimizations(example_compression) + target_link_libraries(example_compression crow) +else() + message(STATUS "example_compression Example deactivated - ZLIB was not found") +endif() - add_executable(example_websocket websocket/example_ws.cpp) - target_compile_options(example_websocket PRIVATE "${compiler_options}") - target_link_libraries(example_websocket ) - target_link_libraries(example_websocket PUBLIC ${REQUIRED_LIBRARIES}) - add_custom_command(OUTPUT ws.html - COMMAND ${CMAKE_COMMAND} -E - copy ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html ${CMAKE_CURRENT_BINARY_DIR}/templates/ws.html - DEPENDS ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html - ) - add_custom_target(example_ws_copy ALL DEPENDS ws.html) +# If OpenSSL is not found, the example won't be built +if(OPENSSL_FOUND) + add_executable(example_ssl ssl/example_ssl.cpp) + add_warnings_optimizations(example_ssl) + target_link_libraries(example_ssl PUBLIC crow) +else() + message(STATUS "example_ssl Example deactivated - OpenSSL was not found") +endif() - add_executable(basic_example example.cpp) - target_compile_options(basic_example PRIVATE "${compiler_options}") - target_link_libraries(basic_example PUBLIC ${REQUIRED_LIBRARIES}) +add_executable(example_websocket websocket/example_ws.cpp) +add_warnings_optimizations(example_websocket) +target_link_libraries(example_websocket PUBLIC crow) +add_custom_command(OUTPUT ws.html + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html ${CMAKE_CURRENT_BINARY_DIR}/templates/ws.html + DEPENDS ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html +) +add_custom_target(example_ws_copy ALL DEPENDS ws.html) - if (Tcmalloc_FOUND) - target_link_libraries(basic_example PRIVATE ${Tcmalloc_LIBRARIES}) - endif(Tcmalloc_FOUND) +add_executable(basic_example example.cpp) +add_warnings_optimizations(basic_example) +target_link_libraries(basic_example PUBLIC crow) +add_custom_command(OUTPUT example_test.py + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/example_test.py ${CMAKE_CURRENT_BINARY_DIR}/example_test.py + DEPENDS ${PROJECT_SOURCE_DIR}/example_test.py +) +add_custom_target(example_copy ALL DEPENDS example_test.py) +if(CROW_AMALGAMATE) add_executable(example_with_all example_with_all.cpp) - add_dependencies(example_with_all amalgamation) - target_compile_options(example_with_all PRIVATE "${compiler_options}") - target_link_libraries(example_with_all PUBLIC ${REQUIRED_LIBRARIES}) - - add_custom_command(OUTPUT example_test.py - COMMAND ${CMAKE_COMMAND} -E - copy ${PROJECT_SOURCE_DIR}/example_test.py ${CMAKE_CURRENT_BINARY_DIR}/example_test.py - DEPENDS ${PROJECT_SOURCE_DIR}/example_test.py - ) - add_custom_target(example_copy ALL DEPENDS example_test.py) + add_dependencies(example_with_all crow_amalgamated) + add_warnings_optimizations(example_with_all) + target_link_libraries(example_with_all PUBLIC crow) +endif() - add_executable(example_chat example_chat.cpp) - target_compile_options(example_chat PRIVATE "${compiler_options}") - target_link_libraries(example_chat PUBLIC ${REQUIRED_LIBRARIES}) +add_executable(example_chat example_chat.cpp) +add_warnings_optimizations(example_chat) +target_link_libraries(example_chat PUBLIC crow) +add_custom_command(OUTPUT example_chat.html + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/example_chat.html ${CMAKE_CURRENT_BINARY_DIR}/example_chat.html + DEPENDS ${PROJECT_SOURCE_DIR}/example_chat.html +) +add_custom_target(example_chat_copy ALL DEPENDS example_chat.html) - add_executable(example_static_file example_static_file.cpp) - target_compile_options(example_static_file PRIVATE "${compiler_options}") - target_link_libraries(example_static_file PUBLIC ${REQUIRED_LIBRARIES}) +add_executable(example_static_file example_static_file.cpp) +add_warnings_optimizations(example_static_file) +target_link_libraries(example_static_file PUBLIC crow) - add_executable(example_catchall example_catchall.cpp) - target_compile_options(example_catchall PRIVATE "${compiler_options}") - target_link_libraries(example_catchall PUBLIC ${REQUIRED_LIBRARIES}) - - add_executable(example_json_map example_json_map.cpp) - target_compile_options(example_json_map PRIVATE "${compiler_options}") - target_link_libraries(example_json_map PUBLIC ${REQUIRED_LIBRARIES}) +add_executable(example_catchall example_catchall.cpp) +add_warnings_optimizations(example_catchall) +target_link_libraries(example_catchall PUBLIC crow) - add_executable(example_blueprint example_blueprint.cpp) - target_compile_options(example_blueprint PRIVATE "${compiler_options}") - target_link_libraries(example_blueprint PUBLIC ${REQUIRED_LIBRARIES}) +add_executable(example_json_map example_json_map.cpp) +add_warnings_optimizations(example_json_map) +target_link_libraries(example_json_map PUBLIC crow) - add_custom_command(OUTPUT example_chat.html - COMMAND ${CMAKE_COMMAND} -E - copy ${PROJECT_SOURCE_DIR}/example_chat.html ${CMAKE_CURRENT_BINARY_DIR}/example_chat.html - DEPENDS ${PROJECT_SOURCE_DIR}/example_chat.html - ) - add_custom_target(example_chat_copy ALL DEPENDS example_chat.html) +add_executable(example_blueprint example_blueprint.cpp) +add_warnings_optimizations(example_blueprint) +target_link_libraries(example_blueprint PUBLIC crow) +if(MSVC) + add_executable(example_vs example_vs.cpp) + add_warnings_optimizations(example_vs) + target_link_libraries(example_vs crow) endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 53fe1ec2d..b190933f7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,16 +1,18 @@ cmake_minimum_required(VERSION 3.15) project(crow_test) +include(${CMAKE_SOURCE_DIR}/cmake/compiler_options.cmake) + set(TEST_SRCS unittest.cpp ) add_executable(unittest ${TEST_SRCS}) -target_link_libraries(unittest ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} z) +target_link_libraries(unittest crow) # set target compile options as defined in the cmake/compiler_options.cmake Module -target_compile_options(unittest PRIVATE ${compiler_options}) +add_warnings_optimizations(unittest) -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_target_properties(unittest PROPERTIES COMPILE_FLAGS "--coverage -fprofile-arcs -ftest-coverage") target_link_libraries(unittest gcov) endif() diff --git a/tests/template/CMakeLists.txt b/tests/template/CMakeLists.txt index 31e2ac266..72a9524ad 100644 --- a/tests/template/CMakeLists.txt +++ b/tests/template/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.15) -project (template_test) +project(template_test) -set(PROJECT_INCLUDE_DIR +set(PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include ) @@ -10,9 +10,10 @@ set(TEST_SRCS ) add_executable(mustachetest ${TEST_SRCS}) +target_link_libraries(mustachetest crow) target_compile_options(mustachetest PRIVATE "${compiler_options}") -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_target_properties(mustachetest PROPERTIES COMPILE_FLAGS "--coverage -fprofile-arcs -ftest-coverage") target_link_libraries(mustachetest gcov) endif()