CMakeLists.txt 13 KB

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