Skip to content

Commit

Permalink
CMake find-package support (#645)
Browse files Browse the repository at this point in the history
* Export cpr targets via CMake standard packaging approach.

* Export cpr cmake targets only if CPR_FORCE_USE_SYSTEM_CURL is set.

Otherwise, we have to export libcurl targets and all deps of libcurl
because it is built in-source. CMake is not very user-friendly when it
comes to exporting and installing targets of in-source built dependencies.

* Move GNUInstallDirs outside of if/else

Co-authored-by: Mirza Avdic <[email protected]>
  • Loading branch information
mirzachi and Mirza Avdic authored Sep 27, 2021
1 parent dfb86c1 commit 956dc98
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmake/cprConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include(CMakeFindDependencyMacro)
@PACKAGE_INIT@

find_dependency(CURL REQUIRED)

include(${CMAKE_CURRENT_LIST_DIR}/cprTargets.cmake)

check_required_components(cpr)
35 changes: 34 additions & 1 deletion cpr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,37 @@ set_target_properties(cpr
VERSION ${${PROJECT_NAME}_VERSION}
SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR})

install(TARGETS cpr)
# Import GNU common install directory variables
include(GNUInstallDirs)

if(CPR_FORCE_USE_SYSTEM_CURL)
install(TARGETS cpr
EXPORT cprTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

# Include CMake helpers for package config files
# Follow this installation guideline: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html
include(CMakePackageConfigHelpers)

write_basic_package_version_file(
"${CMAKE_BINARY_DIR}/cpr/cprConfigVersion.cmake"
VERSION ${${PROJECT_NAME}_VERSION}
COMPATIBILITY ExactVersion)

configure_package_config_file(${CMAKE_SOURCE_DIR}/cmake/cprConfig.cmake.in
"${CMAKE_BINARY_DIR}/cpr/cprConfig.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpr)

install(EXPORT cprTargets
FILE cprTargets.cmake
NAMESPACE cpr::
DESTINATION lib/cmake/cpr)

install(FILES ${CMAKE_BINARY_DIR}/cpr/cprConfig.cmake
${CMAKE_BINARY_DIR}/cpr/cprConfigVersion.cmake DESTINATION lib/cmake/cpr)
else()
install(TARGETS cpr
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

0 comments on commit 956dc98

Please sign in to comment.