CMakeLists.txt 8.4 KB

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