Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using external builds of the compiler-rt profile lib #114069

Merged
merged 2 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,10 @@ changelog-seen = 2
# This option will override the same option under [build] section.
#sanitizers = build.sanitizers (bool)

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

Expand Down
6 changes: 6 additions & 0 deletions library/profiler_builtins/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use std::env;
use std::path::Path;

fn main() {
println!("cargo:rerun-if-env-changed=LLVM_PROFILER_RT_LIB");
if let Ok(rt) = env::var("LLVM_PROFILER_RT_LIB") {
println!("cargo:rustc-link-lib=static:+verbatim={rt}");
return;
}

let target = env::var("TARGET").expect("TARGET was not set");
let cfg = &mut cc::Build::new();

Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
}

if let Some(path) = builder.config.profiler_path(target) {
cargo.env("LLVM_PROFILER_RT_LIB", path);
}

// Determine if we're going to compile in optimized C intrinsics to
// the `compiler-builtins` crate. These intrinsics live in LLVM's
// `compiler-rt` repository, but our `src/llvm-project` submodule isn't
Expand Down
30 changes: 24 additions & 6 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ pub struct Target {
pub linker: Option<PathBuf>,
pub ndk: Option<PathBuf>,
pub sanitizers: Option<bool>,
pub profiler: Option<bool>,
pub profiler: Option<StringOrBool>,
pub rpath: Option<bool>,
pub crt_static: Option<bool>,
pub musl_root: Option<PathBuf>,
Expand Down Expand Up @@ -862,9 +862,9 @@ define_config! {
}
}

#[derive(Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
enum StringOrBool {
pub enum StringOrBool {
String(String),
Bool(bool),
}
Expand All @@ -875,6 +875,12 @@ impl Default for StringOrBool {
}
}

impl StringOrBool {
fn is_string_or_true(&self) -> bool {
matches!(self, Self::String(_) | Self::Bool(true))
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RustOptimize {
String(String),
Expand Down Expand Up @@ -1038,7 +1044,7 @@ define_config! {
llvm_libunwind: Option<String> = "llvm-libunwind",
android_ndk: Option<String> = "android-ndk",
sanitizers: Option<bool> = "sanitizers",
profiler: Option<bool> = "profiler",
profiler: Option<StringOrBool> = "profiler",
rpath: Option<bool> = "rpath",
crt_static: Option<bool> = "crt-static",
musl_root: Option<String> = "musl-root",
Expand Down Expand Up @@ -1951,12 +1957,24 @@ impl Config {
self.target_config.values().any(|t| t.sanitizers == Some(true)) || self.sanitizers
}

pub fn profiler_path(&self, target: TargetSelection) -> Option<&str> {
match self.target_config.get(&target)?.profiler.as_ref()? {
StringOrBool::String(s) => Some(s),
StringOrBool::Bool(_) => None,
}
}

pub fn profiler_enabled(&self, target: TargetSelection) -> bool {
self.target_config.get(&target).map(|t| t.profiler).flatten().unwrap_or(self.profiler)
self.target_config
.get(&target)
.and_then(|t| t.profiler.as_ref())
.map(StringOrBool::is_string_or_true)
.unwrap_or(self.profiler)
}

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

pub fn rpath_enabled(&self, target: TargetSelection) -> bool {
Expand Down
Loading