Skip to content

Commit

Permalink
link clang_rt profile
Browse files Browse the repository at this point in the history
  • Loading branch information
csmoe committed Apr 21, 2023
1 parent b92a41c commit 90c8cd0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
38 changes: 33 additions & 5 deletions src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::config::{LlvmLibunwind, RustcLto, TargetSelection};
use crate::dist;
use crate::llvm;
use crate::tool::SourceType;
use crate::util::get_clang_cl_resource_dir;
use crate::util::get_clang_rt_dir;
use crate::util::{exe, is_debug_info, is_dylib, output, symlink_dir, t, up_to_date};
use crate::LLVM_TOOLS;
use crate::{CLang, Compiler, DependencyType, GitRepo, Mode};
Expand Down Expand Up @@ -923,10 +923,38 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
// found. This is to avoid the linker errors about undefined references to
// `__llvm_profile_instrument_memop` when linking `rustc_driver`.
let mut llvm_linker_flags = String::new();
if builder.config.llvm_profile_generate && target.contains("msvc") {
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
// Add clang's runtime library directory to the search path
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
if builder.config.llvm_profile_generate {
if target.contains("msvc") {
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
// Add clang's runtime library directory to the search path
let clang_rt_dir = get_clang_rt_dir(clang_cl_path, true);
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
}
}

if target.contains("apple") {
let clang_rt_profile_lib_suffix = if target.ends_with("ios-sim") {
"iossim"
} else if target.ends_with("ios") {
"ios"
} else if target.ends_with("tvos-sim") {
"tvossim"
} else if target.ends_with("tvos") {
"tvos"
} else if target.ends_with("darwin") {
"osx"
} else if target.ends_with("watchos-sim") {
"watchossim"
} else if target.ends_with("watchos") {
"watchos"
} else {
panic!("clang has no clang_rt.profile library for {target}");
};
let clang = builder.cc(target);
let clang_rt_dir = get_clang_rt_dir(clang, false);
let clang_rt_profile_lib = format!("libclang_rt.profile_{clang_rt_profile_lib_suffix}");
llvm_linker_flags.push_str(&format!("-l{clang_rt_profile_lib}"));
llvm_linker_flags.push_str(" ");
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::process::Command;
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
use crate::channel;
use crate::config::{Config, TargetSelection};
use crate::util::get_clang_cl_resource_dir;
use crate::util::get_clang_rt_dir;
use crate::util::{self, exe, output, t, up_to_date};
use crate::{CLang, GitRepo};

Expand Down Expand Up @@ -828,7 +828,7 @@ impl Step for Lld {
if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
// Find clang's runtime library directory and push that as a search path to the
// cmake linker flags.
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
let clang_rt_dir = get_clang_rt_dir(clang_cl_path);
ldflags.push_all(&format!("/libpath:{}", clang_rt_dir.display()));
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/bootstrap/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,15 @@ fn absolute_windows(path: &std::path::Path) -> std::io::Result<std::path::PathBu
/// When `clang-cl` is used with instrumentation, we need to add clang's runtime library resource
/// directory to the linker flags, otherwise there will be linker errors about the profiler runtime
/// missing. This function returns the path to that directory.
pub fn get_clang_cl_resource_dir(clang_cl_path: &str) -> PathBuf {
pub fn get_clang_rt_dir(clang: impl AsRef<Path>, is_msvc: bool) -> PathBuf {
// Similar to how LLVM does it, to find clang's library runtime directory:
// - we ask `clang-cl` to locate the `clang_rt.builtins` lib.
let mut builtins_locator = Command::new(clang_cl_path);
builtins_locator.args(&["/clang:-print-libgcc-file-name", "/clang:--rtlib=compiler-rt"]);
let mut builtins_locator = Command::new(clang.as_ref());
if is_msvc {
builtins_locator.args(&["/clang:-print-libgcc-file-name", "/clang:--rtlib=compiler-rt"]);
} else {
builtins_locator.args(&["-print-libgcc-file-name", "-rtlib=compiler-rt"]);
};

let clang_rt_builtins = output(&mut builtins_locator);
let clang_rt_builtins = Path::new(clang_rt_builtins.trim());
Expand Down

0 comments on commit 90c8cd0

Please sign in to comment.