CMakeLists.txt 12 KB

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