Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ Provided you have no stripped the symbols from your binary built, on Linux you c
2.28
```

### Specify macOS version

To build for a specific minimum macOS version, add that version as a suffix to the `--target` value:

```bash
cargo zigbuild --target aarch64-apple-darwin.13.0
```

This passes the version to `zig cc`, and sets `MACOSX_DEPLOYMENT_TARGET` to the same value for `rustc` and any C/C++ build scripts. It overrides a `MACOSX_DEPLOYMENT_TARGET` set in the environment.

> [!NOTE]
> By default `--target` for `*-apple-darwin` will have Zig build for its own default minimum macOS version. This version depends on the Zig release, and it is not the same as the one `rustc` uses. For example, Zig 0.16 defaults to macOS 13.0, while `rustc` defaults to 11.0 for `aarch64-apple-darwin`.

> [!NOTE]
> `MACOSX_DEPLOYMENT_TARGET` on its own is not enough. `zig cc` ignores it, so without the suffix the linked binary uses Zig's default version.

### Specify target CPU

When cross-compiling for a CPU newer than the target's baseline — for example AWS Graviton (ARM Neoverse) instances — specify the target CPU via `RUSTFLAGS`, or you may hit hard-to-diagnose `Illegal instruction` (SIGILL, exit code 132) errors at runtime, or leave performance on the table:
Expand Down
6 changes: 6 additions & 0 deletions src/zig/cargo_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ impl Zig {
cmd.env("PKG_CONFIG_SYSROOT_DIR", sdkroot);
}

if raw_target.contains("apple-darwin")
&& let Some((_, version)) = raw_target.split_once('.')
{
cmd.env("MACOSX_DEPLOYMENT_TARGET", version);
}

// Enable unstable `target-applies-to-host` option automatically
// when target is the same as host but may have specified glibc version
if host_target == parsed_target {
Expand Down
19 changes: 15 additions & 4 deletions src/zig/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ fn zig_target_env(triple: &Triple) -> Environment {
}

/// Build the target triple to pass to `zig cc -target`, where `abi_suffix` is
/// the glibc version including its leading dot (`.2.36`) or an empty string.
/// the glibc or macOS version including its leading dot (`.2.36`, `.13.0`)
/// or an empty string.
fn zig_target_triple(
rust_target: &str,
triple: &Triple,
Expand Down Expand Up @@ -586,9 +587,9 @@ fn zig_target_triple(
// Zig 0.10.0 switched macOS ABI to none
// see https://github.com/ziglang/zig/pull/11684
if *zig_version > semver::Version::new(0, 9, 1) {
format!("{arch}-macos-none{abi_suffix}")
format!("{arch}-macos{abi_suffix}-none")
} else {
format!("{arch}-macos-gnu{abi_suffix}")
format!("{arch}-macos{abi_suffix}-gnu")
}
}
OperatingSystem::Windows => {
Expand Down Expand Up @@ -771,7 +772,7 @@ mod tests {
#[test]
fn test_zig_target_triple() {
let cases = [
// Rust target (with optional glibc suffix), zig version, zig target
// Rust target (with optional version suffix), zig version, zig target
("x86_64-unknown-linux-gnu", "0.15.2", "x86_64-linux-gnu"),
(
"x86_64-unknown-linux-gnu.2.36",
Expand Down Expand Up @@ -805,6 +806,16 @@ mod tests {
// zig 0.10 switched the macOS abi to none
("aarch64-apple-darwin", "0.15.2", "aarch64-macos-none"),
("aarch64-apple-darwin", "0.9.1", "aarch64-macos-gnu"),
(
"aarch64-apple-darwin.13.0",
"0.15.2",
"aarch64-macos.13.0-none",
),
(
"x86_64-apple-darwin.10.15",
"0.15.2",
"x86_64-macos.10.15-none",
),
(
"aarch64-apple-ios-macabi",
"0.15.2",
Expand Down
Loading