Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map system from "darwin" to "macos" when parsing triple #3296

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

christianscott
Copy link

@christianscott christianscott commented Feb 25, 2025

Map darwin to macos when parsing target triples to align with Rust’s internal naming convention

Rust’s target triple format and internal OS naming conventions use different terms for macOS:
• The target triple follows LLVM conventions and uses -darwin, e.g., aarch64-apple-darwin.
• Rust itself uses macos for OS-based conditionals, e.g., #[cfg(target_os = "macos")].

This mismatch originates from upstream toolchains— LLVM and GCC have historically used darwin in triples to denote macOS. Meanwhile, Rust internally adopted macos before 1.0, as discussed in this early thread: Rust Internals Discussion.

Fixes: bazelbuild/rules_rust#3291

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

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

Thanks! Curious though, why not make the change to update the triple function to map darwin to macos?

cpu_arch = component_parts[0]
vendor = component_parts[1]
system = component_parts[2]
abi = None
if cpu_arch.startswith(("thumbv8m", "thumbv7m", "thumbv7e", "thumbv6m")):
abi = system
system = vendor
vendor = None
if system == "androideabi":
system = "android"
abi = "eabi"
if len(component_parts) == 4:
abi = component_parts[3]

It seems like rustc also expects the name to be "macos" which I determine from the json dump from the command below where you can see "os": "macos",

rustc +nightly --print target-spec-json -Z unstable-options
rustc +nightly --print target-spec-json -Z unstable-options
{
  "abi-return-struct-as-int": true,
  "arch": "aarch64",
  "archive-format": "darwin",
  "cpu": "apple-m1",
  "crt-objects-fallback": "false",
  "data-layout": "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32",
  "debuginfo-kind": "dwarf-dsym",
  "dll-suffix": ".dylib",
  "dynamic-linking": true,
  "eh-frame-header": false,
  "emit-debug-gdb-scripts": false,
  "frame-pointer": "non-leaf",
  "function-sections": false,
  "has-rpath": true,
  "has-thread-local": true,
  "is-like-osx": true,
  "link-env": [
    "ZERO_AR_DATE=1"
  ],
  "link-env-remove": [
    "IPHONEOS_DEPLOYMENT_TARGET",
    "TVOS_DEPLOYMENT_TARGET",
    "XROS_DEPLOYMENT_TARGET"
  ],
  "linker-flavor": "darwin-cc",
  "linker-is-gnu": false,
  "lld-flavor": "darwin",
  "llvm-floatabi": "hard",
  "llvm-target": "arm64-apple-macosx",
  "max-atomic-width": 128,
  "metadata": {
    "description": "ARM64 Apple macOS (11.0+, Big Sur+)",
    "host_tools": true,
    "std": true,
    "tier": 1
  },
  "os": "macos",
  "split-debuginfo": "packed",
  "stack-probes": {
    "kind": "inline"
  },
  "supported-sanitizers": [
    "address",
    "thread",
    "cfi"
  ],
  "supported-split-debuginfo": [
    "packed",
    "unpacked",
    "off"
  ],
  "target-family": [
    "unix"
  ],
  "target-mcount": "\u0001mcount",
  "target-pointer-width": "64",
  "vendor": "apple"
}

But there definitely isn't a triple with "macos"

rustc +nightly --print target-list -Z unstable-options | grep macos

prints nothing

rustc +nightly --print target-list -Z unstable-options | grep darwin
aarch64-apple-darwin
arm64e-apple-darwin
i686-apple-darwin
x86_64-apple-darwin
x86_64h-apple-darwin

@christianscott christianscott changed the title CARGO_CFG_TARGET_OS should be "macos" on MacOS, not "darwin" map system from "darwin" to "macos" when parsing triple Feb 25, 2025
@christianscott christianscott force-pushed the macos_cargo_cfg_target_os branch from d13a045 to fc2de05 Compare February 25, 2025 22:33
@UebelAndre
Copy link
Collaborator

@christianscott be advised, there may be places in code that only account for "darwin" you might wanna look at all uses of target_os and make sure anything that compares it to "darwin" also checks "macos". And both should be supported for backward compatibility.

@christianscott
Copy link
Author

Will do!

@christianscott
Copy link
Author

Fixing the CI failure (no bzlmod on macos aarch64). I can repro locally.

Error message
# bazel build --noenable_bzlmod --enable_workspace //...
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/rust/platform/BUILD.bazel:6:24: target '//rust/platform:aarch64-fuchsia' is deprecated: Use `@rules_rust//rust/platform:aarch64-unknown-fuchsia` instead.
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/rust/platform/BUILD.bazel:6:24: target '//rust/platform:darwin' is deprecated: Use `@rules_rust//rust/platform:macos` instead.
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/rust/platform/BUILD.bazel:6:24: target '//rust/platform:wasi' is deprecated: Use `@rules_rust//rust/platform:wasip1` instead
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/rust/platform/BUILD.bazel:6:24: target '//rust/platform:wasm' is deprecated: Use `@rules_rust//rust/platform:wasm32` instead
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/rust/platform/BUILD.bazel:6:24: target '//rust/platform:wasm32-wasi' is deprecated: Use `@rules_rust//rust/platform:wasm-wasip1` instead.
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/rust/platform/BUILD.bazel:6:24: target '//rust/platform:x86_64-fuchsia' is deprecated: Use `@rules_rust//rust/platform:x86_64-unknown-fuchsia` instead.
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/rust/toolchain/BUILD.bazel:55:6: target '//rust/toolchain:current_rustfmt_files' is deprecated: Instead use `@rules_rust//rust/toolchain:current_rustfmt_toolchain
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/tools/rustfmt/BUILD.bazel:36:6: target '//tools/rustfmt:rustfmt' is deprecated: Prefer //tools/upstream_wrapper:rustfmt
WARNING: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/tools/rustfmt/BUILD.bazel:89:6: target '//tools/rustfmt:upstream_rustfmt' is deprecated: Prefer //tools/upstream_wrapper:rustfmt
INFO: Repository cross_rs_host_bin instantiated at:
  /Users/christian.s/Code/github.com/bazelbuild/rules_rust/WORKSPACE.bazel:19:21: in <toplevel>
  /Users/christian.s/Code/github.com/bazelbuild/rules_rust/crate_universe/tools/cross_installer/cross_installer_deps.bzl:45:10: in cross_installer_deps
  /private/var/tmp/_bazel_christian.s/5d56531c13be109e912a77d9e9ce5c4e/external/bazel_tools/tools/build_defs/repo/utils.bzl:268:18: in maybe
Repository rule cargo_bootstrap_repository defined at:
  /Users/christian.s/Code/github.com/bazelbuild/rules_rust/cargo/private/cargo_bootstrap.bzl:240:45: in <toplevel>
ERROR: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/cargo/private/cargo_bootstrap.bzl:222:40: An error occurred during the fetch of repository 'cross_rs_host_bin':
   Traceback (most recent call last):
        File "/Users/christian.s/Code/github.com/bazelbuild/rules_rust/cargo/private/cargo_bootstrap.bzl", line 222, column 40, in _cargo_bootstrap_repository_impl
                cargo_bin = repository_ctx.path(tools.cargo),
Error in path: Unable to load package for @@rust_macos_aarch64__aarch64-apple-darwin__stable_tools//:bin/cargo: The repository '@@rust_macos_aarch64__aarch64-apple-darwin__stable_tools' could not be resolved: Repository '@@rust_macos_aarch64__aarch64-apple-darwin__stable_tools' is not defined
ERROR: no such package '@@cross_rs_host_bin//': Unable to load package for @@rust_macos_aarch64__aarch64-apple-darwin__stable_tools//:bin/cargo: The repository '@@rust_macos_aarch64__aarch64-apple-darwin__stable_tools' could not be resolved: Repository '@@rust_macos_aarch64__aarch64-apple-darwin__stable_tools' is not defined
ERROR: /Users/christian.s/Code/github.com/bazelbuild/rules_rust/crate_universe/tools/cross_installer/BUILD.bazel:9:6: //crate_universe/tools/cross_installer:cross depends on @@cross_rs_host_bin//:cross in repository @@cross_rs_host_bin which failed to fetch. no such package '@@cross_rs_host_bin//': Unable to load package for @@rust_macos_aarch64__aarch64-apple-darwin__stable_tools//:bin/cargo: The repository '@@rust_macos_aarch64__aarch64-apple-darwin__stable_tools' could not be resolved: Repository '@@rust_macos_aarch64__aarch64-apple-darwin__stable_tools' is not defined
ERROR: Analysis of target '//crate_universe/tools/cross_installer:cross' failed; build aborted: Analysis failed
INFO: Elapsed time: 2.166s, Critical Path: 1.26s
INFO: 57 processes: 57 internal.
ERROR: Build did NOT complete successfully
FAILED: 
    Fetching repository @@rust_analyzer_1.84.1_tools; starting
    Fetching repository @@cargo_bazel.buildifier-darwin-arm64; starting
    Fetching repository @@rrra__anyhow-1.0.71; starting
    Fetching repository @@cui__crates-index-3.6.0; starting
    Fetching repository @@rrra__clap_derive-4.3.2; starting
    Fetching repository @@rrra__clap_builder-4.3.11; starting
    Fetching https://static.rust-lang.org/dist/rust-src-1.84.1.tar.xz; 60.1 KiB (2.0%)
    Fetching https://static.crates.io/crates/crates-index/3.6.0/download ... (10 fetches)

@UebelAndre
Copy link
Collaborator

Fixing the CI failure (no bzlmod on macos aarch64). I can repro locally.

I think this is complaining about these not being updated

DEFAULT_TOOLCHAIN_TRIPLES = {
"aarch64-apple-darwin": "rust_darwin_aarch64",
"aarch64-pc-windows-msvc": "rust_windows_aarch64",
"aarch64-unknown-linux-gnu": "rust_linux_aarch64",
"s390x-unknown-linux-gnu": "rust_linux_s390x",
"x86_64-apple-darwin": "rust_darwin_x86_64",
"x86_64-pc-windows-msvc": "rust_windows_x86_64",
"x86_64-unknown-freebsd": "rust_freebsd_x86_64",
"x86_64-unknown-linux-gnu": "rust_linux_x86_64",
}

@christianscott
Copy link
Author

The build has passed 🎉 I've updated the description as well.

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

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

@christianscott responded to all the comments!

@christianscott christianscott force-pushed the macos_cargo_cfg_target_os branch from 3e591e7 to eecccda Compare February 26, 2025 01:26
@@ -46,17 +46,17 @@ def declare_config_settings():
constraint_values = system_to_constraints(system),
)

# Add alias for OSX to "darwin" to match what users will be expecting.
Copy link
Collaborator

Choose a reason for hiding this comment

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

It'd probably be best to add an additional alias.

Co-authored-by: UebelAndre <github@uebelandre.com>
Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

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

Sick! Thank you!

@UebelAndre UebelAndre enabled auto-merge February 26, 2025 02:24
@UebelAndre
Copy link
Collaborator

@christianscott looks like you gotta run buildifier!

@UebelAndre UebelAndre added this pull request to the merge queue Feb 26, 2025
@UebelAndre UebelAndre removed this pull request from the merge queue due to a manual request Feb 26, 2025
@christianscott
Copy link
Author

Oops 🙈 pushed a fix

@UebelAndre UebelAndre enabled auto-merge February 26, 2025 04:32
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.

CARGO_CFG_TARGET_OS should be macos on MacOS, not darwin
2 participants