Skip to content

Commit 6171d94

Browse files
committed
Auto merge of #133092 - madsmtm:bootstrap-deployment-target, r=Mark-Simulacrum,jieyouxu
Always set the deployment target when building std `cc` has [a bug/feature](rust-lang/cc-rs#1171) (I guess depending on how you look at it) where the default deployment target is taken from the SDK instead of from `rustc`. This causes `compiler-builtins` to build `compiler-rt` with the wrong deployment target on iOS. I've been meaning to change how `cc` works in this regard, but that's a lengthy process, so let's fix it in bootstrap for now. The behaviour can be seen locally with `./x build library --set build.optimized-compiler-builtins=true` for various target triples, and then inspecting with `otool -l build/host/stage1/lib/rustlib/*/lib/libcompiler_builtins-*.rlib | rg 'minos|version'`. I have added a rmake test that ensures that we now have the same version everywhere. Fixes #128419 Fixes rust-lang/compiler-builtins#650 Fixes #136523 See also rust-lang/cargo#13115, rust-lang/cc-rs#1171, #136113 See #133092 (comment) for a description of how the change works. try-job: i686-gnu-1 try-job: i686-gnu-2 try-job: x86_64-apple-1 try-job: aarch64-apple try-job: dist-apple-various try-job: dist-aarch64-apple try-job: dist-various-2 try-job: x86_64-fuchsia
2 parents 4bb6ec0 + ea68a47 commit 6171d94

File tree

8 files changed

+134
-26
lines changed

8 files changed

+134
-26
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+45-3
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,49 @@ fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
443443
/// Configure cargo to compile the standard library, adding appropriate env vars
444444
/// and such.
445445
pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, cargo: &mut Cargo) {
446-
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
447-
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
446+
// rustc already ensures that it builds with the minimum deployment
447+
// target, so ideally we shouldn't need to do anything here.
448+
//
449+
// However, `cc` currently defaults to a higher version for backwards
450+
// compatibility, which means that compiler-rt, which is built via
451+
// compiler-builtins' build script, gets built with a higher deployment
452+
// target. This in turn causes warnings while linking, and is generally
453+
// a compatibility hazard.
454+
//
455+
// So, at least until https://github.com/rust-lang/cc-rs/issues/1171, or
456+
// perhaps https://github.com/rust-lang/cargo/issues/13115 is resolved, we
457+
// explicitly set the deployment target environment variables to avoid
458+
// this issue.
459+
//
460+
// This place also serves as an extension point if we ever wanted to raise
461+
// rustc's default deployment target while keeping the prebuilt `std` at
462+
// a lower version, so it's kinda nice to have in any case.
463+
if target.contains("apple") && !builder.config.dry_run() {
464+
// Query rustc for the deployment target, and the associated env var.
465+
// The env var is one of the standard `*_DEPLOYMENT_TARGET` vars, i.e.
466+
// `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET`, etc.
467+
let mut cmd = command(builder.rustc(cargo.compiler()));
468+
cmd.arg("--target").arg(target.rustc_target_arg());
469+
cmd.arg("--print=deployment-target");
470+
let output = cmd.run_capture_stdout(builder).stdout();
471+
472+
let (env_var, value) = output.split_once('=').unwrap();
473+
// Unconditionally set the env var (if it was set in the environment
474+
// already, rustc should've picked that up).
475+
cargo.env(env_var.trim(), value.trim());
476+
477+
// Allow CI to override the deployment target for `std` on macOS.
478+
//
479+
// This is useful because we might want the host tooling LLVM, `rustc`
480+
// and Cargo to have a different deployment target than `std` itself
481+
// (currently, these two versions are the same, but in the past, we
482+
// supported macOS 10.7 for user code and macOS 10.8 in host tooling).
483+
//
484+
// It is not necessary on the other platforms, since only macOS has
485+
// support for host tooling.
486+
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
487+
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
488+
}
448489
}
449490

450491
// Paths needed by `library/profiler_builtins/build.rs`.
@@ -1547,7 +1588,8 @@ pub fn compiler_file(
15471588
return PathBuf::new();
15481589
}
15491590
let mut cmd = command(compiler);
1550-
cmd.args(builder.cflags(target, GitRepo::Rustc, c));
1591+
cmd.args(builder.cc_handled_clags(target, c));
1592+
cmd.args(builder.cc_unhandled_cflags(target, GitRepo::Rustc, c));
15511593
cmd.arg(format!("-print-file-name={file}"));
15521594
let out = cmd.run_capture_stdout(builder).stdout();
15531595
PathBuf::from(out.trim())

src/bootstrap/src/core/build_steps/llvm.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,15 @@ fn configure_cmake(
779779
}
780780

781781
cfg.build_arg("-j").build_arg(builder.jobs().to_string());
782+
// FIXME(madsmtm): Allow `cmake-rs` to select flags by itself by passing
783+
// our flags via `.cflag`/`.cxxflag` instead.
784+
//
785+
// Needs `suppressed_compiler_flag_prefixes` to be gone, and hence
786+
// https://github.com/llvm/llvm-project/issues/88780 to be fixed.
782787
let mut cflags: OsString = builder
783-
.cflags(target, GitRepo::Llvm, CLang::C)
788+
.cc_handled_clags(target, CLang::C)
784789
.into_iter()
790+
.chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::C))
785791
.filter(|flag| {
786792
!suppressed_compiler_flag_prefixes
787793
.iter()
@@ -800,8 +806,9 @@ fn configure_cmake(
800806
}
801807
cfg.define("CMAKE_C_FLAGS", cflags);
802808
let mut cxxflags: OsString = builder
803-
.cflags(target, GitRepo::Llvm, CLang::Cxx)
809+
.cc_handled_clags(target, CLang::Cxx)
804810
.into_iter()
811+
.chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::Cxx))
805812
.filter(|flag| {
806813
!suppressed_compiler_flag_prefixes
807814
.iter()

src/bootstrap/src/core/build_steps/test.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2011,14 +2011,18 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
20112011
// Only pass correct values for these flags for the `run-make` suite as it
20122012
// requires that a C++ compiler was configured which isn't always the case.
20132013
if !builder.config.dry_run() && mode == "run-make" {
2014+
let mut cflags = builder.cc_handled_clags(target, CLang::C);
2015+
cflags.extend(builder.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C));
2016+
let mut cxxflags = builder.cc_handled_clags(target, CLang::Cxx);
2017+
cxxflags.extend(builder.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx));
20142018
cmd.arg("--cc")
20152019
.arg(builder.cc(target))
20162020
.arg("--cxx")
20172021
.arg(builder.cxx(target).unwrap())
20182022
.arg("--cflags")
2019-
.arg(builder.cflags(target, GitRepo::Rustc, CLang::C).join(" "))
2023+
.arg(cflags.join(" "))
20202024
.arg("--cxxflags")
2021-
.arg(builder.cflags(target, GitRepo::Rustc, CLang::Cxx).join(" "));
2025+
.arg(cxxflags.join(" "));
20222026
copts_passed = true;
20232027
if let Some(ar) = builder.ar(target) {
20242028
cmd.arg("--ar").arg(ar);

src/bootstrap/src/core/builder/cargo.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,15 @@ impl Cargo {
323323
let cc = ccacheify(&builder.cc(target));
324324
self.command.env(format!("CC_{triple_underscored}"), &cc);
325325

326-
let cflags = builder.cflags(target, GitRepo::Rustc, CLang::C).join(" ");
327-
self.command.env(format!("CFLAGS_{triple_underscored}"), &cflags);
326+
// Extend `CXXFLAGS_$TARGET` with our extra flags.
327+
let env = format!("CFLAGS_{triple_underscored}");
328+
let mut cflags =
329+
builder.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C).join(" ");
330+
if let Ok(var) = std::env::var(&env) {
331+
cflags.push(' ');
332+
cflags.push_str(&var);
333+
}
334+
self.command.env(env, &cflags);
328335

329336
if let Some(ar) = builder.ar(target) {
330337
let ranlib = format!("{} s", ar.display());
@@ -335,10 +342,17 @@ impl Cargo {
335342

336343
if let Ok(cxx) = builder.cxx(target) {
337344
let cxx = ccacheify(&cxx);
338-
let cxxflags = builder.cflags(target, GitRepo::Rustc, CLang::Cxx).join(" ");
339-
self.command
340-
.env(format!("CXX_{triple_underscored}"), &cxx)
341-
.env(format!("CXXFLAGS_{triple_underscored}"), cxxflags);
345+
self.command.env(format!("CXX_{triple_underscored}"), &cxx);
346+
347+
// Extend `CXXFLAGS_$TARGET` with our extra flags.
348+
let env = format!("CXXFLAGS_{triple_underscored}");
349+
let mut cxxflags =
350+
builder.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx).join(" ");
351+
if let Ok(var) = std::env::var(&env) {
352+
cxxflags.push(' ');
353+
cxxflags.push_str(&var);
354+
}
355+
self.command.env(&env, cxxflags);
342356
}
343357
}
344358

src/bootstrap/src/lib.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ impl Mode {
249249
}
250250
}
251251

252+
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
252253
pub enum CLang {
253254
C,
254255
Cxx,
@@ -1178,9 +1179,9 @@ Executed at: {executed_at}"#,
11781179
self.cc.borrow()[&target].path().into()
11791180
}
11801181

1181-
/// Returns a list of flags to pass to the C compiler for the target
1182-
/// specified.
1183-
fn cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
1182+
/// Returns C flags that `cc-rs` thinks should be enabled for the
1183+
/// specified target by default.
1184+
fn cc_handled_clags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
11841185
if self.config.dry_run() {
11851186
return Vec::new();
11861187
}
@@ -1189,14 +1190,23 @@ Executed at: {executed_at}"#,
11891190
CLang::Cxx => self.cxx.borrow()[&target].clone(),
11901191
};
11911192

1192-
// Filter out -O and /O (the optimization flags) that we picked up from
1193-
// cc-rs because the build scripts will determine that for themselves.
1194-
let mut base = base
1195-
.args()
1193+
// Filter out -O and /O (the optimization flags) that we picked up
1194+
// from cc-rs, that's up to the caller to figure out.
1195+
base.args()
11961196
.iter()
11971197
.map(|s| s.to_string_lossy().into_owned())
11981198
.filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
1199-
.collect::<Vec<String>>();
1199+
.collect::<Vec<String>>()
1200+
}
1201+
1202+
/// Returns extra C flags that `cc-rs` doesn't handle.
1203+
fn cc_unhandled_cflags(
1204+
&self,
1205+
target: TargetSelection,
1206+
which: GitRepo,
1207+
c: CLang,
1208+
) -> Vec<String> {
1209+
let mut base = Vec::new();
12001210

12011211
// If we're compiling C++ on macOS then we add a flag indicating that
12021212
// we want libc++ (more filled out than libstdc++), ensuring that

src/bootstrap/src/utils/cc_detect.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ pub fn find_target(build: &Build, target: TargetSelection) {
142142
};
143143

144144
build.cc.borrow_mut().insert(target, compiler.clone());
145-
let cflags = build.cflags(target, GitRepo::Rustc, CLang::C);
145+
let mut cflags = build.cc_handled_clags(target, CLang::C);
146+
cflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C));
146147

147148
// If we use llvm-libunwind, we will need a C++ compiler as well for all targets
148149
// We'll need one anyways if the target triple is also a host triple
@@ -168,7 +169,8 @@ pub fn find_target(build: &Build, target: TargetSelection) {
168169
build.verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));
169170
build.verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));
170171
if let Ok(cxx) = build.cxx(target) {
171-
let cxxflags = build.cflags(target, GitRepo::Rustc, CLang::Cxx);
172+
let mut cxxflags = build.cc_handled_clags(target, CLang::Cxx);
173+
cxxflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx));
172174
build.verbose(|| println!("CXX_{} = {cxx:?}", target.triple));
173175
build.verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));
174176
}

src/ci/github-actions/jobs.yml

+10-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ envs:
5959
SCRIPT: ./x.py --stage 2 test --skip tests/ui --skip tests/rustdoc -- --exact
6060
RUST_CONFIGURE_ARGS: --build=x86_64-apple-darwin --enable-sanitizers --enable-profiler --set rust.jemalloc
6161
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
62+
# Ensure that host tooling is tested on our minimum supported macOS version.
6263
MACOSX_DEPLOYMENT_TARGET: 10.12
6364
MACOSX_STD_DEPLOYMENT_TARGET: 10.12
6465
SELECT_XCODE: /Applications/Xcode_15.2.app
@@ -370,7 +371,9 @@ auto:
370371
SCRIPT: ./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin
371372
RUST_CONFIGURE_ARGS: --enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set rust.lto=thin --set rust.codegen-units=1
372373
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
374+
# Ensure that host tooling is built to support our minimum support macOS version.
373375
MACOSX_DEPLOYMENT_TARGET: 10.12
376+
MACOSX_STD_DEPLOYMENT_TARGET: 10.12
374377
SELECT_XCODE: /Applications/Xcode_15.2.app
375378
NO_LLVM_ASSERTIONS: 1
376379
NO_DEBUG_ASSERTIONS: 1
@@ -386,7 +389,10 @@ auto:
386389
# https://github.com/rust-lang/rust/issues/129069
387390
RUST_CONFIGURE_ARGS: --enable-sanitizers --enable-profiler --set rust.jemalloc --set target.aarch64-apple-ios-macabi.sanitizers=false --set target.x86_64-apple-ios-macabi.sanitizers=false
388391
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
392+
# Ensure that host tooling is built to support our minimum support macOS version.
393+
# FIXME(madsmtm): This might be redundant, as we're not building host tooling here (?)
389394
MACOSX_DEPLOYMENT_TARGET: 10.12
395+
MACOSX_STD_DEPLOYMENT_TARGET: 10.12
390396
SELECT_XCODE: /Applications/Xcode_15.2.app
391397
NO_LLVM_ASSERTIONS: 1
392398
NO_DEBUG_ASSERTIONS: 1
@@ -404,7 +410,6 @@ auto:
404410
<<: *env-x86_64-apple-tests
405411
<<: *job-macos-xl
406412

407-
# This target only needs to support 11.0 and up as nothing else supports the hardware
408413
- name: dist-aarch64-apple
409414
env:
410415
SCRIPT: ./x.py dist bootstrap --include-default-paths --host=aarch64-apple-darwin --target=aarch64-apple-darwin
@@ -419,6 +424,8 @@ auto:
419424
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
420425
SELECT_XCODE: /Applications/Xcode_15.4.app
421426
USE_XCODE_CLANG: 1
427+
# Aarch64 tooling only needs to support macOS 11.0 and up as nothing else
428+
# supports the hardware.
422429
MACOSX_DEPLOYMENT_TARGET: 11.0
423430
MACOSX_STD_DEPLOYMENT_TARGET: 11.0
424431
NO_LLVM_ASSERTIONS: 1
@@ -428,7 +435,6 @@ auto:
428435
CODEGEN_BACKENDS: llvm,cranelift
429436
<<: *job-macos-m1
430437

431-
# This target only needs to support 11.0 and up as nothing else supports the hardware
432438
- name: aarch64-apple
433439
env:
434440
SCRIPT: ./x.py --stage 2 test --host=aarch64-apple-darwin --target=aarch64-apple-darwin
@@ -439,6 +445,8 @@ auto:
439445
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
440446
SELECT_XCODE: /Applications/Xcode_15.4.app
441447
USE_XCODE_CLANG: 1
448+
# Aarch64 tooling only needs to support macOS 11.0 and up as nothing else
449+
# supports the hardware, so only need to test it there.
442450
MACOSX_DEPLOYMENT_TARGET: 11.0
443451
MACOSX_STD_DEPLOYMENT_TARGET: 11.0
444452
NO_LLVM_ASSERTIONS: 1

tests/run-make/apple-deployment-target/rmake.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
88
//@ only-apple
99

10-
use run_make_support::{apple_os, cmd, run_in_tmpdir, rustc, target};
10+
use std::collections::HashSet;
11+
12+
use run_make_support::{
13+
apple_os, cmd, has_extension, path, regex, run_in_tmpdir, rustc, shallow_find_files, target,
14+
};
1115

1216
/// Run vtool to check the `minos` field in LC_BUILD_VERSION.
1317
///
@@ -166,4 +170,21 @@ fn main() {
166170
rustc().env_remove(env_var).run();
167171
minos("foo.o", default_version);
168172
});
173+
174+
// Test that all binaries in rlibs produced by `rustc` have the same version.
175+
// Regression test for https://github.com/rust-lang/rust/issues/128419.
176+
let sysroot = rustc().print("sysroot").run().stdout_utf8();
177+
let target_sysroot = path(sysroot.trim()).join("lib/rustlib").join(target()).join("lib");
178+
let rlibs = shallow_find_files(&target_sysroot, |path| has_extension(path, "rlib"));
179+
180+
let output = cmd("otool").arg("-l").args(rlibs).run().stdout_utf8();
181+
let re = regex::Regex::new(r"(minos|version) ([0-9.]*)").unwrap();
182+
let mut versions = HashSet::new();
183+
for (_, [_, version]) in re.captures_iter(&output).map(|c| c.extract()) {
184+
versions.insert(version);
185+
}
186+
// FIXME(madsmtm): See above for aarch64-apple-watchos.
187+
if versions.len() != 1 && target() != "aarch64-apple-watchos" {
188+
panic!("std rlibs contained multiple different deployment target versions: {versions:?}");
189+
}
169190
}

0 commit comments

Comments
 (0)