Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving CMakeLists.txt #96

Merged
merged 8 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ html/
*.cxxflags
*.files
*.includes

#VS-Code
.vscode
101 changes: 55 additions & 46 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,68 +1,77 @@
cmake_minimum_required(VERSION 3.15)
project (crow_all)
#####################################
# Define Project-Wide Settings
#####################################
cmake_minimum_required(VERSION 3.15.0 FATAL_ERROR)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

find_package(Tcmalloc)
find_package(Threads)
find_package(OpenSSL)
# Define the Project Name and Description
project (crow_all LANGUAGES CXX)

if(OPENSSL_FOUND)
include_directories(${OPENSSL_INCLUDE_DIR})
endif()
# Define the module path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

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)
# Set required C++ Standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

if (NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type selected, default to Release")
set(CMAKE_BUILD_TYPE "Release")
endif()

if (MSVC)
set(Boost_USE_STATIC_LIBS "On")
find_package( Boost 1.52 COMPONENTS system thread regex REQUIRED )
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14 -pedantic -Wextra")
find_package( Boost 1.52 COMPONENTS system thread REQUIRED )
#####################################
# Define Options
#####################################
option(BUILD_EXAMPLES "Builds the examples in the project" ON)
option(BUILD_TESTING "Builds the tests in the project" ON)

#####################################
# Define CMake Module Imports
#####################################
include(${CMAKE_SOURCE_DIR}/cmake/dependencies.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/compiler_options.cmake)

#####################################
# 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_SOURCE_DIR}/include
)

include_directories("${PROJECT_INCLUDE_DIR}")
include_directories("${CMAKE_SOURCE_DIR}")
include_directories("${CMAKE_CURRENT_BINARY_DIR}") # To include crow_all.h
endif()

include_directories(${Boost_INCLUDE_DIR})

set(PROJECT_INCLUDE_DIR
${PROJECT_SOURCE_DIR}/include
#####################################
# Define Targets
#####################################
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h
COMMAND python ${CMAKE_SOURCE_DIR}/scripts/merge_all.py
${CMAKE_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}/crow_all.h
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${CMAKE_SOURCE_DIR}/include/*.h ${CMAKE_SOURCE_DIR}/include/crow/*.h ${CMAKE_SOURCE_DIR}/include/crow/middlewares/*.h
)

include_directories("${PROJECT_INCLUDE_DIR}")
include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${CMAKE_CURRENT_BINARY_DIR}") # To include crow_all.h
# Amalgamation
add_custom_target(amalgamation ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h)

add_subdirectory(examples)
# Examples
if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

if (MSVC)
else()
# Tests
if (NOT MSVC AND BUILD_TESTING)
add_subdirectory(tests)

enable_testing()
add_test(NAME crow_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/unittest)
add_test(NAME template_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/template/test.py WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests/template)


add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h
COMMAND python ${PROJECT_SOURCE_DIR}/scripts/merge_all.py
${PROJECT_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}/crow_all.h
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${PROJECT_SOURCE_DIR}/include/*.h ${PROJECT_SOURCE_DIR}/include/crow/*.h ${PROJECT_SOURCE_DIR}/include/crow/middlewares/*.h
)

add_custom_target(amalgamation ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h)
endif()

#####################################
# Install Files
#####################################
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/crow_all.h DESTINATION include)
22 changes: 22 additions & 0 deletions cmake/compiler_options.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Compiler options with hardening flags

if(MSVC)

list(APPEND compiler_options
/W4
/permissive-
$<$<CONFIG:RELEASE>:/O2 /Ob2>
$<$<CONFIG:MINSIZEREL>:/O1 /Ob1>
$<$<CONFIG:RELWITHDEBINFO>:/Zi /O2 /Ob1>
$<$<CONFIG:DEBUG>:/Zi /Ob0 /Od /RTC1>)

else(MSVC)

list(APPEND compiler_options
-Wall
-Wextra
-Wpedantic
$<$<CONFIG:RELEASE>:-O2>
$<$<CONFIG:DEBUG>:-O0 -g -p -pg>)

endif()
35 changes: 35 additions & 0 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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()
99 changes: 55 additions & 44 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,59 +1,70 @@
cmake_minimum_required(VERSION 3.15)
project (crow_examples)

# Define Required libraries
list(APPEND REQUIRED_LIBRARIES
${Boost_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)

if (MSVC)
add_executable(example_vs example_vs.cpp)
target_link_libraries(example_vs ${Boost_LIBRARIES})
target_link_libraries(example_vs ${CMAKE_THREAD_LIBS_INIT})
target_compile_options(example_vs PRIVATE "${compiler_options}")
target_link_libraries(example_vs )
else ()
add_executable(helloworld helloworld.cpp)
target_link_libraries(helloworld ${Boost_LIBRARIES})
target_link_libraries(helloworld ${CMAKE_THREAD_LIBS_INIT})
target_compile_options(helloworld PRIVATE "${compiler_options}")
target_link_libraries(helloworld PUBLIC ${REQUIRED_LIBRARIES})

if (OPENSSL_FOUND)
add_executable(example_ssl ssl/example_ssl.cpp)
target_link_libraries(example_ssl ${Boost_LIBRARIES})
target_link_libraries(example_ssl ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES})
endif()
# 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()
madduci marked this conversation as resolved.
Show resolved Hide resolved

add_executable(example_websocket websocket/example_ws.cpp)
target_link_libraries(example_websocket ${Boost_LIBRARIES})
target_link_libraries(example_websocket ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_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)
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)

add_executable(basic_example example.cpp)
target_link_libraries(basic_example ${Boost_LIBRARIES})
target_link_libraries(basic_example ${CMAKE_THREAD_LIBS_INIT})
add_executable(basic_example example.cpp)
target_compile_options(basic_example PRIVATE "${compiler_options}")
target_link_libraries(basic_example PUBLIC ${REQUIRED_LIBRARIES})

if (Tcmalloc_FOUND)
target_link_libraries(basic_example ${Tcmalloc_LIBRARIES})
endif(Tcmalloc_FOUND)
if (Tcmalloc_FOUND)
target_link_libraries(basic_example ${Tcmalloc_LIBRARIES})
endif(Tcmalloc_FOUND)

add_executable(example_with_all example_with_all.cpp)
add_dependencies(example_with_all amalgamation)
target_link_libraries(example_with_all ${Boost_LIBRARIES})
target_link_libraries(example_with_all ${CMAKE_THREAD_LIBS_INIT})
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_executable(example_chat example_chat.cpp)
target_link_libraries(example_chat ${Boost_LIBRARIES})
target_link_libraries(example_chat ${CMAKE_THREAD_LIBS_INIT})
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_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 ${REQUIRED_LIBRARIES})

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)

endif()
6 changes: 3 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ set(TEST_SRCS
)

add_executable(unittest ${TEST_SRCS})
target_link_libraries(unittest ${Boost_LIBRARIES})
target_link_libraries(unittest ${CMAKE_THREAD_LIBS_INIT})
set_target_properties(unittest PROPERTIES COMPILE_FLAGS "-Wall -Werror -std=c++14")
target_link_libraries(unittest ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
# set target compile options as defined in the cmake/compiler_options.cmake Module
target_compile_options(unittest PRIVATE ${compiler_options})

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set_target_properties(unittest PROPERTIES COMPILE_FLAGS "--coverage -fprofile-arcs -ftest-coverage")
Expand Down
3 changes: 1 addition & 2 deletions tests/template/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ set(TEST_SRCS
)

add_executable(mustachetest ${TEST_SRCS})
set_target_properties(mustachetest PROPERTIES COMPILE_FLAGS "-Wall -Werror -std=c++14")

target_compile_options(mustachetest PRIVATE "${compiler_options}")
file(COPY DIRECTORY . DESTINATION ${CMAKE_CURRENT_BINARY_DIR} FILES_MATCHING PATTERN "*.json")

add_custom_command(OUTPUT test.py
Expand Down