libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.49k stars 922 forks source link

CMake target exports are not created if not using system curl #1012

Closed bernedom closed 8 months ago

bernedom commented 8 months ago

Description

When building and installing cpr using curl that is not installed in the system no CMake export sets are created, possibly leading to errors in projects dependent on cpr. When installing projects dependent on cpr the following CMake error might pop up:

CMake Error: install(EXPORT "myProject_Targets" ...) includes target "myProject" which requires target "cpr" that is not in any export set.

Is there a specific reason why the exports are omitted in this case?

For most builds this is not an issue, but it can become an issue if CMakes FetchContent together with find_package is used to build a dependency tree. (This is not a very good way to handle dependencies, but it beats checking in binaries to git)

Example/How to Reproduce

See the CMakeLists.txt in the following example. https://github.com/bernedom/CPR_Export_Failure_Example

To build it use

cmake -S . -B ./build

Configuration should fail with the following error: CMake Error: install(EXPORT "unleash_Targets" ...) includes target "unleash" which requires target "cpr" that is not in any export set.

Possible Fix

the fix would be changing ln 81ff in In the file https://github.com/libcpr/cpr/blob/master/cpr/CMakeLists.txt like this:

 install(TARGETS cpr
                EXPORT cprTargets
                RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
                ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
                LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(EXPORT cprTargets
                FILE cprTargets.cmake
                NAMESPACE cpr::
                DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpr) 

Where did you get it from?

GitHub (branch e.g. master)

Additional Context/Your Environment

CMake version 3.25 or higher (possibly other versions are also affected)

COM8 commented 8 months ago

@bernedom Thanks for this great and detailed issue report.

I created a fix for it in #1013.

FetchContent_Declare(
  cpr
  GIT_REPOSITORY https://github.com/libcpr/cpr.git
  GIT_TAG 4018a76d953b60b227ed496caa7375af465dfc6c
  OVERRIDE_FIND_PACKAGE)

fixes it in your example.

COM8 commented 8 months ago

Once merged, I will backport this fix to 1.10.x

COM8 commented 8 months ago

Decided against porting it to 1.10.x since there will be soonish a 1.11.0 release.