CMakeLists.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. After installation with Cmake, a find_package(httplib) is available.
  8. This creates a httplib::httplib target (if found).
  9. It can be linked like so:
  10. target_link_libraries(your_exe httplib::httplib)
  11. The following will build & install for later use.
  12. Linux/macOS:
  13. mkdir -p build
  14. cd build
  15. cmake -DCMAKE_BUILD_TYPE=Release ..
  16. sudo cmake --build . --target install
  17. Windows:
  18. mkdir build
  19. cd build
  20. cmake ..
  21. runas /user:Administrator "cmake --build . --config Release --target install"
  22. These three variables are available after you run find_package(httplib)
  23. * HTTPLIB_HEADER_PATH - this is the full path to the installed header.
  24. * HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled.
  25. * HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled.
  26. Want to use precompiled headers (Cmake feature since v3.16)?
  27. It's as simple as doing the following (before linking):
  28. target_precompile_headers(httplib::httplib INTERFACE "${HTTPLIB_HEADER_PATH}")
  29. ]]
  30. cmake_minimum_required(VERSION 3.7.0 FATAL_ERROR)
  31. project(httplib LANGUAGES CXX)
  32. # Change as needed to set an OpenSSL minimum version.
  33. # This is used in the installed Cmake config file.
  34. set(_HTTPLIB_OPENSSL_MIN_VER "1.1.1")
  35. # Allow for a build to require OpenSSL to pass, instead of just being optional
  36. option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF)
  37. option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF)
  38. # Allow for a build to casually enable OpenSSL/ZLIB support, but silenty continue if not found.
  39. # Make these options so their automatic use can be specifically disabled (as needed)
  40. option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON)
  41. option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable compression support." ON)
  42. # TODO: implement the option of option to correctly split, building, and export with the split.py script.
  43. # option(HTTPLIB_SPLIT "Uses a Python script to split the header into a header & source file." OFF)
  44. # Threads needed for <thread> on some systems, and for <pthread.h> on Linux
  45. find_package(Threads REQUIRED)
  46. if(HTTPLIB_REQUIRE_OPENSSL)
  47. find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} REQUIRED)
  48. elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE)
  49. # Look quietly since it's optional are optional
  50. find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} QUIET)
  51. endif()
  52. if(HTTPLIB_REQUIRE_ZLIB)
  53. find_package(ZLIB REQUIRED)
  54. elseif(HTTPLIB_USE_ZLIB_IF_AVAILABLE)
  55. find_package(ZLIB QUIET)
  56. endif()
  57. # Used for default, common dirs that the end-user can change (if needed)
  58. # like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR
  59. include(GNUInstallDirs)
  60. add_library(${PROJECT_NAME} INTERFACE)
  61. # Lets you address the target with httplib::httplib
  62. # Only useful if building in-tree, versus using it from an installation.
  63. add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
  64. # Might be missing some, but this list is somewhat comprehensive
  65. target_compile_features(${PROJECT_NAME} INTERFACE
  66. cxx_std_11
  67. cxx_nullptr
  68. cxx_noexcept
  69. cxx_lambdas
  70. cxx_override
  71. cxx_defaulted_functions
  72. cxx_attribute_deprecated
  73. cxx_auto_type
  74. cxx_decltype
  75. cxx_deleted_functions
  76. cxx_range_for
  77. cxx_sizeof_member
  78. )
  79. target_include_directories(${PROJECT_NAME} INTERFACE
  80. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  81. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
  82. target_link_libraries(${PROJECT_NAME} INTERFACE
  83. # Always require threads
  84. Threads::Threads
  85. # Only link zlib & openssl if they're found
  86. $<$<BOOL:${ZLIB_FOUND}>:ZLIB::ZLIB>
  87. $<$<BOOL:${OPENSSL_FOUND}>:OpenSSL::SSL OpenSSL::Crypto>
  88. )
  89. # Auto-define the optional support if those packages were found
  90. target_compile_definitions(${PROJECT_NAME} INTERFACE
  91. $<$<BOOL:${ZLIB_FOUND}>:CPPHTTPLIB_ZLIB_SUPPORT>
  92. $<$<BOOL:${OPENSSL_FOUND}>:CPPHTTPLIB_OPENSSL_SUPPORT>
  93. )
  94. # Cmake's find_package search path is different based on the system
  95. # See https://cmake.org/cmake/help/latest/command/find_package.html for the list
  96. if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  97. set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_PREFIX}/cmake/${PROJECT_NAME}")
  98. else()
  99. # On Non-Windows, it should be /usr/lib/cmake/<name>/<name>Config.cmake
  100. # NOTE: This may or may not work for macOS...
  101. set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  102. endif()
  103. include(CMakePackageConfigHelpers)
  104. # Configures the meta-file httplibConfig.cmake.in to replace variables with paths/values/etc.
  105. configure_package_config_file("${PROJECT_NAME}Config.cmake.in"
  106. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  107. INSTALL_DESTINATION "${_TARGET_INSTALL_CMAKEDIR}"
  108. # Passes the includedir install path
  109. PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR
  110. # There aren't any components, so don't use the macro
  111. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  112. )
  113. # Creates the export httplibTargets.cmake
  114. # This is strictly what holds compilation requirements
  115. # and linkage information (doesn't find deps though).
  116. install(TARGETS ${PROJECT_NAME}
  117. EXPORT httplibTargets
  118. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  119. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  120. )
  121. install(FILES httplib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  122. install(FILES
  123. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  124. DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
  125. )
  126. # NOTE: This path changes depending on if it's on Windows or Linux
  127. install(EXPORT httplibTargets
  128. # Puts the targets into the httplib namespace
  129. # So this makes httplib::httplib linkable after doing find_package(httplib)
  130. NAMESPACE ${PROJECT_NAME}::
  131. DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
  132. )