Skip to content

Commit a6a59d2

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 4353b1e commit a6a59d2

File tree

8 files changed

+46
-22
lines changed

8 files changed

+46
-22
lines changed

config.example.toml

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

331+
# Use the optimized LLVM C intrinsics for `compiler_builtins`, rather than Rust intrinsics.
332+
# Requires the LLVM submodule to be managed by bootstrap (i.e. not external).
333+
#optimized-compiler-builtins = false
334+
331335
# Indicates whether the native libraries linked into Cargo will be statically
332336
# linked or not.
333337
#cargo-native-static = false

src/bootstrap/compile.rs

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

339339
// Determine if we're going to compile in optimized C intrinsics to
340340
// the `compiler-builtins` crate. These intrinsics live in LLVM's
341-
// `compiler-rt` repository, but our `src/llvm-project` submodule isn't
342-
// always checked out, so we need to conditionally look for this. (e.g. if
343-
// an external LLVM is used we skip the LLVM submodule checkout).
341+
// `compiler-rt` repository.
344342
//
345343
// Note that this shouldn't affect the correctness of `compiler-builtins`,
346344
// but only its speed. Some intrinsics in C haven't been translated to Rust
@@ -351,8 +349,15 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
351349
// If `compiler-rt` is available ensure that the `c` feature of the
352350
// `compiler-builtins` crate is enabled and it's configured to learn where
353351
// `compiler-rt` is located.
354-
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
355-
let compiler_builtins_c_feature = if compiler_builtins_root.exists() {
352+
let compiler_builtins_c_feature = if builder.config.optimized_compiler_builtins {
353+
if !builder.is_rust_llvm(target) {
354+
panic!(
355+
"need a managed LLVM submodule for optimized intrinsics support; unset `llvm-config` or `optimized-compiler-builtins`"
356+
);
357+
}
358+
359+
builder.update_submodule(&Path::new("src").join("llvm-project"));
360+
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
356361
// Note that `libprofiler_builtins/build.rs` also computes this so if
357362
// you're changing something here please also change that.
358363
cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);

src/bootstrap/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ pub struct Config {
138138
pub color: Color,
139139
pub patch_binaries_for_nix: bool,
140140
pub stage0_metadata: Stage0Metadata,
141+
/// Whether to use the `c` feature of the `compiler_builtins` crate.
142+
pub optimized_compiler_builtins: bool,
141143

142144
pub stdout_is_tty: bool,
143145
pub stderr_is_tty: bool,
@@ -801,6 +803,7 @@ define_config! {
801803
patch_binaries_for_nix: Option<bool> = "patch-binaries-for-nix",
802804
// NOTE: only parsed by bootstrap.py, `--feature build-metrics` enables metrics unconditionally
803805
metrics: Option<bool> = "metrics",
806+
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
804807
}
805808
}
806809

@@ -1295,6 +1298,7 @@ impl Config {
12951298
set(&mut config.print_step_timings, build.print_step_timings);
12961299
set(&mut config.print_step_rusage, build.print_step_rusage);
12971300
set(&mut config.patch_binaries_for_nix, build.patch_binaries_for_nix);
1301+
set(&mut config.optimized_compiler_builtins, build.optimized_compiler_builtins);
12981302

12991303
config.verbose = cmp::max(config.verbose, flags.verbose as usize);
13001304

src/bootstrap/dist.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -1964,23 +1964,21 @@ fn install_llvm_file(builder: &Builder<'_>, source: &Path, destination: &Path) {
19641964
///
19651965
/// Returns whether the files were actually copied.
19661966
fn maybe_install_llvm(builder: &Builder<'_>, target: TargetSelection, dst_libdir: &Path) -> bool {
1967-
if let Some(config) = builder.config.target_config.get(&target) {
1968-
if config.llvm_config.is_some() && !builder.config.llvm_from_ci {
1969-
// If the LLVM was externally provided, then we don't currently copy
1970-
// artifacts into the sysroot. This is not necessarily the right
1971-
// choice (in particular, it will require the LLVM dylib to be in
1972-
// the linker's load path at runtime), but the common use case for
1973-
// external LLVMs is distribution provided LLVMs, and in that case
1974-
// they're usually in the standard search path (e.g., /usr/lib) and
1975-
// copying them here is going to cause problems as we may end up
1976-
// with the wrong files and isn't what distributions want.
1977-
//
1978-
// This behavior may be revisited in the future though.
1979-
//
1980-
// If the LLVM is coming from ourselves (just from CI) though, we
1981-
// still want to install it, as it otherwise won't be available.
1982-
return false;
1983-
}
1967+
if !builder.is_rust_llvm(target) {
1968+
// If the LLVM was externally provided, then we don't currently copy
1969+
// artifacts into the sysroot. This is not necessarily the right
1970+
// choice (in particular, it will require the LLVM dylib to be in
1971+
// the linker's load path at runtime), but the common use case for
1972+
// external LLVMs is distribution provided LLVMs, and in that case
1973+
// they're usually in the standard search path (e.g., /usr/lib) and
1974+
// copying them here is going to cause problems as we may end up
1975+
// with the wrong files and isn't what distributions want.
1976+
//
1977+
// This behavior may be revisited in the future though.
1978+
//
1979+
// If the LLVM is coming from ourselves (just from CI) though, we
1980+
// still want to install it, as it otherwise won't be available.
1981+
return false;
19841982
}
19851983

19861984
// 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
@@ -138,4 +138,6 @@ ENV RUST_CONFIGURE_ARGS --enable-extended --enable-lld --disable-docs \
138138
--set target.wasm32-wasi.wasi-root=/wasm32-wasi \
139139
--musl-root-armv7=/musl-armv7
140140

141+
ENV EXTERNAL_LLVM 1
142+
141143
ENV SCRIPT python3 ../x.py dist --host='' --target $TARGETS

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

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

4142
# This is not the latest LLVM version, so some components required by tests may
4243
# be missing.

src/ci/run.sh

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ fi
7979
# space required for CI artifacts.
8080
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --dist-compression-formats=xz"
8181

82+
# Enable the `c` feature for compiler_builtins, but only when the `compiler-rt` source is available.
83+
if [ "$EXTERNAL_LLVM" = "" ]; then
84+
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set build.optimized-compiler-builtins"
85+
elif [ "$DEPLOY$DEPLOY_ALT" = "1" ]; then
86+
echo "error: dist builds should always use in-tree LLVM so we use optimized compiler-rt!" >&2
87+
exit 1
88+
fi
89+
8290
if [ "$DIST_SRC" = "" ]; then
8391
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-dist-src"
8492
fi

0 commit comments

Comments
 (0)