Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cannot cross-compile rust crate for macOS #180

Open
hardcore-sushi opened this issue Jul 8, 2021 · 6 comments
Open

Cannot cross-compile rust crate for macOS #180

hardcore-sushi opened this issue Jul 8, 2021 · 6 comments

Comments

@hardcore-sushi
Copy link

Everything goes fine when I try to build the crate for GNU/Linux, Windows or even Android but when I set x86_64-apple-darwin as the target, I get the following error:

   ....
   Compiling blake3 v0.3.8
The following warnings were emitted during compilation:

warning: The C compiler "cc" does not support -mavx512f and -mavx512vl.
warning: cc: error: x86_64: No such file or directory
warning: cc: error: unrecognized command-line option ‘-arch’

error: failed to run custom build command for `blake3 v0.3.8`

Caused by:
  process didn't exit successfully: `/tmp/blake3-test/target/release/build/blake3-4842eee2252bfd15/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=CARGO_FEATURE_PURE
  TARGET = Some("x86_64-apple-darwin")
  HOST = Some("x86_64-unknown-linux-gnu")
  CC_x86_64-apple-darwin = None
  CC_x86_64_apple_darwin = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_x86_64-apple-darwin = None
  CFLAGS_x86_64_apple_darwin = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2,sse3,ssse3")
  OPT_LEVEL = Some("3")
  CC_x86_64-apple-darwin = None
  CC_x86_64_apple_darwin = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_x86_64-apple-darwin = None
  CFLAGS_x86_64_apple_darwin = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2,sse3,ssse3")
  cargo:warning=The C compiler "cc" does not support -mavx512f and -mavx512vl.
  cargo:rerun-if-env-changed=BLAKE3_CI
  cargo:rerun-if-env-changed=CARGO_FEATURE_PREFER_INTRINSICS
  cargo:rerun-if-env-changed=CARGO_FEATURE_PURE
  cargo:rustc-cfg=blake3_sse2_ffi
  cargo:rustc-cfg=blake3_sse41_ffi
  cargo:rustc-cfg=blake3_avx2_ffi
  TARGET = Some("x86_64-apple-darwin")
  OPT_LEVEL = Some("3")
  HOST = Some("x86_64-unknown-linux-gnu")
  CC_x86_64-apple-darwin = None
  CC_x86_64_apple_darwin = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_x86_64-apple-darwin = None
  CFLAGS_x86_64_apple_darwin = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2,sse3,ssse3")
  running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-arch" "x86_64" "-Wall" "-Wextra" "-std=c11" "-o" "/tmp/blake3-test/target/x86_64-apple-darwin/release/build/blake3-d3c1ad758db1c04d/out/c/blake3_sse2_x86-64_unix.o" "-c" "c/blake3_sse2_x86-64_unix.S"
  cargo:warning=cc: error: x86_64: No such file or directory
  cargo:warning=cc: error: unrecognized command-line option ‘-arch’
  exit status: 1

  --- stderr


  error occurred: Command "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-arch" "x86_64" "-Wall" "-Wextra" "-std=c11" "-o" "/tmp/blake3-test/target/x86_64-apple-darwin/release/build/blake3-d3c1ad758db1c04d/out/c/blake3_sse2_x86-64_unix.o" "-c" "c/blake3_sse2_x86-64_unix.S" with args "cc" did not execute successfully (status code exit status: 1).


warning: build failed, waiting for other jobs to finish...
error: build failed
OS: Linux 5.10.0-8-amd64 #1 SMP Debian 5.10.46-1 (2021-06-24) x86_64 GNU/Linux
Rust version: v1.53.0
"cc -v" output:

Using built-in specs.
COLLECT_GCC=cc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 10.2.1-6' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-gcn/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.1 20210110 (Debian 10.2.1-6)
@oconnor663
Copy link
Member

unrecognized command-line option ‘-arch’ seems to be the core issue. I wonder if this is the cc crate getting confused in a cross-compiling scenario. Do you have any generic steps to repro this issue, for example maybe building the b3sum crate?

@hardcore-sushi
Copy link
Author

I get the same error when cross-compiling b3sum, and it seems to use blake3 v0.3.7.

@cipriancraciun
Copy link

So I've hit the same problem compiling my tool that depends on blake3.

The problem (at least for me) seems to be that the crate uses the wrong C compiler, namely cc, which most likely it's a symlink to one of the native compilers either GCC or Clang. You'll have to convince it to use the proper cross-compiler.

Assuming that you are using osxcross, here is how I've managed to make it build:

  • start with a clean environment (in my case I've actually called env -i PATH=... TMPDIR=... ... cargo ...;
  • export PATH="/opt/osxcross/bin:/usr/bin:/bin" -- make sure it doesn't pick the wrong compiler from somewhere else;
  • export CC=x86_64-apple-darwin21.4-clang -- use the osxcross compiler for C;
  • export CXX=x86_64-apple-darwin21.4-clang++ -- the same for C++;
  • export AR=x86_64-apple-darwin21.4-ar -- also needed;
  • export CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER=x86_64-apple-darwin21.4-clang -- or just edit cargo.toml, but this environment variable should be enough;

After doing all this it now builds. (It works for both Linux-GNU, Linux-Musl, Android, Darwin, both x86_64 and aarch64.)

@jedisct1
Copy link

jedisct1 commented Jan 13, 2023

You need to use cargo-zigbuild to cross-compile Rust code that includes C code:

cargo zigbuild --target=x86_64-apple-darwin

@oconnor663
Copy link
Member

oconnor663 commented Jan 15, 2023

@jedisct1 wow that is wonderful.

The complete pair of commands I just used to do a cross-compile was:

rustup target add x86_64-apple-darwin
cargo zigbuild --target=x86_64-apple-darwin

@jedisct1
Copy link

When targeting Linux, the command (actually Zig) also allows choosing a glibc version that may differ from the one installed on the system.

This is very useful to build redistributable binaries that work everywhere, even on CentOS 5.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants