rust-lang / libz-sys

Rust crate package to link to a system libz (zlib)
Apache License 2.0
116 stars 75 forks source link

Build fails with `unrecognized command-line option '-arch'` when cross-compiling #93

Open edmorley opened 2 years ago

edmorley commented 2 years ago

Hi :-)

Has anyone seen anything like this before when cross-compiling on an M1 Mac?

This cross-compile configuration works successfully for a variety of other crates, however this is the first crate I'm using that uses CMake, so I'm not sure if it's a libz-sys specific problem, or with cross-compiling with CMake and this toolchain in general.

Steps to reproduce

  1. brew install messense/macos-cross-toolchains/x86_64-unknown-linux-musl (to install this cross-compile toolchain)
  2. brew install cmake (since libz-sys requires it)
  3. rustup target add x86_64-unknown-linux-musl
  4. Ensure the new toolchain can be found:
    export CC_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-gcc
    export CXX_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-g++
    export AR_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-ar
    export CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-unknown-linux-musl-gcc
  5. cargo new testcase-libz-sys-cross-compile && cd_
  6. cargo build --target x86_64-unknown-linux-musl

Expected

The build completes successfully (like it does on this machine, when not cross-compiling).

Actual

The build fails with:

   Compiling cc v1.0.73
   Compiling pkg-config v0.3.24
   Compiling libc v0.2.121
   Compiling cmake v0.1.48
   Compiling libz-sys v1.1.5
error: failed to run custom build command for `libz-sys v1.1.5`

Caused by:
  process didn't exit successfully: `/Users/emorley/src/testcase-libz-sys-cross-compile/target/debug/build/libz-sys-720c0d8a10ea6099/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=LIBZ_SYS_STATIC
  cargo:rerun-if-changed=build.rs
  CMAKE_TOOLCHAIN_FILE_x86_64-unknown-linux-musl = None
  CMAKE_TOOLCHAIN_FILE_x86_64_unknown_linux_musl = None
  TARGET_CMAKE_TOOLCHAIN_FILE = None
  CMAKE_TOOLCHAIN_FILE = None
  CMAKE_GENERATOR_x86_64-unknown-linux-musl = None
  CMAKE_GENERATOR_x86_64_unknown_linux_musl = None
  TARGET_CMAKE_GENERATOR = None
  CMAKE_GENERATOR = None
  CMAKE_PREFIX_PATH_x86_64-unknown-linux-musl = None
  CMAKE_PREFIX_PATH_x86_64_unknown_linux_musl = None
  TARGET_CMAKE_PREFIX_PATH = None
  CMAKE_PREFIX_PATH = None
  CMAKE_x86_64-unknown-linux-musl = None
  CMAKE_x86_64_unknown_linux_musl = None
  TARGET_CMAKE = None
  CMAKE = None
  running: "cmake" "/Users/emorley/.cargo/registry/src/github.com-1ecc6299db9ec823/libz-sys-1.1.5/src/zlib-ng" "-DBUILD_SHARED_LIBS=OFF" "-DZLIB_COMPAT=ON" "-DWITH_GZFILEOP=ON" "-DCMAKE_INSTALL_PREFIX=/Users/emorley/src/testcase-libz-sys-cross-compile/target/x86_64-unknown-linux-musl/debug/build/libz-sys-2b3ea8f924a72b91/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_C_COMPILER=/opt/homebrew/bin/x86_64-unknown-linux-musl-gcc" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_CXX_COMPILER=/opt/homebrew/bin/x86_64-unknown-linux-musl-g++" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_ASM_COMPILER=/opt/homebrew/bin/x86_64-unknown-linux-musl-gcc" "-DCMAKE_BUILD_TYPE=Debug"
  -- Using CMake version 3.22.3
  -- ZLIB_HEADER_VERSION: 1.2.11
  -- ZLIBNG_HEADER_VERSION: 2.1.0.devel
  -- The C compiler identification is GNU 11.2.0
  -- Checking whether C compiler has -isysroot
  -- Checking whether C compiler has -isysroot - yes
  -- Checking whether C compiler supports OSX deployment target flag
  -- Checking whether C compiler supports OSX deployment target flag - no
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - failed
  -- Check for working C compiler: /opt/homebrew/bin/x86_64-unknown-linux-musl-gcc
  -- Check for working C compiler: /opt/homebrew/bin/x86_64-unknown-linux-musl-gcc - broken
  -- Configuring incomplete, errors occurred!
  See also "/Users/emorley/src/testcase-libz-sys-cross-compile/target/x86_64-unknown-linux-musl/debug/build/libz-sys-2b3ea8f924a72b91/out/build/CMakeFiles/CMakeOutput.log".
  See also "/Users/emorley/src/testcase-libz-sys-cross-compile/target/x86_64-unknown-linux-musl/debug/build/libz-sys-2b3ea8f924a72b91/out/build/CMakeFiles/CMakeError.log".

  --- stderr
  CMake Error at /opt/homebrew/Cellar/cmake/3.22.3/share/cmake/Modules/CMakeTestCCompiler.cmake:69 (message):
    The C compiler

      "/opt/homebrew/bin/x86_64-unknown-linux-musl-gcc"

    is not able to compile a simple test program.

    It fails with the following output:

      Change Dir: /Users/emorley/src/testcase-libz-sys-cross-compile/target/x86_64-unknown-linux-musl/debug/build/libz-sys-2b3ea8f924a72b91/out/build/CMakeFiles/CMakeTmp

      Run Build Command(s):/usr/bin/make -f Makefile cmTC_710b5/fast && /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_710b5.dir/build.make CMakeFiles/cmTC_710b5.dir/build
      Building C object CMakeFiles/cmTC_710b5.dir/testCCompiler.c.o
      /opt/homebrew/bin/x86_64-unknown-linux-musl-gcc   -ffunction-sections -fdata-sections -fPIC -m64  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -std=c11 -o CMakeFiles/cmTC_710b5.dir/testCCompiler.c.o -c /Users/emorley/src/testcase-libz-sys-cross-compile/target/x86_64-unknown-linux-musl/debug/build/libz-sys-2b3ea8f924a72b91/out/build/CMakeFiles/CMakeTmp/testCCompiler.c
      x86_64-unknown-linux-musl-gcc: error: unrecognized command-line option '-arch'
      make[1]: *** [CMakeFiles/cmTC_710b5.dir/testCCompiler.c.o] Error 1
      make: *** [cmTC_710b5/fast] Error 2

    CMake will not be able to correctly generate this project.
  Call Stack (most recent call first):
    CMakeLists.txt:37 (project)

  thread 'main' panicked at '
  command did not execute successfully, got: exit status: 1

  build script failed, must exit now', /Users/emorley/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.48/src/lib.rs:975:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Environment

edmorley commented 2 years ago

The CMakeError.log file mentioned contains:

Detecting C compiler ABI info failed to compile with the following output:
Change Dir: /Users/emorley/src/testcase-libz-sys-cross-compile/target/x86_64-unknown-linux-musl/debug/build/libz-sys-2b3ea8f924a72b91/out/build/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/make -f Makefile cmTC_1760d/fast && /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_1760d.dir/build.make CMakeFiles/cmTC_1760d.dir/build
Building C object CMakeFiles/cmTC_1760d.dir/CMakeCCompilerABI.c.o
/opt/homebrew/bin/x86_64-unknown-linux-musl-gcc   -ffunction-sections -fdata-sections -fPIC -m64  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk   -v -Wl,-v -std=c11 -o CMakeFiles/cmTC_1760d.dir/CMakeCCompilerABI.c.o -c /opt/homebrew/Cellar/cmake/3.22.3/share/cmake/Modules/CMakeCCompilerABI.c
Using built-in specs.
COLLECT_GCC=/opt/homebrew/bin/x86_64-unknown-linux-musl-gcc
x86_64-unknown-linux-musl-gcc: error: unrecognized command-line option '-arch'
Target: x86_64-unknown-linux-musl
Configured with: /Volumes/build/.build/x86_64-unknown-linux-musl/src/gcc/configure --build=aarch64-build_apple-darwin21.3.0 --host=aarch64-build_apple-darwin21.3.0 --target=x86_64-unknown-linux-musl --prefix=/Volumes/tools/x86_64-unknown-linux-musl --exec_prefix=/Volumes/tools/x86_64-unknown-linux-musl --with-sysroot=/Volumes/tools/x86_64-unknown-linux-musl/x86_64-unknown-linux-musl/sysroot --enable-languages=c,c++,fortran --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --disable-libmpx --with-gmp=/Volumes/build/.build/x86_64-unknown-linux-musl/buildtools --with-mpfr=/Volumes/build/.build/x86_64-unknown-linux-musl/buildtools --with-mpc=/Volumes/build/.build/x86_64-unknown-linux-musl/buildtools --with-isl=/Volumes/build/.build/x86_64-unknown-linux-musl/buildtools --enable-lto --enable-threads=posix --enable-target-optspace --with-linker-hash-style=both --enable-plugin --enable-gold --disable-nls --disable-multilib --with-local-prefix=/Volumes/tools/x86_64-unknown-linux-musl/x86_64-unknown-linux-musl/sysroot --enable-long-long
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.0 (GCC) 
make[1]: *** [CMakeFiles/cmTC_1760d.dir/CMakeCCompilerABI.c.o] Error 1
make: *** [cmTC_1760d/fast] Error 2

Determining if the C compiler works failed with the following output:
Change Dir: /Users/emorley/src/testcase-libz-sys-cross-compile/target/x86_64-unknown-linux-musl/debug/build/libz-sys-2b3ea8f924a72b91/out/build/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/make -f Makefile cmTC_bd1e5/fast && /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_bd1e5.dir/build.make CMakeFiles/cmTC_bd1e5.dir/build
Building C object CMakeFiles/cmTC_bd1e5.dir/testCCompiler.c.o
/opt/homebrew/bin/x86_64-unknown-linux-musl-gcc   -ffunction-sections -fdata-sections -fPIC -m64  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -std=c11 -o CMakeFiles/cmTC_bd1e5.dir/testCCompiler.c.o -c /Users/emorley/src/testcase-libz-sys-cross-compile/target/x86_64-unknown-linux-musl/debug/build/libz-sys-2b3ea8f924a72b91/out/build/CMakeFiles/CMakeTmp/testCCompiler.c
x86_64-unknown-linux-musl-gcc: error: unrecognized command-line option '-arch'
make[1]: *** [CMakeFiles/cmTC_bd1e5.dir/testCCompiler.c.o] Error 1
make: *** [cmTC_bd1e5/fast] Error 2

And .../out/build/CMakeFiles/3.22.3/CMakeSystem.cmake contains:

set(CMAKE_HOST_SYSTEM "Darwin-21.4.0")
set(CMAKE_HOST_SYSTEM_NAME "Darwin")
set(CMAKE_HOST_SYSTEM_VERSION "21.4.0")
set(CMAKE_HOST_SYSTEM_PROCESSOR "arm64")

set(CMAKE_SYSTEM "Darwin-21.4.0")
set(CMAKE_SYSTEM_NAME "Darwin")
set(CMAKE_SYSTEM_VERSION "21.4.0")
set(CMAKE_SYSTEM_PROCESSOR "arm64")

set(CMAKE_CROSSCOMPILING "FALSE")

set(CMAKE_SYSTEM_LOADED 1)
edmorley commented 2 years ago

Upgrading from CMake 3.22.3 to the newly released 3.23.0 (both installed via brew), doesn't help.

edmorley commented 2 years ago

I don't suppose anyone has ideas of what to try next? :-)