Skip to content

Set the Apple deployment target for rustc from the linker args - #4282

Merged
UebelAndre merged 3 commits into
bazelbuild:mainfrom
drakeo338:claude/4149-fix
Sep 24, 2026
Merged

UebelAndre merged 3 commits into
bazelbuild:mainfrom
drakeo338:claude/4149-fix

Conversation

@drakeo338

Copy link
Copy Markdown
Contributor

Fixes #4149.

Following keith's suggestion on the issue: when the link args carry an Apple -target triple with an OS version, rustc links now get the matching *_DEPLOYMENT_TARGET env var, so rustc's -m<os>-version-min agrees with the triple and clang stops warning. A value from --action_env, rustc_env or the toolchain's env still wins, and non-Apple targets are untouched.

Tests: a unit test for the triple parser, plus analysis tests for the linkopt case and the precedence rules. The two linkopt tests skip on macOS, because apple_support's toolchain appends its own -target after user linkopts there, so the last triple is the toolchain's. I'd be glad to swap that for a macOS-specific assertion if you prefer.

Verified locally on Linux with bazelisk: 20 passed, 2 unrelated skips. I don't have a Mac, so macOS coverage comes from CI.

An Apple cc_toolchain passes its deployment target to clang inside the
`-target` triple, e.g. `arm64-apple-macosx26.0`, which rules_rust
forwards to rustc as link args. rustc does not read those: it takes the
deployment target from `MACOSX_DEPLOYMENT_TARGET` (or the per-OS
sibling) and otherwise falls back to its own default, which it passes
to the linker as `-mmacosx-version-min=11.0.0`. clang sees both, and
since Rust 1.97 surfaces linker stderr every link prints
`overriding '-mmacosx-version-min=11.0.0' option with '-target ...'`.

Derive the variable from the forwarded link args and set it in the
Rustc action's environment: `-target`/`--target=` triples for macos,
ios (including simulator and Mac Catalyst), tvos, watchos and
xros/visionos, or `-m<os>-version-min=` when no triple carries a
version. The link args are still forwarded unchanged, and a value the
user sets through `--action_env`, `rustc_env` or the toolchain's `env`
is kept.

Fixes bazelbuild#4149

Assisted-by: Claude
The Apple cc_toolchain from apple_support appends its own `-target`
triple after the user's link flags, so on a macOS host the last triple
in the Rustc action's link args is the toolchain's, not the one these
tests pass through `--linkopt`. The derived `MACOSX_DEPLOYMENT_TARGET`
then carries the toolchain's minimum OS rather than `26.0`, and the
test that expects no variable at all sees one. Both would fail on every
macOS CI task.

Mark those two tests incompatible with macOS, as the other unit tests
do for host-specific cases. The precedence tests and the parser unit
test hold on every host and keep running everywhere.

Assisted-by: Claude
@google-cla

google-cla Bot commented Sep 24, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@drakeo338
drakeo338 marked this pull request as ready for review September 24, 2026 19:16
@drakeo338

Copy link
Copy Markdown
Contributor Author

signed

@UebelAndre UebelAndre left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@UebelAndre

Copy link
Copy Markdown
Collaborator

@drakeo338 Looks like some of the tests need to be constrained to Apple platforms

(18:59:58) ERROR: /workdir/test/unit/apple_deployment_target/BUILD.bazel:3:35: Compiling Rust bin bin (1 file) failed: (Exit 1): process_wrapper failed: error executing Rustc command (from rust_binary rule target //test/unit/apple_deployment_target:bin)
  (cd /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/3509/execroot/_main && \
  exec env - \
    CARGO_CFG_TARGET_ARCH=x86_64 \
    CARGO_CFG_TARGET_OS=linux \
    CARGO_CRATE_NAME=bin \
    CARGO_MANIFEST_DIR='${pwd}/test/unit/apple_deployment_target' \
    CARGO_PKG_AUTHORS='' \
    CARGO_PKG_DESCRIPTION='' \
    CARGO_PKG_HOMEPAGE='' \
    CARGO_PKG_NAME=bin \
    CARGO_PKG_VERSION=0.0.0 \
    CARGO_PKG_VERSION_MAJOR=0 \
    CARGO_PKG_VERSION_MINOR=0 \
    CARGO_PKG_VERSION_PATCH=0 \
    CARGO_PKG_VERSION_PRE='' \
    MACOSX_DEPLOYMENT_TARGET=15.0 \
    PATH=/bin:/usr/bin:/usr/local/bin \
    REPOSITORY_NAME='' \
    ZERO_AR_DATE=1 \
  bazel-out/k8-opt-exec/bin/util/process_wrapper/process_wrapper --subst 'pwd=${pwd}' --subst 'exec_root=${exec_root}' --subst 'output_base=${output_base}' -- bazel-out/k8-fastbuild-ST-f8e821cf2346/bin/external/+rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/bin/rustc @bazel-out/k8-fastbuild-ST-f8e821cf2346/bin/test/unit/apple_deployment_target/bin-0.params)
# Configuration: 4c6a8e4a753b8c63d0debdffc22c59ca782f4054d6a72e018cf46fc9d9047f9d
# Execution platform: @@platforms//host:host
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
error: linking with `/usr/bin/gcc` failed: exit status: 1
  |
  = note:  "/usr/bin/gcc" "-m64" "/var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/3509/execroot/_main/bazel-out/k8-fastbuild-ST-f8e821cf2346/bin/test/unit/apple_deployment_target/rustcsDo5iP/symbols.o" "<2 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libprofiler_builtins-*,libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,libcfg_if-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/3509/execroot/_main/bazel-out/k8-fastbuild-ST-f8e821cf2346/bin/test/unit/apple_deployment_target/rustcsDo5iP/raw-dylibs" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "bazel-out/k8-fastbuild-ST-f8e821cf2346/bin/test/unit/apple_deployment_target/bin" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-u" "__llvm_profile_runtime" "--coverage" "-fuse-ld=gold" "-B/usr/bin" "-Wl,-no-as-needed" "-Wl,-z,relro,-z,now" "-pass-exit-codes" "-target" "arm64-apple-macosx26.0" "-Wl,--push-state,-as-needed" "-lstdc++" "-Wl,--pop-state" "-Wl,--push-state,-as-needed" "-lm" "-Wl,--pop-state"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: gcc: error: unrecognized command-line option '-target'
error: aborting due to 1 previous error

bazel coverage builds a test's dependencies, so the binaries analysed under
an Apple -target linkopt were linked by gcc on Linux and failed. The tests
now read the Rustc actions through a small wrapper rule that applies the
link settings as a transition and forwards only the actions, with empty
instrumented files so coverage does not walk into the binary.

Assisted-by: Claude
@drakeo338

Copy link
Copy Markdown
Contributor Author

Thanks! Fixed in 98e0081. The failure was bazel coverage linking the test binaries with the Apple -target linkopt on Linux. The tests now read the Rustc actions through a small wrapper that applies the link settings as a transition, so nothing gets linked. That keeps them running on Linux and Windows, and bazel coverage on the package passes locally. The two linkopt tests still skip on macOS, because apple_support appends its own -target there.

@UebelAndre
UebelAndre added this pull request to the merge queue Sep 24, 2026
Merged via the queue into bazelbuild:main with commit 4139da8 Sep 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apple Rust link actions pass conflicting deployment targets to clang

2 participants