CMakeLists.txt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. cmake_minimum_required(VERSION 3.12...3.13)
  2. set(
  3. CMAKE_TOOLCHAIN_FILE
  4. "${CMAKE_CURRENT_LIST_DIR}/toolchain.cmake"
  5. CACHE
  6. FILEPATH
  7. "Default toolchain"
  8. )
  9. set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard")
  10. set(CMAKE_CXX_STANDARD_REQUIRED ON CACHE BOOL "Require C++ standard to be supported")
  11. set(CMAKE_POSITION_INDEPENDENT_CODE ON CACHE BOOL "compile as PIC by default")
  12. option(HUNTER_ENABLED "Enable Hunter package manager" OFF)
  13. include("cmake/HunterGate.cmake")
  14. HunterGate(
  15. URL "https://github.com/cpp-pm/hunter/archive/v0.25.5.tar.gz"
  16. SHA1 "a20151e4c0740ee7d0f9994476856d813cdead29"
  17. )
  18. option(USE_BUNDLED_SPDLOG "Use the bundled version of spdlog." ${HUNTER_ENABLED})
  19. option(USE_BUNDLED_LIBEVENT "Use the bundled version of spdlog." ${HUNTER_ENABLED})
  20. option(USE_BUNDLED_LIBCURL "Use the bundled version of spdlog." ${HUNTER_ENABLED})
  21. option(USE_BUNDLED_SPDLOG "Use the bundled version of spdlog." ${HUNTER_ENABLED})
  22. project(coeurl
  23. VERSION 0.3.1
  24. DESCRIPTION "Simple library to do http requests asynchronously via CURL in C++."
  25. HOMEPAGE_URL "https://nheko.im/nheko-reborn/cocurl")
  26. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  27. message(FATAL_ERROR "The CMake build for coeurl is not intended for installation or usage outside of FetchContent. Use meson instead.")
  28. endif()
  29. add_library(coeurl lib/client.cpp lib/request.cpp lib/errors.cpp)
  30. add_library(coeurl::coeurl ALIAS coeurl)
  31. target_include_directories(coeurl PUBLIC
  32. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  33. $<INSTALL_INTERFACE:include>)
  34. find_package(Threads REQUIRED)
  35. target_link_libraries(coeurl PUBLIC Threads::Threads)
  36. # libevent
  37. if (USE_BUNDLED_LIBEVENT)
  38. hunter_add_package(Libevent)
  39. find_package(Libevent CONFIG REQUIRED)
  40. target_link_libraries(coeurl PUBLIC Libevent::event_core)
  41. # Not needed with current hunter version
  42. #if (WIN32)
  43. # target_link_libraries(coeurl PUBLIC Libevent::event_windows)
  44. #else()
  45. # target_link_libraries(coeurl PUBLIC Libevent::event_pthreads)
  46. #endif()
  47. else()
  48. find_package(PkgConfig REQUIRED)
  49. pkg_check_modules(libevent_core REQUIRED IMPORTED_TARGET libevent_core)
  50. target_link_libraries(coeurl PUBLIC PkgConfig::libevent_core)
  51. if (WIN32)
  52. pkg_check_modules(libevent_windows REQUIRED IMPORTED_TARGET libevent_windows)
  53. target_link_libraries(coeurl PUBLIC PkgConfig::libevent_windows)
  54. else()
  55. pkg_check_modules(libevent_pthreads REQUIRED IMPORTED_TARGET
  56. libevent_pthreads)
  57. target_link_libraries(coeurl PUBLIC PkgConfig::libevent_pthreads)
  58. endif()
  59. endif()
  60. # curl
  61. if (USE_BUNDLED_LIBCURL)
  62. hunter_add_package(CURL)
  63. find_package(CURL CONFIG REQUIRED)
  64. target_link_libraries(coeurl PUBLIC CURL::libcurl)
  65. else()
  66. find_package(PkgConfig REQUIRED)
  67. pkg_check_modules(libcurl REQUIRED IMPORTED_TARGET libcurl)
  68. target_link_libraries(coeurl PUBLIC PkgConfig::libcurl)
  69. endif()
  70. # spdlog
  71. if(USE_BUNDLED_SPDLOG)
  72. hunter_add_package(spdlog)
  73. endif()
  74. find_package(spdlog 1.0.0 CONFIG REQUIRED)
  75. target_link_libraries(coeurl PUBLIC spdlog::spdlog)
  76. IF(MSVC)
  77. add_compile_definitions(NOMINMAX WIN32_LEAN_AND_MEAN)
  78. target_link_libraries(coeurl PUBLIC wsock32 ws2_32)
  79. target_link_libraries(coeurl PUBLIC iphlpapi)
  80. ENDIF()
  81. # INSTALL MAGIC
  82. include(GNUInstallDirs)
  83. set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/coeurl)
  84. # Include module with fuction 'write_basic_package_version_file'
  85. include(CMakePackageConfigHelpers)
  86. write_basic_package_version_file(
  87. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion
  88. )
  89. configure_package_config_file(
  90. ${CMAKE_CURRENT_LIST_DIR}/cmake/coeurl.cmake.in
  91. ${CMAKE_CURRENT_BINARY_DIR}/coeurlConfig.cmake
  92. INSTALL_DESTINATION "${INSTALL_CONFIGDIR}"
  93. )
  94. install(
  95. TARGETS coeurl
  96. EXPORT "${PROJECT_NAME}Targets"
  97. LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  98. ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  99. RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
  100. INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
  101. )
  102. export(
  103. EXPORT "${PROJECT_NAME}Targets"
  104. FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake
  105. NAMESPACE coeurl::)
  106. export(PACKAGE coeurl)
  107. install(
  108. DIRECTORY "include/"
  109. DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
  110. FILES_MATCHING PATTERN "*.hpp"
  111. )
  112. install(
  113. FILES ${CMAKE_CURRENT_BINARY_DIR}/coeurlConfig.cmake
  114. DESTINATION "${INSTALL_CONFIGDIR}"
  115. )
  116. install(
  117. EXPORT "${PROJECT_NAME}Targets"
  118. NAMESPACE "${PROJECT_NAME}"
  119. DESTINATION "${INSTALL_CONFIGDIR}"
  120. )