ring icon indicating copy to clipboard operation
ring copied to clipboard

Cannot cross-compile to `x86_64-unknown-linux-gnu` from Apple M1

Open setoelkahfi opened this issue 2 years ago • 12 comments

I'm trying to cross-compile a Rust lambda from an Apple M1 with no luck. I follow some tutorials on the internet (this, https://github.com/briansmith/ring/issues/1332)

What I did

  • Add target 86_64-unknown-linux-gnu (rustup target add 86_64-unknown-linux-gnu).
  • arch -arm64 brew install SergioBenitez/osxct/x86_64-unknown-linux-gnu.
  • Add .cargo/config.toml in the project directory.
  • Install llvm arch -arm64 brew install llvm.
  • Setup my ~/.zshrc for llvm.
export RUSTFLAGS="-C link-arg=-L/opt/homebrew/opt/llvm/lib/c++ -C link-arg=-Wl,-rpath,/opt/homebrew/opt/llvm/lib/c++"
export PATH="/opt/homebrew/opt/llvm/bin:$PATH"

config.toml content:

[target.x86_64-unknown-linux-gnu]
linker = "/opt/homebrew/Cellar/x86_64-unknown-linux-gnu/7.2.0/bin/x86_64-unknown-linux-gnu-gcc"

Result:

error: failed to run custom build command for `ring v0.16.20`

Caused by:
  process didn't exit successfully: `/Users/myhome/lambda-function/target/debug/build/ring-b3b2f6ac56d4de6c/build-script-build` (exit status: 101)
  --- stdout
  OPT_LEVEL = Some("0")
  TARGET = Some("x86_64-unknown-linux-gnu")
  HOST = Some("aarch64-apple-darwin")
  cargo:rerun-if-env-changed=CC_x86_64-unknown-linux-gnu
  CC_x86_64-unknown-linux-gnu = None
  cargo:rerun-if-env-changed=CC_x86_64_unknown_linux_gnu
  CC_x86_64_unknown_linux_gnu = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  RUSTC_LINKER = Some("/opt/homebrew/Cellar/x86_64-unknown-linux-gnu/7.2.0/bin/x86_64-unknown-linux-gnu-gcc")
  cargo:rerun-if-env-changed=CROSS_COMPILE
  CROSS_COMPILE = None
  cargo:rerun-if-env-changed=CFLAGS_x86_64-unknown-linux-gnu
  CFLAGS_x86_64-unknown-linux-gnu = None
  cargo:rerun-if-env-changed=CFLAGS_x86_64_unknown_linux_gnu
  CFLAGS_x86_64_unknown_linux_gnu = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")

  --- stderr
  running "x86_64-linux-gnu-gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "-m64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-DNDEBUG" "-c" "-o/Users/myhome/lambda-function/target/x86_64-unknown-linux-gnu/debug/build/ring-4ea4043fbf2278a0/out/aesni-x86_64-elf.o" "/Users/myhome/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/aesni-x86_64-elf.S"
  thread 'main' panicked at 'failed to execute ["x86_64-linux-gnu-gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "-m64" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-DNDEBUG" "-c" "-o/Users/myhome/lambda-function/target/x86_64-unknown-linux-gnu/debug/build/ring-4ea4043fbf2278a0/out/aesni-x86_64-elf.o" "/Users/myhome/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/pregenerated/aesni-x86_64-elf.S"]: No such file or directory (os error 2)', /Users/myhome/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/build.rs:653:9
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

Any pointers?

setoelkahfi avatar Apr 19 '23 09:04 setoelkahfi

having the same issue all of a sudden

ta3pks avatar Apr 23 '23 20:04 ta3pks

I ended up using cross-rs for this purpose.

setoelkahfi avatar Apr 24 '23 07:04 setoelkahfi

I ended up using cross-rs for this purpose.

unfortunately cross-rs produced binary did not work on my server due to version difference of glibc I ended up compiling the code on the server through ssh

ta3pks avatar Apr 25 '23 09:04 ta3pks

i am having a similar issue:

      /opt/homebrew/Cellar/x86_64-unknown-linux-gnu/11.2.0_1/toolchain/bin/../lib/gcc/x86_64-unknown-linux-gnu/11.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld.bfd: /Users/andremarques/Desktop/cybergate/target/x86_64-unknown-linux-gnu/release/deps/libring-1a2175460e1d939d.rlib(curve25519.o):/Users/andremarques/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ring-0.16.20/crypto/curve25519/curve25519.c:520: more undefined references to `__stack_chk_guard' follow
      collect2: error: ld returned 1 exit status

= note: 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)

cybersoulK avatar Apr 27 '23 00:04 cybersoulK

it was working great before... (last time i tried was 3 months ago)

cybersoulK avatar Apr 27 '23 00:04 cybersoulK

If you have unexpectedly have this issue while working in a workspace you might be helped by setting resolver = "2" in Cargo.toml https://nickb.dev/blog/cargo-workspace-and-the-feature-unification-pitfall/

bouk avatar May 02 '23 11:05 bouk

This resolved it for me:

RUSTFLAGS="-C link-args=-fstack-protector-all -lssp" cargo build ...

sbruton avatar May 04 '23 01:05 sbruton

This resolved it for me:

RUSTFLAGS="-C link-args=-fstack-protector-all -lssp" cargo build ...

Does not work for me: /opt/homebrew/bin/aarch64-unknown-linux-gnu-ld.bfd: -f may not be used without -shared. I am cross compiling to aarch64-unknown-linux-gnu though.

vszakd avatar Oct 14 '23 16:10 vszakd

This issue seems resolved. I can cross compile from macOS to Linux without the -fstack-protector-all workaround with ring 0.17.5.

Colerar avatar Nov 10 '23 05:11 Colerar

Not resolved at all :/ 0.17.7 does not build on M1 here :/ With none of the workarounds :/

Just as I wrote this:

export CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc
export CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++
export AR_x86_64_unknown_linux_gnu=x86_64-linux-gnu-ar

fixes it :)

but it needs libssp which is included in gcc-snapshot on ubuntu which is 5GB ...

Yatekii avatar Jan 20 '24 13:01 Yatekii

我遇到了类似的问题:

      /opt/homebrew/Cellar/x86_64-unknown-linux-gnu/11.2.0_1/toolchain/bin/../lib/gcc/x86_64-unknown-linux-gnu/11.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld.bfd: /Users/andremarques/Desktop/cybergate/target/x86_64-unknown-linux-gnu/release/deps/libring-1a2175460e1d939d.rlib(curve25519.o):/Users/andremarques/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ring-0.16.20/crypto/curve25519/curve25519.c:520: more undefined references to `__stack_chk_guard' follow
      collect2: error: ld returned 1 exit status

= 注意:extern找不到某些函数;可能需要安装某些本机库或指定其路径 = 注意:使用-l标志指定要链接的本机库 = 注意:使用cargo:rustc-link-lib指令指定要与 Cargo 链接的本机库(请参阅https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

did you finaly solved this problem?

yang472541642 avatar May 29 '24 03:05 yang472541642