Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cross compile a Rust application from macOS x86 to macOS Silicon?

I want to cross compile a Rust program from my x86 Mac to a binary that can run on a Silicon Mac, and I can't figure out linking.

I have:

  • An x86 Mac running macOS 10.15.7 Catalina
  • A Rust project called riff
  • cargo 1.51.0 (43b129a20 2021-03-16) recently retrieved using rustup
  • Xcode version 12.4 (12D4e)

I want to compile this into a binary that can run on a Silicon (ARM) Mac. This could be one of:

  • A Silicon specific binary
  • A Universal binary that can run on either Silicon or x86

I have tried (through ./release.sh --dry):

  • rustup target add aarch64-apple-darwin
  • cargo build --release --target=aarch64-apple-darwin

The result was too long to paste in here, so this is an excerpt:

   Compiling riff v0.1.0 (/Users/johan/src/riff)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-arch arm64" "-L" "/Users/johan/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib" "/Users/johan/src/riff/target/aarch64-apple-darwin/release/deps/riff.1g47gi93dk96t2va.rcgu.o" ... "/Users/johan/src/riff/target/aarch64-apple-darwin/release/deps/riff.thread_local-9baf7723f72eef37.thread_local.7fl1y1d9-cgu.9.rcgu.o.rcgu.o" "/Users/johan/src/riff/target/aarch64-apple-darwin/release/deps/riff.unwind-082f7f4ff792426f.unwind.7rkx2ya0-cgu.0.rcgu.o.rcgu.o" "-o /Users/johan/src/riff/target/aarch64-apple-darwin/release/deps/riff" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/johan/src/riff/target/aarch64-apple-darwin/release/deps" "-L" "/Users/johan/src/riff/target/release/deps" "-L" "/Users/johan/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib" "/Users/johan/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcompiler_builtins-9eb7dc999785b765.rlib" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libresolv.tbd, missing required architecture arm64 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libresolv.tbd
          ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libm.tbd, missing required architecture arm64 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libm.tbd
          ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libc.tbd, missing required architecture arm64 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libc.tbd
          ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, missing required architecture arm64 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd
          Undefined symbols for architecture arm64:
            "__tlv_atexit", referenced from:
                std::sys::unix::thread_local_dtor::register_dtor::h4ece0fa32a190720 in riff.std-e40bd6a3af835df4.std.5bd9rdky-cgu.0.rcgu.o.rcgu.o
...
            "_free", referenced from:
                alloc::raw_vec::finish_grow::hd210c40bc8674cab (.llvm.18296587876935678160) in riff.addr2line-220464f1af330a49.addr2line.6xizm3st-cgu.1.rcgu.o.rcgu.o
                addr2line::path_push::hf1048f2514c3ed28 in riff.addr2line-220464f1af330a49.addr2line.6xizm3st-cgu.5.rcgu.o.rcgu.o
                alloc::raw_vec::finish_grow::ha5f485527a5c6598 in riff.addr2line-a950b2eed913eb15.addr2line.1hygsg7c-cgu.0.rcgu.o.rcgu.o
                addr2line::path_push::h891160d400ff153e in riff.addr2line-a950b2eed913eb15.addr2line.1hygsg7c-cgu.0.rcgu.o.rcgu.o
                core::ptr::drop_in_place$LT$aho_corasick..prefilter..Packed$GT$::h03bcd2abfab8068d in riff.aho_corasick-5d6c900557669556.aho_corasick.4z4l4vb2-cgu.0.rcgu.o.rcgu.o
                core::ptr::drop_in_place$LT$aho_corasick..packed..pattern..Patterns$GT$::h0c7fda104debb352 in riff.aho_corasick-5d6c900557669556.aho_corasick.4z4l4vb2-cgu.0.rcgu.o.rcgu.o
                core::ptr::drop_in_place$LT$alloc..vec..Vec$LT$alloc..vec..Vec$LT$u8$GT$$GT$$GT$::h75f2b558c0581f8d in riff.aho_corasick-5d6c900557669556.aho_corasick.4z4l4vb2-cgu.0.rcgu.o.rcgu.o
                ...
               (maybe you meant: alloc::alloc::box_free::ha8718384ecb60252 (.llvm.10945926475227631812), alloc::alloc::box_free::h58e664059c03effd (.llvm.10945926475227631812) , alloc::alloc::box_free::h75cae1a8684a32ce (.llvm.8938487907768303252) , alloc::alloc::box_free::h0459f8e1bcc5ae0a (.llvm.8938487907768303252) , alloc::alloc::box_free::hc3b2f94f9be4da06 (.llvm.7882408235892284297) , alloc::alloc::box_free::h75cae1a8684a32ce (.llvm.7882408235892284297) , alloc::alloc::box_free::h0459f8e1bcc5ae0a (.llvm.11547294393004044273) , alloc::alloc::box_free::hcc8806ce168336a8 (.llvm.2537316772799228199) , alloc::alloc::box_free::h305ed20a6d3d49c7 (.llvm.11075277010792284734) , alloc::alloc::box_free::h384e6b725c5ad166 (.llvm.7882408235892284297) , alloc::alloc::box_free::h0459f8e1bcc5ae0a (.llvm.7882408235892284297) , alloc::alloc::box_free::hb8cf920d803cfa22 (.llvm.4992613404806278909) , alloc::alloc::box_free::h90dd1ed16b644fc1 (.llvm.9562203231941335137) , alloc::alloc::box_free::h9e3286b91a0737ba (.llvm.4992613404806278909) , alloc::alloc::box_free::h75cae1a8684a32ce (.llvm.11547294393004044273) )
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
like image 450
Johan Walles Avatar asked Mar 29 '21 05:03

Johan Walles


People also ask

Can rust be cross compiled?

Rust supports a great number of platforms. For many of these platforms The Rust Project publishes binary releases of the standard library, and for some the full compiler. rustup gives easy access to all of them.

Can rust compile to ARM?

A common occurence when working with embedded systems is cross-compiling source code from a larger system in order to build code that can execute on a smaller one. With Rust this is usually all handled by Cargo, by adding the --target option when compiling your code.


Video Answer


1 Answers

Add the appropriate target

rustup target add aarch64-apple-darwin

Compile your code using the macOS 11.x SDK 1:

SDKROOT=$(xcrun -sdk macosx11.1 --show-sdk-path) \
MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx11.1 --show-sdk-platform-version) \
cargo build --target=aarch64-apple-darwin

This produces a single-architecture binary:

file target/aarch64-apple-darwin/debug/riff
target/aarch64-apple-darwin/debug/riff: Mach-O 64-bit executable arm64

1 — Find what the current available SDKs are via xcodebuild -showsdks.

like image 81
Shepmaster Avatar answered Sep 22 '22 00:09

Shepmaster