Skip to content

Commit 1e6c0f9

Browse files
committed
Add automatic OpenSSL & ZLIB detection for cmake
If one is detected, it will automatically link & define the required definition. This lets its dependencies be applied easily to whatever depends on it. Note that OpenSSL and ZLIB are still optional, and merely used if found.
1 parent 3f8f2fc commit 1e6c0f9

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

CMakeLists.txt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
cmake_minimum_required(VERSION 3.7.0 FATAL_ERROR)
22
project(httplib VERSION 0.5.12 LANGUAGES CXX)
33

4+
# Change as needed to set an OpenSSL minimum version.
5+
# This is used in the installed Cmake config file.
6+
set(_HTTPLIB_OPENSSL_MIN_VER "1.1.1")
47

5-
# Include
8+
# TODO: implement the option of option to correctly split, building, and export with the split.py script.
9+
# option(HTTPLIB_SPLIT "Uses a Python script to split the header into a header & source file." OFF)
10+
11+
# Threads needed for <thread> on some systems, and for <pthread.h> on Linux
12+
find_package(Threads REQUIRED)
13+
# Look quietly since these are optional
14+
find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} QUIET)
15+
find_package(ZLIB QUIET)
16+
17+
# Used for default, common dirs that the end-user can change (if needed)
18+
# like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR
619
include(GNUInstallDirs)
720

821
add_library(${PROJECT_NAME} INTERFACE)
@@ -12,6 +25,21 @@ target_include_directories(${PROJECT_NAME} INTERFACE
1225
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
1326
$<INSTALL_INTERFACE:include>)
1427

28+
target_link_libraries(${PROJECT_NAME} INTERFACE
29+
# Always require threads
30+
Threads::Threads
31+
# Only link zlib & openssl if they're found
32+
$<$<BOOL:${ZLIB_FOUND}>:ZLIB::ZLIB>
33+
$<$<BOOL:${OPENSSL_FOUND}>:OpenSSL::SSL OpenSSL::Crypto>
34+
)
35+
36+
# Auto-define the optional support if those packages were found
37+
target_compile_definitions(${PROJECT_NAME} INTERFACE
38+
$<$<BOOL:${ZLIB_FOUND}>:CPPHTTPLIB_ZLIB_SUPPORT>
39+
$<$<BOOL:${OPENSSL_FOUND}>:CPPHTTPLIB_OPENSSL_SUPPORT>
40+
)
41+
42+
1543
install(TARGETS ${PROJECT_NAME} EXPORT httplibConfig
1644
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
1745
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

0 commit comments

Comments
 (0)