Skip to content

Commit f063558

Browse files
rework cmake configs.
See #160 for more info.
1 parent cf86470 commit f063558

File tree

5 files changed

+103
-141
lines changed

5 files changed

+103
-141
lines changed

CMakeLists.txt

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
cmake_minimum_required(VERSION 3.15.0 FATAL_ERROR)
55

66
# Define the Project Name and Description
7-
project (crow_all LANGUAGES CXX)
8-
9-
# Define the module path
10-
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
7+
project (crow LANGUAGES CXX)
118

129
# Set required C++ Standard
1310
set(CMAKE_CXX_STANDARD 11)
@@ -21,50 +18,54 @@ endif()
2118
#####################################
2219
# Define Options
2320
#####################################
24-
option(BUILD_EXAMPLES "Builds the examples in the project" ON)
25-
option(BUILD_TESTING "Builds the tests in the project" ON)
21+
option(CROW_BUILD_EXAMPLES "Build the examples in the project" ON )
22+
option(CROW_BUILD_TESTS "Build the tests in the project" ON )
23+
option(CROW_AMALGAMATE "Combine all headers into one" OFF)
2624

2725
#####################################
28-
# Define CMake Module Imports
26+
# Define Targets
2927
#####################################
30-
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies.cmake)
31-
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/compiler_options.cmake)
28+
add_library(crow INTERFACE)
29+
target_include_directories(crow INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
3230

33-
#####################################
34-
# Define project-wide imports
35-
#####################################
36-
# this can be alternatively (and as recommended way) done with target_include_directories()
37-
if(BUILD_EXAMPLES OR BUILD_TESTING)
38-
set(PROJECT_INCLUDE_DIR
39-
${CMAKE_CURRENT_SOURCE_DIR}/include
40-
)
31+
find_package(Boost 1.70 COMPONENTS date_time REQUIRED)
32+
find_package(Threads REQUIRED)
33+
find_package(ZLIB)
34+
find_package(OpenSSL)
35+
36+
target_link_libraries(crow
37+
INTERFACE
38+
Boost::boost Boost::date_time
39+
Threads::Threads
40+
)
4141

42-
include_directories("${PROJECT_INCLUDE_DIR}")
43-
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
44-
include_directories("${CMAKE_CURRENT_BINARY_DIR}") # To include crow_all.h
42+
if(OPENSSL_FOUND)
43+
target_link_libraries(crow INTERFACE OpenSSL::SSL)
4544
endif()
4645

47-
#####################################
48-
# Define Targets
49-
#####################################
50-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h
51-
COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/merge_all.py
52-
${CMAKE_CURRENT_SOURCE_DIR}/include
53-
${CMAKE_CURRENT_BINARY_DIR}/crow_all.h
54-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
55-
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/crow/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/crow/middlewares/*.h
56-
)
46+
if(ZLIB_FOUND)
47+
target_link_libraries(crow INTERFACE ZLIB::ZLIB)
48+
endif()
5749

58-
# Amalgamation
59-
add_custom_target(amalgamation ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h)
50+
if(CROW_AMALGAMATE)
51+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h
52+
COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/merge_all.py
53+
${CMAKE_CURRENT_SOURCE_DIR}/include
54+
${CMAKE_CURRENT_BINARY_DIR}/crow_all.h
55+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
56+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/crow/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/crow/middlewares/*.h
57+
)
58+
59+
add_custom_target(crow_amalgamated ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h)
60+
endif()
6061

6162
# Examples
62-
if(BUILD_EXAMPLES)
63+
if(CROW_BUILD_EXAMPLES)
6364
add_subdirectory(examples)
6465
endif()
6566

6667
# Tests
67-
if (NOT MSVC AND BUILD_TESTING)
68+
if(NOT MSVC AND CROW_BUILD_TESTS)
6869
add_subdirectory(tests)
6970
enable_testing()
7071
add_test(NAME crow_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/unittest)
@@ -74,7 +75,7 @@ endif()
7475
#####################################
7576
# Install Files
7677
#####################################
77-
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h DESTINATION include)
78+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION include)
7879

7980
set(CPACK_GENERATOR "DEB")
8081
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "CrowCpp")

cmake/dependencies.cmake

Lines changed: 0 additions & 35 deletions
This file was deleted.

examples/CMakeLists.txt

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,86 @@
11
cmake_minimum_required(VERSION 3.15)
22
project (crow_examples)
33

4-
# Define Required libraries
5-
list(APPEND REQUIRED_LIBRARIES
6-
${Boost_LIBRARIES}
7-
${CMAKE_THREAD_LIBS_INIT}
8-
z
9-
)
4+
include(${CMAKE_SOURCE_DIR}/cmake/compiler_options.cmake)
105

11-
if (MSVC)
12-
add_executable(example_vs example_vs.cpp)
13-
target_compile_options(example_vs PRIVATE "${compiler_options}")
14-
target_link_libraries(example_vs )
15-
else ()
16-
add_executable(helloworld helloworld.cpp)
17-
target_compile_options(helloworld PRIVATE "${compiler_options}")
18-
target_link_libraries(helloworld PUBLIC ${REQUIRED_LIBRARIES})
6+
add_executable(helloworld helloworld.cpp)
7+
target_compile_options(helloworld PRIVATE "${compiler_options}")
8+
target_link_libraries(helloworld PUBLIC crow)
199

10+
if(ZLIB_FOUND)
2011
add_executable(example_compression example_compression.cpp)
2112
target_compile_options(example_compression PRIVATE "${compiler_options}")
22-
target_link_libraries(example_compression ${REQUIRED_LIBRARIES})
23-
24-
# If OpenSSL is not found, the example won't be built
25-
if (OPENSSL_FOUND)
26-
add_executable(example_ssl ssl/example_ssl.cpp)
27-
target_compile_options(example_ssl PRIVATE "${compiler_options}")
28-
target_link_libraries(example_ssl PUBLIC ${REQUIRED_LIBRARIES} ${OPENSSL_LIBRARIES})
29-
else()
30-
message(STATUS "example_ssl Example deactivated - OpenSSL was not found")
31-
endif()
13+
target_link_libraries(example_compression crow)
14+
else()
15+
message(STATUS "example_compression Example deactivated - ZLIB was not found")
16+
endif()
3217

33-
add_executable(example_websocket websocket/example_ws.cpp)
34-
target_compile_options(example_websocket PRIVATE "${compiler_options}")
35-
target_link_libraries(example_websocket )
36-
target_link_libraries(example_websocket PUBLIC ${REQUIRED_LIBRARIES})
37-
add_custom_command(OUTPUT ws.html
38-
COMMAND ${CMAKE_COMMAND} -E
39-
copy ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html ${CMAKE_CURRENT_BINARY_DIR}/templates/ws.html
40-
DEPENDS ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html
41-
)
42-
add_custom_target(example_ws_copy ALL DEPENDS ws.html)
18+
# If OpenSSL is not found, the example won't be built
19+
if (OPENSSL_FOUND)
20+
add_executable(example_ssl ssl/example_ssl.cpp)
21+
target_compile_options(example_ssl PRIVATE "${compiler_options}")
22+
target_link_libraries(example_ssl PUBLIC crow)
23+
else()
24+
message(STATUS "example_ssl Example deactivated - OpenSSL was not found")
25+
endif()
4326

44-
add_executable(basic_example example.cpp)
45-
target_compile_options(basic_example PRIVATE "${compiler_options}")
46-
target_link_libraries(basic_example PUBLIC ${REQUIRED_LIBRARIES})
27+
add_executable(example_websocket websocket/example_ws.cpp)
28+
target_compile_options(example_websocket PRIVATE "${compiler_options}")
29+
target_link_libraries(example_websocket PUBLIC crow)
30+
add_custom_command(OUTPUT ws.html
31+
COMMAND ${CMAKE_COMMAND} -E
32+
copy ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html ${CMAKE_CURRENT_BINARY_DIR}/templates/ws.html
33+
DEPENDS ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html
34+
)
35+
add_custom_target(example_ws_copy ALL DEPENDS ws.html)
4736

48-
if (Tcmalloc_FOUND)
49-
target_link_libraries(basic_example PRIVATE ${Tcmalloc_LIBRARIES})
50-
endif(Tcmalloc_FOUND)
37+
add_executable(basic_example example.cpp)
38+
target_compile_options(basic_example PRIVATE "${compiler_options}")
39+
target_link_libraries(basic_example PUBLIC crow)
5140

41+
if(CROW_AMALGAMATE)
5242
add_executable(example_with_all example_with_all.cpp)
53-
add_dependencies(example_with_all amalgamation)
43+
add_dependencies(example_with_all crow_amalgamated)
5444
target_compile_options(example_with_all PRIVATE "${compiler_options}")
55-
target_link_libraries(example_with_all PUBLIC ${REQUIRED_LIBRARIES})
45+
target_link_libraries(example_with_all PUBLIC crow)
46+
endif()
5647

57-
add_custom_command(OUTPUT example_test.py
58-
COMMAND ${CMAKE_COMMAND} -E
59-
copy ${PROJECT_SOURCE_DIR}/example_test.py ${CMAKE_CURRENT_BINARY_DIR}/example_test.py
60-
DEPENDS ${PROJECT_SOURCE_DIR}/example_test.py
61-
)
62-
add_custom_target(example_copy ALL DEPENDS example_test.py)
48+
add_executable(example_chat example_chat.cpp)
49+
target_compile_options(example_chat PRIVATE "${compiler_options}")
50+
target_link_libraries(example_chat PUBLIC crow)
6351

64-
add_executable(example_chat example_chat.cpp)
65-
target_compile_options(example_chat PRIVATE "${compiler_options}")
66-
target_link_libraries(example_chat PUBLIC ${REQUIRED_LIBRARIES})
52+
add_executable(example_static_file example_static_file.cpp)
53+
target_compile_options(example_static_file PRIVATE "${compiler_options}")
54+
target_link_libraries(example_static_file PUBLIC crow)
6755

68-
add_executable(example_static_file example_static_file.cpp)
69-
target_compile_options(example_static_file PRIVATE "${compiler_options}")
70-
target_link_libraries(example_static_file PUBLIC ${REQUIRED_LIBRARIES})
56+
add_executable(example_catchall example_catchall.cpp)
57+
target_compile_options(example_catchall PRIVATE "${compiler_options}")
58+
target_link_libraries(example_catchall PUBLIC crow)
7159

72-
add_executable(example_catchall example_catchall.cpp)
73-
target_compile_options(example_catchall PRIVATE "${compiler_options}")
74-
target_link_libraries(example_catchall PUBLIC ${REQUIRED_LIBRARIES})
75-
76-
add_executable(example_json_map example_json_map.cpp)
77-
target_compile_options(example_json_map PRIVATE "${compiler_options}")
78-
target_link_libraries(example_json_map PUBLIC ${REQUIRED_LIBRARIES})
60+
add_executable(example_json_map example_json_map.cpp)
61+
target_compile_options(example_json_map PRIVATE "${compiler_options}")
62+
target_link_libraries(example_json_map PUBLIC crow)
7963

80-
add_executable(example_blueprint example_blueprint.cpp)
81-
target_compile_options(example_blueprint PRIVATE "${compiler_options}")
82-
target_link_libraries(example_blueprint PUBLIC ${REQUIRED_LIBRARIES})
64+
add_executable(example_blueprint example_blueprint.cpp)
65+
target_compile_options(example_blueprint PRIVATE "${compiler_options}")
66+
target_link_libraries(example_blueprint PUBLIC crow)
8367

84-
add_custom_command(OUTPUT example_chat.html
68+
add_custom_command(OUTPUT example_chat.html
69+
COMMAND ${CMAKE_COMMAND} -E
70+
copy ${PROJECT_SOURCE_DIR}/example_chat.html ${CMAKE_CURRENT_BINARY_DIR}/example_chat.html
71+
DEPENDS ${PROJECT_SOURCE_DIR}/example_chat.html
72+
)
73+
add_custom_target(example_chat_copy ALL DEPENDS example_chat.html)
74+
75+
if (MSVC)
76+
add_executable(example_vs example_vs.cpp)
77+
target_compile_options(example_vs PRIVATE "${compiler_options}")
78+
target_link_libraries(example_vs crow)
79+
else ()
80+
add_custom_command(OUTPUT example_test.py
8581
COMMAND ${CMAKE_COMMAND} -E
86-
copy ${PROJECT_SOURCE_DIR}/example_chat.html ${CMAKE_CURRENT_BINARY_DIR}/example_chat.html
87-
DEPENDS ${PROJECT_SOURCE_DIR}/example_chat.html
82+
copy ${PROJECT_SOURCE_DIR}/example_test.py ${CMAKE_CURRENT_BINARY_DIR}/example_test.py
83+
DEPENDS ${PROJECT_SOURCE_DIR}/example_test.py
8884
)
89-
add_custom_target(example_chat_copy ALL DEPENDS example_chat.html)
90-
85+
add_custom_target(example_copy ALL DEPENDS example_test.py)
9186
endif()

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(TEST_SRCS
66
)
77

88
add_executable(unittest ${TEST_SRCS})
9-
target_link_libraries(unittest ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} z)
9+
target_link_libraries(unittest crow)
1010
# set target compile options as defined in the cmake/compiler_options.cmake Module
1111
target_compile_options(unittest PRIVATE ${compiler_options})
1212

tests/template/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.15)
22
project (template_test)
33

4-
set(PROJECT_INCLUDE_DIR
4+
set(PROJECT_INCLUDE_DIR
55
${PROJECT_SOURCE_DIR}/include
66
)
77

@@ -10,6 +10,7 @@ set(TEST_SRCS
1010
)
1111

1212
add_executable(mustachetest ${TEST_SRCS})
13+
target_link_libraries(mustachetest crow)
1314
target_compile_options(mustachetest PRIVATE "${compiler_options}")
1415

1516
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")

0 commit comments

Comments
 (0)