From 906db45bec35ce9bb4ba0448aaa4c8955f3c62e2 Mon Sep 17 00:00:00 2001 From: Sacha Date: Sat, 10 Jun 2017 14:23:37 +1000 Subject: [PATCH] Add a Beast CMake interface target: fix #461 This target allows other CMake projects to acquire the Beast headers and link libraries easily. --- CHANGELOG.md | 1 + CMakeLists.txt | 85 +++++++++++++++++++------------ examples/CMakeLists.txt | 36 +++---------- examples/ssl/CMakeLists.txt | 14 +---- test/CMakeLists.txt | 6 +-- test/core/CMakeLists.txt | 4 +- test/http/CMakeLists.txt | 12 +---- test/websocket/CMakeLists.txt | 6 +-- test/websocket/ssl/CMakeLists.txt | 8 +-- test/zlib/CMakeLists.txt | 6 +-- 10 files changed, 68 insertions(+), 110 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4429f66..7a8b5d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 55: * Don't allocate memory to handle obs-fold +* Add Beast CMake interface target -------------------------------------------------------------------------------- diff --git a/CMakeLists.txt b/CMakeLists.txt index 6bd5c950..9166cbc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,12 @@ -# Part of Beast +#------------------------------------------------------------------------------- +# +# Beast +# +#------------------------------------------------------------------------------- cmake_minimum_required (VERSION 3.5.2) -project (Beast) +project (Beast VERSION 1.0.0) set_property (GLOBAL PROPERTY USE_FOLDERS ON) @@ -29,10 +33,10 @@ if (MSVC) set (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO ${replacement_flags}) else() - set(THREADS_PREFER_PTHREAD_FLAG ON) - find_package(Threads) + set (THREADS_PREFER_PTHREAD_FLAG ON) + find_package (Threads) - set(CMAKE_CXX_FLAGS + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wpedantic -Wno-unused-parameter") endif() @@ -40,30 +44,25 @@ endif() # # Boost # +#------------------------------------------------------------------------------- option (Boost_USE_STATIC_LIBS "Use static libraries for boost" ON) set (Boost_NO_SYSTEM_PATHS ON) set (Boost_USE_MULTITHREADED ON) -add_definitions (-DBOOST_COROUTINES_NO_DEPRECATION_WARNING=1) # for asio unset (Boost_INCLUDE_DIR CACHE) unset (Boost_LIBRARY_DIRS CACHE) -if (MSVC) - find_package (Boost REQUIRED) -else() - find_package (Boost REQUIRED COMPONENTS - coroutine - context - filesystem - program_options - system - thread - ) - link_libraries (${Boost_LIBRARIES}) -endif() +set (BOOST_COMPONENTS coroutine context filesystem program_options system thread) +find_package (Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS}) -include_directories (SYSTEM ${Boost_INCLUDE_DIRS}) +# Workaround for Jenkins incomplete install +if (NOT TARGET Boost::atomic) + add_library(Boost::atomic INTERFACE IMPORTED) +endif() +if (NOT TARGET Boost::date_time) + add_library(Boost::date_time INTERFACE IMPORTED) +endif() if (MINGW) link_libraries(ws2_32 mswsock) @@ -73,17 +72,18 @@ endif() # # OpenSSL # +#------------------------------------------------------------------------------- if (APPLE AND NOT DEFINED ENV{OPENSSL_ROOT_DIR}) - find_program(HOMEBREW brew) - if (NOT HOMEBREW STREQUAL "HOMEBREW-NOTFOUND") - execute_process(COMMAND brew --prefix openssl - OUTPUT_VARIABLE OPENSSL_ROOT_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE) - endif() + find_program(HOMEBREW brew) + if (NOT HOMEBREW STREQUAL "HOMEBREW-NOTFOUND") + execute_process(COMMAND brew --prefix openssl + OUTPUT_VARIABLE OPENSSL_ROOT_DIR + OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() endif() -find_package(OpenSSL) +find_package (OpenSSL) # #------------------------------------------------------------------------------- @@ -129,6 +129,25 @@ elseif ("${VARIANT}" STREQUAL "release") endif() +#------------------------------------------------------------------------------- +# +# Library interface +# +#------------------------------------------------------------------------------- + +add_library(${PROJECT_NAME} INTERFACE) +target_link_libraries(${PROJECT_NAME} INTERFACE Boost::system Boost::disable_autolinking) +if (NOT MSVC) + target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads) +endif() +target_compile_definitions(${PROJECT_NAME} INTERFACE BOOST_COROUTINES_NO_DEPRECATION_WARNING=1) + +#------------------------------------------------------------------------------- +# +# Tests and Examples +# +#------------------------------------------------------------------------------- + include_directories (.) include_directories (extras) include_directories (include) @@ -170,6 +189,12 @@ file(GLOB_RECURSE EXTRAS_INCLUDES ${PROJECT_SOURCE_DIR}/extras/beast/*.ipp ) +add_subdirectory (test) +add_subdirectory (test/core) +add_subdirectory (test/http) +add_subdirectory (test/websocket) +add_subdirectory (test/zlib) + add_subdirectory (examples) if (NOT OPENSSL_FOUND) message("OpenSSL not found. Not building SSL tests and examples") @@ -177,9 +202,3 @@ else() add_subdirectory (examples/ssl) add_subdirectory (test/websocket/ssl) endif() - -add_subdirectory (test) -add_subdirectory (test/core) -add_subdirectory (test/http) -add_subdirectory (test/websocket) -add_subdirectory (test/zlib) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index ad53efe0..41f2afb5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -10,11 +10,7 @@ add_executable (echo-op echo_op.cpp ) -if (NOT WIN32) - target_link_libraries(echo-op ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(echo-op ${Boost_LIBRARIES}) -endif() +target_link_libraries(echo-op Beast) add_executable (http-crawl ${BEAST_INCLUDES} @@ -24,11 +20,7 @@ add_executable (http-crawl http_crawl.cpp ) -if (NOT WIN32) - target_link_libraries(http-crawl ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(http-crawl ${Boost_LIBRARIES}) -endif() +target_link_libraries(http-crawl Beast) add_executable (http-server ${BEAST_INCLUDES} @@ -40,11 +32,7 @@ add_executable (http-server http_server.cpp ) -if (NOT WIN32) - target_link_libraries(http-server ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(http-server ${Boost_LIBRARIES}) -endif() +target_link_libraries(http-server Beast Boost::program_options Boost::filesystem) add_executable (http-example @@ -53,11 +41,7 @@ add_executable (http-example http_example.cpp ) -if (NOT WIN32) - target_link_libraries(http-example ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(http-example ${Boost_LIBRARIES}) -endif() +target_link_libraries(http-example Beast) add_executable (websocket-echo @@ -67,11 +51,7 @@ add_executable (websocket-echo websocket_echo.cpp ) -if (NOT WIN32) - target_link_libraries(websocket-echo ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(websocket-echo ${Boost_LIBRARIES}) -endif() +target_link_libraries(websocket-echo Beast) add_executable (websocket-example @@ -80,8 +60,4 @@ add_executable (websocket-example websocket_example.cpp ) -if (NOT WIN32) - target_link_libraries(websocket-example ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(websocket-example ${Boost_LIBRARIES}) -endif() +target_link_libraries(websocket-example Beast) diff --git a/examples/ssl/CMakeLists.txt b/examples/ssl/CMakeLists.txt index df35e5f3..fd7d9827 100644 --- a/examples/ssl/CMakeLists.txt +++ b/examples/ssl/CMakeLists.txt @@ -5,19 +5,13 @@ GroupSources(include/beast beast) GroupSources(examples/ssl "/") -include_directories(${OPENSSL_INCLUDE_DIR}) - add_executable (http-ssl-example ${BEAST_INCLUDES} ${EXTRAS_INCLUDES} http_ssl_example.cpp ) -target_link_libraries(http-ssl-example ${OPENSSL_LIBRARIES}) - -if (NOT WIN32) - target_link_libraries(http-ssl-example ${Boost_LIBRARIES} Threads::Threads) -endif() +target_link_libraries(http-ssl-example Beast OpenSSL::SSL) add_executable (websocket-ssl-example ${BEAST_INCLUDES} @@ -25,8 +19,4 @@ add_executable (websocket-ssl-example websocket_ssl_example.cpp ) -target_link_libraries(websocket-ssl-example ${OPENSSL_LIBRARIES}) - -if (NOT WIN32) - target_link_libraries(websocket-ssl-example ${Boost_LIBRARIES} Threads::Threads) -endif() +target_link_libraries(websocket-ssl-example Beast OpenSSL::SSL) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ff15cb4d..de3782c7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -16,11 +16,7 @@ add_executable (lib-tests zlib.cpp ) -if (NOT WIN32) - target_link_libraries(lib-tests ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(lib-tests ${Boost_LIBRARIES}) -endif() +target_link_libraries(lib-tests Beast Boost::program_options) if (MINGW) set_target_properties(lib-tests PROPERTIES COMPILE_FLAGS "-Wa,-mbig-obj") diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index 003f880f..e1369ef2 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -39,6 +39,4 @@ add_executable (core-tests sha1.cpp ) -if (NOT WIN32) - target_link_libraries(core-tests ${Boost_LIBRARIES} Threads::Threads) -endif() +target_link_libraries(core-tests Beast Boost::program_options Boost::filesystem Boost::coroutine Boost::thread Boost::context) diff --git a/test/http/CMakeLists.txt b/test/http/CMakeLists.txt index 4f302ff3..b4c57625 100644 --- a/test/http/CMakeLists.txt +++ b/test/http/CMakeLists.txt @@ -33,11 +33,7 @@ add_executable (http-tests write.cpp ) -if (NOT WIN32) - target_link_libraries(http-tests ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(http-tests ${Boost_LIBRARIES}) -endif() +target_link_libraries(http-tests Beast Boost::program_options Boost::filesystem Boost::coroutine Boost::thread Boost::context) add_executable (http-bench ${BEAST_INCLUDES} @@ -49,8 +45,4 @@ add_executable (http-bench parser_bench.cpp ) -if (NOT WIN32) - target_link_libraries(http-bench ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(http-bench ${Boost_LIBRARIES}) -endif() +target_link_libraries(http-bench Beast Boost::program_options Boost::filesystem) diff --git a/test/websocket/CMakeLists.txt b/test/websocket/CMakeLists.txt index d6c5a034..2f7692a3 100644 --- a/test/websocket/CMakeLists.txt +++ b/test/websocket/CMakeLists.txt @@ -21,11 +21,7 @@ add_executable (websocket-tests utf8_checker.cpp ) -if (NOT WIN32) - target_link_libraries(websocket-tests ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(websocket-tests ${Boost_LIBRARIES}) -endif() +target_link_libraries(websocket-tests Beast Boost::program_options Boost::filesystem Boost::coroutine Boost::thread Boost::context) if (MINGW) set_target_properties(websocket-tests PROPERTIES COMPILE_FLAGS "-Wa,-mbig-obj -Og") diff --git a/test/websocket/ssl/CMakeLists.txt b/test/websocket/ssl/CMakeLists.txt index 4132e4c6..418e8019 100644 --- a/test/websocket/ssl/CMakeLists.txt +++ b/test/websocket/ssl/CMakeLists.txt @@ -5,8 +5,6 @@ GroupSources(include/beast beast) GroupSources(test/websocket/ssl "/") -include_directories(${OPENSSL_INCLUDE_DIR}) - add_executable (websocket-ssl-tests ${BEAST_INCLUDES} ${EXTRAS_INCLUDES} @@ -16,8 +14,4 @@ add_executable (websocket-ssl-tests ssl_server.cpp ) -target_link_libraries(websocket-ssl-tests ${OPENSSL_LIBRARIES}) - -if (NOT WIN32) - target_link_libraries(websocket-ssl-tests ${Boost_LIBRARIES} Threads::Threads) -endif() +target_link_libraries(websocket-ssl-tests Beast Boost::program_options Boost::filesystem OpenSSL::SSL) diff --git a/test/zlib/CMakeLists.txt b/test/zlib/CMakeLists.txt index 4d955e26..16652015 100644 --- a/test/zlib/CMakeLists.txt +++ b/test/zlib/CMakeLists.txt @@ -19,8 +19,4 @@ add_executable (zlib-tests inflate_stream.cpp ) -if (NOT WIN32) - target_link_libraries(zlib-tests ${Boost_LIBRARIES} Threads::Threads) -else() - target_link_libraries(zlib-tests ${Boost_LIBRARIES}) -endif() +target_link_libraries(zlib-tests Beast Boost::program_options Boost::filesystem)