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

Invalid aarch64-unknown-none target produced if the CC_aarch64_unknown_none env var is not set #111142

Open
pietroalbini opened this issue May 3, 2023 · 1 comment
Labels
A-cross Area: Cross compilation C-bug Category: This is a bug. O-AArch64 Armv8-A or later processors in AArch64 mode T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

Comments

@pietroalbini
Copy link
Member

pietroalbini commented May 3, 2023

If you try to compile the core library for the aarch64-unknown-none target, and the CC_aarch64_unknown_none environment variable is not set, you will end up with a broken target that can't link any basic program:

error: linking with `rust-lld` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="<redacted>" VSLANG="1033" "rust-lld" "-flavor" "gnu" "/tmp/rustcPX9tnr/symbols.o" "binary.binary.ec7d7d9e5618561c-cgu.0.rcgu.o" "--as-needed" "-L" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib" "-Bstatic" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/librustc_std_workspace_core-3304236ec67a17db.rlib" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcore-27e4e5b40a434022.rlib" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib" "-Bdynamic" "--eh-frame-hdr" "-z" "noexecstack" "-L" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib" "-o" "binary" "--gc-sections"
  = note: rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas1_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas1_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas1_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas1_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas2_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas2_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas2_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas2_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas4_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas4_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas4_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas4_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas8_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas8_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas8_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas8_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas16_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas16_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas16_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas16_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)

Inspecting the libcompiler_builtins-5d101b3353699a4d.rlib file, we can see that the codegen units produced by rustc are indeed aarch64, but the object files produced by the compiler_builtins's build script are x86_64:

$ file compiler_builtins-5d101b3353699a4d.compiler_builtins.aeeb364441e31d74-cgu.0.rcgu.o 
compiler_builtins-5d101b3353699a4d.compiler_builtins.aeeb364441e31d74-cgu.0.rcgu.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not stripped
$ file lse_cas1_relax.o
lse_cas1_relax.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), stripped

Having a "polyglot" libcompiler_builtins.rlib makes the linker sad, and results in the confusing linker error messages above. Setting the CC_aarch64_unknown_none environment variable pointing to a valid compiler for the target fixes the problem.

CI-built tarballs are not affected by this (as it sets the CC_aarch64_unknown_none environment variable), but right now this is a huge footgun for anyone trying to build the target locally.

We should add some safeguards to prevent this from happening. Ideally we would change the build script of compiler_builtins not to YOLO and use the host compiler if it doesn't find the target compiler. If that doesn't work, bootstrap should abort the build if the c feature of compiler_builtins is enabled and the corresponding CC_* environment variable isn't set.

@jyn514 jyn514 added A-cross Area: Cross compilation T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) C-bug Category: This is a bug. labels May 3, 2023
@Jules-Bertholet
Copy link
Contributor

@rustbot label O-AArch64

@rustbot rustbot added the O-AArch64 Armv8-A or later processors in AArch64 mode label May 4, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 9, 2023
…r-ozkan

Prevent using the default `cc` when that'd result in a broken build

This PR adds a check in bootstrap to prevent issues like rust-lang#111142 to happen. What happened there is, no compiler for `aarch64-unknown-none` was detected by bootstrap, and so the `cc` crate defaulted to the system host C compiler. That resulted in a broken target, as `libcompiler_builtins.rlib` (or well anything compiling C code in its build script) contained half the object files compiled for aarch64 and half of them compiled for x86_64.

The check added in the PR ensures that, for cross-compilation, the detected C compiler is not the same one as the host compiler, unless the detected compiler is clang (as it supports all targets from a single binary). This should not cause too many false positives, as for example a `./x build library --target i686-unknown-linux-gnu` still works from a x86_64 host. If a false positive occurs, the error message still explains the workaround.

Example error message:

```
  running: "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S"
  cargo:warning=
  cargo:warning=   Error: the automatic detection of the C compiler for cross-compiled
  cargo:warning=   target aarch64-unknown-none returned the compiler also used for the
  cargo:warning=   current host platform.
  cargo:warning=
  cargo:warning=   This is likely wrong, and will likely result in a broken compilation
  cargo:warning=   artifact. Please specify the correct compiler for that target, either
  cargo:warning=   with environment variables:
  cargo:warning=
  cargo:warning=       CC_aarch64_unknown_none=path/to/cc
  cargo:warning=       CXX_aarch64_unknown_none=path/to/cxx
  cargo:warning=
  cargo:warning=   ...or in config.toml:
  cargo:warning=
  cargo:warning=       [target."aarch64-unknown-none"]
  cargo:warning=       cc = "path/to/cc"
  cargo:warning=       cxx = "path/to/cxx"
  cargo:warning=
  cargo:warning=   The detected C compiler was:
  cargo:warning=
  cargo:warning=       cc
  cargo:warning=
  exit status: 1

  --- stderr

  error occurred: Command "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S" with args "broken-cc" did not execute successfully (status code exit status: 1).

Build completed unsuccessfully in 0:00:02
```

Fixes rust-lang#111142
r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 10, 2023
…r-ozkan

Prevent using the default `cc` when that'd result in a broken build

This PR adds a check in bootstrap to prevent issues like rust-lang#111142 to happen. What happened there is, no compiler for `aarch64-unknown-none` was detected by bootstrap, and so the `cc` crate defaulted to the system host C compiler. That resulted in a broken target, as `libcompiler_builtins.rlib` (or well anything compiling C code in its build script) contained half the object files compiled for aarch64 and half of them compiled for x86_64.

The check added in the PR ensures that, for cross-compilation, the detected C compiler is not the same one as the host compiler, unless the detected compiler is clang (as it supports all targets from a single binary). This should not cause too many false positives, as for example a `./x build library --target i686-unknown-linux-gnu` still works from a x86_64 host. If a false positive occurs, the error message still explains the workaround.

Example error message:

```
  running: "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S"
  cargo:warning=
  cargo:warning=   Error: the automatic detection of the C compiler for cross-compiled
  cargo:warning=   target aarch64-unknown-none returned the compiler also used for the
  cargo:warning=   current host platform.
  cargo:warning=
  cargo:warning=   This is likely wrong, and will likely result in a broken compilation
  cargo:warning=   artifact. Please specify the correct compiler for that target, either
  cargo:warning=   with environment variables:
  cargo:warning=
  cargo:warning=       CC_aarch64_unknown_none=path/to/cc
  cargo:warning=       CXX_aarch64_unknown_none=path/to/cxx
  cargo:warning=
  cargo:warning=   ...or in config.toml:
  cargo:warning=
  cargo:warning=       [target."aarch64-unknown-none"]
  cargo:warning=       cc = "path/to/cc"
  cargo:warning=       cxx = "path/to/cxx"
  cargo:warning=
  cargo:warning=   The detected C compiler was:
  cargo:warning=
  cargo:warning=       cc
  cargo:warning=
  exit status: 1

  --- stderr

  error occurred: Command "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S" with args "broken-cc" did not execute successfully (status code exit status: 1).

Build completed unsuccessfully in 0:00:02
```

Fixes rust-lang#111142
r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 13, 2023
…r-ozkan

Prevent using the default `cc` when that'd result in a broken build

This PR adds a check in bootstrap to prevent issues like rust-lang#111142 to happen. What happened there is, no compiler for `aarch64-unknown-none` was detected by bootstrap, and so the `cc` crate defaulted to the system host C compiler. That resulted in a broken target, as `libcompiler_builtins.rlib` (or well anything compiling C code in its build script) contained half the object files compiled for aarch64 and half of them compiled for x86_64.

The check added in the PR ensures that, for cross-compilation, the detected C compiler is not the same one as the host compiler, unless the detected compiler is clang (as it supports all targets from a single binary). This should not cause too many false positives, as for example a `./x build library --target i686-unknown-linux-gnu` still works from a x86_64 host. If a false positive occurs, the error message still explains the workaround.

Example error message:

```
  running: "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S"
  cargo:warning=
  cargo:warning=   Error: the automatic detection of the C compiler for cross-compiled
  cargo:warning=   target aarch64-unknown-none returned the compiler also used for the
  cargo:warning=   current host platform.
  cargo:warning=
  cargo:warning=   This is likely wrong, and will likely result in a broken compilation
  cargo:warning=   artifact. Please specify the correct compiler for that target, either
  cargo:warning=   with environment variables:
  cargo:warning=
  cargo:warning=       CC_aarch64_unknown_none=path/to/cc
  cargo:warning=       CXX_aarch64_unknown_none=path/to/cxx
  cargo:warning=
  cargo:warning=   ...or in config.toml:
  cargo:warning=
  cargo:warning=       [target."aarch64-unknown-none"]
  cargo:warning=       cc = "path/to/cc"
  cargo:warning=       cxx = "path/to/cxx"
  cargo:warning=
  cargo:warning=   The detected C compiler was:
  cargo:warning=
  cargo:warning=       cc
  cargo:warning=
  exit status: 1

  --- stderr

  error occurred: Command "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S" with args "broken-cc" did not execute successfully (status code exit status: 1).

Build completed unsuccessfully in 0:00:02
```

Fixes rust-lang#111142
r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 14, 2023
…r-ozkan

Prevent using the default `cc` when that'd result in a broken build

This PR adds a check in bootstrap to prevent issues like rust-lang#111142 to happen. What happened there is, no compiler for `aarch64-unknown-none` was detected by bootstrap, and so the `cc` crate defaulted to the system host C compiler. That resulted in a broken target, as `libcompiler_builtins.rlib` (or well anything compiling C code in its build script) contained half the object files compiled for aarch64 and half of them compiled for x86_64.

The check added in the PR ensures that, for cross-compilation, the detected C compiler is not the same one as the host compiler, unless the detected compiler is clang (as it supports all targets from a single binary). This should not cause too many false positives, as for example a `./x build library --target i686-unknown-linux-gnu` still works from a x86_64 host. If a false positive occurs, the error message still explains the workaround.

Example error message:

```
  running: "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S"
  cargo:warning=
  cargo:warning=   Error: the automatic detection of the C compiler for cross-compiled
  cargo:warning=   target aarch64-unknown-none returned the compiler also used for the
  cargo:warning=   current host platform.
  cargo:warning=
  cargo:warning=   This is likely wrong, and will likely result in a broken compilation
  cargo:warning=   artifact. Please specify the correct compiler for that target, either
  cargo:warning=   with environment variables:
  cargo:warning=
  cargo:warning=       CC_aarch64_unknown_none=path/to/cc
  cargo:warning=       CXX_aarch64_unknown_none=path/to/cxx
  cargo:warning=
  cargo:warning=   ...or in config.toml:
  cargo:warning=
  cargo:warning=       [target."aarch64-unknown-none"]
  cargo:warning=       cc = "path/to/cc"
  cargo:warning=       cxx = "path/to/cxx"
  cargo:warning=
  cargo:warning=   The detected C compiler was:
  cargo:warning=
  cargo:warning=       cc
  cargo:warning=
  exit status: 1

  --- stderr

  error occurred: Command "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S" with args "broken-cc" did not execute successfully (status code exit status: 1).

Build completed unsuccessfully in 0:00:02
```

Fixes rust-lang#111142
r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 15, 2023
…r-ozkan

Prevent using the default `cc` when that'd result in a broken build

This PR adds a check in bootstrap to prevent issues like rust-lang#111142 to happen. What happened there is, no compiler for `aarch64-unknown-none` was detected by bootstrap, and so the `cc` crate defaulted to the system host C compiler. That resulted in a broken target, as `libcompiler_builtins.rlib` (or well anything compiling C code in its build script) contained half the object files compiled for aarch64 and half of them compiled for x86_64.

The check added in the PR ensures that, for cross-compilation, the detected C compiler is not the same one as the host compiler, unless the detected compiler is clang (as it supports all targets from a single binary). This should not cause too many false positives, as for example a `./x build library --target i686-unknown-linux-gnu` still works from a x86_64 host. If a false positive occurs, the error message still explains the workaround.

Example error message:

```
  running: "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S"
  cargo:warning=
  cargo:warning=   Error: the automatic detection of the C compiler for cross-compiled
  cargo:warning=   target aarch64-unknown-none returned the compiler also used for the
  cargo:warning=   current host platform.
  cargo:warning=
  cargo:warning=   This is likely wrong, and will likely result in a broken compilation
  cargo:warning=   artifact. Please specify the correct compiler for that target, either
  cargo:warning=   with environment variables:
  cargo:warning=
  cargo:warning=       CC_aarch64_unknown_none=path/to/cc
  cargo:warning=       CXX_aarch64_unknown_none=path/to/cxx
  cargo:warning=
  cargo:warning=   ...or in config.toml:
  cargo:warning=
  cargo:warning=       [target."aarch64-unknown-none"]
  cargo:warning=       cc = "path/to/cc"
  cargo:warning=       cxx = "path/to/cxx"
  cargo:warning=
  cargo:warning=   The detected C compiler was:
  cargo:warning=
  cargo:warning=       cc
  cargo:warning=
  exit status: 1

  --- stderr

  error occurred: Command "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S" with args "broken-cc" did not execute successfully (status code exit status: 1).

Build completed unsuccessfully in 0:00:02
```

Fixes rust-lang#111142
r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 8, 2023
…r-ozkan

Prevent using the default `cc` when that'd result in a broken build

This PR adds a check in bootstrap to prevent issues like rust-lang#111142 to happen. What happened there is, no compiler for `aarch64-unknown-none` was detected by bootstrap, and so the `cc` crate defaulted to the system host C compiler. That resulted in a broken target, as `libcompiler_builtins.rlib` (or well anything compiling C code in its build script) contained half the object files compiled for aarch64 and half of them compiled for x86_64.

The check added in the PR ensures that, for cross-compilation, the detected C compiler is not the same one as the host compiler, unless the detected compiler is clang (as it supports all targets from a single binary). This should not cause too many false positives, as for example a `./x build library --target i686-unknown-linux-gnu` still works from a x86_64 host. If a false positive occurs, the error message still explains the workaround.

Example error message:

```
  running: "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S"
  cargo:warning=
  cargo:warning=   Error: the automatic detection of the C compiler for cross-compiled
  cargo:warning=   target aarch64-unknown-none returned the compiler also used for the
  cargo:warning=   current host platform.
  cargo:warning=
  cargo:warning=   This is likely wrong, and will likely result in a broken compilation
  cargo:warning=   artifact. Please specify the correct compiler for that target, either
  cargo:warning=   with environment variables:
  cargo:warning=
  cargo:warning=       CC_aarch64_unknown_none=path/to/cc
  cargo:warning=       CXX_aarch64_unknown_none=path/to/cxx
  cargo:warning=
  cargo:warning=   ...or in config.toml:
  cargo:warning=
  cargo:warning=       [target."aarch64-unknown-none"]
  cargo:warning=       cc = "path/to/cc"
  cargo:warning=       cxx = "path/to/cxx"
  cargo:warning=
  cargo:warning=   The detected C compiler was:
  cargo:warning=
  cargo:warning=       cc
  cargo:warning=
  exit status: 1

  --- stderr

  error occurred: Command "/home/pietro/r/github/rust-lang/rust/2/build/bootstrap/debug/broken-cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-ffunction-sections" "-fdata-sections" "-fPIC" "--broken-cc-target=aarch64-unknown-none" "--broken-cc-detected=cc" "-I" "/home/pietro/r/github/rust-lang/rust/2/src/llvm-project/compiler-rt/lib/builtins" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/ea072bc2688e9ac2-lse_cas1_relax.o" "-c" "/home/pietro/r/github/rust-lang/rust/2/build/x86_64-unknown-linux-gnu/stage0-std/aarch64-unknown-none/release/build/compiler_builtins-13da5a491a5cb882/out/lse_cas1_relax.S" with args "broken-cc" did not execute successfully (status code exit status: 1).

Build completed unsuccessfully in 0:00:02
```

Fixes rust-lang#111142
r? `@jyn514`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cross Area: Cross compilation C-bug Category: This is a bug. O-AArch64 Armv8-A or later processors in AArch64 mode T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants