@@ -214,13 +214,111 @@ function(resolve_avro_dependency)
214214 PARENT_SCOPE)
215215endfunction ()
216216
217+ # ----------------------------------------------------------------------
218+ # cURL (vendored if necessary for cpr)
219+
220+ function (resolve_curl_dependency)
221+ prepare_fetchcontent()
222+
223+ # Configuration for the vendored build case. These variables must be set
224+ # before FetchContent_MakeAvailable is called.
225+ set (CMAKE_POSITION_INDEPENDENT_CODE ON ) # Crucial for -fPIC on Linux
226+ set (HTTP_ONLY
227+ ON
228+ CACHE BOOL "" FORCE)
229+ set (BUILD_CURL_EXE
230+ OFF
231+ CACHE BOOL "" FORCE)
232+ set (BUILD_TESTING
233+ OFF
234+ CACHE BOOL "" FORCE)
235+ set (CURL_DISABLE_INSTALL
236+ ON
237+ CACHE BOOL "" FORCE)
238+ set (CURL_USE_OPENSSL
239+ ON
240+ CACHE BOOL "" FORCE)
241+ set (CURL_DISABLE_LDAP
242+ ON
243+ CACHE BOOL "" FORCE)
244+ set (CURL_DISABLE_LDAPS
245+ ON
246+ CACHE BOOL "" FORCE)
247+ set (USE_LIBIDN2
248+ OFF
249+ CACHE BOOL "" FORCE)
250+ set (USE_IDN
251+ OFF
252+ CACHE BOOL "" FORCE)
253+
254+ # Using FIND_PACKAGE_ARGS, FetchContent will first try `find_package(CURL)`.
255+ # If that fails, it will proceed to download and build from the URL.
256+ if (APPLE )
257+ # 在 macOS 上,我们不信任系统 curl,总是自己构建
258+ # 所以我们不使用 FIND_PACKAGE_ARGS
259+ fetchcontent_declare(curl
260+ ${FC_DECLARE_COMMON_OPTIONS}
261+ URL https://github.com/curl/curl/releases/download/curl-8_9_1/curl-8.9.1.tar.xz
262+ )
263+ else ()
264+ # 在其他系统上,继续使用 find-or-fetch 策略
265+ fetchcontent_declare(curl
266+ ${FC_DECLARE_COMMON_OPTIONS}
267+ URL https://github.com/curl/curl/releases/download/curl-8_9_1/curl-8.9.1.tar.xz
268+ FIND_PACKAGE_ARGS
269+ NAMES
270+ CURL
271+ 7.64.0
272+ CONFIG)
273+ endif ()
274+
275+ fetchcontent_makeavailable(curl)
276+
277+ # Check if FetchContent actually downloaded and built the dependency.
278+ # The variable is <lowercase_name>_POPULATED.
279+ if (curl_POPULATED)
280+ message (STATUS "System curl not found. Using vendored curl." )
281+ set (CURL_VENDORED TRUE )
282+
283+ # The curl CMake build creates the 'curl-library' target for the static library.
284+ if (TARGET curl-library)
285+ set_target_properties (curl-library
286+ PROPERTIES OUTPUT_NAME "iceberg_vendored_curl"
287+ # Ensure PIC is set on the target itself for robustness
288+ POSITION_INDEPENDENT_CODE ON )
289+ install (TARGETS curl-library
290+ EXPORT iceberg_targets
291+ ARCHIVE DESTINATION "${ICEBERG_INSTALL_LIBDIR} " )
292+ endif ()
293+ else ()
294+ message (STATUS "Found and using system curl." )
295+ set (CURL_VENDORED FALSE )
296+ list (APPEND ICEBERG_SYSTEM_DEPENDENCIES CURL)
297+ endif ()
298+
299+ set (CURL_VENDORED
300+ ${CURL_VENDORED}
301+ PARENT_SCOPE)
302+ set (ICEBERG_SYSTEM_DEPENDENCIES
303+ ${ICEBERG_SYSTEM_DEPENDENCIES}
304+ PARENT_SCOPE)
305+ endfunction ()
306+
217307# ----------------------------------------------------------------------
218308# cpr (C++ Requests)
219309
220310function (resolve_cpr_dependency)
311+ # First, ensure the curl dependency is resolved
312+ resolve_curl_dependency()
313+
221314 prepare_fetchcontent()
222315
223- find_package (CURL QUIET )
316+ # Always tell cpr to find an external curl. Our resolve_curl_dependency()
317+ # function has guaranteed that one is available, either from the system
318+ # or by building it from source.
319+ set (CPR_USE_SYSTEM_CURL
320+ ON
321+ CACHE BOOL "" FORCE)
224322
225323 set (CPR_BUILD_TESTS
226324 OFF
@@ -235,36 +333,6 @@ function(resolve_cpr_dependency)
235333 ON
236334 CACHE BOOL "" FORCE)
237335
238- if (CURL_FOUND)
239- message (STATUS "Found system curl: ${CURL_VERSION_STRING} . Configuring cpr to use it."
240- )
241- set (CPR_USE_SYSTEM_CURL
242- ON
243- CACHE BOOL "" FORCE)
244- list (APPEND ICEBERG_SYSTEM_DEPENDENCIES CURL)
245- else ()
246- message (STATUS "System curl not found. Vendoring curl for cpr." )
247- set (CMAKE_POSITION_INDEPENDENT_CODE ON )
248- set (CPR_USE_SYSTEM_CURL
249- OFF
250- CACHE BOOL "" FORCE)
251- set (CURL_DISABLE_INSTALL
252- ON
253- CACHE BOOL "" FORCE)
254- set (CURL_DISABLE_LDAP
255- ON
256- CACHE BOOL "" FORCE)
257- set (CURL_DISABLE_LDAPS
258- ON
259- CACHE BOOL "" FORCE)
260- set (USE_LIBIDN2
261- OFF
262- CACHE BOOL "" FORCE)
263- set (USE_IDN
264- OFF
265- CACHE BOOL "" FORCE)
266- endif ()
267-
268336 fetchcontent_declare(cpr
269337 ${FC_DECLARE_COMMON_OPTIONS}
270338 GIT_REPOSITORY https://github.com/libcpr/cpr.git
@@ -273,6 +341,7 @@ function(resolve_cpr_dependency)
273341 fetchcontent_makeavailable(cpr)
274342
275343 if (cpr_SOURCE_DIR)
344+ set (CPR_VENDORED TRUE ) # cpr itself is always vendored by our project
276345 set_target_properties (cpr PROPERTIES OUTPUT_NAME "iceberg_vendored_cpr"
277346 POSITION_INDEPENDENT_CODE ON )
278347
@@ -281,24 +350,17 @@ function(resolve_cpr_dependency)
281350 RUNTIME DESTINATION "${ICEBERG_INSTALL_BINDIR} "
282351 ARCHIVE DESTINATION "${ICEBERG_INSTALL_LIBDIR} "
283352 LIBRARY DESTINATION "${ICEBERG_INSTALL_LIBDIR} " )
284-
285- if (NOT CURL_FOUND AND TARGET libcurl_static)
286- set_target_properties (libcurl_static PROPERTIES EXPORT_NAME iceberg_libcurl_static)
287-
288- install (TARGETS libcurl_static
289- EXPORT iceberg_targets
290- ARCHIVE DESTINATION "${ICEBERG_INSTALL_LIBDIR} " )
291- endif ()
292-
293- set (CPR_VENDORED TRUE )
294353 else ()
354+ # This branch is unlikely to be hit if the git repo is available,
355+ # but is kept for completeness.
295356 set (CPR_VENDORED FALSE )
296357 list (APPEND ICEBERG_SYSTEM_DEPENDENCIES cpr)
297358 endif ()
298359
299360 set (CPR_VENDORED
300361 ${CPR_VENDORED}
301362 PARENT_SCOPE)
363+ # Propagate the system dependencies list, which was modified by resolve_curl_dependency
302364 set (ICEBERG_SYSTEM_DEPENDENCIES
303365 ${ICEBERG_SYSTEM_DEPENDENCIES}
304366 PARENT_SCOPE)
0 commit comments