CMakeLists.txt 18 KB

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