Skip to content

Add bidirectional C++ and Rust interop with rules_rust cdylib support - #32

Open
justhecuke wants to merge 19 commits into
mainfrom
add-cpp-rust-interop
Open

justhecuke wants to merge 19 commits into
mainfrom
add-cpp-rust-interop

Conversation

@justhecuke

Copy link
Copy Markdown
Owner

Description

This PR implements bidirectional C++ and Rust interop along with upstream-ready rules_rust patches to support rust_shared_library (cdylib) on Windows GNU targets.

Summary of Changes

  1. Rust calling C++:

    • //rs:cpp_calculator links //cc:calculator via link_deps and invokes cc_calculator_* C-ABI functions hermetically using rust-lld.
    • Covered by unit test //rs:cpp_calculator_test.
  2. C++ calling Rust:

    • //cc:rust_calculator links //rs:rust_calculator (rust_shared_library / cdylib) and calls rs_calculator_* C-ABI functions via the MinGW import library (librust_calculator.dll.a).
    • Covered by unit test //cc:rust_calculator_test.
  3. New C++ Engine & CLI (cc/):

    • cc/calculator.hh & cc/calculator.cc: Native C++ math expression parser and evaluator compiled with -fno-exceptions -fno-rtti and exposing extern "C" functions.
    • cc/main.cc: CLI supporting info, calc <expr> (native C++ engine), and calc-rs <expr> (Rust FFI engine).
    • cc/calculator_test.cc: C++ unit test suite.
  4. Updated Rust CLI & Library (rs/):

    • rs/rust_calculator.rs: Rust cdylib exporting rs_calculator_eval, rs_calculator_add, rs_calculator_pow, rs_calculator_version.
    • rs/main.rs: CLI supporting calc-cpp <expr> (C++ FFI engine).
  5. rules_rust Windows GNU cdylib patch (patches/rules_rust_cdylib_windows.patch):

    • Addresses linker and import library declaration issues in rules_rust 0.75.0 on Windows GNU targets:
      • Adds --codegen=link-arg=--entry=... for shared libraries (cdylib, dylib, proc-macro) when using rust-lld on Windows GNU (DllMainCRTStartup on 64-bit x86/aarch64, _DllMainCRTStartup@12 on 32-bit x86).
      • Declares correct import library filename based on target ABI (.dll.lib on MSVC, lib<name>.dll.a on GNU/gnullvm).
      • Suppresses propagating transitive toolchain.stdlib_linkflags to downstream C++ targets when linking against a cdylib, preventing duplicate symbol collisions (___chkstk_ms) between Rust and C++ standard runtimes.
      • Updates existing rules_rust unit tests to reflect the new behavior.
  6. Documentation:

    • Updated README.md with CLI usage examples for both //cc:main and //rs:main.

Verification

  • prek run passed 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).

justhecuke and others added 18 commits November 27, 2026 11:33
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.
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants