Skip to content

Commit e7108f6

Browse files
committed
Use FetchContent to include CPR
This also changes the logic to respect CPR's CPR_USE_SYSTEM_CURL. If that variable is not set, we configure CPR to use the system CURL if it is old enough to not cause issues and build its own otherwise. This allows for developing on recent systems but allows the easy use of a system libcurl when building in older distros for deployment.
1 parent 5db93f3 commit e7108f6

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

lib/CMakeLists.txt

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,56 @@ add_subdirectory(zlib)
88
add_subdirectory(librcksum)
99
add_subdirectory(libzsync)
1010

11-
# external dependencies used as submodules
12-
11+
# note: we use functions to import the dependencies to have separate scopes for the variables we apply on them
12+
# https://alexreinking.com/blog/building-a-dual-shared-and-static-library-with-cmake.html
1313
if(NOT USE_SYSTEM_CPR)
14-
# for some reason we don't know, cpr wants to build shared libs by default
15-
# this is not the default behavior of CMake, so we force this option off again
16-
# unfortunately, we have to do this globally...
17-
# long term goal is to replace the submodule in some way, e.g., by using prebuilt cpr binaries
18-
set(BUILD_SHARED_LIBS OFF)
14+
function(import_cpr)
15+
# for some reason we don't know, cpr wants to build shared libs by default
16+
# this is not the default behavior of CMake, so we force this option off again
17+
# unfortunately, we have to do this globally...
18+
# long term goal is to replace the submodule in some way, e.g., by using prebuilt cpr binaries
19+
set(BUILD_SHARED_LIBS OFF)
20+
21+
# make sure that static libraries (especially built cURL libs) can be linked properly
22+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
23+
24+
# there is no need to build our own libcurl
25+
# in fact, building it as a static lib creates a whole circus of problems, and also takes time
26+
27+
# if a system provided installed libcurl is too "new", we want to have CPR build its own
28+
# otherwise, we want to use the system provided library
29+
# this way, we can build against a system-wide libcurl on older systems (e.g., Ubuntu focal) while allowing development on newer systems
30+
# see
31+
find_package(CURL)
32+
if(CURL_FOUND)
33+
message(STATUS "Found libcurl CURL_VERSION_STRING")
1934

20-
# make sure that static libraries (especially built cURL libs) can be linked properly
21-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
35+
if(NOT DEFINED CPR_USE_SYSTEM_CURL)
36+
if (CURL_VERSION_STRING VERSION_LESS_EQUAL 7.80.0)
37+
message(STATUS "libcurl is old enough to work with CPR, so we use the system one (use -DCPR_USE_SYSTEM_CURL=OFF to change this behavior)")
38+
set(CPR_USE_SYSTEM_CURL ON)
39+
else()
40+
message(WARNING "libcurl is too new to work with CPR, so we let CPR build its own (use -DCPR_USE_SYSTEM_CURL=ON to change this behavior)")
41+
set(CPR_USE_SYSTEM_CURL ON)
42+
endif()
43+
else()
44+
message(WARNING "CPR_USE_SYSTEM_CURL is set to ${CPR_USE_SYSTEM_CURL} by the user")
45+
endif()
46+
endif()
2247

23-
# there is no need to build our own libcurl
24-
# in fact, building it as a static lib creates a whole circus of problems, and also takes time
25-
set(CPR_FORCE_USE_SYSTEM_CURL ON)
48+
include(FetchContent)
49+
FetchContent_Declare(cpr
50+
GIT_REPOSITORY https://github.com/libcpr/cpr.git
51+
# note: newer versions don't necessarily work with Ubuntu bionic's libcurl packages!
52+
GIT_TAG 1.8.3
53+
)
54+
FetchContent_MakeAvailable(cpr)
2655

27-
add_subdirectory(cpr)
56+
# since we want to have static builds of cpr, we have to enable -fPIC manually
57+
# set_property(TARGET cpr::cpr PROPERTY POSITION_INDEPENDENT_CODE ON)
58+
endfunction()
2859

29-
# since we want to have static builds of cpr, we have to enable -fPIC manually
30-
set_property(TARGET cpr PROPERTY POSITION_INDEPENDENT_CODE ON)
60+
import_cpr()
3161
endif()
3262

3363
if(NOT USE_SYSTEM_ARGS)

lib/cpr

Submodule cpr deleted from db351ff

0 commit comments

Comments
 (0)