Description
Rust compilation on Ubuntu 22.04.2 LTS with target x86_64-apple-darwin fails with a build error:
cc: error: unrecognized command-line option '-arch'
cargo generated a cc command that can't work.
cc is (Ubuntu 11.3.0-1ubuntu1-22.04.1) 11.3.0
, installed with Ubuntu 22.04.2 LTS.
Reproduce by:
rustup target add x86_64-apple-darwin
cargo new hello_world
cd hello_world
cargo build
Expected a successful cross-compile.
Meta
rustc --version --verbose
:
rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-unknown-linux-gnu
release: 1.69.0
LLVM version: 15.0.7
Discussion
It's clear why this doesn't work. Cargo/rustc is invoking the platform's linker in a cross-compile, and the default linker is not capable of that cross-platform action. So this is a tool dependency problem. "rustup" installed support for that target with no errors, and if dependency checks needed to be made, they were not made at that time.
Note that the hard work is done. The compile is succesful. It's only the linking that does not work.
It can be argued that this particular cross-compilation pair is not supported. However, both the compiling platform and the target platform are Rust Tier 1 supported targets. "Tier 1 targets can be thought of as 'guaranteed to work'." There are no footnotes to the target table indicating that this source->target combination does not work.
Policy on target support says that "Tier 2 targets should, if at all possible, support cross-compiling. Tier 2 targets should not require using the target as the host for builds, even if the target supports host tools." That's for Tier 2. This is a Tier 1 target. For Tier 1 with Host Tools, the policy notes "Providing host tools does not exempt a target from requirements to support cross-compilation if at all possible."
So, if this cross-compile is technically possible, it is a bug that it does not work.
It is technically possible, since at least two unsupported workarounds exist.
Workarounds
There are at least two unsupported workarounds for this, using different tool chains.
- Osxcross This is a set of scripts that collects tools capable of this build. This approach may require the use of proprietary Apple, Inc. libraries, which is inconsistent with Rust policy.
- Zig Using components of the Zig toolchain appears to work. This approach does not seem to require any proprietary Apple, Inc. code.
Industry trends
Cross-platform support is becoming more widespread. Microsoft recently announced that their Windows OS is now fully supported on some Apple hardware. Apple is now offering a game porting toolkit for converting Windows games to Apple targets. The "walled garden" model seems to be weakening, at least for desktops. Zig, a language which competes in Rust's space, already supports this cross-compile.
So this cross-compile should be fully supported. Thank you.