use std::env;
use std::env::consts::EXE_EXTENSION;
use std::ffi::{OsStr, OsString};
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use build_helper::ci::CiEnv;
use build_helper::git::get_closest_merge_commit;
use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
use crate::core::config::{Config, TargetSelection};
use crate::utils::exec::command;
use crate::utils::helpers::{
self, HashStamp, exe, get_clang_cl_resource_dir, t, unhashed_basename, up_to_date,
};
use crate::{CLang, GitRepo, Kind, generate_smart_stamp_hash};
#[derive(Clone)]
pub struct LlvmResult {
pub llvm_config: PathBuf,
pub llvm_cmake_dir: PathBuf,
}
pub struct Meta {
stamp: HashStamp,
res: LlvmResult,
out_dir: PathBuf,
root: String,
}
pub enum LlvmBuildStatus {
AlreadyBuilt(LlvmResult),
ShouldBuild(Meta),
}
impl LlvmBuildStatus {
pub fn should_build(&self) -> bool {
match self {
LlvmBuildStatus::AlreadyBuilt(_) => false,
LlvmBuildStatus::ShouldBuild(_) => true,
}
}
}
#[derive(Debug, Clone, Default)]
struct LdFlags {
exe: OsString,
shared: OsString,
module: OsString,
}
impl LdFlags {
fn push_all(&mut self, s: impl AsRef<OsStr>) {
let s = s.as_ref();
self.exe.push(" ");
self.exe.push(s);
self.shared.push(" ");
self.shared.push(s);
self.module.push(" ");
self.module.push(s);
}
}
pub fn prebuilt_llvm_config(
builder: &Builder<'_>,
target: TargetSelection,
handle_submodule_when_needed: bool,
) -> LlvmBuildStatus {
builder.config.maybe_download_ci_llvm();
if let Some(config) = builder.config.target_config.get(&target) {
if let Some(ref s) = config.llvm_config {
check_llvm_version(builder, s);
let llvm_config = s.to_path_buf();
let mut llvm_cmake_dir = llvm_config.clone();
llvm_cmake_dir.pop();
llvm_cmake_dir.pop();
llvm_cmake_dir.push("lib");
llvm_cmake_dir.push("cmake");
llvm_cmake_dir.push("llvm");
return LlvmBuildStatus::AlreadyBuilt(LlvmResult { llvm_config, llvm_cmake_dir });
}
}
if handle_submodule_when_needed {
builder.config.update_submodule("src/llvm-project");
}
let root = "src/llvm-project/llvm";
let out_dir = builder.llvm_out(target);
let mut llvm_config_ret_dir = builder.llvm_out(builder.config.build);
llvm_config_ret_dir.push("bin");
let build_llvm_config = llvm_config_ret_dir.join(exe("llvm-config", builder.config.build));
let llvm_cmake_dir = out_dir.join("lib/cmake/llvm");
let res = LlvmResult { llvm_config: build_llvm_config, llvm_cmake_dir };
static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
generate_smart_stamp_hash(
builder,
&builder.config.src.join("src/llvm-project"),
builder.in_tree_llvm_info.sha().unwrap_or_default(),
)
});
let stamp = out_dir.join("llvm-finished-building");
let stamp = HashStamp::new(stamp, Some(smart_stamp_hash));
if stamp.is_done() {
if stamp.hash.is_none() {
builder.info(
"Could not determine the LLVM submodule commit hash. \
Assuming that an LLVM rebuild is not necessary.",
);
builder.info(&format!(
"To force LLVM to rebuild, remove the file `{}`",
stamp.path.display()
));
}
return LlvmBuildStatus::AlreadyBuilt(res);
}
LlvmBuildStatus::ShouldBuild(Meta { stamp, res, out_dir, root: root.into() })
}
pub(crate) fn detect_llvm_sha(config: &Config, is_git: bool) -> String {
let llvm_sha = if is_git {
get_closest_merge_commit(Some(&config.src), &config.git_config(), &[
config.src.join("src/llvm-project"),
config.src.join("src/bootstrap/download-ci-llvm-stamp"),
config.src.join("src/version"),
])
.unwrap()
} else if let Some(info) = crate::utils::channel::read_commit_info_file(&config.src) {
info.sha.trim().to_owned()
} else {
"".to_owned()
};
if llvm_sha.is_empty() {
eprintln!("error: could not find commit hash for downloading LLVM");
eprintln!("HELP: maybe your repository history is too shallow?");
eprintln!("HELP: consider disabling `download-ci-llvm`");
eprintln!("HELP: or fetch enough history to include one upstream commit");
panic!();
}
llvm_sha
}
pub(crate) fn is_ci_llvm_available(config: &Config, asserts: bool) -> bool {
let supported_platforms = [
("aarch64-unknown-linux-gnu", false),
("aarch64-apple-darwin", false),
("i686-pc-windows-gnu", false),
("i686-pc-windows-msvc", false),
("i686-unknown-linux-gnu", false),
("x86_64-unknown-linux-gnu", true),
("x86_64-apple-darwin", true),
("x86_64-pc-windows-gnu", true),
("x86_64-pc-windows-msvc", true),
("aarch64-pc-windows-msvc", false),
("aarch64-unknown-linux-musl", false),
("arm-unknown-linux-gnueabi", false),
("arm-unknown-linux-gnueabihf", false),
("armv7-unknown-linux-gnueabihf", false),
("loongarch64-unknown-linux-gnu", false),
("loongarch64-unknown-linux-musl", false),
("mips-unknown-linux-gnu", false),
("mips64-unknown-linux-gnuabi64", false),
("mips64el-unknown-linux-gnuabi64", false),
("mipsel-unknown-linux-gnu", false),
("powerpc-unknown-linux-gnu", false),
("powerpc64-unknown-linux-gnu", false),
("powerpc64le-unknown-linux-gnu", false),
("riscv64gc-unknown-linux-gnu", false),
("s390x-unknown-linux-gnu", false),
("x86_64-unknown-freebsd", false),
("x86_64-unknown-illumos", false),
("x86_64-unknown-linux-musl", false),
("x86_64-unknown-netbsd", false),
];
if !supported_platforms.contains(&(&*config.build.triple, asserts))
&& (asserts || !supported_platforms.contains(&(&*config.build.triple, true)))
{
return false;
}
if is_ci_llvm_modified(config) {
eprintln!("Detected LLVM as non-available: running in CI and modified LLVM in this change");
return false;
}
true
}
pub(crate) fn is_ci_llvm_modified(config: &Config) -> bool {
if !CiEnv::is_ci() {
return false;
}
if CiEnv::is_rust_lang_managed_ci_job() {
assert!(
config.in_tree_llvm_info.is_managed_git_subrepository(),
"LLVM submodule must be fetched in rust-lang/rust managed CI builders."
);
}
else if !config.in_tree_llvm_info.is_managed_git_subrepository() {
return false;
}
let llvm_sha = detect_llvm_sha(config, true);
let head_sha = crate::output(
helpers::git(Some(&config.src)).arg("rev-parse").arg("HEAD").as_command_mut(),
);
let head_sha = head_sha.trim();
llvm_sha == head_sha
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Llvm {
pub target: TargetSelection,
}
impl Step for Llvm {
type Output = LlvmResult;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("src/llvm-project").path("src/llvm-project/llvm")
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Llvm { target: run.target });
}
fn run(self, builder: &Builder<'_>) -> LlvmResult {
let target = self.target;
let target_native = if self.target.starts_with("riscv") {
let idx = target.triple.find('-').unwrap();
format!("riscv{}{}", &target.triple[5..7], &target.triple[idx..])
} else if self.target.starts_with("powerpc") && self.target.ends_with("freebsd") {
format!("{}{}", self.target, "13.0")
} else {
target.to_string()
};
let Meta { stamp, res, out_dir, root } = match prebuilt_llvm_config(builder, target, true) {
LlvmBuildStatus::AlreadyBuilt(p) => return p,
LlvmBuildStatus::ShouldBuild(m) => m,
};
if builder.llvm_link_shared() && target.is_windows() {
panic!("shared linking to LLVM is not currently supported on {}", target.triple);
}
let _guard = builder.msg_unstaged(Kind::Build, "LLVM", target);
t!(stamp.remove());
let _time = helpers::timeit(builder);
t!(fs::create_dir_all(&out_dir));
let mut cfg = cmake::Config::new(builder.src.join(root));
let mut ldflags = LdFlags::default();
let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
(false, _) => "Debug",
(true, false) => "Release",
(true, true) => "RelWithDebInfo",
};
let llvm_targets = match &builder.config.llvm_targets {
Some(s) => s,
None => {
"AArch64;ARM;BPF;Hexagon;LoongArch;MSP430;Mips;NVPTX;PowerPC;RISCV;\
Sparc;SystemZ;WebAssembly;X86"
}
};
let llvm_exp_targets = match builder.config.llvm_experimental_targets {
Some(ref s) => s,
None => "AVR;M68k;CSKY;Xtensa",
};
let assertions = if builder.config.llvm_assertions { "ON" } else { "OFF" };
let plugins = if builder.config.llvm_plugins { "ON" } else { "OFF" };
let enable_tests = if builder.config.llvm_tests { "ON" } else { "OFF" };
let enable_warnings = if builder.config.llvm_enable_warnings { "ON" } else { "OFF" };
cfg.out_dir(&out_dir)
.profile(profile)
.define("LLVM_ENABLE_ASSERTIONS", assertions)
.define("LLVM_UNREACHABLE_OPTIMIZE", "OFF")
.define("LLVM_ENABLE_PLUGINS", plugins)
.define("LLVM_TARGETS_TO_BUILD", llvm_targets)
.define("LLVM_EXPERIMENTAL_TARGETS_TO_BUILD", llvm_exp_targets)
.define("LLVM_INCLUDE_EXAMPLES", "OFF")
.define("LLVM_INCLUDE_DOCS", "OFF")
.define("LLVM_INCLUDE_BENCHMARKS", "OFF")
.define("LLVM_INCLUDE_TESTS", enable_tests)
.define("LLVM_ENABLE_TERMINFO", "OFF")
.define("LLVM_ENABLE_LIBEDIT", "OFF")
.define("LLVM_ENABLE_BINDINGS", "OFF")
.define("LLVM_ENABLE_Z3_SOLVER", "OFF")
.define("LLVM_PARALLEL_COMPILE_JOBS", builder.jobs().to_string())
.define("LLVM_TARGET_ARCH", target_native.split('-').next().unwrap())
.define("LLVM_DEFAULT_TARGET_TRIPLE", target_native)
.define("LLVM_ENABLE_WARNINGS", enable_warnings);
cfg.define("LLVM_INSTALL_UTILS", "ON");
if builder.config.llvm_profile_generate {
cfg.define("LLVM_BUILD_INSTRUMENTED", "IR");
if let Ok(llvm_profile_dir) = std::env::var("LLVM_PROFILE_DIR") {
cfg.define("LLVM_PROFILE_DATA_DIR", llvm_profile_dir);
}
cfg.define("LLVM_BUILD_RUNTIME", "No");
}
if let Some(path) = builder.config.llvm_profile_use.as_ref() {
cfg.define("LLVM_PROFDATA_FILE", path);
}
if !target.is_windows() {
cfg.define("LLVM_ENABLE_ZLIB", "ON");
} else {
cfg.define("LLVM_ENABLE_ZLIB", "OFF");
}
if target.contains("apple-ios")
|| target.contains("apple-tvos")
|| target.contains("apple-watchos")
|| target.contains("apple-visionos")
{
cfg.define("CMAKE_OSX_SYSROOT", "/");
cfg.define("CMAKE_OSX_DEPLOYMENT_TARGET", "");
cfg.define("LLVM_ENABLE_PLUGINS", "OFF");
cfg.define("LLVM_ENABLE_ZLIB", "OFF");
}
if builder.llvm_link_shared() {
cfg.define("LLVM_LINK_LLVM_DYLIB", "ON");
}
if (target.starts_with("csky")
|| target.starts_with("riscv")
|| target.starts_with("sparc-"))
&& !target.contains("freebsd")
&& !target.contains("openbsd")
&& !target.contains("netbsd")
{
ldflags.exe.push(" -latomic");
ldflags.shared.push(" -latomic");
}
if target.starts_with("mips") && target.contains("netbsd") {
ldflags.exe.push(" -latomic");
ldflags.shared.push(" -latomic");
}
if target.is_msvc() {
cfg.define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded");
cfg.static_crt(true);
}
if target.starts_with("i686") {
cfg.define("LLVM_BUILD_32_BITS", "ON");
}
let mut enabled_llvm_projects = Vec::new();
if helpers::forcing_clang_based_tests() {
enabled_llvm_projects.push("clang");
enabled_llvm_projects.push("compiler-rt");
}
if builder.config.llvm_polly {
enabled_llvm_projects.push("polly");
}
if builder.config.llvm_clang {
enabled_llvm_projects.push("clang");
}
cfg.define("LLVM_ENABLE_LIBXML2", "OFF");
if !enabled_llvm_projects.is_empty() {
enabled_llvm_projects.sort();
enabled_llvm_projects.dedup();
cfg.define("LLVM_ENABLE_PROJECTS", enabled_llvm_projects.join(";"));
}
let mut enabled_llvm_runtimes = Vec::new();
if builder.config.llvm_offload {
enabled_llvm_runtimes.push("offload");
enabled_llvm_runtimes.push("openmp");
}
if !enabled_llvm_runtimes.is_empty() {
enabled_llvm_runtimes.sort();
enabled_llvm_runtimes.dedup();
cfg.define("LLVM_ENABLE_RUNTIMES", enabled_llvm_runtimes.join(";"));
}
if let Some(num_linkers) = builder.config.llvm_link_jobs {
if num_linkers > 0 {
cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string());
}
}
if target != builder.config.build {
let LlvmResult { llvm_config, .. } =
builder.ensure(Llvm { target: builder.config.build });
if !builder.config.dry_run() {
let llvm_bindir =
command(&llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
let host_bin = Path::new(llvm_bindir.trim());
cfg.define(
"LLVM_TABLEGEN",
host_bin.join("llvm-tblgen").with_extension(EXE_EXTENSION),
);
cfg.define("LLVM_NM", host_bin.join("llvm-nm").with_extension(EXE_EXTENSION));
}
cfg.define("LLVM_CONFIG_PATH", llvm_config);
if builder.config.llvm_clang {
let build_bin = builder.llvm_out(builder.config.build).join("build").join("bin");
let clang_tblgen = build_bin.join("clang-tblgen").with_extension(EXE_EXTENSION);
if !builder.config.dry_run() && !clang_tblgen.exists() {
panic!("unable to find {}", clang_tblgen.display());
}
cfg.define("CLANG_TABLEGEN", clang_tblgen);
}
}
let llvm_version_suffix = if let Some(ref suffix) = builder.config.llvm_version_suffix {
if !suffix.is_empty() { Some(suffix.to_string()) } else { None }
} else if builder.config.channel == "dev" {
Some("-rust-dev".to_string())
} else {
Some(format!("-rust-{}-{}", builder.version, builder.config.channel))
};
if let Some(ref suffix) = llvm_version_suffix {
cfg.define("LLVM_VERSION_SUFFIX", suffix);
}
configure_cmake(builder, target, &mut cfg, true, ldflags, &[]);
configure_llvm(builder, target, &mut cfg);
for (key, val) in &builder.config.llvm_build_config {
cfg.define(key, val);
}
if builder.config.dry_run() {
return res;
}
cfg.build();
let find_llvm_lib_name = |extension| {
let version =
command(&res.llvm_config).arg("--version").run_capture_stdout(builder).stdout();
let major = version.split('.').next().unwrap();
match &llvm_version_suffix {
Some(version_suffix) => format!("libLLVM-{major}{version_suffix}.{extension}"),
None => format!("libLLVM-{major}.{extension}"),
}
};
if builder.llvm_link_shared() && target.contains("apple-darwin") {
let lib_name = find_llvm_lib_name("dylib");
let lib_llvm = out_dir.join("build").join("lib").join(lib_name);
if !lib_llvm.exists() {
t!(builder.symlink_file("libLLVM.dylib", &lib_llvm));
}
}
if builder.llvm_link_shared()
&& builder.config.llvm_optimize
&& !builder.config.llvm_release_debuginfo
{
let lib_name = find_llvm_lib_name("so");
crate::core::build_steps::compile::strip_debug(
builder,
target,
&out_dir.join("lib").join(&lib_name),
);
crate::core::build_steps::compile::strip_debug(
builder,
target,
&out_dir.join("build").join("lib").join(&lib_name),
);
}
t!(stamp.write());
res
}
}
fn check_llvm_version(builder: &Builder<'_>, llvm_config: &Path) {
if builder.config.dry_run() {
return;
}
let version = command(llvm_config).arg("--version").run_capture_stdout(builder).stdout();
let mut parts = version.split('.').take(2).filter_map(|s| s.parse::<u32>().ok());
if let (Some(major), Some(_minor)) = (parts.next(), parts.next()) {
if major >= 18 {
return;
}
}
panic!("\n\nbad LLVM version: {version}, need >=18\n\n")
}
fn configure_cmake(
builder: &Builder<'_>,
target: TargetSelection,
cfg: &mut cmake::Config,
use_compiler_launcher: bool,
mut ldflags: LdFlags,
suppressed_compiler_flag_prefixes: &[&str],
) {
cfg.define("CMAKE_INSTALL_MESSAGE", "LAZY");
cfg.env("DESTDIR", "");
if builder.ninja() {
cfg.generator("Ninja");
}
cfg.target(&target.triple).host(&builder.config.build.triple);
if target != builder.config.build {
cfg.define("CMAKE_CROSSCOMPILING", "True");
if target.contains("netbsd") {
cfg.define("CMAKE_SYSTEM_NAME", "NetBSD");
} else if target.contains("dragonfly") {
cfg.define("CMAKE_SYSTEM_NAME", "DragonFly");
} else if target.contains("freebsd") {
cfg.define("CMAKE_SYSTEM_NAME", "FreeBSD");
} else if target.is_windows() {
cfg.define("CMAKE_SYSTEM_NAME", "Windows");
} else if target.contains("haiku") {
cfg.define("CMAKE_SYSTEM_NAME", "Haiku");
} else if target.contains("solaris") || target.contains("illumos") {
cfg.define("CMAKE_SYSTEM_NAME", "SunOS");
} else if target.contains("linux") {
cfg.define("CMAKE_SYSTEM_NAME", "Linux");
} else {
builder.info(&format!(
"could not determine CMAKE_SYSTEM_NAME from the target `{target}`, build may fail",
));
}
if target.contains("darwin") {
if target.starts_with("aarch64") {
cfg.define("CMAKE_OSX_ARCHITECTURES", "arm64");
} else if target.starts_with("i686") {
cfg.define("CMAKE_OSX_ARCHITECTURES", "i386");
} else {
cfg.define("CMAKE_OSX_ARCHITECTURES", target.triple.split('-').next().unwrap());
}
}
}
let sanitize_cc = |cc: &Path| {
if target.is_msvc() {
OsString::from(cc.to_str().unwrap().replace('\\', "/"))
} else {
cc.as_os_str().to_owned()
}
};
if target.is_msvc() && !builder.ninja() {
return;
}
let (cc, cxx) = match builder.config.llvm_clang_cl {
Some(ref cl) => (cl.into(), cl.into()),
None => (builder.cc(target), builder.cxx(target).unwrap()),
};
if target.is_msvc() && builder.ninja() && builder.config.ccache.is_some() {
let mut wrap_cc = env::current_exe().expect("failed to get cwd");
wrap_cc.set_file_name("sccache-plus-cl.exe");
cfg.define("CMAKE_C_COMPILER", sanitize_cc(&wrap_cc))
.define("CMAKE_CXX_COMPILER", sanitize_cc(&wrap_cc));
cfg.env("SCCACHE_PATH", builder.config.ccache.as_ref().unwrap())
.env("SCCACHE_TARGET", target.triple)
.env("SCCACHE_CC", &cc)
.env("SCCACHE_CXX", &cxx);
if builder.config.llvm_clang_cl.is_some() && target.contains("i686") {
cfg.env("SCCACHE_EXTRA_ARGS", "-m32");
}
} else {
if use_compiler_launcher {
if let Some(ref ccache) = builder.config.ccache {
cfg.define("CMAKE_C_COMPILER_LAUNCHER", ccache)
.define("CMAKE_CXX_COMPILER_LAUNCHER", ccache);
}
}
cfg.define("CMAKE_C_COMPILER", sanitize_cc(&cc))
.define("CMAKE_CXX_COMPILER", sanitize_cc(&cxx))
.define("CMAKE_ASM_COMPILER", sanitize_cc(&cc));
}
cfg.build_arg("-j").build_arg(builder.jobs().to_string());
let mut cflags: OsString = builder
.cflags(target, GitRepo::Llvm, CLang::C)
.into_iter()
.filter(|flag| {
!suppressed_compiler_flag_prefixes
.iter()
.any(|suppressed_prefix| flag.starts_with(suppressed_prefix))
})
.collect::<Vec<String>>()
.join(" ")
.into();
if let Some(ref s) = builder.config.llvm_cflags {
cflags.push(" ");
cflags.push(s);
}
if builder.config.llvm_clang_cl.is_some() {
cflags.push(format!(" --target={target}"));
}
cfg.define("CMAKE_C_FLAGS", cflags);
let mut cxxflags: OsString = builder
.cflags(target, GitRepo::Llvm, CLang::Cxx)
.into_iter()
.filter(|flag| {
!suppressed_compiler_flag_prefixes
.iter()
.any(|suppressed_prefix| flag.starts_with(suppressed_prefix))
})
.collect::<Vec<String>>()
.join(" ")
.into();
if let Some(ref s) = builder.config.llvm_cxxflags {
cxxflags.push(" ");
cxxflags.push(s);
}
if builder.config.llvm_clang_cl.is_some() {
cxxflags.push(format!(" --target={target}"));
}
cfg.define("CMAKE_CXX_FLAGS", cxxflags);
if let Some(ar) = builder.ar(target) {
if ar.is_absolute() {
cfg.define("CMAKE_AR", sanitize_cc(&ar));
}
}
if let Some(ranlib) = builder.ranlib(target) {
if ranlib.is_absolute() {
cfg.define("CMAKE_RANLIB", sanitize_cc(&ranlib));
}
}
if let Some(ref flags) = builder.config.llvm_ldflags {
ldflags.push_all(flags);
}
if let Some(flags) = get_var("LDFLAGS", &builder.config.build.triple, &target.triple) {
ldflags.push_all(&flags);
}
if builder.config.llvm_static_stdcpp
&& !target.is_msvc()
&& !target.contains("netbsd")
&& !target.contains("solaris")
{
if target.contains("apple") || target.is_windows() {
ldflags.push_all("-static-libstdc++");
} else {
ldflags.push_all("-Wl,-Bsymbolic -static-libstdc++");
}
}
cfg.define("CMAKE_SHARED_LINKER_FLAGS", &ldflags.shared);
cfg.define("CMAKE_MODULE_LINKER_FLAGS", &ldflags.module);
cfg.define("CMAKE_EXE_LINKER_FLAGS", &ldflags.exe);
if env::var_os("SCCACHE_ERROR_LOG").is_some() {
cfg.env("RUSTC_LOG", "sccache=warn");
}
}
fn configure_llvm(builder: &Builder<'_>, target: TargetSelection, cfg: &mut cmake::Config) {
if builder.config.llvm_thin_lto {
cfg.define("LLVM_ENABLE_LTO", "Thin");
if !target.contains("apple") {
cfg.define("LLVM_ENABLE_LLD", "ON");
}
}
if builder.config.llvm_libzstd {
cfg.define("LLVM_ENABLE_ZSTD", "FORCE_ON");
cfg.define("LLVM_USE_STATIC_ZSTD", "TRUE");
} else {
cfg.define("LLVM_ENABLE_ZSTD", "OFF");
}
if let Some(ref linker) = builder.config.llvm_use_linker {
cfg.define("LLVM_USE_LINKER", linker);
}
if builder.config.llvm_allow_old_toolchain {
cfg.define("LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN", "YES");
}
}
fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
let kind = if host == target { "HOST" } else { "TARGET" };
let target_u = target.replace('-', "_");
env::var_os(format!("{var_base}_{target}"))
.or_else(|| env::var_os(format!("{}_{}", var_base, target_u)))
.or_else(|| env::var_os(format!("{}_{}", kind, var_base)))
.or_else(|| env::var_os(var_base))
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Enzyme {
pub target: TargetSelection,
}
impl Step for Enzyme {
type Output = PathBuf;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("src/tools/enzyme/enzyme")
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Enzyme { target: run.target });
}
fn run(self, builder: &Builder<'_>) -> PathBuf {
builder.require_submodule(
"src/tools/enzyme",
Some("The Enzyme sources are required for autodiff."),
);
if builder.config.dry_run() {
let out_dir = builder.enzyme_out(self.target);
return out_dir;
}
let target = self.target;
let LlvmResult { llvm_config, .. } = builder.ensure(Llvm { target: self.target });
static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
generate_smart_stamp_hash(
builder,
&builder.config.src.join("src/tools/enzyme"),
builder.enzyme_info.sha().unwrap_or_default(),
)
});
let out_dir = builder.enzyme_out(target);
let stamp = out_dir.join("enzyme-finished-building");
let stamp = HashStamp::new(stamp, Some(smart_stamp_hash));
if stamp.is_done() {
if stamp.hash.is_none() {
builder.info(
"Could not determine the Enzyme submodule commit hash. \
Assuming that an Enzyme rebuild is not necessary.",
);
builder.info(&format!(
"To force Enzyme to rebuild, remove the file `{}`",
stamp.path.display()
));
}
return out_dir;
}
builder.info(&format!("Building Enzyme for {}", target));
t!(stamp.remove());
let _time = helpers::timeit(builder);
t!(fs::create_dir_all(&out_dir));
builder
.config
.update_submodule(Path::new("src").join("tools").join("enzyme").to_str().unwrap());
let mut cfg = cmake::Config::new(builder.src.join("src/tools/enzyme/enzyme/"));
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), &[]);
let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
(false, _) => "Debug",
(true, false) => "Release",
(true, true) => "RelWithDebInfo",
};
cfg.out_dir(&out_dir)
.profile(profile)
.env("LLVM_CONFIG_REAL", &llvm_config)
.define("LLVM_ENABLE_ASSERTIONS", "ON")
.define("ENZYME_EXTERNAL_SHARED_LIB", "ON")
.define("LLVM_DIR", builder.llvm_out(target));
cfg.build();
t!(stamp.write());
out_dir
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Lld {
pub target: TargetSelection,
}
impl Step for Lld {
type Output = PathBuf;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("src/llvm-project/lld")
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Lld { target: run.target });
}
fn run(self, builder: &Builder<'_>) -> PathBuf {
if builder.config.dry_run() {
return PathBuf::from("lld-out-dir-test-gen");
}
let target = self.target;
let LlvmResult { llvm_config, llvm_cmake_dir } = builder.ensure(Llvm { target });
let ci_llvm_bin = llvm_config.parent().unwrap();
if ci_llvm_bin.is_dir() && ci_llvm_bin.file_name().unwrap() == "bin" {
let lld_path = ci_llvm_bin.join(exe("lld", target));
if lld_path.exists() {
return ci_llvm_bin.parent().unwrap().to_path_buf();
}
}
let out_dir = builder.lld_out(target);
let done_stamp = out_dir.join("lld-finished-building");
if done_stamp.exists() {
return out_dir;
}
let _guard = builder.msg_unstaged(Kind::Build, "LLD", target);
let _time = helpers::timeit(builder);
t!(fs::create_dir_all(&out_dir));
let mut cfg = cmake::Config::new(builder.src.join("src/llvm-project/lld"));
let mut ldflags = LdFlags::default();
if builder.config.llvm_profile_generate && target.is_msvc() {
if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
}
}
if builder.config.rpath_enabled(target)
&& helpers::use_host_linker(target)
&& builder.config.llvm_link_shared()
&& target.contains("linux")
{
ldflags.push_all("-Wl,-rpath,'$ORIGIN/../../../'");
}
configure_cmake(builder, target, &mut cfg, true, ldflags, &[]);
configure_llvm(builder, target, &mut cfg);
let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
(false, _) => "Debug",
(true, false) => "Release",
(true, true) => "RelWithDebInfo",
};
cfg.out_dir(&out_dir)
.profile(profile)
.define("LLVM_CMAKE_DIR", llvm_cmake_dir)
.define("LLVM_INCLUDE_TESTS", "OFF");
if target != builder.config.build {
cfg.define(
"LLVM_TABLEGEN_EXE",
llvm_config.with_file_name("llvm-tblgen").with_extension(EXE_EXTENSION),
);
}
cfg.build();
t!(File::create(&done_stamp));
out_dir
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Sanitizers {
pub target: TargetSelection,
}
impl Step for Sanitizers {
type Output = Vec<SanitizerRuntime>;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.alias("sanitizers")
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Sanitizers { target: run.target });
}
fn run(self, builder: &Builder<'_>) -> Self::Output {
let compiler_rt_dir = builder.src.join("src/llvm-project/compiler-rt");
if !compiler_rt_dir.exists() {
return Vec::new();
}
let out_dir = builder.native_dir(self.target).join("sanitizers");
let runtimes = supported_sanitizers(&out_dir, self.target, &builder.config.channel);
if runtimes.is_empty() {
return runtimes;
}
let LlvmResult { llvm_config, .. } = builder.ensure(Llvm { target: builder.config.build });
if builder.config.dry_run() {
return runtimes;
}
let stamp = out_dir.join("sanitizers-finished-building");
let stamp = HashStamp::new(stamp, builder.in_tree_llvm_info.sha());
if stamp.is_done() {
if stamp.hash.is_none() {
builder.info(&format!(
"Rebuild sanitizers by removing the file `{}`",
stamp.path.display()
));
}
return runtimes;
}
let _guard = builder.msg_unstaged(Kind::Build, "sanitizers", self.target);
t!(stamp.remove());
let _time = helpers::timeit(builder);
let mut cfg = cmake::Config::new(&compiler_rt_dir);
cfg.profile("Release");
cfg.define("CMAKE_C_COMPILER_TARGET", self.target.triple);
cfg.define("COMPILER_RT_BUILD_BUILTINS", "OFF");
cfg.define("COMPILER_RT_BUILD_CRT", "OFF");
cfg.define("COMPILER_RT_BUILD_LIBFUZZER", "OFF");
cfg.define("COMPILER_RT_BUILD_PROFILE", "OFF");
cfg.define("COMPILER_RT_BUILD_SANITIZERS", "ON");
cfg.define("COMPILER_RT_BUILD_XRAY", "OFF");
cfg.define("COMPILER_RT_DEFAULT_TARGET_ONLY", "ON");
cfg.define("COMPILER_RT_USE_LIBCXX", "OFF");
cfg.define("LLVM_CONFIG_PATH", &llvm_config);
let use_compiler_launcher = !self.target.contains("apple-darwin");
let suppressed_compiler_flag_prefixes: &[&str] =
if self.target.contains("apple-darwin") { &["-mmacosx-version-min="] } else { &[] };
configure_cmake(
builder,
self.target,
&mut cfg,
use_compiler_launcher,
LdFlags::default(),
suppressed_compiler_flag_prefixes,
);
t!(fs::create_dir_all(&out_dir));
cfg.out_dir(out_dir);
for runtime in &runtimes {
cfg.build_target(&runtime.cmake_target);
cfg.build();
}
t!(stamp.write());
runtimes
}
}
#[derive(Clone, Debug)]
pub struct SanitizerRuntime {
pub cmake_target: String,
pub path: PathBuf,
pub name: String,
}
fn supported_sanitizers(
out_dir: &Path,
target: TargetSelection,
channel: &str,
) -> Vec<SanitizerRuntime> {
let darwin_libs = |os: &str, components: &[&str]| -> Vec<SanitizerRuntime> {
components
.iter()
.map(move |c| SanitizerRuntime {
cmake_target: format!("clang_rt.{}_{}_dynamic", c, os),
path: out_dir
.join(format!("build/lib/darwin/libclang_rt.{}_{}_dynamic.dylib", c, os)),
name: format!("librustc-{}_rt.{}.dylib", channel, c),
})
.collect()
};
let common_libs = |os: &str, arch: &str, components: &[&str]| -> Vec<SanitizerRuntime> {
components
.iter()
.map(move |c| SanitizerRuntime {
cmake_target: format!("clang_rt.{}-{}", c, arch),
path: out_dir.join(format!("build/lib/{}/libclang_rt.{}-{}.a", os, c, arch)),
name: format!("librustc-{}_rt.{}.a", channel, c),
})
.collect()
};
match &*target.triple {
"aarch64-apple-darwin" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
"aarch64-apple-ios" => darwin_libs("ios", &["asan", "tsan"]),
"aarch64-apple-ios-sim" => darwin_libs("iossim", &["asan", "tsan"]),
"aarch64-apple-ios-macabi" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
"aarch64-unknown-fuchsia" => common_libs("fuchsia", "aarch64", &["asan"]),
"aarch64-unknown-linux-gnu" => {
common_libs("linux", "aarch64", &["asan", "lsan", "msan", "tsan", "hwasan"])
}
"aarch64-unknown-linux-ohos" => {
common_libs("linux", "aarch64", &["asan", "lsan", "msan", "tsan", "hwasan"])
}
"loongarch64-unknown-linux-gnu" | "loongarch64-unknown-linux-musl" => {
common_libs("linux", "loongarch64", &["asan", "lsan", "msan", "tsan"])
}
"x86_64-apple-darwin" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
"x86_64-unknown-fuchsia" => common_libs("fuchsia", "x86_64", &["asan"]),
"x86_64-apple-ios" => darwin_libs("iossim", &["asan", "tsan"]),
"x86_64-apple-ios-macabi" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
"x86_64-unknown-freebsd" => common_libs("freebsd", "x86_64", &["asan", "msan", "tsan"]),
"x86_64-unknown-netbsd" => {
common_libs("netbsd", "x86_64", &["asan", "lsan", "msan", "tsan"])
}
"x86_64-unknown-illumos" => common_libs("illumos", "x86_64", &["asan"]),
"x86_64-pc-solaris" => common_libs("solaris", "x86_64", &["asan"]),
"x86_64-unknown-linux-gnu" => {
common_libs("linux", "x86_64", &["asan", "dfsan", "lsan", "msan", "safestack", "tsan"])
}
"x86_64-unknown-linux-musl" => {
common_libs("linux", "x86_64", &["asan", "lsan", "msan", "tsan"])
}
"s390x-unknown-linux-gnu" => {
common_libs("linux", "s390x", &["asan", "lsan", "msan", "tsan"])
}
"s390x-unknown-linux-musl" => {
common_libs("linux", "s390x", &["asan", "lsan", "msan", "tsan"])
}
"x86_64-unknown-linux-ohos" => {
common_libs("linux", "x86_64", &["asan", "lsan", "msan", "tsan"])
}
_ => Vec::new(),
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CrtBeginEnd {
pub target: TargetSelection,
}
impl Step for CrtBeginEnd {
type Output = PathBuf;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("src/llvm-project/compiler-rt/lib/crt")
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(CrtBeginEnd { target: run.target });
}
fn run(self, builder: &Builder<'_>) -> Self::Output {
builder.require_submodule(
"src/llvm-project",
Some("The LLVM sources are required for the CRT from `compiler-rt`."),
);
let out_dir = builder.native_dir(self.target).join("crt");
if builder.config.dry_run() {
return out_dir;
}
let crtbegin_src = builder.src.join("src/llvm-project/compiler-rt/lib/builtins/crtbegin.c");
let crtend_src = builder.src.join("src/llvm-project/compiler-rt/lib/builtins/crtend.c");
if up_to_date(&crtbegin_src, &out_dir.join("crtbeginS.o"))
&& up_to_date(&crtend_src, &out_dir.join("crtendS.o"))
{
return out_dir;
}
let _guard = builder.msg_unstaged(Kind::Build, "crtbegin.o and crtend.o", self.target);
t!(fs::create_dir_all(&out_dir));
let mut cfg = cc::Build::new();
if let Some(ar) = builder.ar(self.target) {
cfg.archiver(ar);
}
cfg.compiler(builder.cc(self.target));
cfg.cargo_metadata(false)
.out_dir(&out_dir)
.target(&self.target.triple)
.host(&builder.config.build.triple)
.warnings(false)
.debug(false)
.opt_level(3)
.file(crtbegin_src)
.file(crtend_src);
cfg.flag("-std=c11")
.define("CRT_HAS_INITFINI_ARRAY", None)
.define("EH_USE_FRAME_REGISTRY", None);
let objs = cfg.compile_intermediates();
assert_eq!(objs.len(), 2);
for obj in objs {
let base_name = unhashed_basename(&obj);
assert!(base_name == "crtbegin" || base_name == "crtend");
t!(fs::copy(&obj, out_dir.join(format!("{}S.o", base_name))));
t!(fs::rename(&obj, out_dir.join(format!("{}.o", base_name))));
}
out_dir
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Libunwind {
pub target: TargetSelection,
}
impl Step for Libunwind {
type Output = PathBuf;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("src/llvm-project/libunwind")
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Libunwind { target: run.target });
}
fn run(self, builder: &Builder<'_>) -> Self::Output {
builder.require_submodule(
"src/llvm-project",
Some("The LLVM sources are required for libunwind."),
);
if builder.config.dry_run() {
return PathBuf::new();
}
let out_dir = builder.native_dir(self.target).join("libunwind");
let root = builder.src.join("src/llvm-project/libunwind");
if up_to_date(&root, &out_dir.join("libunwind.a")) {
return out_dir;
}
let _guard = builder.msg_unstaged(Kind::Build, "libunwind.a", self.target);
t!(fs::create_dir_all(&out_dir));
let mut cc_cfg = cc::Build::new();
let mut cpp_cfg = cc::Build::new();
cpp_cfg.cpp(true);
cpp_cfg.cpp_set_stdlib(None);
cpp_cfg.flag("-nostdinc++");
cpp_cfg.flag("-fno-exceptions");
cpp_cfg.flag("-fno-rtti");
cpp_cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
for cfg in [&mut cc_cfg, &mut cpp_cfg].iter_mut() {
if let Some(ar) = builder.ar(self.target) {
cfg.archiver(ar);
}
cfg.target(&self.target.triple);
cfg.host(&builder.config.build.triple);
cfg.warnings(false);
cfg.debug(false);
cfg.opt_level(3);
cfg.flag("-fstrict-aliasing");
cfg.flag("-funwind-tables");
cfg.flag("-fvisibility=hidden");
cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
cfg.include(root.join("include"));
cfg.cargo_metadata(false);
cfg.out_dir(&out_dir);
if self.target.contains("x86_64-fortanix-unknown-sgx") {
cfg.static_flag(true);
cfg.flag("-fno-stack-protector");
cfg.flag("-ffreestanding");
cfg.flag("-fexceptions");
cfg.flag("-U_FORTIFY_SOURCE");
cfg.define("_FORTIFY_SOURCE", "0");
cfg.define("RUST_SGX", "1");
cfg.define("__NO_STRING_INLINES", None);
cfg.define("__NO_MATH_INLINES", None);
cfg.define("_LIBUNWIND_IS_BAREMETAL", None);
cfg.define("__LIBUNWIND_IS_NATIVE_ONLY", None);
cfg.define("NDEBUG", None);
}
if self.target.is_windows() {
cfg.define("_LIBUNWIND_HIDE_SYMBOLS", "1");
cfg.define("_LIBUNWIND_IS_NATIVE_ONLY", "1");
}
}
cc_cfg.compiler(builder.cc(self.target));
if let Ok(cxx) = builder.cxx(self.target) {
cpp_cfg.compiler(cxx);
} else {
cc_cfg.compiler(builder.cc(self.target));
}
if cc_cfg.get_compiler().is_like_gnu() {
cc_cfg.flag("-std=c99");
}
if cpp_cfg.get_compiler().is_like_gnu() {
cpp_cfg.flag("-std=c++11");
}
if self.target.contains("x86_64-fortanix-unknown-sgx") || self.target.contains("musl") {
if cpp_cfg.get_compiler().is_like_gnu() {
cpp_cfg.cpp(false);
cpp_cfg.compiler(builder.cc(self.target));
}
}
let mut c_sources = vec![
"Unwind-sjlj.c",
"UnwindLevel1-gcc-ext.c",
"UnwindLevel1.c",
"UnwindRegistersRestore.S",
"UnwindRegistersSave.S",
];
let cpp_sources = vec!["Unwind-EHABI.cpp", "Unwind-seh.cpp", "libunwind.cpp"];
let cpp_len = cpp_sources.len();
if self.target.contains("x86_64-fortanix-unknown-sgx") {
c_sources.push("UnwindRustSgx.c");
}
for src in c_sources {
cc_cfg.file(root.join("src").join(src).canonicalize().unwrap());
}
for src in &cpp_sources {
cpp_cfg.file(root.join("src").join(src).canonicalize().unwrap());
}
cpp_cfg.compile("unwind-cpp");
let mut count = 0;
for entry in fs::read_dir(&out_dir).unwrap() {
let file = entry.unwrap().path().canonicalize().unwrap();
if file.is_file() && file.extension() == Some(OsStr::new("o")) {
let base_name = unhashed_basename(&file);
if cpp_sources.iter().any(|f| *base_name == f[..f.len() - 4]) {
cc_cfg.object(&file);
count += 1;
}
}
}
assert_eq!(cpp_len, count, "Can't get object files from {out_dir:?}");
cc_cfg.compile("unwind");
out_dir
}
}