mirror of
https://github.com/openai/codex.git
synced 2026-10-19 16:17:49 +00:00
ci: run Windows argument-comment-lint via native Bazel (#16120)
## Why Follow-up to #16106. `argument-comment-lint` already runs as a native Bazel aspect on Linux and macOS, but Windows is still the long pole in `rust-ci`. To move Windows onto the same native Bazel lane, the toolchain split has to let exec-side helper binaries build in an MSVC environment while still linting repo crates as `windows-gnullvm`. Pushing the Windows lane onto the native Bazel path exposed a second round of Windows-only issues in the mixed exec-toolchain plumbing after the initial wrapper/target fixes landed. ## What Changed - keep the Windows lint lanes on the native Bazel/aspect path in `rust-ci.yml` and `rust-ci-full.yml` - add a dedicated `local_windows_msvc` platform for exec-side helper binaries while keeping `local_windows` as the `windows-gnullvm` target platform - patch `rules_rust` so `repository_set(...)` preserves explicit exec-platform constraints for the generated toolchains, keep the Windows-specific bootstrap/direct-link fixes needed for the nightly lint driver, and expose exec-side `rustc-dev` `.rlib`s to the MSVC sysroot - register the custom Windows nightly toolchain set with MSVC exec constraints while still exposing both `x86_64-pc-windows-msvc` and `x86_64-pc-windows-gnullvm` targets - enable `dev_components` on the custom Windows nightly repository set so the MSVC exec helper toolchain actually downloads the compiler-internal crates that `clippy_utils` needs - teach `run-argument-comment-lint-bazel.sh` to enumerate concrete Windows Rust rules, normalize the resulting labels, and skip explicitly requested incompatible targets instead of failing before the lint run starts - patch `rules_rust` build-script env propagation so exec-side `windows-msvc` helper crates drop forwarded MinGW include and linker search paths as whole flag/path pairs instead of emitting malformed `CFLAGS`, `CXXFLAGS`, and `LDFLAGS` - export the Windows VS/MSVC SDK environment in `setup-bazel-ci` and pass the relevant variables through `run-bazel-ci.sh` via `--action_env` / `--host_action_env` so Bazel build scripts can see the MSVC and UCRT headers on native Windows runs - add inline comments to the Windows `setup-bazel-ci` MSVC environment export step so it is easier to audit how `vswhere`, `VsDevCmd.bat`, and the filtered `GITHUB_ENV` export fit together - patch `aws-lc-sys` to skip its standalone `memcmp` probe under Bazel `windows-msvc` build-script environments, which avoids a Windows-native toolchain mismatch that blocked the lint lane before it reached the aspect execution - patch `aws-lc-sys` to prefer its bundled `prebuilt-nasm` objects for Bazel `windows-msvc` build-script runs, which avoids missing `generated-src/win-x86_64/*.asm` runfiles in the exec-side helper toolchain - annotate the Linux test-only callsites in `codex-rs/linux-sandbox` and `codex-rs/core` that the wider native lint coverage surfaced ## Patches This PR introduces a large patch stack because the Windows Bazel lint lane currently depends on behavior that upstream dependencies do not provide out of the box in the mixed `windows-gnullvm` target / `windows-msvc` exec-toolchain setup. - Most of the `rules_rust` patches look like upstream candidates rather than OpenAI-only policy. Preserving explicit exec-platform constraints, forwarding the right MSVC/UCRT environment into exec-side build scripts, exposing exec-side `rustc-dev` artifacts, and keeping the Windows bootstrap/linker behavior coherent all look like fixes to the Bazel/Rust integration layer itself. - The two `aws-lc-sys` patches are more tactical. They special-case Bazel `windows-msvc` build-script environments to avoid a `memcmp` probe mismatch and missing NASM runfiles. Those may be harder to upstream as-is because they rely on Bazel-specific detection instead of a general Cargo/build-script contract. - Short term, carrying these patches in-tree is reasonable because they unblock a real CI lane and are still narrow enough to audit. Long term, the goal should not be to keep growing a permanent local fork of either dependency. - My current expectation is that the `rules_rust` patches are less controversial and should be broken out into focused upstream proposals, while the `aws-lc-sys` patches are more likely to be temporary escape hatches unless that crate wants a more general hook for hermetic build systems. Suggested follow-up plan: 1. Split the `rules_rust` deltas into upstream-sized PRs or issues with minimized repros. 2. Revisit the `aws-lc-sys` patches during the next dependency bump and see whether they can be replaced by an upstream fix, a crate upgrade, or a cleaner opt-in mechanism. 3. Treat each dependency update as a chance to delete patches one by one so the local patch set only contains still-needed deltas. ## Verification - `./.github/scripts/run-argument-comment-lint-bazel.sh --config=argument-comment-lint --keep_going` - `RUNNER_OS=Windows ./.github/scripts/run-argument-comment-lint-bazel.sh --nobuild --config=argument-comment-lint --platforms=//:local_windows --keep_going` - `cargo test -p codex-linux-sandbox` - `cargo test -p codex-core shell_snapshot_tests` - `just argument-comment-lint` ## References - #16106
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
exports_files([
|
||||
"abseil_windows_gnullvm_thread_identity.patch",
|
||||
"aws-lc-sys_memcmp_check.patch",
|
||||
"aws-lc-sys_windows_msvc_prebuilt_nasm.patch",
|
||||
"aws-lc-sys_windows_msvc_memcmp_probe.patch",
|
||||
"llvm_windows_symlink_extract.patch",
|
||||
"rules_rust_windows_bootstrap_process_wrapper_linker.patch",
|
||||
"rules_rust_windows_exec_bin_target.patch",
|
||||
"rules_rust_windows_exec_std.patch",
|
||||
"rules_rust_repository_set_exec_constraints.patch",
|
||||
"rules_rust_windows_msvc_direct_link_args.patch",
|
||||
"rules_rust_windows_gnullvm_build_script.patch",
|
||||
"rules_rs_windows_gnullvm_exec.patch",
|
||||
"rusty_v8_prebuilt_out_dir.patch",
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
diff --git a/builder/cc_builder.rs b/builder/cc_builder.rs
|
||||
--- a/builder/cc_builder.rs
|
||||
+++ b/builder/cc_builder.rs
|
||||
@@ -667,12 +667,24 @@
|
||||
if cargo_env("HOST") != target() {
|
||||
return;
|
||||
}
|
||||
+
|
||||
+ let bazel_execroot = Self::bazel_execroot(self.manifest_dir.as_path());
|
||||
+ if bazel_execroot.is_some() && target().ends_with("windows-msvc") {
|
||||
+ // This probe guards a GCC memcmp miscompile. Under Bazel's
|
||||
+ // hermetic Windows/MSVC build-script toolchain we drive clang with
|
||||
+ // MinGW-flavored CFLAGS, which is good enough for compiling
|
||||
+ // aws-lc but not for linking and executing this standalone test
|
||||
+ // binary. Skip the probe in that configuration instead of failing
|
||||
+ // the whole build script.
|
||||
+ emit_warning("Skipping memcmp probe for Bazel windows-msvc build scripts.");
|
||||
+ return;
|
||||
+ }
|
||||
|
||||
let basename = "memcmp_invalid_stripped_check";
|
||||
let exec_path = out_dir().join(basename);
|
||||
let memcmp_build = cc::Build::default();
|
||||
let memcmp_compiler = memcmp_build.get_compiler();
|
||||
if !memcmp_compiler.is_like_clang() && !memcmp_compiler.is_like_gnu() {
|
||||
// The logic below assumes a Clang or GCC compiler is in use
|
||||
return;
|
||||
}
|
||||
@@ -701,7 +713,7 @@
|
||||
memcmp_compile_args.push(flag.into());
|
||||
}
|
||||
}
|
||||
|
||||
- if let Some(execroot) = Self::bazel_execroot(self.manifest_dir.as_path()) {
|
||||
+ if let Some(execroot) = bazel_execroot {
|
||||
// In Bazel build-script sandboxes, `cc` can pass `bazel-out/...` args
|
||||
// relative to the execroot while the process runs from elsewhere.
|
||||
// Normalize those args to absolute paths so this check can still link.
|
||||
@@ -0,0 +1,48 @@
|
||||
diff --git a/builder/main.rs b/builder/main.rs
|
||||
--- a/builder/main.rs
|
||||
+++ b/builder/main.rs
|
||||
@@ -721,16 +721,29 @@
|
||||
fn get_crate_cflags() -> Option<String> {
|
||||
optional_env_optional_crate_target("TARGET_CFLAGS")
|
||||
.or(optional_env_optional_crate_target("CFLAGS"))
|
||||
+}
|
||||
+
|
||||
+fn is_bazel_windows_msvc_build_script() -> bool {
|
||||
+ if !target().ends_with("windows-msvc") {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ let cargo_manifest_dir = cargo_env("CARGO_MANIFEST_DIR");
|
||||
+ let manifest_dir = Path::new(&cargo_manifest_dir);
|
||||
+ manifest_dir
|
||||
+ .components()
|
||||
+ .any(|component| component.as_os_str() == "bazel-out")
|
||||
}
|
||||
|
||||
fn use_prebuilt_nasm() -> bool {
|
||||
+ let use_prebuilt_for_bazel_windows_msvc = is_bazel_windows_msvc_build_script();
|
||||
target_os() == "windows"
|
||||
&& target_arch() == "x86_64"
|
||||
&& !is_no_asm()
|
||||
- && !test_nasm_command() // NASM not found in environment
|
||||
&& Some(false) != allow_prebuilt_nasm() // not prevented by environment
|
||||
&& !is_disable_prebuilt_nasm() // not prevented by feature
|
||||
// permitted by environment or by feature
|
||||
+ && (use_prebuilt_for_bazel_windows_msvc || !test_nasm_command())
|
||||
&& (Some(true) == allow_prebuilt_nasm() || is_prebuilt_nasm())
|
||||
}
|
||||
|
||||
fn allow_prebuilt_nasm() -> Option<bool> {
|
||||
diff --git a/builder/nasm_builder.rs b/builder/nasm_builder.rs
|
||||
--- a/builder/nasm_builder.rs
|
||||
+++ b/builder/nasm_builder.rs
|
||||
@@ -57,7 +57,7 @@
|
||||
if self.files.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
- if test_nasm_command() {
|
||||
+ if test_nasm_command() && !use_prebuilt_nasm() {
|
||||
for src in &self.files {
|
||||
let obj_name = src
|
||||
.file_name()
|
||||
@@ -0,0 +1,26 @@
|
||||
# What: let `rules_rust` repository_set entries specify an explicit exec-platform
|
||||
# constraint set.
|
||||
# Why: codex needs Windows nightly lint toolchains to run helper binaries on an
|
||||
# MSVC exec platform while still targeting `windows-gnullvm` crates.
|
||||
|
||||
diff --git a/rust/extensions.bzl b/rust/extensions.bzl
|
||||
--- a/rust/extensions.bzl
|
||||
+++ b/rust/extensions.bzl
|
||||
@@ -52,6 +52,7 @@ def _rust_impl(module_ctx):
|
||||
"allocator_library": repository_set.allocator_library,
|
||||
"dev_components": repository_set.dev_components,
|
||||
"edition": repository_set.edition,
|
||||
+ "exec_compatible_with": [str(v) for v in repository_set.exec_compatible_with] if repository_set.exec_compatible_with else None,
|
||||
"exec_triple": repository_set.exec_triple,
|
||||
"extra_target_triples": {repository_set.target_triple: [str(v) for v in repository_set.target_compatible_with]},
|
||||
"name": repository_set.name,
|
||||
@@ -166,6 +167,9 @@ _COMMON_TAG_KWARGS = {
|
||||
|
||||
_RUST_REPOSITORY_SET_TAG_ATTRS = {
|
||||
+ "exec_compatible_with": attr.label_list(
|
||||
+ doc = "Execution platform constraints for this repository_set.",
|
||||
+ ),
|
||||
"exec_triple": attr.string(
|
||||
doc = "Exec triple for this repository_set.",
|
||||
),
|
||||
"name": attr.string(
|
||||
@@ -0,0 +1,23 @@
|
||||
--- a/rust/private/rustc.bzl
|
||||
+++ b/rust/private/rustc.bzl
|
||||
@@ -472,7 +472,19 @@
|
||||
)
|
||||
ld_is_direct_driver = False
|
||||
|
||||
- if not ld or toolchain.linker_preference == "rust":
|
||||
+ # The bootstrap process wrapper is built without the normal rules_rust
|
||||
+ # process wrapper. On Windows nightly toolchains that expose rust-lld, the
|
||||
+ # C++ toolchain path currently resolves to clang++ while still emitting
|
||||
+ # MSVC-style arguments, so prefer rust-lld for this one bootstrap binary
|
||||
+ # instead of switching all Rust actions over.
|
||||
+ use_bootstrap_rust_linker = (
|
||||
+ toolchain.target_os.startswith("windows") and
|
||||
+ toolchain.linker != None and
|
||||
+ hasattr(ctx.executable, "_bootstrap_process_wrapper") and
|
||||
+ not ctx.executable._process_wrapper
|
||||
+ )
|
||||
+
|
||||
+ if not ld or toolchain.linker_preference == "rust" or use_bootstrap_rust_linker:
|
||||
ld = toolchain.linker.path
|
||||
ld_is_direct_driver = toolchain.linker_type == "direct"
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
# 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
|
||||
@@ -129,6 +129,20 @@
|
||||
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
|
||||
|
||||
@@ -147,6 +161,6 @@
|
||||
|
||||
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": "",
|
||||
@@ -997,9 +1011,11 @@
|
||||
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
|
||||
@@ -1138,5 +1154,5 @@
|
||||
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"):
|
||||
@@ -1144,6 +1160,6 @@
|
||||
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(rust_flags, map_each = map_flag)
|
||||
@@ -0,0 +1,111 @@
|
||||
diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl
|
||||
--- a/cargo/private/cargo_build_script.bzl
|
||||
+++ b/cargo/private/cargo_build_script.bzl
|
||||
@@ -142,40 +142,82 @@ def _strip_stack_protector_for_windows_llvm_mingw(toolchain, args):
|
||||
def _rewrite_windows_exec_msvc_cc_args(toolchain, args):
|
||||
"""Translate GNU-flavored cc args when exec-side build scripts target Windows MSVC."""
|
||||
if toolchain.target_flag_value != toolchain.exec_triple.str or not toolchain.exec_triple.str.endswith("-pc-windows-msvc"):
|
||||
return args
|
||||
|
||||
- rewritten = []
|
||||
- skip_next = False
|
||||
- for arg in args:
|
||||
- if skip_next:
|
||||
- skip_next = False
|
||||
- continue
|
||||
+ rewritten = [
|
||||
+ "-target",
|
||||
+ toolchain.target_flag_value,
|
||||
+ ]
|
||||
+ skip_next = False
|
||||
+ for index in range(len(args)):
|
||||
+ arg = args[index]
|
||||
+
|
||||
+ if skip_next:
|
||||
+ skip_next = False
|
||||
+ continue
|
||||
|
||||
if arg == "-target":
|
||||
- skip_next = True
|
||||
+ skip_next = True
|
||||
continue
|
||||
|
||||
if arg.startswith("-target=") or arg.startswith("--target="):
|
||||
continue
|
||||
|
||||
if arg == "-nostdlibinc" or arg.startswith("--sysroot"):
|
||||
continue
|
||||
|
||||
- if "mingw-w64-" in arg or "mingw_import_libraries_directory" in arg or "mingw_crt_library_search_directory" in arg:
|
||||
+ if arg.startswith("-fstack-protector") or arg.startswith("-D_FORTIFY_SOURCE="):
|
||||
continue
|
||||
|
||||
- if arg.startswith("-fstack-protector"):
|
||||
- continue
|
||||
-
|
||||
- if arg.startswith("-D_FORTIFY_SOURCE="):
|
||||
- continue
|
||||
+ if arg == "-isystem" and index + 1 < len(args):
|
||||
+ path = args[index + 1]
|
||||
+ if "mingw-w64-" in path or "mingw_import_libraries_directory" in path or "mingw_crt_library_search_directory" in path:
|
||||
+ skip_next = True
|
||||
+ continue
|
||||
|
||||
rewritten.append(arg)
|
||||
|
||||
- return [
|
||||
- "-target",
|
||||
- toolchain.target_flag_value,
|
||||
- ] + rewritten
|
||||
+ return rewritten
|
||||
+
|
||||
+def _rewrite_windows_exec_msvc_link_args(toolchain, args):
|
||||
+ """Translate GNU-flavored link args when exec-side build scripts target Windows MSVC."""
|
||||
+ if toolchain.target_flag_value != toolchain.exec_triple.str or not toolchain.exec_triple.str.endswith("-pc-windows-msvc"):
|
||||
+ return args
|
||||
+
|
||||
+ rewritten = []
|
||||
+ skip_next = False
|
||||
+ for index in range(len(args)):
|
||||
+ arg = args[index]
|
||||
+
|
||||
+ if skip_next:
|
||||
+ skip_next = False
|
||||
+ continue
|
||||
+
|
||||
+ if arg == "--sysroot":
|
||||
+ skip_next = True
|
||||
+ continue
|
||||
+
|
||||
+ if arg.startswith("--sysroot="):
|
||||
+ continue
|
||||
+
|
||||
+ if arg == "-L" and index + 1 < len(args):
|
||||
+ path = args[index + 1]
|
||||
+ if "mingw_import_libraries_directory" in path or "mingw_crt_library_search_directory" in path:
|
||||
+ skip_next = True
|
||||
+ continue
|
||||
+ rewritten.extend([arg, path])
|
||||
+ skip_next = True
|
||||
+ continue
|
||||
+
|
||||
+ if arg.startswith("-L") and (
|
||||
+ "mingw_import_libraries_directory" in arg or
|
||||
+ "mingw_crt_library_search_directory" in arg
|
||||
+ ):
|
||||
+ continue
|
||||
+
|
||||
+ rewritten.append(arg)
|
||||
+
|
||||
+ return rewritten
|
||||
|
||||
def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
|
||||
"""Gather cc environment variables from the given `cc_toolchain`
|
||||
@@ -509,6 +550,7 @@ def _construct_build_script_env(
|
||||
linker, _, link_args, linker_env = get_linker_and_args(ctx, "bin", toolchain, cc_toolchain, feature_configuration, None)
|
||||
env.update(**linker_env)
|
||||
env["LD"] = linker
|
||||
+ link_args = _rewrite_windows_exec_msvc_link_args(toolchain, link_args)
|
||||
env["LDFLAGS"] = " ".join(_pwd_flags(link_args))
|
||||
|
||||
# Defaults for cxx flags.
|
||||
@@ -0,0 +1,11 @@
|
||||
diff --git a/rust/private/repository_utils.bzl b/rust/private/repository_utils.bzl
|
||||
--- a/rust/private/repository_utils.bzl
|
||||
+++ b/rust/private/repository_utils.bzl
|
||||
@@ -53,6 +53,7 @@ filegroup(
|
||||
"lib/*{dylib_ext}*",
|
||||
"lib/rustlib/{target_triple}/codegen-backends/*{dylib_ext}",
|
||||
"lib/rustlib/{target_triple}/lib/*{dylib_ext}*",
|
||||
+ "lib/rustlib/{target_triple}/lib/*.rlib",
|
||||
"lib/rustlib/{target_triple}/lib/*.rmeta",
|
||||
],
|
||||
allow_empty = True,
|
||||
@@ -0,0 +1,181 @@
|
||||
# What: expose an exec-side Rust standard library alongside the target stdlib.
|
||||
# Why: mixed Windows toolchains compile repo crates for `windows-gnullvm`, but
|
||||
# exec-side helper binaries (build.rs, runners) may need the host MSVC stdlib.
|
||||
# The toolchain sysroot must therefore carry both stdlib trees so rustc can
|
||||
# resolve the correct one for each `--target`.
|
||||
|
||||
diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl
|
||||
--- a/rust/toolchain.bzl
|
||||
+++ b/rust/toolchain.bzl
|
||||
@@ -209,6 +209,7 @@ def _generate_sysroot(
|
||||
clippy = None,
|
||||
cargo_clippy = None,
|
||||
llvm_tools = None,
|
||||
+ exec_rust_std = None,
|
||||
rust_std = None,
|
||||
rustfmt = None,
|
||||
linker = None):
|
||||
@@ -312,7 +313,15 @@ def _generate_sysroot(
|
||||
|
||||
# Made available to support $(location) expansion in stdlib_linkflags and extra_rustc_flags.
|
||||
transitive_file_sets.append(depset(ctx.files.rust_std))
|
||||
+
|
||||
+ sysroot_exec_rust_std = None
|
||||
+ if exec_rust_std:
|
||||
+ sysroot_exec_rust_std = _symlink_sysroot_tree(ctx, name, exec_rust_std)
|
||||
+ transitive_file_sets.extend([sysroot_exec_rust_std])
|
||||
|
||||
+ # Made available to support $(location) expansion in extra_exec_rustc_flags.
|
||||
+ transitive_file_sets.append(depset(ctx.files.exec_rust_std))
|
||||
+
|
||||
# Declare a file in the root of the sysroot to make locating the sysroot easy
|
||||
sysroot_anchor = ctx.actions.declare_file("{}/rust.sysroot".format(name))
|
||||
ctx.actions.write(
|
||||
@@ -323,6 +332,7 @@ def _generate_sysroot(
|
||||
"cargo-clippy: {}".format(cargo_clippy),
|
||||
"linker: {}".format(linker),
|
||||
"llvm_tools: {}".format(llvm_tools),
|
||||
+ "exec_rust_std: {}".format(exec_rust_std),
|
||||
"rust_std: {}".format(rust_std),
|
||||
"rustc_lib: {}".format(rustc_lib),
|
||||
"rustc: {}".format(rustc),
|
||||
@@ -340,6 +350,7 @@ def _generate_sysroot(
|
||||
cargo_clippy = sysroot_cargo_clippy,
|
||||
clippy = sysroot_clippy,
|
||||
linker = sysroot_linker,
|
||||
+ exec_rust_std = sysroot_exec_rust_std,
|
||||
rust_std = sysroot_rust_std,
|
||||
rustc = sysroot_rustc,
|
||||
rustc_lib = sysroot_rustc_lib,
|
||||
@@ -410,12 +421,14 @@ def _rust_toolchain_impl(ctx):
|
||||
)
|
||||
|
||||
rust_std = ctx.attr.rust_std
|
||||
+ exec_rust_std = ctx.attr.exec_rust_std if ctx.attr.exec_rust_std else rust_std
|
||||
|
||||
sysroot = _generate_sysroot(
|
||||
ctx = ctx,
|
||||
rustc = ctx.file.rustc,
|
||||
rustdoc = ctx.file.rust_doc,
|
||||
rustc_lib = ctx.attr.rustc_lib,
|
||||
+ exec_rust_std = exec_rust_std,
|
||||
rust_std = rust_std,
|
||||
rustfmt = ctx.file.rustfmt,
|
||||
clippy = ctx.file.clippy_driver,
|
||||
@@ -452,7 +465,7 @@ def _rust_toolchain_impl(ctx):
|
||||
|
||||
expanded_stdlib_linkflags = _expand_flags(ctx, "stdlib_linkflags", rust_std[rust_common.stdlib_info].srcs, make_variables)
|
||||
expanded_extra_rustc_flags = _expand_flags(ctx, "extra_rustc_flags", rust_std[rust_common.stdlib_info].srcs, make_variables)
|
||||
- expanded_extra_exec_rustc_flags = _expand_flags(ctx, "extra_exec_rustc_flags", rust_std[rust_common.stdlib_info].srcs, make_variables)
|
||||
+ expanded_extra_exec_rustc_flags = _expand_flags(ctx, "extra_exec_rustc_flags", exec_rust_std[rust_common.stdlib_info].srcs, make_variables)
|
||||
|
||||
linking_context = cc_common.create_linking_context(
|
||||
linker_inputs = depset([
|
||||
@@ -793,6 +806,10 @@ rust_toolchain = rule(
|
||||
doc = "The Rust standard library.",
|
||||
mandatory = True,
|
||||
),
|
||||
+ "exec_rust_std": attr.label(
|
||||
+ doc = "Optional Rust standard library for exec-configuration Rust tools. Defaults to rust_std.",
|
||||
+ mandatory = False,
|
||||
+ ),
|
||||
"rustc": attr.label(
|
||||
doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item.",
|
||||
allow_single_file = True,
|
||||
diff --git a/rust/private/repository_utils.bzl b/rust/private/repository_utils.bzl
|
||||
--- a/rust/private/repository_utils.bzl
|
||||
+++ b/rust/private/repository_utils.bzl
|
||||
@@ -341,6 +341,7 @@ rust_toolchain(
|
||||
name = "{toolchain_name}",
|
||||
rust_doc = "//:rustdoc",
|
||||
rust_std = "//:rust_std-{target_triple}",
|
||||
+ exec_rust_std = {exec_rust_std_label},
|
||||
rustc = "//:rustc",
|
||||
linker = {linker_label},
|
||||
linker_type = {linker_type},
|
||||
@@ -384,6 +385,7 @@ def BUILD_for_rust_toolchain(
|
||||
include_llvm_tools,
|
||||
include_linker,
|
||||
include_objcopy = False,
|
||||
+ exec_rust_std_label = None,
|
||||
stdlib_linkflags = None,
|
||||
extra_rustc_flags = None,
|
||||
extra_exec_rustc_flags = None,
|
||||
@@ -405,6 +407,7 @@ def BUILD_for_rust_toolchain(
|
||||
include_llvm_tools (bool): Whether llvm-tools are present in the toolchain.
|
||||
include_linker (bool): Whether a linker is available in the toolchain.
|
||||
include_objcopy (bool): Whether rust-objcopy is available in the toolchain.
|
||||
+ exec_rust_std_label (str, optional): Label for an exec-side stdlib when it differs from rust_std.
|
||||
stdlib_linkflags (list, optional): Overridden flags needed for linking to rust
|
||||
stdlib, akin to BAZEL_LINKLIBS. Defaults to
|
||||
None.
|
||||
@@ -453,6 +456,7 @@ def BUILD_for_rust_toolchain(
|
||||
staticlib_ext = system_to_staticlib_ext(target_triple.system),
|
||||
dylib_ext = system_to_dylib_ext(target_triple.system),
|
||||
allocator_library = repr(allocator_library_label),
|
||||
+ exec_rust_std_label = repr(exec_rust_std_label),
|
||||
global_allocator_library = repr(global_allocator_library_label),
|
||||
stdlib_linkflags = stdlib_linkflags,
|
||||
default_edition = default_edition,
|
||||
diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl
|
||||
--- a/rust/private/rustc.bzl
|
||||
+++ b/rust/private/rustc.bzl
|
||||
@@ -1011,7 +1011,10 @@ 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"
|
||||
+ # Exec-configuration crates (build scripts, proc-macros, and their
|
||||
+ # dependencies) must all target the exec triple so they can link against
|
||||
+ # each other and the exec-side standard library.
|
||||
+ use_exec_target = is_exec_configuration(ctx)
|
||||
|
||||
output_dir = getattr(crate_info.output, "dirname", None)
|
||||
linker_script = getattr(file, "linker_script", None)
|
||||
diff --git a/rust/repositories.bzl b/rust/repositories.bzl
|
||||
--- a/rust/repositories.bzl
|
||||
+++ b/rust/repositories.bzl
|
||||
@@ -536,6 +536,18 @@ def _rust_toolchain_tools_repository_impl(ctx):
|
||||
build_components.append(rust_stdlib_content)
|
||||
sha256s.update(rust_stdlib_sha256)
|
||||
|
||||
+ exec_rust_std_label = None
|
||||
+ if exec_triple.str != target_triple.str:
|
||||
+ exec_rust_stdlib_content, exec_rust_stdlib_sha256 = load_rust_stdlib(
|
||||
+ ctx = ctx,
|
||||
+ target_triple = exec_triple,
|
||||
+ version = version,
|
||||
+ iso_date = iso_date,
|
||||
+ )
|
||||
+ build_components.append(exec_rust_stdlib_content)
|
||||
+ sha256s.update(exec_rust_stdlib_sha256)
|
||||
+ exec_rust_std_label = "//:rust_std-{}".format(exec_triple.str)
|
||||
+
|
||||
stdlib_linkflags = None
|
||||
if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx.os.environ:
|
||||
stdlib_linkflags = ctx.os.environ["BAZEL_RUST_STDLIB_LINKFLAGS"].split(":")
|
||||
@@ -552,6 +564,7 @@ def _rust_toolchain_tools_repository_impl(ctx):
|
||||
include_llvm_tools = include_llvm_tools,
|
||||
include_linker = include_linker,
|
||||
include_objcopy = include_objcopy,
|
||||
+ exec_rust_std_label = exec_rust_std_label,
|
||||
extra_rustc_flags = ctx.attr.extra_rustc_flags,
|
||||
extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
|
||||
opt_level = ctx.attr.opt_level if ctx.attr.opt_level else None,
|
||||
@@ -575,8 +588,16 @@ def _rust_toolchain_tools_repository_impl(ctx):
|
||||
if ctx.attr.dev_components:
|
||||
rustc_dev_sha256 = load_rustc_dev_nightly(
|
||||
ctx = ctx,
|
||||
target_triple = target_triple,
|
||||
version = version,
|
||||
iso_date = iso_date,
|
||||
)
|
||||
sha256s.update(rustc_dev_sha256)
|
||||
+ if exec_triple.str != target_triple.str:
|
||||
+ exec_rustc_dev_sha256 = load_rustc_dev_nightly(
|
||||
+ ctx = ctx,
|
||||
+ target_triple = exec_triple,
|
||||
+ version = version,
|
||||
+ iso_date = iso_date,
|
||||
+ )
|
||||
+ sha256s.update(exec_rustc_dev_sha256)
|
||||
@@ -1,7 +1,7 @@
|
||||
diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl
|
||||
--- a/cargo/private/cargo_build_script.bzl
|
||||
+++ b/cargo/private/cargo_build_script.bzl
|
||||
@@ -120,6 +120,25 @@
|
||||
@@ -120,6 +120,63 @@
|
||||
executable = True,
|
||||
)
|
||||
|
||||
@@ -23,16 +23,56 @@ diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_sc
|
||||
+ # flags through CFLAGS/CXXFLAGS breaks build.rs probe binaries compiled via
|
||||
+ # cc-rs.
|
||||
+ return [arg for arg in args if not arg.startswith("-fstack-protector")]
|
||||
+
|
||||
+def _rewrite_windows_exec_msvc_cc_args(toolchain, args):
|
||||
+ """Translate GNU-flavored cc args when exec-side build scripts target Windows MSVC."""
|
||||
+ if toolchain.target_flag_value != toolchain.exec_triple.str or not toolchain.exec_triple.str.endswith("-pc-windows-msvc"):
|
||||
+ return args
|
||||
+
|
||||
+ rewritten = []
|
||||
+ skip_next = False
|
||||
+ for arg in args:
|
||||
+ if skip_next:
|
||||
+ skip_next = False
|
||||
+ continue
|
||||
+
|
||||
+ if arg == "-target":
|
||||
+ skip_next = True
|
||||
+ continue
|
||||
+
|
||||
+ if arg.startswith("-target=") or arg.startswith("--target="):
|
||||
+ continue
|
||||
+
|
||||
+ if arg == "-nostdlibinc" or arg.startswith("--sysroot"):
|
||||
+ continue
|
||||
+
|
||||
+ if "mingw-w64-" in arg or "mingw_import_libraries_directory" in arg or "mingw_crt_library_search_directory" in arg:
|
||||
+ continue
|
||||
+
|
||||
+ if arg.startswith("-fstack-protector"):
|
||||
+ continue
|
||||
+
|
||||
+ if arg.startswith("-D_FORTIFY_SOURCE="):
|
||||
+ continue
|
||||
+
|
||||
+ rewritten.append(arg)
|
||||
+
|
||||
+ return [
|
||||
+ "-target",
|
||||
+ toolchain.target_flag_value,
|
||||
+ ] + rewritten
|
||||
+
|
||||
def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
|
||||
"""Gather cc environment variables from the given `cc_toolchain`
|
||||
|
||||
@@ -503,6 +522,8 @@
|
||||
@@ -503,6 +560,10 @@
|
||||
if not env["AR"]:
|
||||
env["AR"] = cc_toolchain.ar_executable
|
||||
|
||||
+ cc_c_args = _strip_stack_protector_for_windows_llvm_mingw(toolchain, cc_c_args)
|
||||
+ cc_cxx_args = _strip_stack_protector_for_windows_llvm_mingw(toolchain, cc_cxx_args)
|
||||
+ cc_c_args = _rewrite_windows_exec_msvc_cc_args(toolchain, cc_c_args)
|
||||
+ cc_cxx_args = _rewrite_windows_exec_msvc_cc_args(toolchain, cc_cxx_args)
|
||||
# Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular
|
||||
# to determine the deployment target when building for apple platforms (`macosx-version-min`
|
||||
# for example, itself derived from the `macos_minimum_os` Bazel argument).
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
--- a/rust/private/rustc.bzl
|
||||
+++ b/rust/private/rustc.bzl
|
||||
@@ -2305,7 +2305,7 @@
|
||||
return crate.metadata.dirname
|
||||
return crate.output.dirname
|
||||
|
||||
-def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False, flavor_msvc = False):
|
||||
+def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False, flavor_msvc = False, use_direct_driver = False):
|
||||
artifact = get_preferred_artifact(lib, use_pic)
|
||||
if ambiguous_libs and artifact.path in ambiguous_libs:
|
||||
artifact = ambiguous_libs[artifact.path]
|
||||
@@ -2344,6 +2344,11 @@
|
||||
artifact.basename.startswith("test-") or artifact.basename.startswith("std-")
|
||||
):
|
||||
return [] if for_darwin else ["-lstatic=%s" % get_lib_name(artifact)]
|
||||
+
|
||||
+ if for_windows and use_direct_driver and not artifact.basename.endswith(".lib"):
|
||||
+ return [
|
||||
+ "-Clink-arg={}".format(artifact.path),
|
||||
+ ]
|
||||
|
||||
if flavor_msvc:
|
||||
return [
|
||||
@@ -2381,7 +2386,7 @@
|
||||
])
|
||||
elif include_link_flags:
|
||||
get_lib_name = get_lib_name_for_windows if flavor_msvc else get_lib_name_default
|
||||
- ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, flavor_msvc = flavor_msvc))
|
||||
+ ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, flavor_msvc = flavor_msvc, use_direct_driver = use_direct_driver))
|
||||
|
||||
# Windows toolchains can inherit POSIX defaults like -pthread from C deps,
|
||||
# which fails to link with the MinGW/LLD toolchain. Drop them here.
|
||||
@@ -2558,17 +2563,25 @@
|
||||
else:
|
||||
# For all other crate types we want to link C++ runtime library statically
|
||||
# (for example libstdc++.a or libc++.a).
|
||||
+ runtime_libs = cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration)
|
||||
args.add_all(
|
||||
- cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration),
|
||||
+ runtime_libs,
|
||||
map_each = _get_dirname,
|
||||
format_each = "-Lnative=%s",
|
||||
)
|
||||
if include_link_flags:
|
||||
- args.add_all(
|
||||
- cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration),
|
||||
- map_each = get_lib_name,
|
||||
- format_each = "-lstatic=%s",
|
||||
- )
|
||||
+ if toolchain.target_os == "windows" and use_direct_link_driver:
|
||||
+ for runtime_lib in runtime_libs.to_list():
|
||||
+ if runtime_lib.basename.endswith(".lib"):
|
||||
+ args.add(get_lib_name(runtime_lib), format = "-lstatic=%s")
|
||||
+ else:
|
||||
+ args.add(runtime_lib.path, format = "--codegen=link-arg=%s")
|
||||
+ else:
|
||||
+ args.add_all(
|
||||
+ runtime_libs,
|
||||
+ map_each = get_lib_name,
|
||||
+ format_each = "-lstatic=%s",
|
||||
+ )
|
||||
|
||||
def _get_dirname(file):
|
||||
"""A helper function for `_add_native_link_flags`.
|
||||
Reference in New Issue
Block a user