1
0

CMakeLists.txt 15 KB

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