CMakeLists.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #[[
  2. Build options:
  3. * Standard BUILD_SHARED_LIBS is supported and sets HTTPLIB_SHARED default value.
  4. * HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on)
  5. * HTTPLIB_USE_WOLFSSL_IF_AVAILABLE (default off)
  6. * HTTPLIB_USE_MBEDTLS_IF_AVAILABLE (default off)
  7. * HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on)
  8. * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on)
  9. * HTTPLIB_USE_ZSTD_IF_AVAILABLE (default on)
  10. * HTTPLIB_BUILD_MODULES (default off)
  11. * HTTPLIB_REQUIRE_OPENSSL (default off)
  12. * HTTPLIB_REQUIRE_WOLFSSL (default off)
  13. * HTTPLIB_REQUIRE_MBEDTLS (default off)
  14. * HTTPLIB_REQUIRE_ZLIB (default off)
  15. * HTTPLIB_REQUIRE_BROTLI (default off)
  16. * HTTPLIB_REQUIRE_ZSTD (default off)
  17. * HTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES (default off)
  18. * HTTPLIB_USE_NON_BLOCKING_GETADDRINFO (default on)
  19. * HTTPLIB_COMPILE (default off)
  20. * HTTPLIB_INSTALL (default on)
  21. * HTTPLIB_SHARED (default off) builds as a shared library (if HTTPLIB_COMPILE is ON)
  22. * HTTPLIB_TEST (default off)
  23. * BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed).
  24. * OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed).
  25. -------------------------------------------------------------------------------
  26. After installation with Cmake, a find_package(httplib COMPONENTS OpenSSL wolfssl MbedTLS ZLIB Brotli zstd) is available.
  27. This creates a httplib::httplib target (if found and if listed components are supported).
  28. It can be linked like so:
  29. target_link_libraries(your_exe httplib::httplib)
  30. The following will build & install for later use.
  31. Linux/macOS:
  32. mkdir -p build
  33. cd build
  34. cmake -DCMAKE_BUILD_TYPE=Release ..
  35. sudo cmake --build . --target install
  36. Windows:
  37. mkdir build
  38. cd build
  39. cmake ..
  40. runas /user:Administrator "cmake --build . --config Release --target install"
  41. -------------------------------------------------------------------------------
  42. These variables are available after you run find_package(httplib)
  43. * HTTPLIB_HEADER_PATH - this is the full path to the installed header (e.g. /usr/include/httplib.h).
  44. * HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled.
  45. * HTTPLIB_IS_USING_WOLFSSL - a bool for if wolfSSL support is enabled.
  46. * HTTPLIB_IS_USING_MBEDTLS - a bool for if MbedTLS support is enabled.
  47. * HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled.
  48. * HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled.
  49. * HTTPLIB_IS_USING_ZSTD - a bool for if ZSTD support is enabled.
  50. * HTTPLIB_IS_USING_MACOSX_AUTOMATIC_ROOT_CERTIFICATES - a bool for if support of loading system certs from the Apple Keychain is enabled.
  51. * HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO - a bool for if nonblocking getaddrinfo is enabled.
  52. * HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only.
  53. * HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include).
  54. * HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so).
  55. * httplib_VERSION or HTTPLIB_VERSION - the project's version string.
  56. * HTTPLIB_FOUND - a bool for if the target was found.
  57. Want to use precompiled headers (Cmake feature since v3.16)?
  58. It's as simple as doing the following (before linking):
  59. target_precompile_headers(httplib::httplib INTERFACE "${HTTPLIB_HEADER_PATH}")
  60. -------------------------------------------------------------------------------
  61. ARCH_INDEPENDENT option of write_basic_package_version_file() requires Cmake v3.14
  62. ]]
  63. cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
  64. # Get the CPPHTTPLIB_VERSION value and use it as a version
  65. # This gets the string with the CPPHTTPLIB_VERSION value from the header.
  66. # This is so the maintainer doesn't actually need to update this manually.
  67. file(STRINGS httplib.h _raw_version_string REGEX "CPPHTTPLIB_VERSION \"([0-9]+\\.[0-9]+\\.[0-9]+)\"")
  68. # Extracts just the version string itself from the whole string contained in _raw_version_string
  69. # since _raw_version_string would contain the entire line of code where it found the version string
  70. string(REGEX MATCH "([0-9]+\\.?)+" _httplib_version "${_raw_version_string}")
  71. project(httplib
  72. VERSION ${_httplib_version}
  73. LANGUAGES CXX
  74. DESCRIPTION "A C++ header-only HTTP/HTTPS server and client library."
  75. HOMEPAGE_URL "https://github.com/yhirose/cpp-httplib"
  76. )
  77. # Change as needed to set an OpenSSL minimum version.
  78. # This is used in the installed Cmake config file.
  79. set(_HTTPLIB_OPENSSL_MIN_VER "3.0.0")
  80. # Lets you disable C++ exception during CMake configure time.
  81. # The value is used in the install CMake config file.
  82. option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF)
  83. # Allow for a build to require OpenSSL to pass, instead of just being optional
  84. option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF)
  85. option(HTTPLIB_REQUIRE_WOLFSSL "Requires wolfSSL to be found & linked, or fails build." OFF)
  86. option(HTTPLIB_REQUIRE_MBEDTLS "Requires MbedTLS to be found & linked, or fails build." OFF)
  87. option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF)
  88. # Allow for a build to casually enable OpenSSL/ZLIB support, but silently continue if not found.
  89. # Make these options so their automatic use can be specifically disabled (as needed)
  90. option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON)
  91. option(HTTPLIB_USE_WOLFSSL_IF_AVAILABLE "Uses wolfSSL (if available) to enable HTTPS support." OFF)
  92. option(HTTPLIB_USE_MBEDTLS_IF_AVAILABLE "Uses MbedTLS (if available) to enable HTTPS support." OFF)
  93. option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib compression support." ON)
  94. # Lets you compile the program as a regular library instead of header-only
  95. option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF)
  96. # Lets you disable the installation (useful when fetched from another CMake project)
  97. option(HTTPLIB_INSTALL "Enables the installation target" ON)
  98. option(HTTPLIB_TEST "Enables testing and builds tests" OFF)
  99. option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF)
  100. option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON)
  101. option(HTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES "Disable loading system certs from the Apple Keychain on macOS." OFF)
  102. option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternatives for getaddrinfo." ON)
  103. option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF)
  104. option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON)
  105. # C++20 modules support requires CMake 3.28 or later
  106. if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.28")
  107. option(HTTPLIB_BUILD_MODULES "Build httplib modules (requires HTTPLIB_COMPILE to be ON)." OFF)
  108. else()
  109. set(HTTPLIB_BUILD_MODULES OFF CACHE INTERNAL "Build httplib modules disabled (requires CMake 3.28+)" FORCE)
  110. if(DEFINED CACHE{HTTPLIB_BUILD_MODULES} AND HTTPLIB_BUILD_MODULES)
  111. message(WARNING "HTTPLIB_BUILD_MODULES requires CMake 3.28 or later. Current version is ${CMAKE_VERSION}. Modules support has been disabled.")
  112. endif()
  113. endif()
  114. # Incompatibility between TLS libraries
  115. set(TLS_LIBRARIES_USED_TMP 0)
  116. foreach(tls_library OPENSSL WOLFSSL MBEDTLS)
  117. set(TLS_REQUIRED ${HTTPLIB_REQUIRE_${tls_library}})
  118. set(TLS_IF_AVAILABLE ${HTTPLIB_USE_${tls_library}_IF_AVAILABLE})
  119. if(TLS_REQUIRED OR TLS_IF_AVAILABLE)
  120. math(EXPR TLS_LIBRARIES_USED_TMP "${TLS_LIBRARIES_USED_TMP} + 1")
  121. endif()
  122. endforeach()
  123. if(TLS_LIBRARIES_USED_TMP GREATER 1)
  124. message(FATAL_ERROR "TLS libraries are mutually exclusive.")
  125. endif()
  126. # Defaults to static library but respects standard BUILD_SHARED_LIBS if set
  127. include(CMakeDependentOption)
  128. cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only."
  129. "${BUILD_SHARED_LIBS}" HTTPLIB_COMPILE OFF
  130. )
  131. if(HTTPLIB_SHARED)
  132. set(HTTPLIB_LIB_TYPE SHARED)
  133. if(WIN32)
  134. # Necessary for Windows if building shared libs
  135. # See https://stackoverflow.com/a/40743080
  136. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  137. endif()
  138. else()
  139. set(HTTPLIB_LIB_TYPE STATIC)
  140. endif()
  141. if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  142. if(CMAKE_SYSTEM_VERSION)
  143. if(${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0")
  144. message(WARNING "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later.")
  145. endif()
  146. else()
  147. set(CMAKE_SYSTEM_VERSION "10.0.19041.0")
  148. message(WARNING "The target is Windows but CMAKE_SYSTEM_VERSION is not set, the default system version is set to Windows 10.")
  149. endif()
  150. endif()
  151. # Set some variables that are used in-tree and while building based on our options
  152. set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE})
  153. set(HTTPLIB_IS_USING_MACOSX_AUTOMATIC_ROOT_CERTIFICATES TRUE)
  154. if(HTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES)
  155. set(HTTPLIB_IS_USING_MACOSX_AUTOMATIC_ROOT_CERTIFICATES FALSE)
  156. endif()
  157. set(HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO ${HTTPLIB_USE_NON_BLOCKING_GETADDRINFO})
  158. # Threads needed for <thread> on some systems, and for <pthread.h> on Linux
  159. set(THREADS_PREFER_PTHREAD_FLAG TRUE)
  160. find_package(Threads REQUIRED)
  161. # Since Cmake v3.11, Crypto & SSL became optional when not specified as COMPONENTS.
  162. if(HTTPLIB_REQUIRE_OPENSSL)
  163. find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL REQUIRED)
  164. set(HTTPLIB_IS_USING_OPENSSL TRUE)
  165. elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE)
  166. find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL QUIET)
  167. # Avoid a rare circumstance of not finding all components but the end-user did their
  168. # own call for OpenSSL, which might trick us into thinking we'd otherwise have what we wanted
  169. if (TARGET OpenSSL::SSL AND TARGET OpenSSL::Crypto)
  170. set(HTTPLIB_IS_USING_OPENSSL ${OPENSSL_FOUND})
  171. else()
  172. set(HTTPLIB_IS_USING_OPENSSL FALSE)
  173. endif()
  174. endif()
  175. if(HTTPLIB_REQUIRE_WOLFSSL)
  176. find_package(wolfssl REQUIRED)
  177. set(HTTPLIB_IS_USING_WOLFSSL TRUE)
  178. elseif(HTTPLIB_USE_WOLFSSL_IF_AVAILABLE)
  179. find_package(wolfssl QUIET)
  180. set(HTTPLIB_IS_USING_WOLFSSL ${wolfssl_FOUND})
  181. endif()
  182. if(HTTPLIB_REQUIRE_MBEDTLS)
  183. find_package(MbedTLS REQUIRED)
  184. set(HTTPLIB_IS_USING_MBEDTLS TRUE)
  185. elseif(HTTPLIB_USE_MBEDTLS_IF_AVAILABLE)
  186. find_package(MbedTLS QUIET)
  187. set(HTTPLIB_IS_USING_MBEDTLS ${MbedTLS_FOUND})
  188. endif()
  189. if(HTTPLIB_REQUIRE_ZLIB)
  190. find_package(ZLIB REQUIRED)
  191. set(HTTPLIB_IS_USING_ZLIB TRUE)
  192. elseif(HTTPLIB_USE_ZLIB_IF_AVAILABLE)
  193. find_package(ZLIB QUIET)
  194. # FindZLIB doesn't have a ZLIB_FOUND variable, so check the target.
  195. if(TARGET ZLIB::ZLIB)
  196. set(HTTPLIB_IS_USING_ZLIB TRUE)
  197. endif()
  198. endif()
  199. # Adds our cmake folder to the search path for find_package
  200. # This is so we can use our custom FindBrotli.cmake
  201. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  202. if(HTTPLIB_REQUIRE_BROTLI)
  203. find_package(Brotli COMPONENTS encoder decoder common REQUIRED)
  204. set(HTTPLIB_IS_USING_BROTLI TRUE)
  205. elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE)
  206. find_package(Brotli COMPONENTS encoder decoder common QUIET)
  207. set(HTTPLIB_IS_USING_BROTLI ${Brotli_FOUND})
  208. endif()
  209. # NOTE:
  210. # zstd < 1.5.6 does not provide the CMake imported target `zstd::libzstd`.
  211. # Older versions must be consumed via their pkg-config file.
  212. if(HTTPLIB_REQUIRE_ZSTD)
  213. if (NOT TARGET zstd::libzstd)
  214. find_package(zstd 1.5.6 CONFIG)
  215. if(NOT zstd_FOUND)
  216. find_package(PkgConfig REQUIRED)
  217. pkg_check_modules(zstd REQUIRED IMPORTED_TARGET libzstd)
  218. add_library(zstd::libzstd ALIAS PkgConfig::zstd)
  219. endif()
  220. endif()
  221. set(HTTPLIB_IS_USING_ZSTD TRUE)
  222. elseif(HTTPLIB_USE_ZSTD_IF_AVAILABLE)
  223. if (TARGET zstd::libzstd)
  224. set(HTTPLIB_IS_USING_ZSTD TRUE)
  225. else()
  226. find_package(zstd 1.5.6 CONFIG QUIET)
  227. if(NOT zstd_FOUND)
  228. find_package(PkgConfig QUIET)
  229. if(PKG_CONFIG_FOUND)
  230. pkg_check_modules(zstd QUIET IMPORTED_TARGET libzstd)
  231. if(TARGET PkgConfig::zstd)
  232. add_library(zstd::libzstd ALIAS PkgConfig::zstd)
  233. endif()
  234. endif()
  235. endif()
  236. # Both find_package and PkgConf set a XXX_FOUND var
  237. set(HTTPLIB_IS_USING_ZSTD ${zstd_FOUND})
  238. endif()
  239. endif()
  240. # Used for default, common dirs that the end-user can change (if needed)
  241. # like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR
  242. include(GNUInstallDirs)
  243. if(HTTPLIB_COMPILE)
  244. # Put the split script into the build dir
  245. configure_file(split.py "${CMAKE_CURRENT_BINARY_DIR}/split.py"
  246. COPYONLY
  247. )
  248. # Needs to be in the same dir as the python script
  249. configure_file(httplib.h "${CMAKE_CURRENT_BINARY_DIR}/httplib.h"
  250. COPYONLY
  251. )
  252. # Used outside of this if-else
  253. set(_INTERFACE_OR_PUBLIC PUBLIC)
  254. # Brings in the Python3_EXECUTABLE path we can use.
  255. find_package(Python3 REQUIRED)
  256. # Actually split the file
  257. # Keeps the output in the build dir to not pollute the main dir
  258. execute_process(COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR}/split.py"
  259. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  260. ERROR_VARIABLE _httplib_split_error
  261. )
  262. if(_httplib_split_error)
  263. message(FATAL_ERROR "Failed when trying to split cpp-httplib with the Python script.\n${_httplib_split_error}")
  264. endif()
  265. # If building modules, also generate the module file
  266. if(HTTPLIB_BUILD_MODULES)
  267. # Put the generate_module script into the build dir
  268. configure_file(generate_module.py "${CMAKE_CURRENT_BINARY_DIR}/generate_module.py"
  269. COPYONLY
  270. )
  271. # Generate the module file
  272. execute_process(COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR}/generate_module.py"
  273. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  274. ERROR_VARIABLE _httplib_module_error
  275. )
  276. if(_httplib_module_error)
  277. message(FATAL_ERROR "Failed when trying to generate cpp-httplib module with the Python script.\n${_httplib_module_error}")
  278. endif()
  279. endif()
  280. # split.py puts output in "out"
  281. set(_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR}/out")
  282. add_library(${PROJECT_NAME} ${HTTPLIB_LIB_TYPE} "${_httplib_build_includedir}/httplib.cc")
  283. target_sources(${PROJECT_NAME}
  284. PUBLIC
  285. $<BUILD_INTERFACE:${_httplib_build_includedir}/httplib.h>
  286. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/httplib.h>
  287. )
  288. # Add C++20 module support if requested
  289. # Include from separate file to prevent parse errors on older CMake versions
  290. if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.28")
  291. include(cmake/modules.cmake)
  292. endif()
  293. set_target_properties(${PROJECT_NAME}
  294. PROPERTIES
  295. VERSION ${${PROJECT_NAME}_VERSION}
  296. SOVERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}"
  297. OUTPUT_NAME cpp-httplib
  298. )
  299. else()
  300. # This is for header-only.
  301. set(_INTERFACE_OR_PUBLIC INTERFACE)
  302. add_library(${PROJECT_NAME} INTERFACE)
  303. set(_httplib_build_includedir "${CMAKE_CURRENT_SOURCE_DIR}")
  304. endif()
  305. # Lets you address the target with httplib::httplib
  306. # Only useful if building in-tree, versus using it from an installation.
  307. add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
  308. # Require C++11, or C++20 if modules are enabled
  309. if(HTTPLIB_BUILD_MODULES)
  310. target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_20)
  311. else()
  312. target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11)
  313. endif()
  314. target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC}
  315. $<BUILD_INTERFACE:${_httplib_build_includedir}>
  316. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  317. )
  318. target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
  319. # Always require threads
  320. Threads::Threads
  321. # Needed for Windows libs on Mingw, as the pragma comment(lib, "xyz") aren't triggered.
  322. $<$<PLATFORM_ID:Windows>:ws2_32>
  323. $<$<PLATFORM_ID:Windows>:crypt32>
  324. # Needed for API from MacOS Security framework
  325. "$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>,$<BOOL:${HTTPLIB_IS_USING_MACOSX_AUTOMATIC_ROOT_CERTIFICATES}>>:-framework CFNetwork -framework CoreFoundation -framework Security>"
  326. # Needed for non-blocking getaddrinfo on MacOS
  327. "$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_USE_NON_BLOCKING_GETADDRINFO}>>:-framework CFNetwork -framework CoreFoundation>"
  328. # Can't put multiple targets in a single generator expression or it bugs out.
  329. $<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::common>
  330. $<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::encoder>
  331. $<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::decoder>
  332. $<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:ZLIB::ZLIB>
  333. $<$<BOOL:${HTTPLIB_IS_USING_ZSTD}>:zstd::libzstd>
  334. $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::SSL>
  335. $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::Crypto>
  336. $<$<BOOL:${HTTPLIB_IS_USING_WOLFSSL}>:wolfssl::wolfssl>
  337. $<$<BOOL:${HTTPLIB_IS_USING_MBEDTLS}>:MbedTLS::mbedtls>
  338. )
  339. # Set the definitions to enable optional features
  340. target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
  341. $<$<BOOL:${HTTPLIB_NO_EXCEPTIONS}>:CPPHTTPLIB_NO_EXCEPTIONS>
  342. $<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:CPPHTTPLIB_BROTLI_SUPPORT>
  343. $<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:CPPHTTPLIB_ZLIB_SUPPORT>
  344. $<$<BOOL:${HTTPLIB_IS_USING_ZSTD}>:CPPHTTPLIB_ZSTD_SUPPORT>
  345. $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:CPPHTTPLIB_OPENSSL_SUPPORT>
  346. $<$<BOOL:${HTTPLIB_IS_USING_WOLFSSL}>:CPPHTTPLIB_WOLFSSL_SUPPORT>
  347. $<$<BOOL:${HTTPLIB_IS_USING_MBEDTLS}>:CPPHTTPLIB_MBEDTLS_SUPPORT>
  348. $<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES}>>:CPPHTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES>
  349. $<$<BOOL:${HTTPLIB_USE_NON_BLOCKING_GETADDRINFO}>:CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO>
  350. )
  351. # CMake configuration files installation directory
  352. set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  353. include(CMakePackageConfigHelpers)
  354. # Configures the meta-file httplibConfig.cmake.in to replace variables with paths/values/etc.
  355. configure_package_config_file("cmake/${PROJECT_NAME}Config.cmake.in"
  356. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  357. INSTALL_DESTINATION "${_TARGET_INSTALL_CMAKEDIR}"
  358. # Passes the includedir install path
  359. PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR
  360. )
  361. if(HTTPLIB_COMPILE)
  362. write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
  363. # Example: if you find_package(httplib 0.5.4)
  364. # then anything >= 0.5.4 and < 0.6 is accepted
  365. COMPATIBILITY SameMinorVersion
  366. )
  367. else()
  368. write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
  369. # Example: if you find_package(httplib 0.5.4)
  370. # then anything >= 0.5.4 and < 0.6 is accepted
  371. COMPATIBILITY SameMinorVersion
  372. # Tells Cmake that it's a header-only lib
  373. # Mildly useful for end-users :)
  374. ARCH_INDEPENDENT
  375. )
  376. endif()
  377. if(HTTPLIB_INSTALL)
  378. # Creates the export httplibTargets.cmake
  379. # This is strictly what holds compilation requirements
  380. # and linkage information (doesn't find deps though).
  381. if(HTTPLIB_BUILD_MODULES)
  382. install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/httplib/modules CXX_MODULES_BMI DESTINATION ${CMAKE_INSTALL_LIBDIR}/httplib/modules)
  383. else()
  384. install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets)
  385. endif()
  386. install(FILES "${_httplib_build_includedir}/httplib.h" TYPE INCLUDE)
  387. install(FILES
  388. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  389. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
  390. # Install it so it can be used later by the httplibConfig.cmake file.
  391. # Put it in the same dir as our config file instead of a global path so we don't potentially stomp on other packages.
  392. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindBrotli.cmake"
  393. DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
  394. )
  395. # NOTE: This path changes depending on if it's on Windows or Linux
  396. install(EXPORT httplibTargets
  397. # Puts the targets into the httplib namespace
  398. # So this makes httplib::httplib linkable after doing find_package(httplib)
  399. NAMESPACE ${PROJECT_NAME}::
  400. DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
  401. )
  402. # Install documentation & license
  403. # ex: /usr/share/doc/httplib/README.md and /usr/share/licenses/httplib/LICENSE
  404. install(FILES "README.md" DESTINATION "${CMAKE_INSTALL_DOCDIR}")
  405. install(FILES "LICENSE" DESTINATION "${CMAKE_INSTALL_DATADIR}/licenses/${PROJECT_NAME}")
  406. include(CPack)
  407. endif()
  408. if(HTTPLIB_BUILD_MODULES AND NOT HTTPLIB_COMPILE)
  409. message(FATAL_ERROR "HTTPLIB_BUILD_MODULES requires HTTPLIB_COMPILE to be ON.")
  410. endif()
  411. if(HTTPLIB_TEST)
  412. include(CTest)
  413. add_subdirectory(test)
  414. endif()