Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Use rust-lld for Bazel Windows MSVC Rust links (#40438)

felixxia-oai3470b807b437
diff --git a/MODULE.bazel b/MODULE.bazel
index 2fca2a60ab..1a2a7ef6a0 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -151,6 +151,7 @@ single_version_override(
patch_strip = 1,
patches = [
"//patches:rules_rs_build_script_deps_annotation.patch",
+ "//patches:rules_rs_windows_msvc_linker.patch",
],
version = "0.0.96",
)
@@ -161,8 +162,8 @@ rules_rust.patch(
# Carry the OpenAI setup fix that makes build-script tools available
# through their runfiles after the rules_rs upgrade.
"//patches:rules_rust_build_script_tools_transition.patch",
- # Keep direct Windows/MSVC links compatible with hermetic LLVM's
- # non-.lib runtime artifacts.
+ # Prefer Rust's direct linker for Windows/MSVC and keep hermetic
+ # LLVM's non-.lib runtime artifacts compatible with it.
"//patches:rules_rust_windows_msvc_direct_link_args.patch",
# Skip transient native-Windows linker outputs while consolidating
# dependency search paths.
diff --git a/patches/BUILD.bazel b/patches/BUILD.bazel
index eb1fe27b41..62386715f2 100644
--- a/patches/BUILD.bazel
+++ b/patches/BUILD.bazel
@@ -9,6 +9,7 @@ exports_files([
"rules_rust_windows_process_wrapper_skip_temp_outputs.patch",
"rules_cc_rusty_v8_custom_libcxx.patch",
"rules_rs_build_script_deps_annotation.patch",
+ "rules_rs_windows_msvc_linker.patch",
"rusty_v8_prebuilt_out_dir.patch",
"ring_windows_msvc_include_dirs.patch",
"v8_bazel_rules.patch",
diff --git a/patches/rules_rs_windows_msvc_linker.patch b/patches/rules_rs_windows_msvc_linker.patch
new file mode 100644
index 0000000000..66feb78569
--- /dev/null
+++ b/patches/rules_rs_windows_msvc_linker.patch
@@ -0,0 +1,14 @@
+# What: use Rust's bundled direct linker for Windows x86_64 MSVC toolchains.
+# Scope: standard and bootstrap rules_rs Rust toolchains targeting x86_64 MSVC.
+
+diff --git a/rs/toolchains/declare_rustc_toolchains.bzl b/rs/toolchains/declare_rustc_toolchains.bzl
+--- a/rs/toolchains/declare_rustc_toolchains.bzl
++++ b/rs/toolchains/declare_rustc_toolchains.bzl
+@@ -88,6 +88,7 @@ def declare_rustc_toolchains(
+ llvm_cov = "@llvm//tools:llvm-cov",
+ llvm_profdata = "@llvm//tools:llvm-profdata",
+ linker = select({
++ "@rules_rs//rs/platforms/config:x86_64-pc-windows-msvc": "{}rust-lld".format(rustc_repo_label),
+ "@platforms//cpu:wasm32": "{}rust-lld".format(rustc_repo_label),
+ "@platforms//cpu:wasm64": "{}rust-lld".format(rustc_repo_label),
+ "//conditions:default": None,
diff --git a/patches/rules_rust_windows_msvc_direct_link_args.patch b/patches/rules_rust_windows_msvc_direct_link_args.patch
index ad8160b1e2..aa5fb274e1 100644
--- a/patches/rules_rust_windows_msvc_direct_link_args.patch
+++ b/patches/rules_rust_windows_msvc_direct_link_args.patch
@@ -1,5 +1,18 @@
--- a/rust/private/rustc.bzl
+++ b/rust/private/rustc.bzl
+@@ -438,4 +438,11 @@
+ use_bpf_linker = toolchain.target_arch in ("bpfeb", "bpfel") and toolchain.linker
+- if not ld or toolchain.linker_preference == "rust" or use_bpf_linker:
++ use_windows_msvc_linker = (
++ toolchain.target_arch == "x86_64" and
++ toolchain.target_os == "windows" and
++ toolchain.target_abi == "msvc" and
++ toolchain.linker != None and
++ toolchain.linker_preference != "cc"
++ )
++ if not ld or toolchain.linker_preference == "rust" or use_bpf_linker or use_windows_msvc_linker:
+ ld = toolchain.linker.path
+ ld_is_direct_driver = toolchain.linker_type == "direct"
@@ -468,11 +468,25 @@
filtered_args.append(version)
# Keep library search path flags