From 09c364fed3a83b0c488d251af8f91ba008c92f21 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Thu, 16 Dec 2021 10:53:06 +0000 Subject: [PATCH] cargo_build_script: Populate LD and LDFLAGS (#1067) These are sometimes invoked by -sys crates. --- cargo/cargo_build_script.bzl | 4 +++- cargo/cargo_build_script_runner/bin.rs | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cargo/cargo_build_script.bzl b/cargo/cargo_build_script.bzl index 8e6f62f844..870916f24b 100644 --- a/cargo/cargo_build_script.bzl +++ b/cargo/cargo_build_script.bzl @@ -92,8 +92,10 @@ def _build_script_impl(ctx): # Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version). # We hope that the linker env is sufficient for the whole cc_toolchain. cc_toolchain, feature_configuration = find_cc_toolchain(ctx) - _, _, linker_env = get_linker_and_args(ctx, ctx.attr, cc_toolchain, feature_configuration, None) + linker, link_args, linker_env = get_linker_and_args(ctx, ctx.attr, cc_toolchain, feature_configuration, None) env.update(**linker_env) + env["LD"] = linker + env["LDFLAGS"] = " ".join(link_args) # MSVC requires INCLUDE to be set cc_env = get_cc_compile_env(cc_toolchain, feature_configuration) diff --git a/cargo/cargo_build_script_runner/bin.rs b/cargo/cargo_build_script_runner/bin.rs index 2d92293013..e99c102899 100644 --- a/cargo/cargo_build_script_runner/bin.rs +++ b/cargo/cargo_build_script_runner/bin.rs @@ -110,6 +110,10 @@ fn run_buildrs() -> Result<(), String> { } } + if let Some(ld_path) = env::var_os("LD") { + command.env("LD", exec_root.join(ld_path)); + } + // replace env vars with a ${pwd} prefix with the exec_root for (key, value) in env::vars() { let exec_root_str = exec_root.to_str().expect("exec_root not in utf8");