CMakeLists.txt 12 KB

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