Skip to content

Commit 194b968

Browse files
Rollup merge of #78354 - 12101111:rustbuild_profiler, r=Mark-Simulacrum
Support enable/disable sanitizers/profiler per target This PR add options under `[target.*]` of `config.toml` which can enable or disable sanitizers/profiler runtime for corresponding target. If these options are empty, the global options under `[build]` will take effect. Fix #78329
2 parents 62f0a78 + b989d46 commit 194b968

File tree

9 files changed

+55
-17
lines changed

9 files changed

+55
-17
lines changed

config.toml.example

+9
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,15 @@ changelog-seen = 2
586586
# build native code.
587587
#android-ndk = "/path/to/ndk"
588588

589+
# Build the sanitizer runtimes for this target.
590+
# This option will override the same option under [build] section.
591+
#sanitizers = false
592+
593+
# Build the profiler runtime for this target(required when compiling with options that depend
594+
# on this runtime, such as `-C profile-generate` or `-Z instrument-coverage`).
595+
# This option will override the same option under [build] section.
596+
#profiler = false
597+
589598
# Force static or dynamic linkage of the standard library for this target. If
590599
# this target is a host for rustc, this will also affect the linkage of the
591600
# compiler itself. This is useful for building rustc on targets that normally

src/bootstrap/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<'a> ShouldRun<'a> {
264264
/// `all_krates` should probably be removed at some point.
265265
pub fn all_krates(mut self, name: &str) -> Self {
266266
let mut set = BTreeSet::new();
267-
for krate in self.builder.in_tree_crates(name) {
267+
for krate in self.builder.in_tree_crates(name, None) {
268268
let path = krate.local_path(self.builder);
269269
set.insert(path);
270270
}
@@ -277,7 +277,7 @@ impl<'a> ShouldRun<'a> {
277277
///
278278
/// `make_run` will be called separately for each matching command-line path.
279279
pub fn krate(mut self, name: &str) -> Self {
280-
for krate in self.builder.in_tree_crates(name) {
280+
for krate in self.builder.in_tree_crates(name, None) {
281281
let path = krate.local_path(self.builder);
282282
self.paths.insert(PathSet::one(path));
283283
}

src/bootstrap/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl Step for Std {
108108
// Explicitly pass -p for all dependencies krates -- this will force cargo
109109
// to also check the tests/benches/examples for these crates, rather
110110
// than just the leaf crate.
111-
for krate in builder.in_tree_crates("test") {
111+
for krate in builder.in_tree_crates("test", Some(target)) {
112112
cargo.arg("-p").arg(krate.name);
113113
}
114114

@@ -172,7 +172,7 @@ impl Step for Rustc {
172172
// Explicitly pass -p for all compiler krates -- this will force cargo
173173
// to also check the tests/benches/examples for these crates, rather
174174
// than just the leaf crate.
175-
for krate in builder.in_tree_crates("rustc-main") {
175+
for krate in builder.in_tree_crates("rustc-main", Some(target)) {
176176
cargo.arg("-p").arg(krate.name);
177177
}
178178

src/bootstrap/compile.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn copy_third_party_objects(
143143
}
144144
}
145145

146-
if builder.config.sanitizers && compiler.stage != 0 {
146+
if builder.config.sanitizers_enabled(target) && compiler.stage != 0 {
147147
// The sanitizers are only copied in stage1 or above,
148148
// to avoid creating dependency on LLVM.
149149
target_deps.extend(
@@ -251,7 +251,7 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
251251
.arg("--features")
252252
.arg(features);
253253
} else {
254-
let mut features = builder.std_features();
254+
let mut features = builder.std_features(target);
255255
features.push_str(compiler_builtins_c_feature);
256256

257257
cargo

src/bootstrap/config.rs

+22
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ pub struct Target {
279279
pub ranlib: Option<PathBuf>,
280280
pub linker: Option<PathBuf>,
281281
pub ndk: Option<PathBuf>,
282+
pub sanitizers: bool,
283+
pub profiler: bool,
282284
pub crt_static: Option<bool>,
283285
pub musl_root: Option<PathBuf>,
284286
pub musl_libdir: Option<PathBuf>,
@@ -503,6 +505,8 @@ struct TomlTarget {
503505
llvm_config: Option<String>,
504506
llvm_filecheck: Option<String>,
505507
android_ndk: Option<String>,
508+
sanitizers: Option<bool>,
509+
profiler: Option<bool>,
506510
crt_static: Option<bool>,
507511
musl_root: Option<String>,
508512
musl_libdir: Option<String>,
@@ -890,6 +894,8 @@ impl Config {
890894
target.musl_libdir = cfg.musl_libdir.map(PathBuf::from);
891895
target.wasi_root = cfg.wasi_root.map(PathBuf::from);
892896
target.qemu_rootfs = cfg.qemu_rootfs.map(PathBuf::from);
897+
target.sanitizers = cfg.sanitizers.unwrap_or(build.sanitizers.unwrap_or_default());
898+
target.profiler = cfg.profiler.unwrap_or(build.profiler.unwrap_or_default());
893899

894900
config.target_config.insert(TargetSelection::from_user(&triple), target);
895901
}
@@ -999,6 +1005,22 @@ impl Config {
9991005
self.verbose > 1
10001006
}
10011007

1008+
pub fn sanitizers_enabled(&self, target: TargetSelection) -> bool {
1009+
self.target_config.get(&target).map(|t| t.sanitizers).unwrap_or(self.sanitizers)
1010+
}
1011+
1012+
pub fn any_sanitizers_enabled(&self) -> bool {
1013+
self.target_config.values().any(|t| t.sanitizers) || self.sanitizers
1014+
}
1015+
1016+
pub fn profiler_enabled(&self, target: TargetSelection) -> bool {
1017+
self.target_config.get(&target).map(|t| t.profiler).unwrap_or(self.profiler)
1018+
}
1019+
1020+
pub fn any_profiler_enabled(&self) -> bool {
1021+
self.target_config.values().any(|t| t.profiler) || self.profiler
1022+
}
1023+
10021024
pub fn llvm_enabled(&self) -> bool {
10031025
self.rust_codegen_backends.contains(&INTERNER.intern_str("llvm"))
10041026
}

src/bootstrap/doc.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,12 @@ impl Step for Rustc {
535535
// Find dependencies for top level crates.
536536
let mut compiler_crates = HashSet::new();
537537
for root_crate in &["rustc_driver", "rustc_codegen_llvm", "rustc_codegen_ssa"] {
538-
compiler_crates
539-
.extend(builder.in_tree_crates(root_crate).into_iter().map(|krate| krate.name));
538+
compiler_crates.extend(
539+
builder
540+
.in_tree_crates(root_crate, Some(target))
541+
.into_iter()
542+
.map(|krate| krate.name),
543+
);
540544
}
541545

542546
for krate in &compiler_crates {

src/bootstrap/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ impl Build {
541541

542542
/// Gets the space-separated set of activated features for the standard
543543
/// library.
544-
fn std_features(&self) -> String {
544+
fn std_features(&self, target: TargetSelection) -> String {
545545
let mut features = "panic-unwind".to_string();
546546

547547
match self.config.llvm_libunwind.unwrap_or_default() {
@@ -552,7 +552,7 @@ impl Build {
552552
if self.config.backtrace {
553553
features.push_str(" backtrace");
554554
}
555-
if self.config.profiler {
555+
if self.config.profiler_enabled(target) {
556556
features.push_str(" profiler");
557557
}
558558
features
@@ -1115,7 +1115,7 @@ impl Build {
11151115
/// Returns a Vec of all the dependencies of the given root crate,
11161116
/// including transitive dependencies and the root itself. Only includes
11171117
/// "local" crates (those in the local source tree, not from a registry).
1118-
fn in_tree_crates(&self, root: &str) -> Vec<&Crate> {
1118+
fn in_tree_crates(&self, root: &str, target: Option<TargetSelection>) -> Vec<&Crate> {
11191119
let mut ret = Vec::new();
11201120
let mut list = vec![INTERNER.intern_str(root)];
11211121
let mut visited = HashSet::new();
@@ -1136,7 +1136,10 @@ impl Build {
11361136
// metadata::build.
11371137
if visited.insert(dep)
11381138
&& dep != "build_helper"
1139-
&& (dep != "profiler_builtins" || self.config.profiler)
1139+
&& (dep != "profiler_builtins"
1140+
|| target
1141+
.map(|t| self.config.profiler_enabled(t))
1142+
.unwrap_or(self.config.any_profiler_enabled()))
11401143
&& (dep != "rustc_codegen_llvm" || self.config.llvm_enabled())
11411144
{
11421145
list.push(*dep);

src/bootstrap/sanity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn check(build: &mut Build) {
9191
.unwrap_or(true)
9292
})
9393
.any(|build_llvm_ourselves| build_llvm_ourselves);
94-
if building_llvm || build.config.sanitizers {
94+
if building_llvm || build.config.any_sanitizers_enabled() {
9595
cmd_finder.must_have("cmake");
9696
}
9797

src/bootstrap/test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1271,11 +1271,11 @@ note: if you're sure you want to do this, please open an issue as to why. In the
12711271
cmd.env("RUSTC_BOOTSTRAP", "1");
12721272
builder.add_rust_test_threads(&mut cmd);
12731273

1274-
if builder.config.sanitizers {
1274+
if builder.config.sanitizers_enabled(target) {
12751275
cmd.env("RUSTC_SANITIZER_SUPPORT", "1");
12761276
}
12771277

1278-
if builder.config.profiler {
1278+
if builder.config.profiler_enabled(target) {
12791279
cmd.env("RUSTC_PROFILER_SUPPORT", "1");
12801280
}
12811281

@@ -1591,7 +1591,7 @@ impl Step for CrateLibrustc {
15911591
let builder = run.builder;
15921592
let compiler = builder.compiler(builder.top_stage, run.build_triple());
15931593

1594-
for krate in builder.in_tree_crates("rustc-main") {
1594+
for krate in builder.in_tree_crates("rustc-main", Some(run.target)) {
15951595
if krate.path.ends_with(&run.path) {
15961596
let test_kind = builder.kind.into();
15971597

@@ -1698,7 +1698,7 @@ impl Step for Crate {
16981698
});
16991699
};
17001700

1701-
for krate in builder.in_tree_crates("test") {
1701+
for krate in builder.in_tree_crates("test", Some(run.target)) {
17021702
if krate.path.ends_with(&run.path) {
17031703
make(Mode::Std, krate);
17041704
}

0 commit comments

Comments
 (0)