Skip to content

Commit 1acf188

Browse files
authored
CMake: Add HTTPLIB_SHARED option, don't define BUILD_SHARED_LIBS (#2266)
To avoid surprises in the projects consuming the library, don't define BUILD_SHARED_LIBS option ourselves but just use its value, if provided, to define HTTPLIB_SHARED option which can be also set directly to specify whether we should build static or shared library. Closes #2263.
1 parent 4b2b851 commit 1acf188

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

CMakeLists.txt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[[
22
Build options:
3-
* BUILD_SHARED_LIBS (default off) builds as a shared library (if HTTPLIB_COMPILE is ON)
3+
* Standard BUILD_SHARED_LIBS is supported and sets HTTPLIB_SHARED default value.
44
* HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on)
55
* HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on)
66
* HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on)
@@ -13,6 +13,7 @@
1313
* HTTPLIB_USE_NON_BLOCKING_GETADDRINFO (default on)
1414
* HTTPLIB_COMPILE (default off)
1515
* HTTPLIB_INSTALL (default on)
16+
* HTTPLIB_SHARED (default off) builds as a shared library (if HTTPLIB_COMPILE is ON)
1617
* HTTPLIB_TEST (default off)
1718
* BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed).
1819
* OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed).
@@ -109,12 +110,20 @@ option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system cer
109110
option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternatives for getaddrinfo." ON)
110111
option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF)
111112
option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON)
112-
# Defaults to static library
113-
option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF)
114-
if(BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
115-
# Necessary for Windows if building shared libs
116-
# See https://stackoverflow.com/a/40743080
117-
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
113+
# Defaults to static library but respects standard BUILD_SHARED_LIBS if set
114+
include(CMakeDependentOption)
115+
cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only."
116+
"${BUILD_SHARED_LIBS}" HTTPLIB_COMPILE OFF
117+
)
118+
if(HTTPLIB_SHARED)
119+
set(HTTPLIB_LIB_TYPE SHARED)
120+
if(WIN32)
121+
# Necessary for Windows if building shared libs
122+
# See https://stackoverflow.com/a/40743080
123+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
124+
endif()
125+
else()
126+
set(HTTPLIB_LIB_TYPE STATIC)
118127
endif()
119128

120129
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
@@ -230,8 +239,7 @@ if(HTTPLIB_COMPILE)
230239

231240
# split.py puts output in "out"
232241
set(_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR}/out")
233-
# This will automatically be either static or shared based on the value of BUILD_SHARED_LIBS
234-
add_library(${PROJECT_NAME} "${_httplib_build_includedir}/httplib.cc")
242+
add_library(${PROJECT_NAME} ${HTTPLIB_LIB_TYPE} "${_httplib_build_includedir}/httplib.cc")
235243
target_sources(${PROJECT_NAME}
236244
PUBLIC
237245
$<BUILD_INTERFACE:${_httplib_build_includedir}/httplib.h>

0 commit comments

Comments
 (0)