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

Error when cross-compiling to macOS with cc 1.2.12 #1388

Closed
tuxu opened this issue Feb 4, 2025 · 6 comments · Fixed by #1389
Closed

Error when cross-compiling to macOS with cc 1.2.12 #1388

tuxu opened this issue Feb 4, 2025 · 6 comments · Fixed by #1389
Labels
bug O-apple Apple targets and toolchains

Comments

@tuxu
Copy link

tuxu commented Feb 4, 2025

I have cross-compilation from Linux to macOS set up for a project. cc 1.2.12 omits the --target argument, which Clang (currently using 18.1.8) doesn't like.

Minimum reproducible example with a dummy project:

$ export CARGO_TARGET_AARCH64_APPLE_DARWIN_RUSTFLAGS="-C linker=clang -C link-arg=--target=aarch64-apple-darwin -C link-arg=-fuse-ld=lld"
$ cargo new hello
$ cd hello
$ cargo add bzip2
$ cargo build --target aarch64-apple-darwin

This fails with:

  cargo:warning=clang: error: unsupported option '-arch' for target 'aarch64-unknown-linux-gnu'
  cargo:warning=clang: warning: argument unused during compilation: '-mmacosx-version-min=11.0' [-Wunused-command-line-argument]

  --- stderr


  error occurred in cc-rs: Command LC_ALL="C" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-arch" "arm64" "-mmacosx-version-min=11.0" "-I" "bzip2-1.0.8" "-D_FILE_OFFSET_BITS=64" "-DBZ_NO_STDIO" "-o" "/tmp/hello/target/aarch64-apple-darwin/debug/build/bzip2-sys-9c6e283515f24e27/out/lib/a9dc8ba631b1466a-blocksort.o" "-c" "bzip2-1.0.8/blocksort.c" with args clang did not execute successfully (status code exit status: 1).
Click for full output
root@ac6839d585fb:/tmp# curl -L "https://github.com/phracker/MacOSX-SDKs/releases/download/11.0-11.1/MacOSX11.0.sdk.tar.xz" | tar xJ -C /opt
root@ac6839d585fb:/tmp# export SDKROOT=/opt/MacOSX11.0.sdk
root@ac6839d585fb:/tmp# export CARGO_TARGET_AARCH64_APPLE_DARWIN_RUSTFLAGS="-C linker=clang -C link-arg=--target=aarch64-apple-darwin -C link-arg=-fuse-ld=lld"
root@ac6839d585fb:/tmp# rustc --version
rustc 1.84.1 (e71f9a9a9 2025-01-27)
root@ac6839d585fb:/tmp# clang --version
clang version 18.1.8 (http://git.linaro.org/toolchain/jenkins-scripts.git e5def089cd9f5aa71524f82fef301ca66eaa38d2)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm/18.1.8/bin
root@ac6839d585fb:/tmp# cargo new hello
    Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
root@ac6839d585fb:/tmp# cd hello
root@ac6839d585fb:/tmp/hello# cargo add bzip2
    Updating crates.io index
      Adding bzip2 v0.5.0 to dependencies
             Features:
             - libbz2-rs-sys
             - static
    Updating crates.io index
     Locking 6 packages to latest compatible versions
root@ac6839d585fb:/tmp/hello# cargo build --target aarch64-apple-darwin
   Compiling shlex v1.3.0
   Compiling pkg-config v0.3.31
   Compiling libc v0.2.169
   Compiling cc v1.2.12
   Compiling bzip2-sys v0.1.11+1.0.8
warning: bzip2-sys@0.1.11+1.0.8: clang: error: unsupported option '-arch' for target 'aarch64-unknown-linux-gnu'
warning: bzip2-sys@0.1.11+1.0.8: clang: warning: argument unused during compilation: '-mmacosx-version-min=11.0' [-Wunused-command-line-argument]
error: failed to run custom build command for `bzip2-sys v0.1.11+1.0.8`

Caused by:
  process didn't exit successfully: `/tmp/hello/target/debug/build/bzip2-sys-a1f70d461238d3ac/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=BZIP2_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
  cargo:rerun-if-env-changed=PKG_CONFIG_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_aarch64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64_apple_darwin
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  OPT_LEVEL = Some(0)
  TARGET = Some(aarch64-apple-darwin)
  HOST = Some(aarch64-unknown-linux-gnu)
  cargo:rerun-if-env-changed=CC_aarch64-apple-darwin
  CC_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CC_aarch64_apple_darwin
  CC_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = Some(clang)
  cargo:rerun-if-env-changed=CC_KNOWN_WRAPPER_CUSTOM
  CC_KNOWN_WRAPPER_CUSTOM = None
  RUSTC_WRAPPER = None
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some(true)
  cargo:rerun-if-env-changed=MACOSX_DEPLOYMENT_TARGET
  MACOSX_DEPLOYMENT_TARGET = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64-apple-darwin
  CFLAGS_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64_apple_darwin
  CFLAGS_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  CARGO_ENCODED_RUSTFLAGS = Some(-Clinker=clang-Clink-arg=--target=aarch64-apple-darwin-Clink-arg=-fuse-ld=lld)
  cargo:warning=clang: error: unsupported option '-arch' for target 'aarch64-unknown-linux-gnu'
  cargo:warning=clang: warning: argument unused during compilation: '-mmacosx-version-min=11.0' [-Wunused-command-line-argument]

  --- stderr


  error occurred in cc-rs: Command LC_ALL="C" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-arch" "arm64" "-mmacosx-version-min=11.0" "-I" "bzip2-1.0.8" "-D_FILE_OFFSET_BITS=64" "-DBZ_NO_STDIO" "-o" "/tmp/hello/target/aarch64-apple-darwin/debug/build/bzip2-sys-9c6e283515f24e27/out/lib/a9dc8ba631b1466a-blocksort.o" "-c" "bzip2-1.0.8/blocksort.c" with args clang did not execute successfully (status code exit status: 1).

It works when downgrading cc to 1.2.11:

root@ac6839d585fb:/tmp/hello# cargo update -p cc --precise 1.2.11
    Updating crates.io index
 Downgrading cc v1.2.12 -> v1.2.11
root@ac6839d585fb:/tmp/hello# cargo build --target aarch64-apple-darwin
  Downloaded cc v1.2.11
  Downloaded 1 crate (102.0 KB) in 0.13s
   Compiling cc v1.2.11
   Compiling bzip2-sys v0.1.11+1.0.8
   Compiling bzip2 v0.5.0
   Compiling hello v0.1.0 (/tmp/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.80s

I can work around by adding --target=aarch64-apple-darwin to CFLAGS_aarch64_apple_darwin.

@madsmtm
Copy link
Collaborator

madsmtm commented Feb 4, 2025

Argh!

@madsmtm
Copy link
Collaborator

madsmtm commented Feb 4, 2025

I'll have a look, thanks for the report.

@madsmtm madsmtm added bug O-apple Apple targets and toolchains labels Feb 4, 2025
@madsmtm
Copy link
Collaborator

madsmtm commented Feb 5, 2025

I have cross-compilation from Linux to macOS set up for a project

Could you share your project setup (if it's GitHub Actions or similar)? I'd like to configure something similar for this repo when fixing the bug.

@tuxu
Copy link
Author

tuxu commented Feb 5, 2025

It's an internal project, but I'm happy to share a trimmed down Dockerfile:

Click here
FROM ubuntu:22.04

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get -qq update \
    && apt-get -qq install -y curl \
       make cmake ninja-build pkg-config \
       libc6-dev libgcc-11-dev libstdc++-11-dev \
    && rm -rf /var/lib/apt/lists/*

ENV LLVM_VERSION=18.1.8
RUN mkdir -p /opt/llvm/$LLVM_VERSION \
    && url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VERSION}/$(test $(arch) = aarch64 && echo "clang+llvm-${LLVM_VERSION}-aarch64-linux-gnu.tar.xz" || echo "clang+llvm-${LLVM_VERSION}-x86_64-linux-gnu-ubuntu-18.04.tar.xz") \
    && curl --fail -L $url | tar xJ --strip-components=1 -C /opt/llvm/$LLVM_VERSION
ENV PATH=/opt/llvm/$LLVM_VERSION/bin:$PATH

ARG RUST_VERSION=1.84.1
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
    | bash -s -- -y --default-toolchain=$RUST_VERSION
ENV PATH="/root/.cargo/bin:$PATH"

# Cross-compilation targets
RUN rustup target add \
    x86_64-apple-darwin \
    aarch64-apple-darwin

# Install macOS SDKs
RUN curl -L "https://github.com/phracker/MacOSX-SDKs/releases/download/11.0-11.1/MacOSX11.0.sdk.tar.xz" | tar xJ -C /opt
ENV SDKROOT=/opt/MacOSX11.0.sdk

# Set default compiler
ENV CC=clang CXX=clang++

# Set flags for cross-compilation
ENV CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS="-C linker=clang -C link-arg=--target=x86_64-apple-darwin -C link-arg=-fuse-ld=lld"
ENV CARGO_TARGET_AARCH64_APPLE_DARWIN_RUSTFLAGS="-C linker=clang -C link-arg=--target=aarch64-apple-darwin -C link-arg=-fuse-ld=lld"

# Workaround for cc-rs 1.2.12, see: https://github.com/rust-lang/cc-rs/issues/1388
ENV CFLAGS_x86_64_apple_darwin="--target=x86_64-apple-darwin"
ENV CFLAGS_aarch64_apple_darwin="--target=aarch64-apple-darwin"

# Trial build for supported platforms
RUN cd /tmp \
    && cargo new foo \
    && cd foo \
    # add dependency on C library for testing
    && cargo add bzip2 \
    # macOS builds through Clang + LLD
    && cargo build --target aarch64-apple-darwin \
    && cargo build --target x86_64-apple-darwin \
    && rm -rf /tmp/foo

@madsmtm
Copy link
Collaborator

madsmtm commented Feb 5, 2025

Thanks! I've filed #1389, and added a CI step loosely based on your work.

@tuxu
Copy link
Author

tuxu commented Feb 5, 2025

That's fantastic. Thanks for the swift fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug O-apple Apple targets and toolchains
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants