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

can not cross build for aarch64-apple-darwin and x86_64-apple-darwin #50

Closed
Dreamacro opened this issue Jul 25, 2022 · 14 comments · Fixed by #51
Closed

can not cross build for aarch64-apple-darwin and x86_64-apple-darwin #50

Dreamacro opened this issue Jul 25, 2022 · 14 comments · Fixed by #51

Comments

@Dreamacro
Copy link

Rust version: rustc 1.62.1 (e092d0b6b 2022-07-16)
OS: macOS Monterey
Arch: aarch64

It fails on cross build (x86_64-apple-darwin and aarch64-apple-darwin)

$ cargo zigbuild --release --target x86_64-apple-darwin
...
error: linking with `/Users/dreamacro/Library/Caches/cargo-zigbuild/0.11.2/zigcc-x86_64-apple-darwin.sh` failed: exit status: 1
  |
  = note: "/Users/dreamacro/Library/Caches/cargo-zigbuild/0.11.2/zigcc-x86_64-apple-darwin.sh" "-m64" "-arch" "x86_64" "/var/folders/ht/jj81z6n17gv271vm6bllrz8w0000gn/T/rustcYEWEOm/symbols.o" "/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps/glitchd-7ef0ef6677cc18c2.glitchd.66a11018-cgu.0.rcgu.o" "-L" "/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps" "-L" "/Users/dreamacro/Dev/projects/glitch-rs/target/release/deps" "-L" "/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/build/ring-a894445f88a9a04c/out" "-L" "/Users/dreamacro/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/var/folders/ht/jj81z6n17gv271vm6bllrz8w0000gn/T/rustcYEWEOm/libring-cc28e94b2a7ffb3d.rlib" "/Users/dreamacro/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-68d0fd74539b07e1.rlib" "-framework" "Security" "-liconv" "-lSystem" "-lresolv" "-lc" "-lm" "-liconv" "-L" "/Users/dreamacro/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps/glitchd-7ef0ef6677cc18c2" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: warning: unsupported linker arg: -dead_strip
          warning(link): framework not found for '-framework Security'
          warning(link): Framework search paths:
          warning(link): unable to resolve dependency /usr/lib/libcharset.1.dylib
          error(link): undefined reference to symbol '_kSecRandomDefault'
          error(link):   first referenced in '/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps/glitchd-7ef0ef6677cc18c2.glitchd.66a11018-cgu.0.rcgu.o'
          error(link): undefined reference to symbol '_SecRandomCopyBytes'
          error(link):   first referenced in '/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps/glitchd-7ef0ef6677cc18c2.glitchd.66a11018-cgu.0.rcgu.o'
          error: UndefinedSymbolReference
          
  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `glitch` due to previous error
warning: build failed, waiting for other jobs to finish...
error: linking with `/Users/dreamacro/Library/Caches/cargo-zigbuild/0.11.2/zigcc-x86_64-apple-darwin.sh` failed: exit status: 1
  |
  = note: "/Users/dreamacro/Library/Caches/cargo-zigbuild/0.11.2/zigcc-x86_64-apple-darwin.sh" "-m64" "-arch" "x86_64" "/var/folders/ht/jj81z6n17gv271vm6bllrz8w0000gn/T/rustcztjxHF/symbols.o" "/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps/glitch-4648c56b6280f928.glitch.de0635cd-cgu.0.rcgu.o" "-L" "/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps" "-L" "/Users/dreamacro/Dev/projects/glitch-rs/target/release/deps" "-L" "/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/build/ring-a894445f88a9a04c/out" "-L" "/Users/dreamacro/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/var/folders/ht/jj81z6n17gv271vm6bllrz8w0000gn/T/rustcztjxHF/libring-cc28e94b2a7ffb3d.rlib" "/Users/dreamacro/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-68d0fd74539b07e1.rlib" "-framework" "Security" "-liconv" "-lSystem" "-lresolv" "-lc" "-lm" "-liconv" "-L" "/Users/dreamacro/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps/glitch-4648c56b6280f928" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: warning: unsupported linker arg: -dead_strip
          warning(link): framework not found for '-framework Security'
          warning(link): Framework search paths:
          warning(link): unable to resolve dependency /usr/lib/libcharset.1.dylib
          error(link): undefined reference to symbol '_kSecRandomDefault'
          error(link):   first referenced in '/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps/glitch-4648c56b6280f928.glitch.de0635cd-cgu.0.rcgu.o'
          error(link): undefined reference to symbol '_SecRandomCopyBytes'
          error(link):   first referenced in '/Users/dreamacro/Dev/projects/glitch-rs/target/x86_64-apple-darwin/release/deps/glitch-4648c56b6280f928.glitch.de0635cd-cgu.0.rcgu.o'
          error: UndefinedSymbolReference
          
  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

seems it's a zig linker args issue: ziglang/zig#10999?

@messense
Copy link
Member

No, the error is from warning(link): framework not found for '-framework Security' because zig does not bundle all the macOS frameworks.

@messense
Copy link
Member

You can supply a SDKROOT env var to tell zig cc about the path to macOS SDK.

@messense
Copy link
Member

messense commented Jul 25, 2022

I think you can use

SDKROOT=`xcrun --show-sdk-path` cargo zigbuild ...

if you are building on macOS.

@Dreamacro
Copy link
Author

Thanks for the quick reply. It works 👍

@Dreamacro
Copy link
Author

Dreamacro commented Jul 26, 2022

Unfortunately, the artifacts of zigbuild are not executable on macOS.

project on main [$!] is 📦 v0.1.0 via 🦀 v1.62.1 
❯ cargo zigbuild --release --target aarch64-apple-darwin
    Finished release [optimized] target(s) in 0.06s

project on main [$!] is 📦 v0.1.0 via 🦀 v1.62.1 
❯ ./target/aarch64-apple-darwin/release/binary          
[1]    92924 killed     ./target/aarch64-apple-darwin/release/binary

But the result of cargo build --release --target aarch64-apple-darwin is worked.

How can I help you debug this problem?

@messense
Copy link
Member

Can you run it through lldb and paste the backtrace?

@Dreamacro
Copy link
Author

I tried some parameters and finally found that the build profile was the problem. When I removed strip = true, everything was fine.

[profile.release]
strip = true
lto = true
codegen-units = 1
panic = "abort"

Could you please describe in more detail some of the operations of lldb if you need some additional information?

@messense
Copy link
Member

lldb ./target/aarch64-apple-darwin/release/binary
run

If it crashes lldb should stop and present you a stacktrace, run bt all to get full backtrace.

@Dreamacro
Copy link
Author

Dreamacro commented Jul 26, 2022

Something strange happened:

When I run cargo build --release --target aarch64-apple-darwin for the first time and execute it with lldb run, it executes fine.

But when I execute it the second time using lldb it exits directly.

❯ lldb ./target/aarch64-apple-darwin/release/binary
(lldb) target create "./target/aarch64-apple-darwin/release/binary"
[1]    4572 killed     lldb ./target/aarch64-apple-darwin/release/binary

@Dreamacro
Copy link
Author

Dreamacro commented Aug 4, 2022

After simple testing, the problem appears to exist only in target aarch64-apple-darwin.

on Intel Macbook Pro:
aarch64-apple-darwin: killed
x86_64-apple-darwin: work
x86_64-unknown-linux-gnu: work

on M1 Pro:
aarch64-apple-darwin: killed
x86_64-apple-darwin: work
x86_64-unknown-linux-gnu: work

@messense
Copy link
Member

messense commented Aug 4, 2022

FYI, if you are already building on macOS, cargo build should just work, no need to use cargo-zigbuild.

@Dreamacro
Copy link
Author

Thanks, I mainly use cargo-zigbuild for Linux artifacts. Just tried macOS out of curiosity, but found this problem

@messense
Copy link
Member

messense commented Aug 4, 2022

I suspect it's a zig issue, consider report to zig if you can reproduce it with zig cc only.

@appelgriebsch
Copy link

appelgriebsch commented Aug 22, 2022

FYI, if you are already building on macOS, cargo build should just work, no need to use cargo-zigbuild.

Might still be helpful because of this issue: rust-lang/rust#55235.

Just stumbled over this today... currently not able to build a x86_64 Apple binary on M1 MacBook Pro :(

EDIT: found a workaround.... install the x86_64 toolchain along with the aarch64 one

rustup toolchain install stable-x86_64-apple-darwin

and then run

cargo +stable-x86_64-apple-darwin build --release

Worked even with some C (-sys) crates involved.

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 a pull request may close this issue.

3 participants