|
| 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_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) |
| 10 | + * HTTPLIB_REQUIRE_BROTLI (default off) |
| 11 | + * HTTPLIB_COMPILE (default off) |
| 12 | + * HTTPLIB_INSTALL (default on) |
| 13 | + * HTTPLIB_TEST (default off) |
| 14 | + * BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed). |
| 15 | + * OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed). |
| 16 | +
|
| 17 | + ------------------------------------------------------------------------------- |
| 18 | +
|
| 19 | + After installation with Cmake, a find_package(httplib COMPONENTS OpenSSL ZLIB Brotli) is available. |
| 20 | + This creates a httplib::httplib target (if found and if listed components are supported). |
| 21 | + It can be linked like so: |
| 22 | +
|
| 23 | + target_link_libraries(your_exe httplib::httplib) |
| 24 | +
|
| 25 | + The following will build & install for later use. |
| 26 | +
|
| 27 | + Linux/macOS: |
| 28 | +
|
| 29 | + mkdir -p build |
| 30 | + cd build |
| 31 | + cmake -DCMAKE_BUILD_TYPE=Release .. |
| 32 | + sudo cmake --build . --target install |
| 33 | +
|
| 34 | + Windows: |
| 35 | +
|
| 36 | + mkdir build |
| 37 | + cd build |
| 38 | + cmake .. |
| 39 | + runas /user:Administrator "cmake --build . --config Release --target install" |
| 40 | +
|
| 41 | + ------------------------------------------------------------------------------- |
| 42 | +
|
| 43 | + These variables are available after you run find_package(httplib) |
| 44 | + * HTTPLIB_HEADER_PATH - this is the full path to the installed header (e.g. /usr/include/httplib.h). |
| 45 | + * HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled. |
| 46 | + * HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled. |
| 47 | + * HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled. |
| 48 | + * HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN - a bool for if support of loading system certs from the Apple Keychain is enabled. |
| 49 | + * HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only. |
| 50 | + * HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include). |
| 51 | + * HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so). |
| 52 | + * httplib_VERSION or HTTPLIB_VERSION - the project's version string. |
| 53 | + * HTTPLIB_FOUND - a bool for if the target was found. |
| 54 | +
|
| 55 | + Want to use precompiled headers (Cmake feature since v3.16)? |
| 56 | + It's as simple as doing the following (before linking): |
| 57 | +
|
| 58 | + target_precompile_headers(httplib::httplib INTERFACE "${HTTPLIB_HEADER_PATH}") |
| 59 | +
|
| 60 | + ------------------------------------------------------------------------------- |
| 61 | +
|
| 62 | + FindPython3 requires Cmake v3.12 |
| 63 | + ARCH_INDEPENDENT option of write_basic_package_version_file() requires Cmake v3.14 |
| 64 | +]] |
| 65 | +cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR) |
| 66 | + |
| 67 | +# Get the CPPHTTPLIB_VERSION value and use it as a version |
| 68 | +# This gets the string with the CPPHTTPLIB_VERSION value from the header. |
| 69 | +# This is so the maintainer doesn't actually need to update this manually. |
| 70 | +file(STRINGS httplib.h _raw_version_string REGEX "CPPHTTPLIB_VERSION \"([0-9]+\\.[0-9]+\\.[0-9]+)\"") |
| 71 | + |
| 72 | +# Needed since git tags have "v" prefixing them. |
| 73 | +# Also used if the fallback to user agent string is being used. |
| 74 | +string(REGEX MATCH "([0-9]+\\.?)+" _httplib_version "${_raw_version_string}") |
| 75 | + |
| 76 | +project(httplib VERSION ${_httplib_version} LANGUAGES CXX) |
| 77 | + |
| 78 | +# Lets you disable C++ exception during CMake configure time. |
| 79 | +# The value is used in the install CMake config file. |
| 80 | +option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF) |
| 81 | + |
| 82 | +# Change as needed to set an OpenSSL minimum version. |
| 83 | +# This is used in the installed Cmake config file. |
| 84 | +set(_HTTPLIB_OPENSSL_MIN_VER "1.1.1") |
| 85 | + |
| 86 | +# Allow for a build to require OpenSSL to pass, instead of just being optional |
| 87 | +option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF) |
| 88 | +option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF) |
| 89 | +# Allow for a build to casually enable OpenSSL/ZLIB support, but silently continue if not found. |
| 90 | +# Make these options so their automatic use can be specifically disabled (as needed) |
| 91 | +option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON) |
| 92 | +option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib compression support." ON) |
| 93 | +# Lets you compile the program as a regular library instead of header-only |
| 94 | +option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF) |
| 95 | +# Lets you disable the installation (useful when fetched from another CMake project) |
| 96 | +option(HTTPLIB_INSTALL "Enables the installation target" ON) |
| 97 | +# Just setting this variable here for people building in-tree |
| 98 | +if(HTTPLIB_COMPILE) |
| 99 | + set(HTTPLIB_IS_COMPILED TRUE) |
| 100 | +endif() |
| 101 | +option(HTTPLIB_TEST "Enables testing and builds tests" OFF) |
| 102 | +option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) |
| 103 | +option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) |
| 104 | +option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON) |
| 105 | +# Defaults to static library |
| 106 | +option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF) |
| 107 | +if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) |
| 108 | + # Necessary for Windows if building shared libs |
| 109 | + # See https://stackoverflow.com/a/40743080 |
| 110 | + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) |
| 111 | +endif() |
| 112 | + |
| 113 | +# Threads needed for <thread> on some systems, and for <pthread.h> on Linux |
| 114 | +set(THREADS_PREFER_PTHREAD_FLAG true) |
| 115 | +find_package(Threads REQUIRED) |
| 116 | +# Since Cmake v3.11, Crypto & SSL became optional when not specified as COMPONENTS. |
| 117 | +if(HTTPLIB_REQUIRE_OPENSSL) |
| 118 | + find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL REQUIRED) |
| 119 | +elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE) |
| 120 | + find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL QUIET) |
| 121 | +endif() |
| 122 | +# Just setting this variable here for people building in-tree |
| 123 | +if(OPENSSL_FOUND AND NOT DEFINED HTTPLIB_IS_USING_OPENSSL) |
| 124 | + set(HTTPLIB_IS_USING_OPENSSL TRUE) |
| 125 | +endif() |
| 126 | + |
| 127 | +if(HTTPLIB_REQUIRE_ZLIB) |
| 128 | + find_package(ZLIB REQUIRED) |
| 129 | +elseif(HTTPLIB_USE_ZLIB_IF_AVAILABLE) |
| 130 | + find_package(ZLIB QUIET) |
| 131 | +endif() |
| 132 | +# Just setting this variable here for people building in-tree |
| 133 | +# FindZLIB doesn't have a ZLIB_FOUND variable, so check the target. |
| 134 | +if(TARGET ZLIB::ZLIB) |
| 135 | + set(HTTPLIB_IS_USING_ZLIB TRUE) |
| 136 | +endif() |
| 137 | + |
| 138 | +# Adds our cmake folder to the search path for find_package |
| 139 | +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") |
| 140 | +if(HTTPLIB_REQUIRE_BROTLI) |
| 141 | + find_package(Brotli COMPONENTS encoder decoder common REQUIRED) |
| 142 | +elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE) |
| 143 | + find_package(Brotli COMPONENTS encoder decoder common QUIET) |
| 144 | +endif() |
| 145 | +# Just setting this variable here for people building in-tree |
| 146 | +if(Brotli_FOUND) |
| 147 | + set(HTTPLIB_IS_USING_BROTLI TRUE) |
| 148 | +endif() |
| 149 | + |
| 150 | +if(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) |
| 151 | + set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN TRUE) |
| 152 | +endif() |
| 153 | + |
| 154 | +# Used for default, common dirs that the end-user can change (if needed) |
| 155 | +# like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR |
| 156 | +include(GNUInstallDirs) |
| 157 | + |
| 158 | +if(HTTPLIB_COMPILE) |
| 159 | + # Put the split script into the build dir |
| 160 | + configure_file(split.py "${CMAKE_CURRENT_BINARY_DIR}/split.py" |
| 161 | + COPYONLY |
| 162 | + ) |
| 163 | + # Needs to be in the same dir as the python script |
| 164 | + configure_file(httplib.h "${CMAKE_CURRENT_BINARY_DIR}/httplib.h" |
| 165 | + COPYONLY |
| 166 | + ) |
| 167 | + |
| 168 | + # Used outside of this if-else |
| 169 | + set(_INTERFACE_OR_PUBLIC PUBLIC) |
| 170 | + # Brings in the Python3_EXECUTABLE path we can use. |
| 171 | + find_package(Python3 REQUIRED) |
| 172 | + # Actually split the file |
| 173 | + # Keeps the output in the build dir to not pollute the main dir |
| 174 | + execute_process(COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR}/split.py" |
| 175 | + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 176 | + ERROR_VARIABLE _httplib_split_error |
| 177 | + ) |
| 178 | + if(_httplib_split_error) |
| 179 | + message(FATAL_ERROR "Failed when trying to split cpp-httplib with the Python script.\n${_httplib_split_error}") |
| 180 | + endif() |
| 181 | + |
| 182 | + # split.py puts output in "out" |
| 183 | + set(_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR}/out") |
| 184 | + # This will automatically be either static or shared based on the value of BUILD_SHARED_LIBS |
| 185 | + add_library(${PROJECT_NAME} "${_httplib_build_includedir}/httplib.cc") |
| 186 | + target_sources(${PROJECT_NAME} |
| 187 | + PUBLIC |
| 188 | + $<BUILD_INTERFACE:${_httplib_build_includedir}/httplib.h> |
| 189 | + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/httplib.h> |
| 190 | + ) |
| 191 | + set_target_properties(${PROJECT_NAME} |
| 192 | + PROPERTIES |
| 193 | + VERSION ${${PROJECT_NAME}_VERSION} |
| 194 | + SOVERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}" |
| 195 | + ) |
| 196 | +else() |
| 197 | + # This is for header-only. |
| 198 | + set(_INTERFACE_OR_PUBLIC INTERFACE) |
| 199 | + add_library(${PROJECT_NAME} INTERFACE) |
| 200 | + set(_httplib_build_includedir "${CMAKE_CURRENT_SOURCE_DIR}") |
| 201 | +endif() |
| 202 | +# Lets you address the target with httplib::httplib |
| 203 | +# Only useful if building in-tree, versus using it from an installation. |
| 204 | +add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) |
| 205 | + |
| 206 | +# Require C++11 |
| 207 | +target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} |
| 208 | + cxx_std_11 |
| 209 | +) |
| 210 | + |
| 211 | +target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC} |
| 212 | + $<BUILD_INTERFACE:${_httplib_build_includedir}> |
| 213 | + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> |
| 214 | +) |
| 215 | + |
| 216 | +# Always require threads |
| 217 | +target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} |
| 218 | + Threads::Threads |
| 219 | + # Needed for Windows libs on Mingw, as the pragma comment(lib, "xyz") aren't triggered. |
| 220 | + $<$<PLATFORM_ID:Windows>:ws2_32> |
| 221 | + $<$<PLATFORM_ID:Windows>:crypt32> |
| 222 | + $<$<PLATFORM_ID:Windows>:cryptui> |
| 223 | + # Needed for API from MacOS Security framework |
| 224 | + "$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>,$<BOOL:${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN}>>:-framework CoreFoundation -framework Security>" |
| 225 | + # Can't put multiple targets in a single generator expression or it bugs out. |
| 226 | + $<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::common> |
| 227 | + $<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::encoder> |
| 228 | + $<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::decoder> |
| 229 | + $<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:ZLIB::ZLIB> |
| 230 | + $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::SSL> |
| 231 | + $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::Crypto> |
| 232 | +) |
| 233 | + |
| 234 | +# Set the definitions to enable optional features |
| 235 | +target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} |
| 236 | + $<$<BOOL:${HTTPLIB_NO_EXCEPTIONS}>:CPPHTTPLIB_NO_EXCEPTIONS> |
| 237 | + $<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:CPPHTTPLIB_BROTLI_SUPPORT> |
| 238 | + $<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:CPPHTTPLIB_ZLIB_SUPPORT> |
| 239 | + $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:CPPHTTPLIB_OPENSSL_SUPPORT> |
| 240 | + $<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>,$<BOOL:${HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN}>>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN> |
| 241 | +) |
| 242 | + |
| 243 | +# CMake configuration files installation directory |
| 244 | +set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") |
| 245 | + |
| 246 | +include(CMakePackageConfigHelpers) |
| 247 | + |
| 248 | +# Configures the meta-file httplibConfig.cmake.in to replace variables with paths/values/etc. |
| 249 | +configure_package_config_file("${PROJECT_NAME}Config.cmake.in" |
| 250 | + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" |
| 251 | + INSTALL_DESTINATION "${_TARGET_INSTALL_CMAKEDIR}" |
| 252 | + # Passes the includedir install path |
| 253 | + PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR |
| 254 | +) |
| 255 | + |
| 256 | +if(HTTPLIB_COMPILE) |
| 257 | + write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" |
| 258 | + # Example: if you find_package(httplib 0.5.4) |
| 259 | + # then anything >= 0.5.4 and < 0.6 is accepted |
| 260 | + COMPATIBILITY SameMinorVersion |
| 261 | + ) |
| 262 | +else() |
| 263 | + write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" |
| 264 | + # Example: if you find_package(httplib 0.5.4) |
| 265 | + # then anything >= 0.5.4 and < 0.6 is accepted |
| 266 | + COMPATIBILITY SameMinorVersion |
| 267 | + # Tells Cmake that it's a header-only lib |
| 268 | + # Mildly useful for end-users :) |
| 269 | + ARCH_INDEPENDENT |
| 270 | + ) |
| 271 | +endif() |
| 272 | + |
| 273 | +if(HTTPLIB_INSTALL) |
| 274 | + # Creates the export httplibTargets.cmake |
| 275 | + # This is strictly what holds compilation requirements |
| 276 | + # and linkage information (doesn't find deps though). |
| 277 | + install(TARGETS ${PROJECT_NAME} |
| 278 | + EXPORT httplibTargets |
| 279 | + ) |
| 280 | + |
| 281 | + install(FILES "${_httplib_build_includedir}/httplib.h" TYPE INCLUDE) |
| 282 | + |
| 283 | + install(FILES |
| 284 | + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" |
| 285 | + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" |
| 286 | + # Install it so it can be used later by the httplibConfig.cmake file. |
| 287 | + # Put it in the same dir as our config file instead of a global path so we don't potentially stomp on other packages. |
| 288 | + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindBrotli.cmake" |
| 289 | + DESTINATION ${_TARGET_INSTALL_CMAKEDIR} |
| 290 | + ) |
| 291 | + |
| 292 | + # NOTE: This path changes depending on if it's on Windows or Linux |
| 293 | + install(EXPORT httplibTargets |
| 294 | + # Puts the targets into the httplib namespace |
| 295 | + # So this makes httplib::httplib linkable after doing find_package(httplib) |
| 296 | + NAMESPACE ${PROJECT_NAME}:: |
| 297 | + DESTINATION ${_TARGET_INSTALL_CMAKEDIR} |
| 298 | + ) |
| 299 | +endif() |
| 300 | + |
| 301 | +if(HTTPLIB_TEST) |
| 302 | + include(CTest) |
| 303 | + add_subdirectory(test) |
| 304 | +endif() |
0 commit comments