@@ -89,24 +89,29 @@ option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF)
8989# Allow for a build to require OpenSSL to pass, instead of just being optional
9090option (HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF )
9191option (HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF )
92+
9293# Allow for a build to casually enable OpenSSL/ZLIB support, but silently continue if not found.
9394# Make these options so their automatic use can be specifically disabled (as needed)
9495option (HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON )
9596option (HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib compression support." ON )
97+
9698# Lets you compile the program as a regular library instead of header-only
9799option (HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF )
100+
98101# Lets you disable the installation (useful when fetched from another CMake project)
99102option (HTTPLIB_INSTALL "Enables the installation target" ON )
100103option (HTTPLIB_TEST "Enables testing and builds tests" OFF )
101104option (HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF )
102105option (HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON )
103106option (HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON )
107+
104108# Defaults to static library
105109option (BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF )
106- if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
107- # Necessary for Windows if building shared libs
108- # See https://stackoverflow.com/a/40743080
109- set (CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON )
110+
111+ if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
112+ # Necessary for Windows if building shared libs
113+ # See https://stackoverflow.com/a/40743080
114+ set (CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON )
110115endif ()
111116
112117# Set some variables that are used in-tree and while building based on our options
@@ -116,6 +121,7 @@ set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_
116121# Threads needed for <thread> on some systems, and for <pthread.h> on Linux
117122set (THREADS_PREFER_PTHREAD_FLAG TRUE )
118123find_package (Threads REQUIRED)
124+
119125# Since Cmake v3.11, Crypto & SSL became optional when not specified as COMPONENTS.
120126if (HTTPLIB_REQUIRE_OPENSSL)
121127 find_package (OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL REQUIRED)
@@ -145,6 +151,7 @@ endif()
145151# Adds our cmake folder to the search path for find_package
146152# This is so we can use our custom FindBrotli.cmake
147153list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR} /cmake" )
154+
148155if (HTTPLIB_REQUIRE_BROTLI)
149156 find_package (Brotli COMPONENTS encoder decoder common REQUIRED)
150157 set (HTTPLIB_IS_USING_BROTLI TRUE )
@@ -158,50 +165,69 @@ endif()
158165include (GNUInstallDirs)
159166
160167if (HTTPLIB_COMPILE)
161- # Put the split script into the build dir
162- configure_file (split.py "${CMAKE_CURRENT_BINARY_DIR} /split.py"
163- COPYONLY
164- )
165- # Needs to be in the same dir as the python script
166- configure_file (httplib.h "${CMAKE_CURRENT_BINARY_DIR} /httplib.h"
167- COPYONLY
168- )
169-
170- # Used outside of this if-else
171- set (_INTERFACE_OR_PUBLIC PUBLIC )
172- # Brings in the Python3_EXECUTABLE path we can use.
173- find_package (Python3 REQUIRED)
174- # Actually split the file
175- # Keeps the output in the build dir to not pollute the main dir
176- execute_process (COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR} /split.py"
177- WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
178- ERROR_VARIABLE _httplib_split_error
179- )
180- if (_httplib_split_error)
181- message (FATAL_ERROR "Failed when trying to split cpp-httplib with the Python script.\n ${_httplib_split_error} " )
182- endif ()
183-
184- # split.py puts output in "out"
185- set (_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR} /out" )
186- # This will automatically be either static or shared based on the value of BUILD_SHARED_LIBS
187- add_library (${PROJECT_NAME} "${_httplib_build_includedir} /httplib.cc" )
188- target_sources (${PROJECT_NAME}
189- PUBLIC
190- $<BUILD_INTERFACE:${_httplib_build_includedir} /httplib.h>
191- $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} /httplib.h>
192- )
193- set_target_properties (${PROJECT_NAME}
194- PROPERTIES
195- VERSION ${${PROJECT_NAME} _VERSION}
196- SOVERSION "${${PROJECT_NAME} _VERSION_MAJOR}.${${PROJECT_NAME} _VERSION_MINOR}"
197- OUTPUT_NAME cpp-httplib
198- )
168+ # Put the split script into the build dir
169+ configure_file (split.py "${CMAKE_CURRENT_BINARY_DIR} /split.py"
170+ COPYONLY
171+ )
172+
173+ # Needs to be in the same dir as the python script
174+ configure_file (httplib.h "${CMAKE_CURRENT_BINARY_DIR} /httplib.h"
175+ COPYONLY
176+ )
177+
178+ # Used outside of this if-else
179+ set (_INTERFACE_OR_PUBLIC PUBLIC )
180+
181+ # Brings in the Python3_EXECUTABLE path we can use.
182+ find_package (Python3 REQUIRED)
183+
184+ # Actually split the file
185+ # Keeps the output in the build dir to not pollute the main dir
186+ execute_process (COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR} /split.py"
187+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
188+ ERROR_VARIABLE _httplib_split_error
189+ )
190+
191+ if (_httplib_split_error)
192+ message (FATAL_ERROR "Failed when trying to split cpp-httplib with the Python script.\n ${_httplib_split_error} " )
193+ endif ()
194+
195+ # split.py puts output in "out"
196+ set (_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR} /out" )
197+
198+ # This will automatically be either static or shared based on the value of BUILD_SHARED_LIBS
199+ add_library (${PROJECT_NAME} "${_httplib_build_includedir} /httplib.cc" )
200+ target_sources (${PROJECT_NAME}
201+ PUBLIC
202+ $<BUILD_INTERFACE:${_httplib_build_includedir} /httplib.h>
203+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} /httplib.h>
204+ )
205+ set_target_properties (${PROJECT_NAME}
206+ PROPERTIES
207+ VERSION ${${PROJECT_NAME} _VERSION}
208+ SOVERSION "${${PROJECT_NAME} _VERSION_MAJOR}.${${PROJECT_NAME} _VERSION_MINOR}"
209+ OUTPUT_NAME cpp-httplib
210+ )
211+
212+ if (MSVC )
213+ target_compile_options (${PROJECT_NAME} PRIVATE
214+ /W4 /permissive-
215+
216+ # Turn off warnings of external libraries
217+ /external:anglebrackets /external:W0 /analyze:external-
218+ )
219+ else ()
220+ target_compile_options (${PROJECT_NAME} PRIVATE
221+ -Wall -Wextra -pedantic
222+ )
223+ endif ()
199224else ()
200- # This is for header-only.
201- set (_INTERFACE_OR_PUBLIC INTERFACE )
202- add_library (${PROJECT_NAME} INTERFACE )
203- set (_httplib_build_includedir "${CMAKE_CURRENT_SOURCE_DIR} " )
225+ # This is for header-only.
226+ set (_INTERFACE_OR_PUBLIC INTERFACE )
227+ add_library (${PROJECT_NAME} INTERFACE )
228+ set (_httplib_build_includedir "${CMAKE_CURRENT_SOURCE_DIR} " )
204229endif ()
230+
205231# Lets you address the target with httplib::httplib
206232# Only useful if building in-tree, versus using it from an installation.
207233add_library (${PROJECT_NAME} ::${PROJECT_NAME} ALIAS ${PROJECT_NAME} )
@@ -216,28 +242,31 @@ target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC}
216242
217243# Always require threads
218244target_link_libraries (${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
219- Threads::Threads
220- # Needed for Windows libs on Mingw, as the pragma comment(lib, "xyz") aren't triggered.
221- $<$<PLATFORM_ID:Windows>:ws2_32>
222- $<$<PLATFORM_ID:Windows>:crypt32>
223- # Needed for API from MacOS Security framework
224- "$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL} >,$<BOOL:${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN} >>:-framework CoreFoundation -framework Security>"
225- # Can't put multiple targets in a single generator expression or it bugs out.
226- $<$<BOOL :${HTTPLIB_IS_USING_BROTLI} >:Brotli::common>
227- $<$<BOOL :${HTTPLIB_IS_USING_BROTLI} >:Brotli::encoder>
228- $<$<BOOL :${HTTPLIB_IS_USING_BROTLI} >:Brotli::decoder>
229- $<$<BOOL :${HTTPLIB_IS_USING_ZLIB} >:ZLIB::ZLIB>
230- $<$<BOOL :${HTTPLIB_IS_USING_OPENSSL} >:OpenSSL::SSL>
231- $<$<BOOL :${HTTPLIB_IS_USING_OPENSSL} >:OpenSSL::Crypto>
245+ Threads::Threads
246+
247+ # Needed for Windows libs on Mingw, as the pragma comment(lib, "xyz") aren't triggered.
248+ $<$<PLATFORM_ID:Windows>:ws2_32>
249+ $<$<PLATFORM_ID:Windows>:crypt32>
250+
251+ # Needed for API from MacOS Security framework
252+ "$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL} >,$<BOOL:${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN} >>:-framework CoreFoundation -framework Security>"
253+
254+ # Can't put multiple targets in a single generator expression or it bugs out.
255+ $<$<BOOL :${HTTPLIB_IS_USING_BROTLI} >:Brotli::common>
256+ $<$<BOOL :${HTTPLIB_IS_USING_BROTLI} >:Brotli::encoder>
257+ $<$<BOOL :${HTTPLIB_IS_USING_BROTLI} >:Brotli::decoder>
258+ $<$<BOOL :${HTTPLIB_IS_USING_ZLIB} >:ZLIB::ZLIB>
259+ $<$<BOOL :${HTTPLIB_IS_USING_OPENSSL} >:OpenSSL::SSL>
260+ $<$<BOOL :${HTTPLIB_IS_USING_OPENSSL} >:OpenSSL::Crypto>
232261)
233262
234263# Set the definitions to enable optional features
235264target_compile_definitions (${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
236- $<$<BOOL :${HTTPLIB_NO_EXCEPTIONS} >:CPPHTTPLIB_NO_EXCEPTIONS>
237- $<$<BOOL :${HTTPLIB_IS_USING_BROTLI} >:CPPHTTPLIB_BROTLI_SUPPORT>
238- $<$<BOOL :${HTTPLIB_IS_USING_ZLIB} >:CPPHTTPLIB_ZLIB_SUPPORT>
239- $<$<BOOL :${HTTPLIB_IS_USING_OPENSSL} >:CPPHTTPLIB_OPENSSL_SUPPORT>
240- $<$<AND :$<PLATFORM_ID:Darwin>,$<BOOL :${HTTPLIB_IS_USING_OPENSSL} >,$<BOOL :${HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN} >>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN>
265+ $<$<BOOL :${HTTPLIB_NO_EXCEPTIONS} >:CPPHTTPLIB_NO_EXCEPTIONS>
266+ $<$<BOOL :${HTTPLIB_IS_USING_BROTLI} >:CPPHTTPLIB_BROTLI_SUPPORT>
267+ $<$<BOOL :${HTTPLIB_IS_USING_ZLIB} >:CPPHTTPLIB_ZLIB_SUPPORT>
268+ $<$<BOOL :${HTTPLIB_IS_USING_OPENSSL} >:CPPHTTPLIB_OPENSSL_SUPPORT>
269+ $<$<AND :$<PLATFORM_ID:Darwin>,$<BOOL :${HTTPLIB_IS_USING_OPENSSL} >,$<BOOL :${HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN} >>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN>
241270)
242271
243272# CMake configuration files installation directory
@@ -254,20 +283,23 @@ configure_package_config_file("cmake/${PROJECT_NAME}Config.cmake.in"
254283)
255284
256285if (HTTPLIB_COMPILE)
257- write_basic_package_version_file("${PROJECT_NAME} ConfigVersion.cmake"
258- # Example: if you find_package(httplib 0.5.4)
259- # then anything >= 0.5.4 and < 0.6 is accepted
260- COMPATIBILITY SameMinorVersion
261- )
286+ write_basic_package_version_file("${PROJECT_NAME} ConfigVersion.cmake"
287+
288+ # Example: if you find_package(httplib 0.5.4)
289+ # then anything >= 0.5.4 and < 0.6 is accepted
290+ COMPATIBILITY SameMinorVersion
291+ )
262292else ()
263- write_basic_package_version_file("${PROJECT_NAME} ConfigVersion.cmake"
264- # Example: if you find_package(httplib 0.5.4)
265- # then anything >= 0.5.4 and < 0.6 is accepted
266- COMPATIBILITY SameMinorVersion
267- # Tells Cmake that it's a header-only lib
268- # Mildly useful for end-users :)
269- ARCH_INDEPENDENT
270- )
293+ write_basic_package_version_file("${PROJECT_NAME} ConfigVersion.cmake"
294+
295+ # Example: if you find_package(httplib 0.5.4)
296+ # then anything >= 0.5.4 and < 0.6 is accepted
297+ COMPATIBILITY SameMinorVersion
298+
299+ # Tells Cmake that it's a header-only lib
300+ # Mildly useful for end-users :)
301+ ARCH_INDEPENDENT
302+ )
271303endif ()
272304
273305if (HTTPLIB_INSTALL)
0 commit comments