From d588587ff6b13f7d72657f66709ca4341849e7ff Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Wed, 17 Dec 2025 10:48:13 -0800 Subject: [PATCH] Set LD_LIBRARY_PATH to point at rustc's library directory. This unblocks dynamically linking aginst librustc_driver in the bazel build of cc_bindings_from_rs. PiperOrigin-RevId: 845832583 --- .../cc_bindings_from_rust_rule.bzl | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl b/cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl index 8ce511ea0..69b8b0755 100644 --- a/cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl +++ b/cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl @@ -103,6 +103,29 @@ def _target_name_to_include_guard(target): for c in (target.label.package + "/" + target.label.name).upper().elems() ]) +def _rustc_lib_env(ctx): + """Returns an environment that sets the dylib search path to include rustc libraries. + + This is needed in bazel where rustc_private dynamically links against librustc_driver. + Internally, we statically link against the compiler so this isn't an issue. If the path to + rustc's libraries cannot be determined this returns an empty dictionary. + + Args: + ctx: The rule context. + + Returns: + A dictionary of environment variables to set. + """ + rust_toolchain = ctx.toolchains["@rules_rust//rust:toolchain_type"] + if rust_toolchain == None: + return {} + rustc_lib = rust_toolchain.rustc_lib.to_list() + if len(rustc_lib) <= 0: + return {} + return { + "LD_LIBRARY_PATH": rustc_lib[0].dirname, + } + def _generate_bindings(ctx, target, basename, inputs, args, rustc_env, proto_crate_renames): """Invokes the `cc_bindings_from_rs` tool to generate C++ bindings for a Rust crate. @@ -188,7 +211,7 @@ def _generate_bindings(ctx, target, basename, inputs, args, rustc_env, proto_cra [ctx.file._clang_format, ctx.file._rustfmt, ctx.file._rustfmt_cfg], transitive = [inputs], ), - env = rustc_env | verbose_log_env, + env = rustc_env | verbose_log_env | _rustc_lib_env(ctx), tools = [toolchain.binary], executable = ctx.executable._process_wrapper, mnemonic = "CcBindingsFromRust",