Skip to content

Commit

Permalink
rework cmake configs.
Browse files Browse the repository at this point in the history
See #160 for more info.
  • Loading branch information
luca-schlecker committed Aug 24, 2021
1 parent 43dbad2 commit dc92762
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 141 deletions.
71 changes: 36 additions & 35 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
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/")
project (crow LANGUAGES CXX)

# Set required C++ Standard
set(CMAKE_CXX_STANDARD 11)
Expand All @@ -21,50 +18,54 @@ 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)

#####################################
# 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 COMPONENTS date_time REQUIRED)
find_package(Threads REQUIRED)
find_package(ZLIB)
find_package(OpenSSL)

target_link_libraries(crow
INTERFACE
Boost::boost Boost::date_time
Threads::Threads
)

include_directories("${PROJECT_INCLUDE_DIR}")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
include_directories("${CMAKE_CURRENT_BINARY_DIR}") # To include crow_all.h
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)
Expand All @@ -74,7 +75,7 @@ endif()
#####################################
# Install Files
#####################################
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h DESTINATION include)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION include)

set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "CrowCpp")
Expand Down
35 changes: 0 additions & 35 deletions cmake/dependencies.cmake

This file was deleted.

133 changes: 64 additions & 69 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,91 +1,86 @@
cmake_minimum_required(VERSION 3.15)
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)
target_compile_options(helloworld PRIVATE "${compiler_options}")
target_link_libraries(helloworld PUBLIC crow)

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()
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)
target_compile_options(example_ssl PRIVATE "${compiler_options}")
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)
target_compile_options(example_websocket PRIVATE "${compiler_options}")
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)
target_compile_options(basic_example PRIVATE "${compiler_options}")
target_link_libraries(basic_example PUBLIC crow)

if(CROW_AMALGAMATE)
add_executable(example_with_all example_with_all.cpp)
add_dependencies(example_with_all amalgamation)
add_dependencies(example_with_all crow_amalgamated)
target_compile_options(example_with_all PRIVATE "${compiler_options}")
target_link_libraries(example_with_all PUBLIC ${REQUIRED_LIBRARIES})
target_link_libraries(example_with_all PUBLIC crow)
endif()

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_executable(example_chat example_chat.cpp)
target_compile_options(example_chat PRIVATE "${compiler_options}")
target_link_libraries(example_chat PUBLIC crow)

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_static_file example_static_file.cpp)
target_compile_options(example_static_file PRIVATE "${compiler_options}")
target_link_libraries(example_static_file PUBLIC crow)

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_catchall example_catchall.cpp)
target_compile_options(example_catchall PRIVATE "${compiler_options}")
target_link_libraries(example_catchall 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_json_map example_json_map.cpp)
target_compile_options(example_json_map PRIVATE "${compiler_options}")
target_link_libraries(example_json_map 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_blueprint example_blueprint.cpp)
target_compile_options(example_blueprint PRIVATE "${compiler_options}")
target_link_libraries(example_blueprint PUBLIC crow)

add_custom_command(OUTPUT example_chat.html
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)

if (MSVC)
add_executable(example_vs example_vs.cpp)
target_compile_options(example_vs PRIVATE "${compiler_options}")
target_link_libraries(example_vs crow)
else ()
add_custom_command(OUTPUT example_test.py
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
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_chat_copy ALL DEPENDS example_chat.html)

add_custom_target(example_copy ALL DEPENDS example_test.py)
endif()
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(TEST_SRCS
)

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})

Expand Down
3 changes: 2 additions & 1 deletion tests/template/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.15)
project (template_test)

set(PROJECT_INCLUDE_DIR
set(PROJECT_INCLUDE_DIR
${PROJECT_SOURCE_DIR}/include
)

Expand All @@ -10,6 +10,7 @@ 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")
Expand Down

0 comments on commit dc92762

Please sign in to comment.