From ab368d8d7eece1c63a1e1a6c564a505f1952c4d1 Mon Sep 17 00:00:00 2001 From: Anurag Raut Date: Sun, 15 Nov 2026 18:12:12 +0530 Subject: [PATCH] MINOR: Export bundled dependencies as installable targets --- cpp/cmake_modules/BuildUtils.cmake | 34 +++++--- cpp/cmake_modules/ThirdpartyToolchain.cmake | 22 +++++ cpp/src/arrow/ArrowConfig.cmake.in | 57 ------------- cpp/src/arrow/ArrowGcsConfig.cmake.in | 34 -------- cpp/src/arrow/CMakeLists.txt | 89 ++++++++++++++++++--- 5 files changed, 123 insertions(+), 113 deletions(-) diff --git a/cpp/cmake_modules/BuildUtils.cmake b/cpp/cmake_modules/BuildUtils.cmake index 8556273a0d4c..27a88fa1933b 100644 --- a/cpp/cmake_modules/BuildUtils.cmake +++ b/cpp/cmake_modules/BuildUtils.cmake @@ -176,24 +176,34 @@ function(arrow_create_merged_static_lib output_target) set(ar_tool ${CMAKE_CXX_COMPILER_AR}) endif() - set(BUNDLE_COMMAND ${ar_tool} -M < ${ar_script_path}) + set(ar_script_runner "${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}-merge.cmake") + file(WRITE "${ar_script_runner}" + "execute_process(COMMAND \"${ar_tool}\" -M INPUT_FILE \"${ar_script_path}\" RESULT_VARIABLE result)\n" + "if(NOT \"\${result}\" STREQUAL \"0\")\n" + " message(FATAL_ERROR \"Failed to merge ${ARG_NAME}: \${result}\")\n" + "endif()\n") + set(BUNDLE_COMMAND ${CMAKE_COMMAND} -P ${ar_script_runner}) else() message(FATAL_ERROR "Unknown bundle scenario!") endif() - add_custom_target(${output_target}_merge ALL - ${BUNDLE_COMMAND} - DEPENDS ${ARG_ROOT} ${ARG_TO_MERGE} - BYPRODUCTS ${output_lib_path} - COMMENT "Bundling ${output_lib_path}" - VERBATIM) + set(dummy_source "${CMAKE_CURRENT_BINARY_DIR}/${output_target}.cc") + file(WRITE "${dummy_source}" "void ${output_target}_anchor() {}\n") - message(STATUS "Creating bundled static library target ${output_target} at ${output_lib_path}" - ) + add_library(${output_target} STATIC "${dummy_source}") + set_target_properties(${output_target} + PROPERTIES ARCHIVE_OUTPUT_DIRECTORY + "${BUILD_OUTPUT_ROOT_DIRECTORY}" OUTPUT_NAME + "${ARG_NAME}") + set_property(TARGET ${output_target} PROPERTY LINK_DEPENDS "${all_library_paths}") + + add_dependencies(${output_target} ${ARG_ROOT} ${ARG_TO_MERGE}) - add_library(${output_target} STATIC IMPORTED GLOBAL) - set_target_properties(${output_target} PROPERTIES IMPORTED_LOCATION ${output_lib_path}) - add_dependencies(${output_target} ${output_target}_merge) + add_custom_command(TARGET ${output_target} + POST_BUILD + COMMAND ${BUNDLE_COMMAND} + COMMENT "Bundling ${output_lib_path}" + VERBATIM) endfunction() function(arrow_install_cmake_package PACKAGE_NAME EXPORT_NAME) diff --git a/cpp/cmake_modules/ThirdpartyToolchain.cmake b/cpp/cmake_modules/ThirdpartyToolchain.cmake index f4aa19bab59b..73dd44564f6f 100644 --- a/cpp/cmake_modules/ThirdpartyToolchain.cmake +++ b/cpp/cmake_modules/ThirdpartyToolchain.cmake @@ -4323,6 +4323,28 @@ function(build_awssdk) endif() endif() endforeach() + # Keep platform libraries required by the bundled AWS CRT with the + # aws-c-common target so they are propagated to the bundled archive. + if(TARGET aws-c-common) + if(APPLE) + find_library(CORE_FOUNDATION CoreFoundation REQUIRED) + find_library(NETWORK Network REQUIRED) + find_library(SECURITY Security REQUIRED) + target_link_libraries(aws-c-common INTERFACE "${CORE_FOUNDATION}" "${NETWORK}" + "${SECURITY}") + elseif(WIN32) + target_link_libraries(aws-c-common + INTERFACE "winhttp.lib" + "bcrypt.lib" + "wininet.lib" + "userenv.lib" + "version.lib" + "ncrypt.lib" + "Secur32.lib" + "Shlwapi.lib") + endif() + endif() + list(PREPEND AWSSDK_LINK_LIBRARIES aws-cpp-sdk-identity-management diff --git a/cpp/src/arrow/ArrowConfig.cmake.in b/cpp/src/arrow/ArrowConfig.cmake.in index 6bbb53a2f76e..fbe497ca9e0a 100644 --- a/cpp/src/arrow/ArrowConfig.cmake.in +++ b/cpp/src/arrow/ArrowConfig.cmake.in @@ -110,63 +110,6 @@ endif() include("${CMAKE_CURRENT_LIST_DIR}/ArrowTargets.cmake") -if(TARGET Arrow::arrow_static AND NOT TARGET Arrow::arrow_bundled_dependencies) - add_library(Arrow::arrow_bundled_dependencies STATIC IMPORTED) - get_target_property(arrow_static_configurations Arrow::arrow_static - IMPORTED_CONFIGURATIONS) - foreach(CONFIGURATION ${arrow_static_configurations}) - string(TOUPPER "${CONFIGURATION}" CONFIGURATION) - get_target_property(arrow_static_location Arrow::arrow_static - LOCATION_${CONFIGURATION}) - get_filename_component(arrow_lib_dir "${arrow_static_location}" DIRECTORY) - set_property(TARGET Arrow::arrow_bundled_dependencies - APPEND - PROPERTY IMPORTED_CONFIGURATIONS ${CONFIGURATION}) - set_target_properties(Arrow::arrow_bundled_dependencies - PROPERTIES IMPORTED_LOCATION_${CONFIGURATION} - "${arrow_lib_dir}/${CMAKE_STATIC_LIBRARY_PREFIX}arrow_bundled_dependencies${CMAKE_STATIC_LIBRARY_SUFFIX}" - ) - endforeach() - - if(ARROW_BUNDLED_STATIC_LIBS) - get_target_property(arrow_bundled_dependencies_link_libs Arrow::arrow_static - INTERFACE_LINK_LIBRARIES) - list(REMOVE_ITEM arrow_bundled_dependencies_link_libs - Arrow::arrow_bundled_dependencies) - target_link_libraries(Arrow::arrow_bundled_dependencies - INTERFACE ${arrow_bundled_dependencies_link_libs}) - endif() - - # CMP0057: Support new if() IN_LIST operator. - # https://cmake.org/cmake/help/latest/policy/CMP0057.html - cmake_policy(PUSH) - cmake_policy(SET CMP0057 NEW) - if(APPLE AND ("aws-c-common" IN_LIST ARROW_BUNDLED_STATIC_LIBS - OR "absl::time" IN_LIST ARROW_BUNDLED_STATIC_LIBS)) - find_library(CORE_FOUNDATION CoreFoundation) - target_link_libraries(Arrow::arrow_bundled_dependencies INTERFACE ${CORE_FOUNDATION}) - endif() - if("aws-c-common" IN_LIST ARROW_BUNDLED_STATIC_LIBS) - if(APPLE) - find_library(NETWORK Network) - target_link_libraries(Arrow::arrow_bundled_dependencies INTERFACE ${NETWORK}) - find_library(SECURITY Security) - target_link_libraries(Arrow::arrow_bundled_dependencies INTERFACE ${SECURITY}) - elseif(WIN32) - target_link_libraries(Arrow::arrow_bundled_dependencies - INTERFACE "winhttp.lib" - "bcrypt.lib" - "wininet.lib" - "userenv.lib" - "version.lib" - "ncrypt.lib" - "Secur32.lib" - "Shlwapi.lib") - endif() - endif() - cmake_policy(POP) -endif() - macro(arrow_keep_backward_compatibility namespace target_base_name) string(TOUPPER ${target_base_name} target_base_name_upper) diff --git a/cpp/src/arrow/ArrowGcsConfig.cmake.in b/cpp/src/arrow/ArrowGcsConfig.cmake.in index 7768a307b329..bdd8d75a6ded 100644 --- a/cpp/src/arrow/ArrowGcsConfig.cmake.in +++ b/cpp/src/arrow/ArrowGcsConfig.cmake.in @@ -38,40 +38,6 @@ endif() include("${CMAKE_CURRENT_LIST_DIR}/ArrowGcsTargets.cmake") -if(TARGET ArrowGcs::arrow_gcs_static - AND ARROW_GCS_BUNDLED_STATIC_LIBS - AND NOT TARGET ArrowGcs::arrow_gcs_bundled_dependencies) - add_library(ArrowGcs::arrow_gcs_bundled_dependencies STATIC IMPORTED) - get_target_property(arrow_gcs_bundled_dependencies_link_libs ArrowGcs::arrow_gcs_static - INTERFACE_LINK_LIBRARIES) - list(REMOVE_ITEM arrow_gcs_bundled_dependencies_link_libs - ArrowGcs::arrow_gcs_bundled_dependencies Arrow::arrow_static) - if("@absl_SOURCE@" STREQUAL "BUNDLED") - list(APPEND arrow_gcs_bundled_dependencies_link_libs - Arrow::arrow_bundled_dependencies) - endif() - if(NOT WIN32) - list(APPEND arrow_gcs_bundled_dependencies_link_libs OpenSSL::SSL OpenSSL::Crypto) - endif() - target_link_libraries(ArrowGcs::arrow_gcs_bundled_dependencies - INTERFACE ${arrow_gcs_bundled_dependencies_link_libs}) - get_target_property(arrow_gcs_static_configurations ArrowGcs::arrow_gcs_static - IMPORTED_CONFIGURATIONS) - foreach(CONFIGURATION ${arrow_gcs_static_configurations}) - string(TOUPPER "${CONFIGURATION}" CONFIGURATION) - get_target_property(arrow_gcs_static_location ArrowGcs::arrow_gcs_static - IMPORTED_LOCATION_${CONFIGURATION}) - get_filename_component(arrow_gcs_lib_dir "${arrow_gcs_static_location}" DIRECTORY) - set_property(TARGET ArrowGcs::arrow_gcs_bundled_dependencies - APPEND - PROPERTY IMPORTED_CONFIGURATIONS ${CONFIGURATION}) - set_target_properties(ArrowGcs::arrow_gcs_bundled_dependencies - PROPERTIES IMPORTED_LOCATION_${CONFIGURATION} - "${arrow_gcs_lib_dir}/${CMAKE_STATIC_LIBRARY_PREFIX}arrow_gcs_bundled_dependencies${CMAKE_STATIC_LIBRARY_SUFFIX}" - ) - endforeach() -endif() - arrow_keep_backward_compatibility(ArrowGcs arrow_gcs) check_required_components(ArrowGcs) diff --git a/cpp/src/arrow/CMakeLists.txt b/cpp/src/arrow/CMakeLists.txt index 6ceca297be3d..194903e2b3d5 100644 --- a/cpp/src/arrow/CMakeLists.txt +++ b/cpp/src/arrow/CMakeLists.txt @@ -980,10 +980,9 @@ if(ARROW_FILESYSTEM) ${_FIRST_GCS_LIB} TO_MERGE ${_OTHER_GCS_LIBS}) - get_target_property(arrow_gcs_bundled_dependencies_path - arrow_gcs_bundled_dependencies IMPORTED_LOCATION) - install(FILES ${arrow_gcs_bundled_dependencies_path} ${INSTALL_IS_OPTIONAL} - DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS arrow_gcs_bundled_dependencies ${INSTALL_IS_OPTIONAL} + EXPORT arrow_gcs_targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) string(PREPEND ARROW_GCS_PC_LIBS_PRIVATE " -larrow_gcs_bundled_dependencies") # Resolve bundled Abseil after SDK list(PREPEND ARROW_GCS_STATIC_INSTALL_INTERFACE_LIBS @@ -1051,6 +1050,56 @@ if(ARROW_FILESYSTEM) set(ARROW_GCS_PC_LIBS "") set(ARROW_GCS_PC_REQUIRES "") endif() + if(ARROW_BUILD_STATIC AND ARROW_GCS_BUNDLED_STATIC_LIBS) + set(_ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS) + foreach(_bundled_lib IN LISTS ARROW_GCS_BUNDLED_STATIC_LIBS) + if(TARGET ${_bundled_lib}) + get_target_property(_bundled_lib_link_libraries ${_bundled_lib} + INTERFACE_LINK_LIBRARIES) + if(_bundled_lib_link_libraries) + list(APPEND _ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS + ${_bundled_lib_link_libraries}) + endif() + endif() + endforeach() + + foreach(_bundled_lib IN LISTS ARROW_GCS_BUNDLED_STATIC_LIBS) + list(REMOVE_ITEM _ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS ${_bundled_lib}) + endforeach() + + if(absl_SOURCE STREQUAL "BUNDLED") + list(FILTER + _ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS + EXCLUDE + REGEX + "^absl::") + list(APPEND _ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS + Arrow::arrow_bundled_dependencies) + endif() + + if(NOT WIN32) + list(APPEND ARROW_GCS_SYSTEM_DEPENDENCIES OpenSSL) + list(APPEND _ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS OpenSSL::SSL + OpenSSL::Crypto) + endif() + + # nlohmann_json is header-only and only needed to compile + # google-cloud-cpp (bundled or system), not to link against it. + list(FILTER + _ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS + EXCLUDE + REGEX + "nlohmann_json") + + list(REMOVE_DUPLICATES _ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS) + + if(_ARROW_GCS_BUNDLED_DEPENDENCIES_LINK_LIBS) + target_link_libraries(arrow_gcs_bundled_dependencies + INTERFACE "$" + ) + endif() + endif() + add_arrow_lib(arrow_gcs CMAKE_PACKAGE_NAME ArrowGcs @@ -1236,14 +1285,34 @@ if(ARROW_BUILD_BUNDLED_DEPENDENCIES) ${_FIRST_LIB} TO_MERGE ${_OTHER_LIBS}) - # We can't use install(TARGETS) here because - # arrow_bundled_dependencies is an IMPORTED library. - get_target_property(arrow_bundled_dependencies_path arrow_bundled_dependencies - IMPORTED_LOCATION) - install(FILES ${arrow_bundled_dependencies_path} ${INSTALL_IS_OPTIONAL} - DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS arrow_bundled_dependencies ${INSTALL_IS_OPTIONAL} + EXPORT arrow_targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) string(PREPEND ARROW_PC_LIBS_PRIVATE " -larrow_bundled_dependencies") list(INSERT ARROW_STATIC_INSTALL_INTERFACE_LIBS 0 "Arrow::arrow_bundled_dependencies") + + # Propagate only the external link requirements of libraries merged into + # arrow_bundled_dependencies. + set(_ARROW_BUNDLED_DEPENDENCIES_LINK_LIBS) + foreach(_bundled_lib IN LISTS ARROW_BUNDLED_STATIC_LIBS) + if(TARGET ${_bundled_lib}) + get_target_property(_bundled_lib_link_libraries ${_bundled_lib} + INTERFACE_LINK_LIBRARIES) + if(_bundled_lib_link_libraries) + list(APPEND _ARROW_BUNDLED_DEPENDENCIES_LINK_LIBS ${_bundled_lib_link_libraries}) + endif() + endif() + endforeach() + foreach(_bundled_lib IN LISTS ARROW_BUNDLED_STATIC_LIBS) + list(REMOVE_ITEM _ARROW_BUNDLED_DEPENDENCIES_LINK_LIBS ${_bundled_lib}) + endforeach() + list(REMOVE_DUPLICATES _ARROW_BUNDLED_DEPENDENCIES_LINK_LIBS) + if(_ARROW_BUNDLED_DEPENDENCIES_LINK_LIBS) + target_link_libraries(arrow_bundled_dependencies + INTERFACE "$" + ) + endif() + endif() if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")