Skip to content

Commit 778285a

Browse files
committed
Add options to allow for strictly requiring OpenSSL/ZLIB
HTTPLIB_REQUIRE_OPENSSL & HTTPLIB_REQUIRE_ZLIB require the libs be found, or the build fails. HTTPLIB_USE_OPENSSL_IF_AVAILABLE & HTTPLIB_USE_ZLIB_IF_AVAILABLE silently search for the libs. If they aren't found, the build still continues, but shuts off support for those features.
1 parent f408a4e commit 778285a

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

CMakeLists.txt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,30 @@ project(httplib VERSION 0.5.12 LANGUAGES CXX)
55
# This is used in the installed Cmake config file.
66
set(_HTTPLIB_OPENSSL_MIN_VER "1.1.1")
77

8+
# Allow for a build to require OpenSSL to pass, instead of just being optional
9+
option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF)
10+
option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF)
11+
# Allow for a build to casually enable OpenSSL/ZLIB support, but silenty continue if not found.
12+
# Make these options so their automatic use can be specifically disabled (as needed)
13+
option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON)
14+
option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable compression support." ON)
15+
816
# TODO: implement the option of option to correctly split, building, and export with the split.py script.
917
# option(HTTPLIB_SPLIT "Uses a Python script to split the header into a header & source file." OFF)
1018

1119
# Threads needed for <thread> on some systems, and for <pthread.h> on Linux
1220
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)
21+
if(HTTPLIB_REQUIRE_OPENSSL)
22+
find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} REQUIRED)
23+
elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE)
24+
# Look quietly since it's optional are optional
25+
find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} QUIET)
26+
endif()
27+
if(HTTPLIB_REQUIRE_ZLIB)
28+
find_package(ZLIB REQUIRED)
29+
elseif(HTTPLIB_USE_ZLIB_IF_AVAILABLE)
30+
find_package(ZLIB QUIET)
31+
endif()
1632

1733
# Used for default, common dirs that the end-user can change (if needed)
1834
# like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,12 @@ res = cli.Post("/resource/foo", "...", "text/plain");
515515
Building & Installation (Cmake)
516516
-------------------------------
517517

518+
Build options:
519+
* `HTTPLIB_USE_OPENSSL_IF_AVAILABLE` (default on)
520+
* `HTTPLIB_USE_ZLIB_IF_AVAILABLE` (default on)
521+
* `HTTPLIB_REQUIRE_OPENSSL` (default off)
522+
* `HTTPLIB_REQUIRE_ZLIB` (default off)
523+
518524
After installation with Cmake, a `find_package(httplib)` is available.
519525
This creates a `httplib::httplib` target (if found).
520526
It can be linked like so:

httplibConfig.cmake.in

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ include(CMakeFindDependencyMacro)
55

66
# We add find_dependency calls here to not make the end-user have to call them.
77
find_dependency(Threads REQUIRED)
8-
find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ QUIET)
9-
find_dependency(ZLIB QUIET)
8+
if(@HTTPLIB_REQUIRE_OPENSSL@)
9+
find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ REQUIRED)
10+
elseif(@HTTPLIB_USE_OPENSSL_IF_AVAILABLE@)
11+
# Look quietly since it's optional are optional
12+
find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ QUIET)
13+
endif()
14+
if(@HTTPLIB_REQUIRE_ZLIB@)
15+
find_dependency(ZLIB REQUIRED)
16+
elseif(@HTTPLIB_USE_ZLIB_IF_AVAILABLE@)
17+
find_dependency(ZLIB QUIET)
18+
endif()
1019

1120
# Lets the end-user find the header path if needed
1221
# This is helpful if you're using Cmake's pre-compiled header feature

0 commit comments

Comments
 (0)