CMakeLists.txt 10 KB

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