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)
1414 * HTTPLIB_USE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE (default on)
1515 * HTTPLIB_COMPILE (default off)
1616 * HTTPLIB_INSTALL (default on)
17+ * HTTPLIB_SHARED (default off) builds as a shared library (if HTTPLIB_COMPILE is ON)
1718 * HTTPLIB_TEST (default off)
1819 * BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed).
1920 * OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed).
@@ -111,12 +112,34 @@ option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternativ
111112option (HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF )
112113option (HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON )
113114option (HTTPLIB_USE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE "Enable automatic root certificates update on Windows." ON )
114- # Defaults to static library
115- option (BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF )
116- if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
117- # Necessary for Windows if building shared libs
118- # See https://stackoverflow.com/a/40743080
119- set (CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON )
115+ # Defaults to static library but respects standard BUILD_SHARED_LIBS if set
116+ include (CMakeDependentOption)
117+ cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only."
118+ "${BUILD_SHARED_LIBS} " HTTPLIB_COMPILE OFF
119+ )
120+ if (HTTPLIB_SHARED)
121+ set (HTTPLIB_LIB_TYPE SHARED)
122+ if (WIN32 )
123+ # Necessary for Windows if building shared libs
124+ # See https://stackoverflow.com/a/40743080
125+ set (CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON )
126+ endif ()
127+ else ()
128+ set (HTTPLIB_LIB_TYPE STATIC )
129+ endif ()
130+
131+ if (CMAKE_SYSTEM_NAME MATCHES "Windows" )
132+ if (CMAKE_SYSTEM_VERSION )
133+ if (${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0" )
134+ message (SEND_ERROR "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later." )
135+ endif ()
136+ else ()
137+ set (CMAKE_SYSTEM_VERSION "10.0.19041.0" )
138+ message (WARNING "The target is Windows but CMAKE_SYSTEM_VERSION is not set, the default system version is set to Windows 10." )
139+ endif ()
140+ endif ()
141+ if (CMAKE_SIZEOF_VOID_P LESS 8)
142+ message (WARNING "Pointer size ${CMAKE_SIZEOF_VOID_P} is not supported. Please use a 64-bit compiler." )
120143endif ()
121144
122145# Set some variables that are used in-tree and while building based on our options
@@ -218,8 +241,7 @@ if(HTTPLIB_COMPILE)
218241
219242 # split.py puts output in "out"
220243 set (_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR} /out" )
221- # This will automatically be either static or shared based on the value of BUILD_SHARED_LIBS
222- add_library (${PROJECT_NAME} "${_httplib_build_includedir} /httplib.cc" )
244+ add_library (${PROJECT_NAME} ${HTTPLIB_LIB_TYPE} "${_httplib_build_includedir} /httplib.cc" )
223245 target_sources (${PROJECT_NAME}
224246 PUBLIC
225247 $<BUILD_INTERFACE:${_httplib_build_includedir} /httplib.h>
@@ -245,8 +267,8 @@ add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
245267target_compile_features (${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11)
246268
247269target_include_directories (${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC}
248- $<BUILD_INTERFACE:${_httplib_build_includedir} >
249- $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >
270+ $<BUILD_INTERFACE:${_httplib_build_includedir} >
271+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >
250272)
251273
252274# Always require threads
@@ -343,6 +365,6 @@ if(HTTPLIB_INSTALL)
343365endif ()
344366
345367if (HTTPLIB_TEST)
346- include (CTest)
347- add_subdirectory (test )
368+ include (CTest)
369+ add_subdirectory (test )
348370endif ()
0 commit comments