# What: compile exec-side Rust binaries against the exec Windows triple instead # of the lint target triple. # Why: Windows native argument-comment-lint keeps the repo target platform on # `windows-gnullvm` to preserve cfg coverage, but exec-side helper binaries # (build.rs, runners, bootstrap tools) must link as host tools. With # `toolchain_linker_preference=rust`, rules_rust was still feeding those exec # binaries the `windows-gnullvm` target/std path, which broke linking under the # native Bazel lint lane. diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -130,7 +130,21 @@ is_proc_macro_dep_enabled = rule( build_setting = config.bool(flag = True), ) -def _get_rustc_env(attr, toolchain, crate_name): +def _effective_target_arch(toolchain, use_exec_target): + return toolchain.exec_triple.arch if use_exec_target else toolchain.target_arch + +def _effective_target_os(toolchain, use_exec_target): + return toolchain.exec_triple.system if use_exec_target else toolchain.target_os + +def _effective_target_flag_value(toolchain, use_exec_target): + return toolchain.exec_triple.str if use_exec_target else toolchain.target_flag_value + +def _effective_rust_std_paths(toolchain, use_exec_target): + if use_exec_target: + return ["{}/lib/rustlib/{}/lib".format(toolchain.sysroot, toolchain.exec_triple.str)] + return toolchain.rust_std_paths + +def _get_rustc_env(attr, toolchain, crate_name, use_exec_target = False): """Gathers rustc environment variables Args: @@ -149,8 +163,8 @@ def _get_rustc_env(attr, toolchain, crate_name): pre = "" result = { - "CARGO_CFG_TARGET_ARCH": "" if toolchain.target_arch == None else toolchain.target_arch, - "CARGO_CFG_TARGET_OS": "" if toolchain.target_os == None else toolchain.target_os, + "CARGO_CFG_TARGET_ARCH": "" if _effective_target_arch(toolchain, use_exec_target) == None else _effective_target_arch(toolchain, use_exec_target), + "CARGO_CFG_TARGET_OS": "" if _effective_target_os(toolchain, use_exec_target) == None else _effective_target_os(toolchain, use_exec_target), "CARGO_CRATE_NAME": crate_name, "CARGO_PKG_AUTHORS": "", "CARGO_PKG_DESCRIPTION": "", @@ -1020,10 +1034,12 @@ def construct_arguments( if build_metadata and not use_json_output: fail("build_metadata requires parse_json_output") + use_exec_target = is_exec_configuration(ctx) and crate_info.type == "bin" + output_dir = getattr(crate_info.output, "dirname", None) linker_script = getattr(file, "linker_script", None) - env = _get_rustc_env(attr, toolchain, crate_info.name) + env = _get_rustc_env(attr, toolchain, crate_info.name, use_exec_target) # Wrapper args first process_wrapper_flags = ctx.actions.args() @@ -1164,14 +1180,14 @@ def construct_arguments( if error_format != "json": # Color is not compatible with json output. rustc_flags.add("--color=always") - rustc_flags.add(toolchain.target_flag_value, format = "--target=%s") + rustc_flags.add(_effective_target_flag_value(toolchain, use_exec_target), format = "--target=%s") if hasattr(attr, "crate_features"): rustc_flags.add_all(getattr(attr, "crate_features"), before_each = "--cfg", format_each = 'feature="%s"') if linker_script: rustc_flags.add(linker_script, format = "--codegen=link-arg=-T%s") # Tell Rustc where to find the standard library (or libcore) - rustc_flags.add_all(toolchain.rust_std_paths, before_each = "-L", format_each = "%s") + rustc_flags.add_all(_effective_rust_std_paths(toolchain, use_exec_target), before_each = "-L", format_each = "%s") - + rustc_flags.add_all( _extract_allowed_unstable_features_from_flags(rust_flags, all_allowed_unstable_features),