From 0d8caeca153d1597e62e69d884e23b70a6095903 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 7 Nov 2017 11:35:44 +0100 Subject: [PATCH] Modernised all CMakeLists.txt files, and made it easier to use Simple-Web-Server as a sub-project --- .travis.yml | 2 +- CMakeLists.txt | 89 ++++++++++++++++++++++++++++---------------- tests/CMakeLists.txt | 19 +++------- 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/.travis.yml b/.travis.yml index 61370c3..2860748 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ script: make && CTEST_OUTPUT_ON_FAILURE=1 make test && rm -r * && - CXX=g++ cmake -DCMAKE_CXX_FLAGS=\"-Werror -O3 -DUSE_STANDALONE_ASIO\" .. && + CXX=g++ cmake -DUSE_STANDALONE_ASIO=ON -DCMAKE_CXX_FLAGS=\"-Werror -O3\" .. && make && CTEST_OUTPUT_ON_FAILURE=1 make test " diff --git a/CMakeLists.txt b/CMakeLists.txt index 950bd82..2dc18e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,52 +1,75 @@ cmake_minimum_required (VERSION 2.8.8) -project (Simple-Web-Server) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wsign-conversion") -include_directories(.) +project (Simple-Web-Server) + +option(USE_STANDALONE_ASIO "set ON to use standalone Asio instead of Boost.Asio" OFF) +option(BUILD_TESTING "set ON to build library tests" OFF) + +add_compile_options(-std=c++11 -Wall -Wextra -Wsign-conversion) + +add_library(simple-web-server INTERFACE) + +target_include_directories(simple-web-server INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) find_package(Threads REQUIRED) +target_link_libraries(simple-web-server INTERFACE ${CMAKE_THREAD_LIBS_INIT}) -set(BOOST_COMPONENTS system filesystem thread) -# Late 2017 TODO: remove the following checks and always use std::regex -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) - set(BOOST_COMPONENTS ${BOOST_COMPONENTS} regex) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_BOOST_REGEX") +if(USE_STANDALONE_ASIO) + target_compile_definitions(simple-web-server INTERFACE USE_STANDALONE_ASIO) + # TODO: Fix this: + # include(CheckIncludeFileCXX) + # CHECK_INCLUDE_FILE_CXX(asio.hpp HAVE_ASIO) + # if(NOT HAVE_ASIO) + # message(FATAL_ERROR "Standalone Asio not found") + # endif() +else() + find_package(Boost 1.53.0 COMPONENTS system thread REQUIRED) + target_link_libraries(simple-web-server INTERFACE ${Boost_LIBRARIES}) + target_include_directories(simple-web-server INTERFACE ${Boost_INCLUDE_DIR}) + # 2020 TODO: remove the following checks and always use std::regex + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) + target_compile_definitions(simple-web-server INTERFACE USE_BOOST_REGEX) + find_package(Boost 1.53.0 COMPONENTS regex REQUIRED) + target_link_libraries(simple-web-server INTERFACE ${Boost_LIBRARIES}) + target_include_directories(simple-web-server INTERFACE ${Boost_INCLUDE_DIR}) endif() endif() -find_package(Boost 1.53.0 COMPONENTS ${BOOST_COMPONENTS} REQUIRED) -include_directories(SYSTEM ${Boost_INCLUDE_DIR}) +if(WIN32) + target_link_libraries(simple-web-server INTERFACE ws2_32 wsock32) +endif() if(APPLE) set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl") endif() - -add_executable(http_examples http_examples.cpp) -target_link_libraries(http_examples ${Boost_LIBRARIES}) -target_link_libraries(http_examples ${CMAKE_THREAD_LIBS_INIT}) - -#TODO: add requirement for version 1.0.1g (can it be done in one line?) find_package(OpenSSL) - if(OPENSSL_FOUND) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_OPENSSL") - target_link_libraries(http_examples ${OPENSSL_LIBRARIES}) - include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR}) - - add_executable(https_examples https_examples.cpp) - target_link_libraries(https_examples ${Boost_LIBRARIES}) - target_link_libraries(https_examples ${OPENSSL_LIBRARIES}) - target_link_libraries(https_examples ${CMAKE_THREAD_LIBS_INIT}) + target_compile_definitions(simple-web-server INTERFACE HAVE_OPENSSL) + target_link_libraries(simple-web-server INTERFACE ${OPENSSL_LIBRARIES}) + target_include_directories(simple-web-server INTERFACE ${OPENSSL_INCLUDE_DIR}) endif() -if(MSYS) #TODO: Is MSYS true when MSVC is true? - target_link_libraries(http_examples ws2_32 wsock32) +# if Simple-Web-Server is not a sub-project: +if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(http_examples http_examples.cpp) + target_link_libraries(http_examples simple-web-server) + + find_package(Boost 1.53.0 COMPONENTS system filesystem REQUIRED) + target_link_libraries(http_examples ${Boost_LIBRARIES}) + target_include_directories(http_examples PRIVATE ${Boost_INCLUDE_DIR}) + if(OPENSSL_FOUND) - target_link_libraries(https_examples ws2_32 wsock32) + add_executable(https_examples https_examples.cpp) + target_link_libraries(https_examples simple-web-server) + target_link_libraries(https_examples ${Boost_LIBRARIES}) + target_include_directories(https_examples PRIVATE ${Boost_INCLUDE_DIR}) endif() + + set(BUILD_TESTING ON) + + install(FILES server_http.hpp client_http.hpp server_https.hpp client_https.hpp crypto.hpp utility.hpp status_code.hpp DESTINATION include/simple-web-server) endif() -enable_testing() -add_subdirectory(tests) - -install(FILES server_http.hpp client_http.hpp server_https.hpp client_https.hpp crypto.hpp utility.hpp status_code.hpp DESTINATION include/simple-web-server) +if(BUILD_TESTING) + enable_testing() + add_subdirectory(tests) +endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 08fc773..a8a0d45 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,26 +1,19 @@ -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-access-control") +add_compile_options(-fno-access-control) add_executable(io_test io_test.cpp) -target_link_libraries(io_test ${Boost_LIBRARIES}) -target_link_libraries(io_test ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(io_test simple-web-server) +add_test(io_test io_test) add_executable(parse_test parse_test.cpp) -target_link_libraries(parse_test ${Boost_LIBRARIES}) -target_link_libraries(parse_test ${CMAKE_THREAD_LIBS_INIT}) - -if(MSYS) #TODO: Is MSYS true when MSVC is true? - target_link_libraries(io_test ws2_32 wsock32) - target_link_libraries(parse_test ws2_32 wsock32) -endif() - -add_test(io_test io_test) +target_link_libraries(parse_test simple-web-server) add_test(parse_test parse_test) if(OPENSSL_FOUND) add_executable(crypto_test crypto_test.cpp) - target_link_libraries(crypto_test ${OPENSSL_CRYPTO_LIBRARY}) + target_link_libraries(crypto_test simple-web-server) add_test(crypto_test crypto_test) endif() add_executable(status_code_test status_code_test.cpp) +target_link_libraries(status_code_test simple-web-server) add_test(status_code_test status_code_test)