rust-lang / rustc_codegen_cranelift

Cranelift based backend for rustc
Apache License 2.0
1.64k stars 101 forks source link

macOS aarch64 compile bug #1501

Closed andrewgazelka closed 4 months ago

andrewgazelka commented 5 months ago
hyperion on  main via  v1.81.0-nightly
❯ cargo clif build
   Compiling proc-macro2 v1.0.85
   Compiling libc v0.2.155
   Compiling serde v1.0.203
   Compiling backtrace v0.3.73
   Compiling anyhow v1.0.86
   Compiling ahash v0.8.11
   Compiling typenum v1.17.0
   Compiling rustix v0.38.34
   Compiling generic-array v0.14.7
   Compiling allocator-api2 v0.2.18
   Compiling stable_deref_trait v1.2.0
   Compiling slab v0.4.9
error: linking with `cc` failed: exit status: 1
  |
  = note: env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET -u XROS_DEPLOYMENT_TARGET LC_ALL="C" PATH="/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/bin:/Users/andrewgazelka/.rustup/toolchains/nightly-2024-06-18-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/andrewgazelka/.rustup/toolchains/nightly-2024-06-18-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/opt/homebrew/opt/llvm/bin:/opt/homebrew/bin:/Users/andrewgazelka/.local/cg_clif/dist/bin:/Users/andrewgazelka/.local/cg_clif:/Users/andrewgazelka/.local/cg_clif-aarch64-apple-darwin/dist/bin:/opt/homebrew/opt/llvm/bin:/opt/homebrew/opt/binutils/bin:/Users/andrewgazelka/Library/Python/3.11/bin:/Users/andrewgazelka/.local/bin:/Users/andrewgazelka/.yarn/bin:/Users/andrewgazelka/.gem/bin:/opt/homebrew/opt/ruby/bin:/opt/homebrew/anaconda3/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/Users/andrewgazelka/.cargo/bin:/Users/andrewgazelka/Library/Application Support/JetBrains/Toolbox/scripts:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/MacGPG2/bin:/Library/TeX/texbin:/Applications/Wireshark.app/Contents/MacOS:/Applications/iTerm.app/Contents/Resources/utilities:/Users/andrewgazelka/.cache/lm-studio/bin" VSLANG="1033" ZERO_AR_DATE="1" "cc" "-arch" "arm64" "/var/folders/l4/42q4b0sn73xfvtpsgb1tl1p40000gn/T/rustcjCykug/symbols.o" "/Users/andrewgazelka/Projects/minecraft/hyperion/target/debug/build/generic-array-c32dbcdeb705cd43/build_script_build-c32dbcdeb705cd43.build_script_build.bf7f4eeab54dea1f-cgu.0.rcgu.o" "/Users/andrewgazelka/Projects/minecraft/hyperion/target/debug/build/generic-array-c32dbcdeb705cd43/build_script_build-c32dbcdeb705cd43.allocator_shim.rcgu.o" "-L" "/Users/andrewgazelka/Projects/minecraft/hyperion/target/debug/deps" "-L" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib" "/Users/andrewgazelka/Projects/minecraft/hyperion/target/debug/deps/libversion_check-27c07903635a32eb.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libstd-ee33e0d1e3017a4b.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libpanic_abort-c425f59d0ca9d45b.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libobject-d15823d12c75bc32.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libmemchr-a8ff483cc2f11ae5.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libaddr2line-8c5ba62f24a76b08.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libgimli-2718fef7dc9bf396.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/librustc_demangle-fc522c8d71869030.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libstd_detect-82d187adba722f3c.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libhashbrown-04dd01106a9d4215.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_alloc-af47ba15492bd3cf.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libminiz_oxide-3c1ee9530caed579.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libadler-79d3d4230509fc0f.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libunwind-6afa1aa1e12009cc.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libcfg_if-5724a9d36da6a6b9.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/liblibc-e586022e613d049f.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/liballoc-de13c2530d9dc79b.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_core-3041aca1ff2d337b.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libcore-e0fa6d55a668bdb1.rlib" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib/libcompiler_builtins-d6fa3a9c4bed08aa.rlib" "-lSystem" "-lc" "-lm" "-L" "/Users/andrewgazelka/.local/cg_clif/dist/lib/rustlib/aarch64-apple-darwin/lib" "-o" "/Users/andrewgazelka/Projects/minecraft/hyperion/target/debug/build/generic-array-c32dbcdeb705cd43/build_script_build-c32dbcdeb705cd43" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: 0  0x100f6a074  __assert_rtn + 72
          1  0x100ea6db8  ld::InputFiles::SliceParser::parseObjectFile(mach_o::Header const*) const + 22712
          2  0x100eb3830  ld::InputFiles::parseAllFiles(void (ld::AtomFile const*) block_pointer)::$_8::operator()(unsigned long, ld::FileInfo const&) const + 440
          3  0x18b082428  _dispatch_client_callout2 + 20
          4  0x18b096850  _dispatch_apply_invoke3 + 336
          5  0x18b0823e8  _dispatch_client_callout + 20
          6  0x18b083c68  _dispatch_once_callout + 32
          7  0x18b096eec  _dispatch_apply_invoke_and_wait + 372
          8  0x18b095e9c  _dispatch_apply_with_attr_f + 1212
          9  0x18b09608c  dispatch_apply + 96
          10  0x100f38564  ld::AtomFileConsolidator::parseFiles(bool) + 292
          11  0x100ed3ee8  main + 9532
          ld: Assertion failed: (pattern[0].addrMode == addr_other), function addFixupFromRelocations, file Relocations.cpp, line 701.
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
bjorn3 commented 5 months ago

This is https://github.com/bytecodealliance/wasmtime/issues/8730, which is an incompatibility with the new linker of XCode 15. As workaround you can use RUSTFLAGS="-Clink-arg=-ld_classic". Or alternatively downgrade to XCode 14.

andrewgazelka commented 5 months ago

This is bytecodealliance/wasmtime#8730, which is an incompatibility with the new linker of XCode 15. As workaround you can use RUSTFLAGS="-Clink-arg=-ld_classic". Or alternatively downgrade to XCode 14.

ok cool... how would I add this to .cargo/config?

which [target.{x}] would I use

bjorn3 commented 5 months ago

I think the following would work:

[target. "aarch64-apple-darwin"]
rustflags = ["-Clink-arg=-ld_classic"]
andrewgazelka commented 5 months ago

I think the following would work:

[target. "aarch64-apple-darwin"]
rustflags = ["-Clink-arg=-ld_classic"]

yea I was trying that but for some reason I needed to add RUSTFLAGS env variable instead 🤷‍♂️. welp maybe I will figure out why later.

ChiefMateStarbuck commented 5 months ago

What is the exact command that works? The following command still fails for me.

RUSTFLAGS="-Clink-arg=-ld_classic" ./y.sh build

Using the normal cargo build command works but the executable is not functional.

# The following command builds
RUSTFLAGS="-Clink-arg=-ld_classic" cargo build

# The executable gives the following error
cargo-clif build

error[E0463]: can't find crate for `core`
  |
  = note: the `aarch64-apple-darwin` target may not be installed
  = help: consider downloading the target with `rustup target add aarch64-apple-darwin`
  = help: consider building the standard library from source with `cargo build -Zbuild-std`

Thank you in advance.

bjorn3 commented 5 months ago

There are two options:

  1. Download a precompiled version from https://github.com/rust-lang/rustc_codegen_cranelift/releases/tag/dev and then using RUSTFLAGS="-Clink-arg=-Wl,-ld_classic" /path/to/cargo-clif build when building your project.
  2. Apply the following patch before building yourself:
```diff diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index dfbe0f51..9e0327b9 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -241,7 +241,7 @@ fn build_clif_sysroot_for_triple( } // Build sysroot - let mut rustflags = vec!["-Zforce-unstable-if-unmarked".to_owned(), "-Cpanic=abort".to_owned()]; + let mut rustflags = vec!["-Zforce-unstable-if-unmarked".to_owned(), "-Cpanic=abort".to_owned(), "-Clink-arg=-Wl,-ld_classic".to_owned()]; match cg_clif_dylib_path { CodegenBackend::Local(path) => { rustflags.push(format!("-Zcodegen-backend={}", path.to_str().unwrap())); diff --git a/scripts/cargo-clif.rs b/scripts/cargo-clif.rs index 1e14f41d..ed29f8d3 100644 --- a/scripts/cargo-clif.rs +++ b/scripts/cargo-clif.rs @@ -12,7 +12,7 @@ fn main() { sysroot = sysroot.parent().unwrap(); } - let mut rustflags = vec!["-Cpanic=abort".to_owned(), "-Zpanic-abort-tests".to_owned()]; + let mut rustflags = vec!["-Cpanic=abort".to_owned(), "-Zpanic-abort-tests".to_owned(), "-Clink-arg=-Wl,-ld_classic".to_owned()]; if let Some(name) = option_env!("BUILTIN_BACKEND") { rustflags.push(format!("-Zcodegen-backend={name}")); } else { diff --git a/scripts/rustc-clif.rs b/scripts/rustc-clif.rs index 92defd21..bf432f2f 100644 --- a/scripts/rustc-clif.rs +++ b/scripts/rustc-clif.rs @@ -19,6 +19,7 @@ fn main() { let mut args = vec![]; args.push(OsString::from("-Cpanic=abort")); args.push(OsString::from("-Zpanic-abort-tests")); + args.push(OsString::from("-Clink-arg=-Wl,-ld_classic")); if let Some(name) = option_env!("BUILTIN_BACKEND") { args.push(OsString::from(format!("-Zcodegen-backend={name}"))) } else { diff --git a/scripts/rustdoc-clif.rs b/scripts/rustdoc-clif.rs index 1cad312b..47592db4 100644 --- a/scripts/rustdoc-clif.rs +++ b/scripts/rustdoc-clif.rs @@ -19,6 +19,7 @@ fn main() { let mut args = vec![]; args.push(OsString::from("-Cpanic=abort")); args.push(OsString::from("-Zpanic-abort-tests")); + args.push(OsString::from("-Clink-arg=-Wl,-ld_classic")); if let Some(name) = option_env!("BUILTIN_BACKEND") { args.push(OsString::from(format!("-Zcodegen-backend={name}"))) } else { ```
ChiefMateStarbuck commented 5 months ago

Thank you very much @bjorn3. The second solution worked great.

Should the cargo-clif build work for this project? When attempting to build this project with clif there are errors of missing dependencies. This is still for AppleARM.

errors ``` bash error[E0463]: can't find crate for `jobserver` --> src/lib.rs:15:1 | 15 | extern crate jobserver; | ^^^^^^^^^^^^^^^^^^^^^^^ can't find crate error[E0463]: can't find crate for `rustc_middle` --> src/lib.rs:17:1 | 17 | extern crate rustc_middle; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_ast` --> src/lib.rs:18:1 | 18 | extern crate rustc_ast; | ^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_codegen_ssa` --> src/lib.rs:19:1 | 19 | extern crate rustc_codegen_ssa; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_data_structures` --> src/lib.rs:20:1 | 20 | extern crate rustc_data_structures; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_errors` --> src/lib.rs:21:1 | 21 | extern crate rustc_errors; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_fs_util` --> src/lib.rs:22:1 | 22 | extern crate rustc_fs_util; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_hir` --> src/lib.rs:23:1 | 23 | extern crate rustc_hir; | ^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_incremental` --> src/lib.rs:24:1 | 24 | extern crate rustc_incremental; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_index` --> src/lib.rs:25:1 | 25 | extern crate rustc_index; | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_metadata` --> src/lib.rs:26:1 | 26 | extern crate rustc_metadata; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_monomorphize` --> src/lib.rs:27:1 | 27 | extern crate rustc_monomorphize; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_session` --> src/lib.rs:28:1 | 28 | extern crate rustc_session; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_span` --> src/lib.rs:29:1 | 29 | extern crate rustc_span; | ^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_target` --> src/lib.rs:30:1 | 30 | extern crate rustc_target; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` error[E0463]: can't find crate for `rustc_driver` --> src/lib.rs:34:1 | 34 | extern crate rustc_driver; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate | = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview` ```
bjorn3 commented 5 months ago

cargo-clif build is not expected to work on cg_clif itself. cargo-clif is one of the outputs of building cg_clif and is meant to be used to build your own project using cg_clif.