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

Cross-compiling from aarch64 to x86_64 fails (unrecognized command-line option '-m64) #119500

Closed
erikschul opened this issue Jan 1, 2024 · 3 comments
Labels
C-bug Category: This is a bug.

Comments

@erikschul
Copy link

erikschul commented Jan 1, 2024

Setup code
The runner should ideally setup correctly on either platform, and compile to either target.
Using debian12, running in docker.
Using cross is not an option (docker-in-docker etc.).

apt-get install -y gcc make \
    gcc-aarch64-linux-gnu \
    gcc-i686-linux-gnu \
    gcc-arm-linux-gnueabi \
    binutils \
    binutils-aarch64-linux-gnu \
    g++-aarch64-linux-gnu \
    g++-x86-64-linux-gnu \
    libc6-dev-arm64-cross

rustup update
rustup target add x86_64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-gnu

Error

cargo build --target=x86_64-unknown-linux-gnu

fails with

cc: error: unrecognized command-line option '-m64'

which makes sense if it's using cc, which only supports these targets:
cc -E -march=help -xc /dev/null

# 0 "/dev/null"
cc1: error: unknown value ‘help’ for ‘-march’
cc1: note: valid arguments are: armv8-a armv8.1-a armv8.2-a armv8.3-a armv8.4-a armv8.5-a armv8.6-a armv8.7-a armv8.8-a armv8-r armv9-a native

So I tried installing x86-64 gcc and setting it:

export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=/usr/bin/aarch64-linux-gnu-gcc

but its valid targets are:

nocona core2 nehalem corei7 westmere sandybridge corei7-avx ivybridge core-avx-i haswell core-avx2 broadwell skylake skylake-avx512 cannonlake icelake-client rocketlake icelake-server cascadelake tigerlake cooperlake sapphirerapids alderlake bonnell atom silvermont slm goldmont goldmont-plus tremont knl knm x86-64 x86-64-v2 x86-64-v3 x86-64-v4 eden-x2 nano nano-1000 nano-2000 nano-3000 nano-x2 eden-x4 nano-x4 k8 k8-sse3 opteron opteron-sse3 athlon64 athlon64-sse3 athlon-fx amdfam10 barcelona bdver1 bdver2 bdver3 bdver4 znver1 znver2 znver3 btver1 btver2

so I need to change the target to -mx86-64.

I tried overriding with RUSTFLAGS but that didn't work.

Problem

  • how can I override the -m target?
  • is there a better way?

Alternatives
It works with zigbuild: (I haven't tried to run the x86_64 binary)

cargo install cargo-zigbuild
cargo zigbuild --target aarch64-unknown-linux-gnu.2.17
cargo zigbuild --target x86_64-unknown-linux-gnu.2.17
@erikschul erikschul added the C-bug Category: This is a bug. label Jan 1, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 1, 2024
@adamgemmell
Copy link
Contributor

"g++-x86-64-linux-gnu" is the cross-compiler to x86_64 - so you want export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=/usr/bin/x86_64-linux-gnu-gcc (rather than aarch64-linux-gnu-gcc)

This is enough to get cargo build --target=x86_64-unknown-linux-gnu working.

However something's not quite adding up since https://packages.debian.org/stable/devel/g++-12-aarch64-linux-gnu isn't even available for aarch64 since it's a cross compiler TO aarch64. Are you definitely running docker on an aarch64 host?

@erikschul
Copy link
Author

@adamgemmell Thanks Adam, that seems to work!
I'm surprised that 64 is not in the targets list (/usr/bin/x86_64-linux-gnu-gcc -E -march=help -xc /dev/null), so I assumed it was an invalid argument.

It seems you're right about the package; apt performs a substitution:

apt-get install -y g++-aarch64-linux-gnu
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'g++' instead of 'g++-aarch64-linux-gnu'
The following additional packages will be installed:
  binutils binutils-aarch64-linux-gnu binutils-common cpp cpp-12 fontconfig-config fonts-dejavu-core g++-12 gcc gcc-12 ...

I feel it would make sense to make cross-compilation easier, by auto-detecting cross-platform, using relevant search paths, and auto-detecting missing dependencies? But that's a separate issue for the cargo project.

I will close this issue.

@adamgemmell
Copy link
Contributor

@erikschul -m64 is a separate command line option from -march.

The former enables code generation for x86-64, while the latter chooses a version of x86-64 to target. -mcpu is even more specific, letting you choose a certain CPU to optimise for.

I think you're also mixing up the flags - -m isn't a flag, the m is part of the name of the flag. Treat it as a prefix to signify something about the flag (maybe it stands for "machine"?)

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Jan 22, 2024
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants