CMakeLists.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.23.260.tar.gz"
  16. SHA1 "13775235910a3fa85644568d1c5be8271de72e1c"
  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.1.1
  24. DESCRIPTION "Simple library to do http requests asynchronously via CURL in C++."
  25. HOMEPAGE_URL "https://nheko.im/nheko-reborn/cocurl")
  26. add_library(coeurl lib/client.cpp lib/request.cpp lib/errors.cpp)
  27. add_library(coeurl::coeurl ALIAS coeurl)
  28. target_include_directories(coeurl PUBLIC
  29. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  30. $<INSTALL_INTERFACE:include>)
  31. find_package(Threads REQUIRED)
  32. target_link_libraries(coeurl PUBLIC Threads::Threads)
  33. # libevent
  34. if (USE_BUNDLED_LIBEVENT)
  35. hunter_add_package(Libevent)
  36. find_package(Libevent CONFIG REQUIRED)
  37. target_link_libraries(coeurl PUBLIC Libevent::event_core)
  38. # Not needed with current hunter version
  39. #if (WIN32)
  40. # target_link_libraries(coeurl PUBLIC Libevent::event_windows)
  41. #else()
  42. # target_link_libraries(coeurl PUBLIC Libevent::event_pthreads)
  43. #endif()
  44. else()
  45. find_package(PkgConfig REQUIRED)
  46. pkg_check_modules(libevent_core REQUIRED IMPORTED_TARGET libevent_core)
  47. target_link_libraries(coeurl PUBLIC PkgConfig::libevent_core)
  48. if (WIN32)
  49. pkg_check_modules(libevent_windows REQUIRED IMPORTED_TARGET libevent_windows)
  50. target_link_libraries(coeurl PUBLIC PkgConfig::libevent_windows)
  51. else()
  52. pkg_check_modules(libevent_pthreads REQUIRED IMPORTED_TARGET
  53. libevent_pthreads)
  54. target_link_libraries(coeurl PUBLIC PkgConfig::libevent_pthreads)
  55. endif()
  56. endif()
  57. # curl
  58. if (USE_BUNDLED_LIBCURL)
  59. hunter_add_package(CURL)
  60. find_package(CURL CONFIG REQUIRED)
  61. target_link_libraries(coeurl PUBLIC CURL::libcurl)
  62. else()
  63. find_package(PkgConfig REQUIRED)
  64. pkg_check_modules(libcurl REQUIRED IMPORTED_TARGET libcurl)
  65. target_link_libraries(coeurl PUBLIC PkgConfig::libcurl)
  66. endif()
  67. # spdlog
  68. if(USE_BUNDLED_SPDLOG)
  69. hunter_add_package(spdlog)
  70. endif()
  71. find_package(spdlog 1.0.0 CONFIG REQUIRED)
  72. target_link_libraries(coeurl PUBLIC spdlog::spdlog)
  73. IF(MSVC)
  74. add_compile_definitions(NOMINMAX WIN32_LEAN_AND_MEAN)
  75. target_link_libraries(coeurl PUBLIC wsock32 ws2_32)
  76. target_link_libraries(coeurl PUBLIC iphlpapi)
  77. ENDIF()
  78. # INSTALL MAGIC
  79. include(GNUInstallDirs)
  80. set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/coeurl)
  81. # Include module with fuction 'write_basic_package_version_file'
  82. include(CMakePackageConfigHelpers)
  83. write_basic_package_version_file(
  84. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion
  85. )
  86. configure_package_config_file(
  87. ${CMAKE_CURRENT_LIST_DIR}/cmake/coeurl.cmake.in
  88. ${CMAKE_CURRENT_BINARY_DIR}/coeurlConfig.cmake
  89. INSTALL_DESTINATION "${INSTALL_CONFIGDIR}"
  90. )
  91. install(
  92. TARGETS coeurl
  93. EXPORT "${PROJECT_NAME}Targets"
  94. LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  95. ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  96. RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
  97. INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
  98. )
  99. export(
  100. EXPORT "${PROJECT_NAME}Targets"
  101. FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake
  102. NAMESPACE coeurl::)
  103. export(PACKAGE coeurl)
  104. install(
  105. DIRECTORY "include/"
  106. DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
  107. FILES_MATCHING PATTERN "*.hpp"
  108. )
  109. install(
  110. FILES ${CMAKE_CURRENT_BINARY_DIR}/coeurlConfig.cmake
  111. DESTINATION "${INSTALL_CONFIGDIR}"
  112. )
  113. install(
  114. EXPORT "${PROJECT_NAME}Targets"
  115. NAMESPACE "${PROJECT_NAME}"
  116. DESTINATION "${INSTALL_CONFIGDIR}"
  117. )