Skip to content

Commit 3acb505

Browse files
committed
Make the c feature for compiler-builtins opt-in instead of inferred
The build script for `compiler_builtins` doesn't support cross-compilation. I tried fixing it, but the cc crate itself doesn't appear to support cross-compiling to windows either unless you use the -gnu toolchain: ``` error occurred: Failed to find tool. Is `lib.exe` installed? ``` Rather than trying to fix it or special-case the platforms without bugs, make it opt-in instead of automatic.
1 parent 88a1922 commit 3acb505

File tree

9 files changed

+44
-22
lines changed

9 files changed

+44
-22
lines changed

config.toml.example

+4
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ changelog-seen = 2
291291
# on this runtime, such as `-C profile-generate` or `-C instrument-coverage`).
292292
#profiler = false
293293

294+
# Use the optimized LLVM C intrinsics for `compiler_builtins`, rather than Rust intrinsics.
295+
# Requires the LLVM submodule to be managed by bootstrap (i.e. not external).
296+
#optimized-compiler-builtins = false
297+
294298
# Indicates whether the native libraries linked into Cargo will be statically
295299
# linked or not.
296300
#cargo-native-static = false

src/bootstrap/compile.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,7 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
299299

300300
// Determine if we're going to compile in optimized C intrinsics to
301301
// the `compiler-builtins` crate. These intrinsics live in LLVM's
302-
// `compiler-rt` repository, but our `src/llvm-project` submodule isn't
303-
// always checked out, so we need to conditionally look for this. (e.g. if
304-
// an external LLVM is used we skip the LLVM submodule checkout).
302+
// `compiler-rt` repository.
305303
//
306304
// Note that this shouldn't affect the correctness of `compiler-builtins`,
307305
// but only its speed. Some intrinsics in C haven't been translated to Rust
@@ -312,8 +310,15 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
312310
// If `compiler-rt` is available ensure that the `c` feature of the
313311
// `compiler-builtins` crate is enabled and it's configured to learn where
314312
// `compiler-rt` is located.
315-
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
316-
let compiler_builtins_c_feature = if compiler_builtins_root.exists() {
313+
let compiler_builtins_c_feature = if builder.config.optimized_compiler_builtins {
314+
if !builder.is_rust_llvm(target) {
315+
panic!(
316+
"need a managed LLVM submodule for optimized intrinsics support; unset `llvm-config` or `optimized-compiler-builtins`"
317+
);
318+
}
319+
320+
builder.update_submodule(&Path::new("src").join("llvm-project"));
321+
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
317322
// Note that `libprofiler_builtins/build.rs` also computes this so if
318323
// you're changing something here please also change that.
319324
cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);

src/bootstrap/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ pub struct Config {
7373
pub color: Color,
7474
pub patch_binaries_for_nix: bool,
7575
pub stage0_metadata: Stage0Metadata,
76+
/// Whether to use the `c` feature of the `compiler_builtins` crate.
77+
pub optimized_compiler_builtins: bool,
7678

7779
pub on_fail: Option<String>,
7880
pub stage: u32,
@@ -597,6 +599,7 @@ define_config! {
597599
bench_stage: Option<u32> = "bench-stage",
598600
patch_binaries_for_nix: Option<bool> = "patch-binaries-for-nix",
599601
metrics: Option<bool> = "metrics",
602+
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
600603
}
601604
}
602605

@@ -916,6 +919,7 @@ impl Config {
916919
set(&mut config.print_step_timings, build.print_step_timings);
917920
set(&mut config.print_step_rusage, build.print_step_rusage);
918921
set(&mut config.patch_binaries_for_nix, build.patch_binaries_for_nix);
922+
set(&mut config.optimized_compiler_builtins, build.optimized_compiler_builtins);
919923

920924
config.verbose = cmp::max(config.verbose, flags.verbose);
921925

src/bootstrap/dist.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -1838,23 +1838,21 @@ fn add_env(builder: &Builder<'_>, cmd: &mut Command, target: TargetSelection) {
18381838
///
18391839
/// Returns whether the files were actually copied.
18401840
fn maybe_install_llvm(builder: &Builder<'_>, target: TargetSelection, dst_libdir: &Path) -> bool {
1841-
if let Some(config) = builder.config.target_config.get(&target) {
1842-
if config.llvm_config.is_some() && !builder.config.llvm_from_ci {
1843-
// If the LLVM was externally provided, then we don't currently copy
1844-
// artifacts into the sysroot. This is not necessarily the right
1845-
// choice (in particular, it will require the LLVM dylib to be in
1846-
// the linker's load path at runtime), but the common use case for
1847-
// external LLVMs is distribution provided LLVMs, and in that case
1848-
// they're usually in the standard search path (e.g., /usr/lib) and
1849-
// copying them here is going to cause problems as we may end up
1850-
// with the wrong files and isn't what distributions want.
1851-
//
1852-
// This behavior may be revisited in the future though.
1853-
//
1854-
// If the LLVM is coming from ourselves (just from CI) though, we
1855-
// still want to install it, as it otherwise won't be available.
1856-
return false;
1857-
}
1841+
if !builder.is_rust_llvm(target) {
1842+
// If the LLVM was externally provided, then we don't currently copy
1843+
// artifacts into the sysroot. This is not necessarily the right
1844+
// choice (in particular, it will require the LLVM dylib to be in
1845+
// the linker's load path at runtime), but the common use case for
1846+
// external LLVMs is distribution provided LLVMs, and in that case
1847+
// they're usually in the standard search path (e.g., /usr/lib) and
1848+
// copying them here is going to cause problems as we may end up
1849+
// with the wrong files and isn't what distributions want.
1850+
//
1851+
// This behavior may be revisited in the future though.
1852+
//
1853+
// If the LLVM is coming from ourselves (just from CI) though, we
1854+
// still want to install it, as it otherwise won't be available.
1855+
return false;
18581856
}
18591857

18601858
// On macOS, rustc (and LLVM tools) link to an unversioned libLLVM.dylib

src/ci/docker/host-x86_64/disabled/dist-x86_64-haiku/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ ENV RUST_CONFIGURE_ARGS --disable-jemalloc \
4747
--set=$TARGET.cc=x86_64-unknown-haiku-gcc \
4848
--set=$TARGET.cxx=x86_64-unknown-haiku-g++ \
4949
--set=$TARGET.llvm-config=/bin/llvm-config-haiku
50+
ENV EXTERNAL_LLVM 1
51+
5052
ENV SCRIPT python3 ../x.py dist --host=$HOST --target=$HOST

src/ci/docker/host-x86_64/dist-various-2/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,6 @@ ENV RUST_CONFIGURE_ARGS --enable-extended --enable-lld --disable-docs \
129129
--set target.wasm32-wasi.wasi-root=/wasm32-wasi \
130130
--musl-root-armv7=/musl-armv7
131131

132+
ENV EXTERNAL_LLVM 1
133+
132134
ENV SCRIPT python3 ../x.py dist --host='' --target $TARGETS

src/ci/docker/host-x86_64/x86_64-gnu-llvm-13-stage1/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ RUN sh /scripts/sccache.sh
2929
# We are disabling CI LLVM since this builder is intentionally using a host
3030
# LLVM, rather than the typical src/llvm-project LLVM.
3131
ENV NO_DOWNLOAD_CI_LLVM 1
32+
ENV EXTERNAL_LLVM 1
3233

3334
# Using llvm-link-shared due to libffi issues -- see #34486
3435
ENV RUST_CONFIGURE_ARGS \

src/ci/docker/host-x86_64/x86_64-gnu-llvm-13/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ RUN sh /scripts/sccache.sh
4040
# We are disabling CI LLVM since this builder is intentionally using a host
4141
# LLVM, rather than the typical src/llvm-project LLVM.
4242
ENV NO_DOWNLOAD_CI_LLVM 1
43+
ENV EXTERNAL_LLVM 1
4344

4445
# Using llvm-link-shared due to libffi issues -- see #34486
4546
ENV RUST_CONFIGURE_ARGS \

src/ci/run.sh

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-units-std=1"
6969
# space required for CI artifacts.
7070
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --dist-compression-formats=xz"
7171

72+
# Enable the `c` feature for compiler_builtins, but only when the `compiler-rt` source is available.
73+
if [ "$EXTERNAL_LLVM" = "" ]; then
74+
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set build.optimized-compiler-builtins"
75+
fi
76+
7277
if [ "$DIST_SRC" = "" ]; then
7378
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-dist-src"
7479
fi

0 commit comments

Comments
 (0)