This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 60ed241e16 fix(bindings/cpp): propagate Rust native link dependencies 
to Linux consumers (#9406)
60ed241e16 is described below

commit 60ed241e160101d7ecadb114b3ae9bcc97b2639d
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 30 05:21:48 2026 +0800

    fix(bindings/cpp): propagate Rust native link dependencies to Linux 
consumers (#9406)
    
    fix(bindings/cpp): propagate Rust native libraries on Linux
---
 bindings/cpp/CMakeLists.txt   | 19 +++++++--
 bindings/cpp/cargo-link.cmake | 92 +++++++++++++++++++++++++++++++++++++++++++
 bindings/cpp/cargo-link/rustc | 53 +++++++++++++++++++++++++
 3 files changed, 161 insertions(+), 3 deletions(-)

diff --git a/bindings/cpp/CMakeLists.txt b/bindings/cpp/CMakeLists.txt
index c6a5b90b56..d3926e613a 100644
--- a/bindings/cpp/CMakeLists.txt
+++ b/bindings/cpp/CMakeLists.txt
@@ -160,11 +160,18 @@ if (OPENDAL_FEATURES)
     list(APPEND CARGO_BUILD_FLAGS "--features" "${OPENDAL_FEATURES}")
 endif ()
 
+set(CARGO_BUILD_COMMAND cargo build)
+if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+    set(RUST_LINK_RESPONSE "${CMAKE_CURRENT_BINARY_DIR}/rust-link.rsp")
+    set(CARGO_BUILD_COMMAND ${CMAKE_COMMAND} 
"-DRUST_LINK_RESPONSE=${RUST_LINK_RESPONSE}"
+        -P "${PROJECT_SOURCE_DIR}/cargo-link.cmake" --)
+endif()
+
 add_custom_target(cargo_build
         COMMAND ${CMAKE_COMMAND} -E env ${CARGO_BUILD_ENV} 
"OPENDAL_CPP_BRIDGE_DIR=${CXX_BRIDGE_DIR}"
-                cargo build --manifest-path ${CARGO_MANIFEST} 
${CARGO_BUILD_FLAGS}
+                ${CARGO_BUILD_COMMAND} --manifest-path ${CARGO_MANIFEST} 
${CARGO_BUILD_FLAGS}
         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
-        BYPRODUCTS ${RUST_BRIDGE_CPP} ${RUST_LIB} ${RUST_HEADER_FILE}
+        BYPRODUCTS ${RUST_BRIDGE_CPP} ${RUST_LIB} ${RUST_HEADER_FILE} 
${RUST_LINK_RESPONSE}
         USES_TERMINAL
         VERBATIM
         COMMENT "Running cargo..."
@@ -178,7 +185,13 @@ if (OPENDAL_ENABLE_ASYNC)
     target_compile_options(opendal_cpp PUBLIC -include 
${PROJECT_SOURCE_DIR}/include/async_defs.hpp)
 endif()
 target_link_libraries(opendal_cpp PUBLIC ${RUST_LIB})
-target_link_libraries(opendal_cpp PRIVATE ${CMAKE_DL_LIBS})
+if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+    # Consumers need the native libraries after the Rust static archive.
+    target_link_libraries(opendal_cpp PRIVATE "-Xlinker 
\"@${RUST_LINK_RESPONSE}\"")
+    set_property(TARGET opendal_cpp APPEND PROPERTY INTERFACE_LINK_DEPENDS 
"${RUST_LINK_RESPONSE}")
+else()
+    target_link_libraries(opendal_cpp PRIVATE ${CMAKE_DL_LIBS})
+endif()
 
 add_dependencies(opendal_cpp cargo_build)
 
diff --git a/bindings/cpp/cargo-link.cmake b/bindings/cpp/cargo-link.cmake
new file mode 100644
index 0000000000..b190f9c916
--- /dev/null
+++ b/bindings/cpp/cargo-link.cmake
@@ -0,0 +1,92 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+cmake_minimum_required(VERSION 3.22)
+
+# Forward the Cargo arguments after the script and the -- separator.
+math(EXPR last_arg "${CMAKE_ARGC} - 1")
+foreach(index RANGE 5 ${last_arg})
+    list(APPEND cargo_args "${CMAKE_ARGV${index}}")
+endforeach()
+
+# Ask Cargo to resolve its compiler with the same config and working directory
+# as the binding build. Replacing only the compiler keeps both Cargo wrappers.
+cmake_path(GET RUST_LINK_RESPONSE PARENT_PATH build_dir)
+set(probe_dir "${build_dir}/cargo-link-probe")
+file(MAKE_DIRECTORY "${probe_dir}")
+file(CONFIGURE OUTPUT "${probe_dir}/Cargo.toml" CONTENT [=[
+[package]
+name = "opendal-cpp-link-probe"
+version = "0.0.0"
+edition = "2024"
+publish = false
+
+[lib]
+path = "lib.rs"
+
+[workspace]
+]=])
+file(CONFIGURE OUTPUT "${probe_dir}/lib.rs" CONTENT "")
+file(CONFIGURE OUTPUT "${probe_dir}/build.rs" CONTENT [=[
+fn main() {
+    let rustc = std::env::var("RUSTC").expect("Cargo did not provide RUSTC");
+    println!("cargo::rerun-if-changed=compiler-probe");
+    println!("cargo::rustc-env=OPENDAL_CPP_RUSTC={rustc}");
+}
+]=])
+file(TOUCH "${probe_dir}/compiler-probe")
+execute_process(
+    COMMAND cargo check --manifest-path "${probe_dir}/Cargo.toml" --target 
host-tuple
+            --quiet --color=never --message-format=json-render-diagnostics
+    OUTPUT_VARIABLE probe_output
+    COMMAND_ERROR_IS_FATAL ANY
+)
+string(REGEX MATCH "[^\r\n]*\"reason\":\"build-script-executed\"[^\r\n]*" 
probe_result "${probe_output}")
+if(NOT probe_result)
+    message(FATAL_ERROR "Cargo did not report the configured Rust compiler")
+endif()
+string(JSON rustc GET "${probe_result}" env 0 1)
+string(SHA256 compiler_id "${rustc}")
+
+# The cfg distinguishes this final compilation from one without the compiler
+# shim. Cargo replays its diagnostics when it reuses the same artifact.
+execute_process(
+    COMMAND "${CMAKE_COMMAND}" -E env 
"RUSTC=${CMAKE_CURRENT_LIST_DIR}/cargo-link/rustc"
+            "OPENDAL_CPP_RUSTC=${rustc}" cargo rustc --lib ${cargo_args}
+            --color=never --message-format=json-render-diagnostics
+            -- "--cfg=opendal_cpp_cmake_link=\"${compiler_id}\"" 
--print=native-static-libs
+    OUTPUT_QUIET
+    ERROR_VARIABLE diagnostics
+    RESULT_VARIABLE status
+)
+set(search_result "(^|\n)opendal-cpp-link-search:([^\r\n]*)")
+string(REGEX REPLACE "${search_result}" "\\1" display "${diagnostics}")
+message("${display}")
+if(NOT status STREQUAL "0")
+    message(FATAL_ERROR "Cargo failed to build the OpenDAL C++ static library 
(${status})")
+endif()
+if(NOT diagnostics MATCHES "${search_result}")
+    message(FATAL_ERROR "Cargo did not report native search paths for 
opendal-cpp")
+endif()
+set(search_paths "${CMAKE_MATCH_2}")
+if(NOT diagnostics MATCHES "(^|\n)note: native-static-libs: ([^\r\n]+)")
+    message(FATAL_ERROR "Cargo did not report native-static-libs for 
opendal-cpp")
+endif()
+set(native_libraries "${CMAKE_MATCH_2}")
+# Clang can expand the response in the driver. A library as the first token
+# keeps -Xlinker from pulling the first -L out of the ordered search paths.
+file(CONFIGURE OUTPUT "${RUST_LINK_RESPONSE}" CONTENT 
"@native_libraries@\n@search_paths@\n" @ONLY)
diff --git a/bindings/cpp/cargo-link/rustc b/bindings/cpp/cargo-link/rustc
new file mode 100755
index 0000000000..9f27c82df0
--- /dev/null
+++ b/bindings/cpp/cargo-link/rustc
@@ -0,0 +1,53 @@
+#!/bin/sh
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set -eu
+
+report_search_paths() {
+    paths=
+    while [ "$#" -gt 0 ]; do
+        case "$1" in
+            -L)
+                shift
+                [ "$#" -gt 0 ] || break
+                path=$1
+                ;;
+            -L*) path=${1#-L} ;;
+            *) shift; continue ;;
+        esac
+        shift
+        case "$path" in
+            native=*|all=*) path=${path#*=} ;;
+            crate=*|dependency=*|framework=*) continue ;;
+        esac
+        case "$path" in
+            /*) ;;
+            *) path=$PWD/$path ;;
+        esac
+        escaped=$(printf '%s\n' "$path" | sed 's/[\\"]/\\&/g')
+        paths="$paths \"-L$escaped\""
+    done
+    printf 'opendal-cpp-link-search:%s\n' "$paths" >&2
+}
+
+for arg in "$@"; do
+    case "$arg" in
+        --cfg=opendal_cpp_cmake_link=*) report_search_paths "$@"; break ;;
+    esac
+done
+exec "$OPENDAL_CPP_RUSTC" "$@"

Reply via email to