1cmake_minimum_required(VERSION 3.0) 2 3project(Simple-Web-Server) 4 5option(USE_STANDALONE_ASIO "set ON to use standalone Asio instead of Boost.Asio" OFF) 6if(CMAKE_SOURCE_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") 7 option(BUILD_TESTING "set ON to build library tests" ON) 8else() 9 option(BUILD_TESTING "set ON to build library tests" OFF) 10endif() 11option(BUILD_FUZZING "set ON to build library fuzzers" OFF) 12option(USE_OPENSSL "set OFF to build without OpenSSL" ON) 13 14add_library(simple-web-server INTERFACE) 15 16target_include_directories(simple-web-server INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) 17 18find_package(Threads REQUIRED) 19target_link_libraries(simple-web-server INTERFACE ${CMAKE_THREAD_LIBS_INIT}) 20 21# TODO 2020 when Debian Jessie LTS ends: 22# Remove Boost system, thread, regex components; use Boost::<component> aliases; remove Boost target_include_directories 23if(USE_STANDALONE_ASIO) 24 target_compile_definitions(simple-web-server INTERFACE ASIO_STANDALONE) 25 find_path(ASIO_PATH asio.hpp) 26 if(NOT ASIO_PATH) 27 message(FATAL_ERROR "Standalone Asio not found") 28 else() 29 target_include_directories(simple-web-server INTERFACE ${ASIO_PATH}) 30 endif() 31else() 32 find_package(Boost 1.53.0 COMPONENTS system thread REQUIRED) 33 target_link_libraries(simple-web-server INTERFACE ${Boost_LIBRARIES}) 34 target_include_directories(simple-web-server INTERFACE ${Boost_INCLUDE_DIR}) 35 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) 36 target_compile_definitions(simple-web-server INTERFACE USE_BOOST_REGEX) 37 find_package(Boost 1.53.0 COMPONENTS regex REQUIRED) 38 target_link_libraries(simple-web-server INTERFACE ${Boost_LIBRARIES}) 39 target_include_directories(simple-web-server INTERFACE ${Boost_INCLUDE_DIR}) 40 endif() 41endif() 42if(WIN32) 43 target_link_libraries(simple-web-server INTERFACE ws2_32 wsock32) 44endif() 45 46if(APPLE) 47 if(EXISTS /usr/local/opt/openssl) 48 set(OPENSSL_ROOT_DIR /usr/local/opt/openssl) 49 elseif(EXISTS /opt/homebrew/opt/openssl) 50 set(OPENSSL_ROOT_DIR /opt/homebrew/opt/openssl) 51 endif() 52endif() 53if(USE_OPENSSL) 54 find_package(OpenSSL) 55endif() 56if(OPENSSL_FOUND) 57 target_compile_definitions(simple-web-server INTERFACE HAVE_OPENSSL) 58 target_link_libraries(simple-web-server INTERFACE ${OPENSSL_LIBRARIES}) 59 target_include_directories(simple-web-server INTERFACE ${OPENSSL_INCLUDE_DIR}) 60endif() 61 62# If Simple-Web-Server is not a sub-project: 63if(CMAKE_SOURCE_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") 64 if(NOT MSVC) 65 add_compile_options(-std=c++11 -Wall -Wextra) 66 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 67 add_compile_options(-Wthread-safety) 68 endif() 69 else() 70 add_compile_options(/W1) 71 endif() 72 73 find_package(Boost 1.53.0 COMPONENTS system thread filesystem) 74 if(Boost_FOUND) 75 add_executable(http_examples http_examples.cpp) 76 target_link_libraries(http_examples simple-web-server) 77 target_link_libraries(http_examples ${Boost_LIBRARIES}) 78 target_include_directories(http_examples PRIVATE ${Boost_INCLUDE_DIR}) 79 if(OPENSSL_FOUND) 80 add_executable(https_examples https_examples.cpp) 81 target_link_libraries(https_examples simple-web-server) 82 target_link_libraries(https_examples ${Boost_LIBRARIES}) 83 target_include_directories(https_examples PRIVATE ${Boost_INCLUDE_DIR}) 84 endif() 85 endif() 86 87 install(FILES asio_compatibility.hpp server_http.hpp client_http.hpp server_https.hpp client_https.hpp crypto.hpp utility.hpp status_code.hpp mutex.hpp DESTINATION include/simple-web-server) 88endif() 89 90if(BUILD_TESTING OR BUILD_FUZZING) 91 if(BUILD_TESTING) 92 enable_testing() 93 endif() 94 add_subdirectory(tests) 95endif() 96