CMakeLists.txt 9.8 KB

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