Repository navigation
Add bidirectional C++ and Rust interop with rules_rust cdylib support - #32
Open
justhecuke wants to merge 19 commits into
Open
justhecuke wants to merge 19 commits into
justhecuke wants to merge 19 commits into
Conversation
Add bidirectional FFI interop between C++ and Rust, including C++ calling Rust and Rust calling C++. Features: - Rust calling C++: //rs:cpp_calculator links //cc:calculator via link_deps and invokes cc_calculator_* C-ABI functions hermetically using rust-lld. - C++ calling Rust: //cc:rust_calculator links //rs:rust_calculator (rust_shared_library / cdylib) and invokes rs_calculator_* C-ABI functions via GNU import library. - cc/: native C++ expression calculator engine, C-ABI export, CLI supporting info, calc, and calc-rs, and unit tests (calculator_test, rust_calculator_test). - rs/: extended with cpp_calculator wrapper, rust_calculator cdylib export, cpp_calculator_test, and CLI calc-cpp command. - patches/rules_rust_cdylib_windows.patch: fixes rules_rust cdylib support on Windows GNU targets: - Adds --entry=DllMainCRTStartup for direct linker drivers (rust-lld) linking shared libraries on Windows GNU. - Declares correct import library filename by target ABI (.dll.lib on MSVC, lib<name>.dll.a on GNU/LLVM-GNU). - Omits transitive stdlib_linkflags from CcInfo on cdylib since cdylib is already linked against Rust stdlib. - README.md: updated with usage instructions for both CLI tools.
…t Apple ld64 flags
…rules_rust - Add patches/hermetic_cc_toolchain_dynamic_linking.patch to inject runtime_library_search_directories, soname (ELF), and set_install_name (macOS) into hermetic_cc_toolchain, allowing dynamically linked binaries and tests to find shared libraries at runtime without workarounds. - Update patches/rules_rust_cdylib_windows.patch to include cdylib artifacts in runfiles so dependent binaries receive transitive dynamic libraries in their runfiles tree. - Remove workarounds from cc/BUILD.bazel: strip linkstatic=True, data overrides, LD_LIBRARY_PATH/DYLD_LIBRARY_PATH, and manual -Wl,-rpath flags.
…e macos clang bypass - Set dynamic_library_symlink_path to crate_info.output.basename in rules_rust cdylib create_library_to_link, ensuring the solib symlink in _solib_<arch> matches the DT_SONAME embedded by rustc so dynamic linkers can locate the shared library at runtime. - Combine hermetic_cc_toolchain patches into patches/hermetic_cc_toolchain.patch: includes runtime_library_search_directories, soname, and restores the macOS Darwin libc bypass so native Apple Clang is used on macOS runners.
justhecuke
force-pushed
the
add-cpp-rust-interop
branch
from
November 27, 2026 17:48
843838b to
f5f42c7
Compare
…++abi across cc_library .so boundaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR implements bidirectional C++ and Rust interop along with upstream-ready
rules_rustpatches to supportrust_shared_library(cdylib) on Windows GNU targets.Summary of Changes
Rust calling C++:
//rs:cpp_calculatorlinks//cc:calculatorvialink_depsand invokescc_calculator_*C-ABI functions hermetically usingrust-lld.//rs:cpp_calculator_test.C++ calling Rust:
//cc:rust_calculatorlinks//rs:rust_calculator(rust_shared_library/cdylib) and callsrs_calculator_*C-ABI functions via the MinGW import library (librust_calculator.dll.a).//cc:rust_calculator_test.New C++ Engine & CLI (
cc/):cc/calculator.hh&cc/calculator.cc: Native C++ math expression parser and evaluator compiled with-fno-exceptions -fno-rttiand exposingextern "C"functions.cc/main.cc: CLI supportinginfo,calc <expr>(native C++ engine), andcalc-rs <expr>(Rust FFI engine).cc/calculator_test.cc: C++ unit test suite.Updated Rust CLI & Library (
rs/):rs/rust_calculator.rs: Rustcdylibexportingrs_calculator_eval,rs_calculator_add,rs_calculator_pow,rs_calculator_version.rs/main.rs: CLI supportingcalc-cpp <expr>(C++ FFI engine).rules_rustWindows GNU cdylib patch (patches/rules_rust_cdylib_windows.patch):rules_rust0.75.0 on Windows GNU targets:--codegen=link-arg=--entry=...for shared libraries (cdylib,dylib,proc-macro) when usingrust-lldon Windows GNU (DllMainCRTStartupon 64-bit x86/aarch64,_DllMainCRTStartup@12on 32-bit x86)..dll.libon MSVC,lib<name>.dll.aon GNU/gnullvm).toolchain.stdlib_linkflagsto downstream C++ targets when linking against acdylib, preventing duplicate symbol collisions (___chkstk_ms) between Rust and C++ standard runtimes.rules_rustunit tests to reflect the new behavior.Documentation:
README.mdwith CLI usage examples for both//cc:mainand//rs:main.Verification
prek runpassed with all hooks (keep-sorted,buildifier,rustfmt,clippy).bazel test --config=lint //...passed with 10/10 tests and 68 aspect checks passing.bazel run //cc:main -- calc-rs "(2 + 3) * 4 ^ 2"->80.000000(computed via Rust engine).bazel run //rs:main -- calc-cpp "(2 + 3) * 4 ^ 2"->80(computed via C++ engine).