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 confuses wrong target environment variables such as CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER when building for aarch64 #1598

Closed
4 of 11 tasks
tgrushka opened this issue Dec 26, 2024 · 2 comments

Comments

@tgrushka
Copy link

tgrushka commented Dec 26, 2024

Checklist

Describe your issue

Build fails with cross when I build with cross for Raspberry Pi, etc.:

cross build --target aarch64-unknown-linux-gnu

on my amd64 Linux system, and have local system environment for all my other, non-cross Rust projects:

export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER='/usr/bin/clang'
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-Z threads=8 -C linker=/usr/bin/clang -C link-arg=-fuse-ld=/usr/local/bin/mold"
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTC_WRAPPER='/home/tom/.cargo/bin/sccache'

Error

cross build --target aarch64-unknown-linux-gnu
   Compiling proc-macro2 v1.0.92
   Compiling serde v1.0.216
   Compiling serde_json v1.0.134
error: linker `/usr/bin/clang` not found
  |
  = note: No such file or directory (os error 2)

error: could not compile `serde_json` (build script) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
...

When I remove those environment variables, my cross build succeeds:

set -e CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER
set -e CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS
set -e CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTC_WRAPPER
cross build --target aarch64-unknown-linux-gnu
   Compiling proc-macro2 v1.0.92
   Compiling unicode-ident v1.0.14
   Compiling serde v1.0.216
   Compiling serde_json v1.0.134
...
   Compiling cross-test v0.1.0 (/project)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.83s

Conclusion: It seems cross is "confusing" the environment variables specific to one target with a different target.

Versions

cross --version
cross 0.2.5
[cross] note: Falling back to `cargo` on the host.
cargo 1.85.0-nightly (652623b77 2024-12-20)

rustc --version
rustc 1.85.0-nightly (bdc6b3de4 2024-12-23)

uname -a
Linux framework-mint 6.8.0-51-generic #52-Ubuntu SMP PREEMPT_DYNAMIC Thu Dec  5 13:09:44 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

What target(s) are you cross-compiling for?

aarch64-unknown-linux-gnu

Which operating system is the host (e.g computer cross is on) running?

  • macOS
  • Windows
  • Linux / BSD
  • other OS (specify in description)

What architecture is the host?

  • x86_64 / AMD64
  • arm32
  • arm64 (including Mac M1)

What container engine is cross using?

  • docker
  • podman
  • other container engine (specify in description)

cross version

cross 0.2.5

Example

This error seems not to occur in a new project with no dependencies, so let's add some:

cargo new cross-test
cd cross-test
cargo add serde serde-json

Ensure it works with default config (ensure a clean shell with no CARGO_TARGET_ variables set):

env | grep CARGO_TARGET_

cross build --target aarch64-unknown-linux-gnu

Build succeeds. Add different target config than building with cross:

export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER='/usr/bin/clang'
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-Z threads=8 -C linker=/usr/bin/clang -C link-arg=-fuse-ld=/usr/local/bin/mold"
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTC_WRAPPER='/home/tom/.cargo/bin/sccache'

cross build --target aarch64-unknown-linux-gnu

Build fails:

cross build --target aarch64-unknown-linux-gnu
   Compiling serde v1.0.216
   Compiling serde_json v1.0.134
   Compiling ryu v1.0.18
   Compiling itoa v1.0.14
   Compiling memchr v2.7.4
error: linker `/usr/bin/clang` not found
  |
  = note: No such file or directory (os error 2)

error: could not compile `serde_json` (build script) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
...

Additional information / notes

A whole bunch of results come up when I search issues for environment variables. I can't tell if they're related. There was another issue closed back in 2020 that seems related, but still not sure.

@tgrushka tgrushka changed the title cross confuses wrong target environment variables such as CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER cross confuses wrong target environment variables such as CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER when building for aarch64 Dec 26, 2024
@tgrushka
Copy link
Author

Verbose output

It seems there may be 2 issues:

  • cross is passing all the CARGO_TARGET_ env vars, even if they are blank. Shouldn't cargo itself ignore them if they are blank? That's a cargo issue, but why is cross passing them if empty?
  • cross seems to then confuse the target architectures -- shouldn't it pass only the intended target env vars to cargo?

Without CARGO_TARGET_ env:

cross build --verbose --target aarch64-unknown-linux-gnu
+ cargo metadata --format-version 1 --filter-platform aarch64-unknown-linux-gnu
+ rustc --print sysroot
+ rustup toolchain list
+ rustup target list --toolchain nightly-x86_64-unknown-linux-gnu
+ rustup component list --toolchain nightly-x86_64-unknown-linux-gnu
+ /usr/local/bin/docker
+ /usr/local/bin/docker run --userns host -e 'PKG_CONFIG_ALLOW_CROSS=1' -e 'XARGO_HOME=/xargo' -e 'CARGO_HOME=/cargo' -e 'CARGO_TARGET_DIR=/target' -e 'CROSS_RUNNER=' -e CARGO_REGISTRY_TOKEN -e TERM -e 'USER=tom' --rm --user 1000:1000 -v /home/tom/.xargo:/xargo:z -v /home/tom/.cargo:/cargo:z -v /cargo/bin -v /home/tom/src/cross-test:/project:z -v /home/tom/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu:/rust:z,ro -v /home/tom/src/cross-test/target:/target:z -w /project -i -t ghcr.io/cross-rs/aarch64-unknown-linux-gnu:0.2.5 sh -c 'PATH=$PATH:/rust/bin cargo build --verbose --target aarch64-unknown-linux-gnu'
...

With even blank CARGO_TARGET_ env:

export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=''
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS=''
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTC_WRAPPER=''

cross build --verbose --target aarch64-unknown-linux-gnu
+ cargo metadata --format-version 1 --filter-platform aarch64-unknown-linux-gnu
+ rustc --print sysroot
+ rustup toolchain list
+ rustup target list --toolchain nightly-x86_64-unknown-linux-gnu
+ rustup component list --toolchain nightly-x86_64-unknown-linux-gnu
+ /usr/local/bin/docker
+ /usr/local/bin/docker run --userns host -e 'PKG_CONFIG_ALLOW_CROSS=1' -e 'XARGO_HOME=/xargo' -e 'CARGO_HOME=/cargo' -e 'CARGO_TARGET_DIR=/target' -e 'CROSS_RUNNER=' -e CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTC_WRAPPER -e TERM -e CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS -e CARGO_REGISTRY_TOKEN -e CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER -e 'USER=tom' --rm --user 1000:1000 -v /home/tom/.xargo:/xargo:z -v /home/tom/.cargo:/cargo:z -v /cargo/bin -v /home/tom/src/cross-test:/project:z -v /home/tom/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu:/rust:z,ro -v /home/tom/src/cross-test/target:/target:z -w /project -i -t ghcr.io/cross-rs/aarch64-unknown-linux-gnu:0.2.5 sh -c 'PATH=$PATH:/rust/bin cargo build --verbose --target aarch64-unknown-linux-gnu'
       Dirty serde v1.0.216: the config settings changed
   Compiling serde v1.0.216
       Dirty serde_json v1.0.134: the config settings changed
   Compiling serde_json v1.0.134
       Fresh ryu v1.0.18
       Fresh itoa v1.0.14
       Fresh memchr v2.7.4
     Running `rustc --crate-name build_script_build --edition=2018 /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde-1.0.216/build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=87 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="std"' --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("alloc", "default", "derive", "rc", "serde_derive", "std", "unstable"))' -C metadata=8d6fac6aa058f87e -C extra-filename=-50b94897c7732765 --out-dir /target/debug/build/serde-50b94897c7732765 -C linker= -L dependency=/target/debug/deps --cap-lints allow`
     Running `rustc --crate-name build_script_build --edition=2021 /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde_json-1.0.134/build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=87 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="std"' --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("alloc", "arbitrary_precision", "default", "float_roundtrip", "indexmap", "preserve_order", "raw_value", "std", "unbounded_depth"))' -C metadata=08be64cc0f951fa7 -C extra-filename=-c6d6a1facbc9aa6a --out-dir /target/debug/build/serde_json-c6d6a1facbc9aa6a -C linker= -L dependency=/target/debug/deps --cap-lints allow`
error: couldn't extract file stem from specified linker

error: could not compile `serde_json` (build script) due to 1 previous error

Caused by:
  process didn't exit successfully: `rustc --crate-name build_script_build --edition=2021 /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde_json-1.0.134/build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=87 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="std"' --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("alloc", "arbitrary_precision", "default", "float_roundtrip", "indexmap", "preserve_order", "raw_value", "std", "unbounded_depth"))' -C metadata=08be64cc0f951fa7 -C extra-filename=-c6d6a1facbc9aa6a --out-dir /target/debug/build/serde_json-c6d6a1facbc9aa6a -C linker= -L dependency=/target/debug/deps --cap-lints allow` (exit status: 1)
warning: build failed, waiting for other jobs to finish...
error: could not compile `serde` (build script) due to 1 previous error

Caused by:
  process didn't exit successfully: `rustc --crate-name build_script_build --edition=2018 /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde-1.0.216/build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=87 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="std"' --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("alloc", "default", "derive", "rc", "serde_derive", "std", "unstable"))' -C metadata=8d6fac6aa058f87e -C extra-filename=-50b94897c7732765 --out-dir /target/debug/build/serde-50b94897c7732765 -C linker= -L dependency=/target/debug/deps --cap-lints allow` (exit status: 1)
+ rustup component list --toolchain nightly-x86_64-unknown-linux-gnu

@Emilgardis
Copy link
Member

Emilgardis commented Dec 27, 2024

This is expected behaviour, the problem is the build scripts target is x86_64-unknown-linux-gnu, and it's the build script binary build that is complaining, not the actual crate binary

error: could not compile serde_json (build script) due to 1 previous error

note (build script) here

@Emilgardis Emilgardis closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants