Search code examples
rustclangx86-64cross-compilingapple-m1

How to Build For M1 Macs From x86_64 Macs?


I'm trying to build a rust program for OSX. It'll be a universal binary (x86_64 intel and arm64 M1).

I've already successfully built SDL as a universal binary by using xcodebuild.

libSDL2.dylib:   Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit x86_64 dynamically linked shared library, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|NO_REEXPORTED_DYLIBS>] [arm64]
libSDL2_image.a: Mach-O universal binary with 2 architectures: [arm64:current ar archive random library] [x86_64:current ar archive random library]
libSDL2_ttf.a:   Mach-O universal binary with 2 architectures: [arm64:current ar archive] [x86_64]

I've also successfully built the rust program for x86_64 with the target triplet, x86_64-apple-darwin.

I'm currently failing to build the rust program for aarch64-apple-darwin as clang believes I'm building for iOS and fails to link libSDL2.dylib and all frameworks.

My cargo.toml looks like:

[target.x86_64-apple-darwin]
ar = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar"
linker = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
rustflags = ["-L$WORKSPACE/target/libs", "-L$WORKSPACE/target/sdk/usr/lib", "-Clink-arg=--target=x86_64-apple-darwin", "-Clink-arg=-Wl,-F$WORKSPACE/target/sdk/System/Library/Frameworks,-F/System/Library/Frameworks,-framework,CoreAudio,-framework,CoreFoundation,-framework,CoreGraphics,-framework,ImageIO,-framework,CoreServices"]

[target.aarch64-apple-darwin]
ar = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar"
linker = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
rustflags = ["-L$WORKSPACE/target/libs", "-L$WORKSPACE/target/sdk/usr/lib", "-Clink-arg=--target=aarch64-apple-darwin", "-Clink-arg=-Wl,-F$WORKSPACE/target/sdk/System/Library/Frameworks,-F/System/Library/Frameworks,-framework,CoreAudio,-framework,CoreFoundation,-framework,CoreGraphics,-framework,ImageIO,-framework,CoreServices"]

$WORKSPACE is converted to the correct path by the build script running sed before building the rust portion of the program.

How would I fix the build script so I can compile the arm64 build before I combine it with the x86_64 build via lipo?

Edit:

Here's the error message:

 = note: ld: warning: building for iOS, but linking in dylib file (/Users/runner/work/catgirl-engine/catgirl-engine/target/libs/libSDL2.dylib) built for macOS
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/usr/lib/libiconv.tbd) built for macOS/Mac Catalyst
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/usr/lib/libm.tbd) built for macOS/Mac Catalyst
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/System/Library/Frameworks/CoreAudio.framework/CoreAudio.tbd) built for macOS/Mac Catalyst
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation.tbd) built for macOS/Mac Catalyst
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/usr/lib/libSystem.tbd) built for macOS/Mac Catalyst
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/usr/lib/libc.tbd) built for macOS/Mac Catalyst
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/System/Library/Frameworks/ImageIO.framework/ImageIO.tbd) built for macOS/Mac Catalyst
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics.tbd) built for macOS/Mac Catalyst
          ld: warning: building for iOS, but linking in .tbd file (/Users/runner/work/catgirl-engine/catgirl-engine/target/sdk/System/Library/Frameworks/CoreServices.framework/CoreServices.tbd) built for macOS/Mac Catalyst
          ld: building for iOS, but linking in object file built for macOS, file '/Users/runner/work/catgirl-engine/catgirl-engine/target/aarch64-apple-darwin/release/deps/catgirl_engine-c308cec5d59a8390.catgirl_engine.065dccbf-cgu.7.rcgu.o' for architecture arm64

Solution

  • darwin is an ambiguous target.
    Clang seems to treat x86_64-apple-darwin as a synonym for x86_64-apple-macos, but aarch64-apple-darwin as a synonym for aarch64-apple-ios (presumably because that was the first AArch64 Darwin target to exist).

    Just use aarch64-apple-macos.