From cdbabe7056f4616786d3ba085a7ff60a908968ff Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Mon, 5 Jan 2026 08:33:12 -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: 852313412 --- bazel/llvm.bzl | 2 +- .../cc_bindings_from_rust_rule.bzl | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/bazel/llvm.bzl b/bazel/llvm.bzl index 5e02879e9..735f11303 100644 --- a/bazel/llvm.bzl +++ b/bazel/llvm.bzl @@ -53,7 +53,7 @@ def _llvm_loader_repository(repository_ctx): executable = False, ) -LLVM_COMMIT_SHA = "11d9694b757b2e2c9f5169967fcc85f25f9a5645" +LLVM_COMMIT_SHA = "f5dab90875c4435890a19410fcb4bcd8a96357b2" def llvm_loader_repository_dependencies(): # This *declares* the dependency, but it won't actually be *downloaded* unless it's used. 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 c54f4f957..a8cf57a7c 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",