Skip to content

Commit 99144c3

Browse files
Rollup merge of #114069 - cuviper:profiler-path, r=Mark-Simulacrum
Allow using external builds of the compiler-rt profile lib This changes the bootstrap config `target.*.profiler` from a plain bool to also allow a string, which will be used as a path to the pre-built profiling runtime for that target. Then `profiler_builtins/build.rs` reads that in a `LLVM_PROFILER_RT_LIB` environment variable.
2 parents 7f787e3 + 6e05f59 commit 99144c3

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

config.example.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,10 @@ changelog-seen = 2
758758
# This option will override the same option under [build] section.
759759
#sanitizers = build.sanitizers (bool)
760760

761-
# Build the profiler runtime for this target(required when compiling with options that depend
762-
# on this runtime, such as `-C profile-generate` or `-C instrument-coverage`).
761+
# When true, build the profiler runtime for this target (required when compiling
762+
# with options that depend on this runtime, such as `-C profile-generate` or
763+
# `-C instrument-coverage`). This may also be given a path to an existing build
764+
# of the profiling runtime library from LLVM's compiler-rt.
763765
# This option will override the same option under [build] section.
764766
#profiler = build.profiler (bool)
765767

library/profiler_builtins/build.rs

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ use std::env;
66
use std::path::Path;
77

88
fn main() {
9+
println!("cargo:rerun-if-env-changed=LLVM_PROFILER_RT_LIB");
10+
if let Ok(rt) = env::var("LLVM_PROFILER_RT_LIB") {
11+
println!("cargo:rustc-link-lib=static:+verbatim={rt}");
12+
return;
13+
}
14+
915
let target = env::var("TARGET").expect("TARGET was not set");
1016
let cfg = &mut cc::Build::new();
1117

src/bootstrap/compile.rs

+4
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
325325
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
326326
}
327327

328+
if let Some(path) = builder.config.profiler_path(target) {
329+
cargo.env("LLVM_PROFILER_RT_LIB", path);
330+
}
331+
328332
// Determine if we're going to compile in optimized C intrinsics to
329333
// the `compiler-builtins` crate. These intrinsics live in LLVM's
330334
// `compiler-rt` repository, but our `src/llvm-project` submodule isn't

src/bootstrap/config.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ pub struct Target {
534534
pub linker: Option<PathBuf>,
535535
pub ndk: Option<PathBuf>,
536536
pub sanitizers: Option<bool>,
537-
pub profiler: Option<bool>,
537+
pub profiler: Option<StringOrBool>,
538538
pub rpath: Option<bool>,
539539
pub crt_static: Option<bool>,
540540
pub musl_root: Option<PathBuf>,
@@ -863,9 +863,9 @@ define_config! {
863863
}
864864
}
865865

866-
#[derive(Debug, Deserialize)]
866+
#[derive(Clone, Debug, Deserialize)]
867867
#[serde(untagged)]
868-
enum StringOrBool {
868+
pub enum StringOrBool {
869869
String(String),
870870
Bool(bool),
871871
}
@@ -876,6 +876,12 @@ impl Default for StringOrBool {
876876
}
877877
}
878878

879+
impl StringOrBool {
880+
fn is_string_or_true(&self) -> bool {
881+
matches!(self, Self::String(_) | Self::Bool(true))
882+
}
883+
}
884+
879885
#[derive(Clone, Debug, PartialEq, Eq)]
880886
pub enum RustOptimize {
881887
String(String),
@@ -1038,7 +1044,7 @@ define_config! {
10381044
llvm_libunwind: Option<String> = "llvm-libunwind",
10391045
android_ndk: Option<String> = "android-ndk",
10401046
sanitizers: Option<bool> = "sanitizers",
1041-
profiler: Option<bool> = "profiler",
1047+
profiler: Option<StringOrBool> = "profiler",
10421048
rpath: Option<bool> = "rpath",
10431049
crt_static: Option<bool> = "crt-static",
10441050
musl_root: Option<String> = "musl-root",
@@ -1957,12 +1963,24 @@ impl Config {
19571963
self.target_config.values().any(|t| t.sanitizers == Some(true)) || self.sanitizers
19581964
}
19591965

1966+
pub fn profiler_path(&self, target: TargetSelection) -> Option<&str> {
1967+
match self.target_config.get(&target)?.profiler.as_ref()? {
1968+
StringOrBool::String(s) => Some(s),
1969+
StringOrBool::Bool(_) => None,
1970+
}
1971+
}
1972+
19601973
pub fn profiler_enabled(&self, target: TargetSelection) -> bool {
1961-
self.target_config.get(&target).map(|t| t.profiler).flatten().unwrap_or(self.profiler)
1974+
self.target_config
1975+
.get(&target)
1976+
.and_then(|t| t.profiler.as_ref())
1977+
.map(StringOrBool::is_string_or_true)
1978+
.unwrap_or(self.profiler)
19621979
}
19631980

19641981
pub fn any_profiler_enabled(&self) -> bool {
1965-
self.target_config.values().any(|t| t.profiler == Some(true)) || self.profiler
1982+
self.target_config.values().any(|t| matches!(&t.profiler, Some(p) if p.is_string_or_true()))
1983+
|| self.profiler
19661984
}
19671985

19681986
pub fn rpath_enabled(&self, target: TargetSelection) -> bool {

0 commit comments

Comments
 (0)