Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 926772 - dev-lang/rust-1.75.0-r1 mishandles host and build CFLAGS when cross-compiling
Summary: dev-lang/rust-1.75.0-r1 mishandles host and build CFLAGS when cross-compiling
Status: UNCONFIRMED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Randy Barlow
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-03-11 16:14 UTC by jonys
Modified: 2024-03-13 20:49 UTC (History)
4 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
build.log of the failed cross-compile attempt (build.log.xz,47.51 KB, application/x-xz)
2024-03-11 16:14 UTC, jonys
Details

Note You need to log in before you can comment on or make changes to this bug.
Description jonys 2024-03-11 16:14:24 UTC
Created attachment 887367 [details]
build.log of the failed cross-compile attempt

When cross-compiling dev-lang/rust-1.75.0-r1 for aarch64-unknown-linux-gnu, the compilation failed with the following error:
```
  running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-march=armv8-a+crypto+crc" "-mtune=cortex-a72.cortex-a53" "-mfix-cortex-a53-835769" "-mfix-cortex-a53-843419" "-pipe" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/usr/aarch64-unknown-linux-gnu/tmp/portage/dev-lang/rust-1.75.0-r1/work/rustc-1.75.0-src/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/build/compiler_builtins-c76748dc165629dc/out/5e8c8d8503d8d0b7-absvdi2.o" "-c" "/usr/aarch64-unknown-linux-gnu/tmp/portage/dev-lang/rust-1.75.0-r1/work/rustc-1.75.0-src/src/llvm-project/compiler-rt/lib/builtins/absvdi2.c"
  cargo:warning=cc: error: unrecognized command-line option ‘-mfix-cortex-a53-835769’
  cargo:warning=cc: error: unrecognized command-line option ‘-mfix-cortex-a53-843419’
  exit status: 1
```

The reason is that the build system uses CHOST CFLAGS with the CBUILD toolchain. An identical failure was observed when cross-compiling to ppc and ppc64 hosts, so it is arch-independent.

The ebuild environment file contains the correct flags:
```
declare -x BUILD_CFLAGS="-O2 -ggdb -march=znver1 -pipe"
declare -x BUILD_CPPFLAGS=""
declare -x BUILD_CXXFLAGS="-O2 -ggdb -march=znver1 -pipe"
declare -x BUILD_LDFLAGS="-Wl,-O1 -Wl,--as-needed"
declare -x CBUILD="x86_64-pc-linux-gnu"
declare -x CCASFLAGS=""
declare -x CFLAGS="-O2 -march=armv8-a+crypto+crc -mtune=cortex-a72.cortex-a53 -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 -pipe"
declare -x CFLAGS_aarch64_unknown_linux_gnu=""
declare -x CFLAGS_default
declare -x CHOST="aarch64-unknown-linux-gnu"
declare -x CHOST_arm64="aarch64-unknown-linux-gnu"
declare -x CHOST_default="aarch64-unknown-linux-gnu"
declare -x CPPFLAGS=""
declare -x CTARGET_default="aarch64-unknown-linux-gnu"
declare -x CXXFLAGS="-O2 -march=armv8-a+crypto+crc -mtune=cortex-a72.cortex-a53 -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 -pipe"
declare -x DEFAULT_ABI="arm64"
```

But when the build system prints the flags it used during compilation, CFLAGS_x86_64-unknown-linux-gnu and CXXFLAGS_x86_64-unknown-linux-gnu are wrongly set to the host (arm64) CFLAGS, while CFLAGS_aarch64-unknown-linux-gnu is wrongly set to empty defaults; only CXXFLAGS_aarch64-unknown-linux-gnu are correct:
```
CC_aarch64-unknown-linux-gnu = "aarch64-unknown-linux-gnu-gcc"
CFLAGS_aarch64-unknown-linux-gnu = ["-ffunction-sections", "-fdata-sections", "-fPIC"]
CXX_aarch64-unknown-linux-gnu = "aarch64-unknown-linux-gnu-g++"
CXXFLAGS_aarch64-unknown-linux-gnu = ["-ffunction-sections", "-fdata-sections", "-fPIC", "-march=armv8-a+crypto+crc", "-mtune=cortex-a72.cortex-a53", "-mfix-cortex-a53-835769", "-mfix-cortex-a53-843419", "-pipe"]
AR_aarch64-unknown-linux-gnu = "aarch64-unknown-linux-gnu-ar"
CC_x86_64-unknown-linux-gnu = "cc"
CFLAGS_x86_64-unknown-linux-gnu = ["-ffunction-sections", "-fdata-sections", "-fPIC", "-m64", "-march=armv8-a+crypto+crc", "-mtune=cortex-a72.cortex-a53", "-mfix-cortex-a53-835769", "-mfix-cortex-a53-843419", "-pipe"]
CXX_x86_64-unknown-linux-gnu = "c++"
CXXFLAGS_x86_64-unknown-linux-gnu = ["-ffunction-sections", "-fdata-sections", "-fPIC", "-m64", "-march=armv8-a+crypto+crc", "-mtune=cortex-a72.cortex-a53", "-mfix-cortex-a53-835769", "-mfix-cortex-a53-843419", "-pipe"]
AR_x86_64-unknown-linux-gnu = "ar"
```
Comment 1 jonys 2024-03-11 16:27:08 UTC
I forgot to mention an important thing: I am cross-compiling Rust using a cross-toolchain installed on CBUILD, i.e., with the `system-bootstrap` USE flag.

This is not necessary for aarch64-unknown-linux-gnu, which can be bootstrapped from the upstream stage0, but the build fails in an identical manner when cross-compiling for tier3 hosts such as powerpc-gentoo-linux-musl, where system-bootstrap is the only possibility.
Comment 2 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-03-11 16:29:43 UTC
Can you give a set of commands to reproduce it?
Comment 3 jonys 2024-03-11 16:40:53 UTC
Sure. If you already have the cross build environment set up (rust installed on the build with proper LLVM_TARGETS, crossdev set up), then it's this emerge:
```
USE='dist system-bootstrap' LLVM_TARGETS='X86' CFLAGS='-O2 -march=armv8-a+crypto+crc -mtune=cortex-a72.cortex-a53 -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 -pipe' aarch64-unknown-linux-gnu-emerge -1 =dev-lang/rust-1.75.0-r1
```
Comment 4 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-03-11 16:56:28 UTC
(In reply to jonys from comment #3)
> Sure. If you already have the cross build environment set up (rust installed
> on the build with proper LLVM_TARGETS, crossdev set up), then it's this
> emerge:
> ```
> USE='dist system-bootstrap' LLVM_TARGETS='X86' CFLAGS='-O2
> -march=armv8-a+crypto+crc -mtune=cortex-a72.cortex-a53
> -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 -pipe'
> aarch64-unknown-linux-gnu-emerge -1 =dev-lang/rust-1.75.0-r1
> ```

I was hoping for a full set of commands as someone who knows crossdev but has no idea about it with Rust yet.
Comment 5 jonys 2024-03-12 08:05:24 UTC
(In reply to Sam James from comment #4)
> I was hoping for a full set of commands as someone who knows crossdev but
> has no idea about it with Rust yet.

OK. For most of the steps, you can just follow the guide on the Gentoo Wiki: https://wiki.gentoo.org/wiki/Cross_build_environment

```
## Crossdev setup
# build the C cross toolchain
crossdev -t aarch64-unknown-linux-gnu
# edit /usr/aarch64-unknown-linux-gnu/etc/portage/make.conf and
#    1. add CFLAGS="-O2 -march=armv8-a+crypto+crc -mtune=cortex-a72.cortex-a53 -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 -pipe"
#    2. add `-pam -su` to USE flags to prevent failures when emerging @system
# set the profile
ln -s /var/db/repos/gentoo/profiles/default/linux/arm64/17.0 /usr/aarch64-unknown-linux-gnu/etc/portage/make.profile
# build the host system
aarch64-unknown-linux-gnu-emerge -avu --keep-going @system

## Build system setup
# build the Rust cross toolchain
LLVM_TARGETS='AArch64 X86' USE=rust-src emerge -av1 dev-lang/rust

## Rust setup
# bootstrap Rust on the host system using the Rust toolchain from above
LLVM_TARGETS='AArch64 X86' USE='dist system-bootstrap' aarch64-unknown-linux-gnu-emerge -av1 =dev-lang/rust-1.75.0-r1
```

I hope this list is complete – I can't fully test it without removing my cross toolchains first, which I'm reluctant to do.