CMakeLists.txt 10 KB

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