2023-01-24 11:56:42 -06:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-06-15 12:56:01 -05:00
|
|
|
use std::process::Command;
|
2024-07-28 17:13:50 -05:00
|
|
|
use std::{env, fs};
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-08-09 13:20:12 -05:00
|
|
|
use crate::path::{Dirs, RelPath};
|
|
|
|
use crate::rustc_info::get_file_name;
|
|
|
|
use crate::utils::{
|
2024-04-05 11:20:23 -05:00
|
|
|
remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler, LogGroup,
|
2023-06-15 12:56:01 -05:00
|
|
|
};
|
2023-08-09 13:20:12 -05:00
|
|
|
use crate::{config, CodegenBackend, SysrootKind};
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2022-12-14 12:30:46 -06:00
|
|
|
static DIST_DIR: RelPath = RelPath::DIST;
|
|
|
|
static BIN_DIR: RelPath = RelPath::DIST.join("bin");
|
|
|
|
static LIB_DIR: RelPath = RelPath::DIST.join("lib");
|
|
|
|
|
2021-07-07 04:14:20 -05:00
|
|
|
pub(crate) fn build_sysroot(
|
2022-12-14 12:30:46 -06:00
|
|
|
dirs: &Dirs,
|
2021-07-07 04:14:20 -05:00
|
|
|
sysroot_kind: SysrootKind,
|
2023-06-15 12:56:01 -05:00
|
|
|
cg_clif_dylib_src: &CodegenBackend,
|
2023-01-24 11:56:42 -06:00
|
|
|
bootstrap_host_compiler: &Compiler,
|
2023-06-15 12:56:01 -05:00
|
|
|
rustup_toolchain_name: Option<&str>,
|
2023-01-24 11:56:42 -06:00
|
|
|
target_triple: String,
|
|
|
|
) -> Compiler {
|
2023-07-22 08:32:34 -05:00
|
|
|
let _guard = LogGroup::guard("Build sysroot");
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
|
|
|
|
|
2022-12-14 12:30:46 -06:00
|
|
|
DIST_DIR.ensure_fresh(dirs);
|
|
|
|
BIN_DIR.ensure_exists(dirs);
|
|
|
|
LIB_DIR.ensure_exists(dirs);
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
let is_native = bootstrap_host_compiler.triple == target_triple;
|
|
|
|
|
2023-06-15 12:56:01 -05:00
|
|
|
let cg_clif_dylib_path = match cg_clif_dylib_src {
|
|
|
|
CodegenBackend::Local(src_path) => {
|
|
|
|
// Copy the backend
|
|
|
|
let cg_clif_dylib_path = if cfg!(windows) {
|
|
|
|
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
|
|
|
|
// binaries.
|
|
|
|
BIN_DIR
|
|
|
|
} else {
|
|
|
|
LIB_DIR
|
|
|
|
}
|
|
|
|
.to_path(dirs)
|
|
|
|
.join(src_path.file_name().unwrap());
|
|
|
|
try_hard_link(src_path, &cg_clif_dylib_path);
|
|
|
|
CodegenBackend::Local(cg_clif_dylib_path)
|
|
|
|
}
|
|
|
|
CodegenBackend::Builtin(name) => CodegenBackend::Builtin(name.clone()),
|
|
|
|
};
|
2022-05-15 05:32:19 -05:00
|
|
|
|
|
|
|
// Build and copy rustc and cargo wrappers
|
2023-06-15 12:56:01 -05:00
|
|
|
let wrapper_base_name = get_file_name(&bootstrap_host_compiler.rustc, "____", "bin");
|
2022-12-14 12:30:46 -06:00
|
|
|
for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] {
|
2023-01-24 11:56:42 -06:00
|
|
|
let wrapper_name = wrapper_base_name.replace("____", wrapper);
|
2022-08-24 11:40:58 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
|
2023-03-15 09:41:48 -05:00
|
|
|
let wrapper_path = DIST_DIR.to_path(dirs).join(&wrapper_name);
|
2022-05-15 05:32:19 -05:00
|
|
|
build_cargo_wrapper_cmd
|
2022-12-14 12:30:46 -06:00
|
|
|
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
|
2022-05-15 05:32:19 -05:00
|
|
|
.arg("-o")
|
2023-03-15 09:41:48 -05:00
|
|
|
.arg(&wrapper_path)
|
2023-01-24 11:56:42 -06:00
|
|
|
.arg("-Cstrip=debuginfo");
|
2023-06-15 12:56:01 -05:00
|
|
|
if let Some(rustup_toolchain_name) = &rustup_toolchain_name {
|
|
|
|
build_cargo_wrapper_cmd
|
|
|
|
.env("TOOLCHAIN_NAME", rustup_toolchain_name)
|
|
|
|
.env_remove("CARGO")
|
|
|
|
.env_remove("RUSTC")
|
|
|
|
.env_remove("RUSTDOC");
|
|
|
|
} else {
|
|
|
|
build_cargo_wrapper_cmd
|
|
|
|
.env_remove("TOOLCHAIN_NAME")
|
|
|
|
.env("CARGO", &bootstrap_host_compiler.cargo)
|
|
|
|
.env("RUSTC", &bootstrap_host_compiler.rustc)
|
|
|
|
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
|
|
|
|
}
|
|
|
|
if let CodegenBackend::Builtin(name) = cg_clif_dylib_src {
|
|
|
|
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", name);
|
|
|
|
}
|
2022-05-15 05:32:19 -05:00
|
|
|
spawn_and_wait(build_cargo_wrapper_cmd);
|
2023-03-15 09:41:48 -05:00
|
|
|
try_hard_link(wrapper_path, BIN_DIR.to_path(dirs).join(wrapper_name));
|
2022-05-15 05:32:19 -05:00
|
|
|
}
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
let host = build_sysroot_for_triple(
|
|
|
|
dirs,
|
|
|
|
bootstrap_host_compiler.clone(),
|
|
|
|
&cg_clif_dylib_path,
|
|
|
|
sysroot_kind,
|
|
|
|
);
|
|
|
|
host.install_into_sysroot(&DIST_DIR.to_path(dirs));
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
if !is_native {
|
|
|
|
build_sysroot_for_triple(
|
|
|
|
dirs,
|
|
|
|
{
|
|
|
|
let mut bootstrap_target_compiler = bootstrap_host_compiler.clone();
|
|
|
|
bootstrap_target_compiler.triple = target_triple.clone();
|
|
|
|
bootstrap_target_compiler.set_cross_linker_and_runner();
|
|
|
|
bootstrap_target_compiler
|
|
|
|
},
|
|
|
|
&cg_clif_dylib_path,
|
|
|
|
sysroot_kind,
|
|
|
|
)
|
|
|
|
.install_into_sysroot(&DIST_DIR.to_path(dirs));
|
|
|
|
}
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
|
|
|
|
// libstd.
|
|
|
|
for lib in host.libs {
|
|
|
|
let filename = lib.file_name().unwrap().to_str().unwrap();
|
|
|
|
if filename.contains("std-") && !filename.contains(".rlib") {
|
|
|
|
try_hard_link(&lib, LIB_DIR.to_path(dirs).join(lib.file_name().unwrap()));
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
2023-01-24 11:56:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut target_compiler = {
|
|
|
|
let dirs: &Dirs = &dirs;
|
|
|
|
let rustc_clif =
|
|
|
|
RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustc-clif"));
|
|
|
|
let rustdoc_clif =
|
|
|
|
RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustdoc-clif"));
|
|
|
|
|
|
|
|
Compiler {
|
|
|
|
cargo: bootstrap_host_compiler.cargo.clone(),
|
|
|
|
rustc: rustc_clif.clone(),
|
|
|
|
rustdoc: rustdoc_clif.clone(),
|
2023-08-09 13:20:12 -05:00
|
|
|
rustflags: vec![],
|
|
|
|
rustdocflags: vec![],
|
2023-01-24 11:56:42 -06:00
|
|
|
triple: target_triple,
|
|
|
|
runner: vec![],
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
2023-01-24 11:56:42 -06:00
|
|
|
};
|
|
|
|
if !is_native {
|
|
|
|
target_compiler.set_cross_linker_and_runner();
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
2023-01-24 11:56:42 -06:00
|
|
|
target_compiler
|
|
|
|
}
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
struct SysrootTarget {
|
|
|
|
triple: String,
|
|
|
|
libs: Vec<PathBuf>,
|
|
|
|
}
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
impl SysrootTarget {
|
|
|
|
fn install_into_sysroot(&self, sysroot: &Path) {
|
|
|
|
if self.libs.is_empty() {
|
|
|
|
return;
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
let target_rustlib_lib = sysroot.join("lib").join("rustlib").join(&self.triple).join("lib");
|
|
|
|
fs::create_dir_all(&target_rustlib_lib).unwrap();
|
|
|
|
|
|
|
|
for lib in &self.libs {
|
|
|
|
try_hard_link(lib, target_rustlib_lib.join(lib.file_name().unwrap()));
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:56:01 -05:00
|
|
|
pub(crate) static STDLIB_SRC: RelPath = RelPath::BUILD.join("stdlib");
|
2023-01-24 11:56:42 -06:00
|
|
|
pub(crate) static STANDARD_LIBRARY: CargoProject =
|
2023-06-15 12:56:01 -05:00
|
|
|
CargoProject::new(&STDLIB_SRC.join("library/sysroot"), "stdlib_target");
|
2023-01-24 11:56:42 -06:00
|
|
|
pub(crate) static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup");
|
2022-12-14 12:30:46 -06:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
#[must_use]
|
|
|
|
fn build_sysroot_for_triple(
|
|
|
|
dirs: &Dirs,
|
|
|
|
compiler: Compiler,
|
2023-06-15 12:56:01 -05:00
|
|
|
cg_clif_dylib_path: &CodegenBackend,
|
2023-01-24 11:56:42 -06:00
|
|
|
sysroot_kind: SysrootKind,
|
|
|
|
) -> SysrootTarget {
|
|
|
|
match sysroot_kind {
|
|
|
|
SysrootKind::None => build_rtstartup(dirs, &compiler)
|
|
|
|
.unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }),
|
|
|
|
SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler),
|
2024-08-10 09:16:53 -05:00
|
|
|
SysrootKind::Clif => build_clif_sysroot_for_triple(dirs, compiler, cg_clif_dylib_path),
|
2023-01-24 11:56:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
fn build_llvm_sysroot_for_triple(compiler: Compiler) -> SysrootTarget {
|
2023-08-09 13:20:12 -05:00
|
|
|
let default_sysroot = crate::rustc_info::get_default_sysroot(&compiler.rustc);
|
2023-01-24 11:56:42 -06:00
|
|
|
|
|
|
|
let mut target_libs = SysrootTarget { triple: compiler.triple, libs: vec![] };
|
|
|
|
|
|
|
|
for entry in fs::read_dir(
|
|
|
|
default_sysroot.join("lib").join("rustlib").join(&target_libs.triple).join("lib"),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
if entry.file_type().unwrap().is_dir() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let file = entry.path();
|
|
|
|
let file_name_str = file.file_name().unwrap().to_str().unwrap();
|
|
|
|
if (file_name_str.contains("rustc_")
|
|
|
|
&& !file_name_str.contains("rustc_std_workspace_")
|
|
|
|
&& !file_name_str.contains("rustc_demangle"))
|
|
|
|
|| file_name_str.contains("chalk")
|
|
|
|
|| file_name_str.contains("tracing")
|
|
|
|
|| file_name_str.contains("regex")
|
|
|
|
{
|
|
|
|
// These are large crates that are part of the rustc-dev component and are not
|
|
|
|
// necessary to run regular programs.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
target_libs.libs.push(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
target_libs
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2021-07-07 04:14:20 -05:00
|
|
|
fn build_clif_sysroot_for_triple(
|
2022-12-14 12:30:46 -06:00
|
|
|
dirs: &Dirs,
|
2023-01-24 11:56:42 -06:00
|
|
|
mut compiler: Compiler,
|
2023-06-15 12:56:01 -05:00
|
|
|
cg_clif_dylib_path: &CodegenBackend,
|
2023-01-24 11:56:42 -06:00
|
|
|
) -> SysrootTarget {
|
|
|
|
let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] };
|
|
|
|
|
|
|
|
if let Some(rtstartup_target_libs) = build_rtstartup(dirs, &compiler) {
|
|
|
|
rtstartup_target_libs.install_into_sysroot(&RTSTARTUP_SYSROOT.to_path(dirs));
|
|
|
|
|
|
|
|
target_libs.libs.extend(rtstartup_target_libs.libs);
|
|
|
|
}
|
|
|
|
|
2024-08-10 09:16:53 -05:00
|
|
|
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join("release");
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-08-09 13:20:12 -05:00
|
|
|
if !config::get_bool("keep_sysroot") {
|
2022-05-15 05:32:19 -05:00
|
|
|
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
|
|
|
|
// recompilation as they are not affected by changes in cg_clif.
|
2023-01-24 11:56:42 -06:00
|
|
|
remove_dir_if_exists(&build_dir.join("deps"));
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build sysroot
|
2023-08-09 13:20:12 -05:00
|
|
|
let mut rustflags = vec!["-Zforce-unstable-if-unmarked".to_owned(), "-Cpanic=abort".to_owned()];
|
2023-06-15 12:56:01 -05:00
|
|
|
match cg_clif_dylib_path {
|
|
|
|
CodegenBackend::Local(path) => {
|
2023-08-09 13:20:12 -05:00
|
|
|
rustflags.push(format!("-Zcodegen-backend={}", path.to_str().unwrap()));
|
2023-06-15 12:56:01 -05:00
|
|
|
}
|
|
|
|
CodegenBackend::Builtin(name) => {
|
2023-08-09 13:20:12 -05:00
|
|
|
rustflags.push(format!("-Zcodegen-backend={name}"));
|
2023-06-15 12:56:01 -05:00
|
|
|
}
|
|
|
|
};
|
2023-01-24 11:56:42 -06:00
|
|
|
// Necessary for MinGW to find rsbegin.o and rsend.o
|
2023-08-09 13:20:12 -05:00
|
|
|
rustflags.push("--sysroot".to_owned());
|
|
|
|
rustflags.push(RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap().to_owned());
|
2024-08-10 09:16:53 -05:00
|
|
|
|
|
|
|
// Incremental compilation by default disables mir inlining. This leads to both a decent
|
|
|
|
// compile perf and a significant runtime perf regression. As such forcefully enable mir
|
|
|
|
// inlining.
|
|
|
|
rustflags.push("-Zinline-mir".to_owned());
|
|
|
|
|
2023-10-21 14:54:51 -05:00
|
|
|
if let Some(prefix) = env::var_os("CG_CLIF_STDLIB_REMAP_PATH_PREFIX") {
|
|
|
|
rustflags.push("--remap-path-prefix".to_owned());
|
|
|
|
rustflags.push(format!(
|
|
|
|
"{}={}",
|
|
|
|
STDLIB_SRC.to_path(dirs).to_str().unwrap(),
|
|
|
|
prefix.to_str().unwrap()
|
|
|
|
));
|
|
|
|
}
|
2023-08-09 13:20:12 -05:00
|
|
|
compiler.rustflags.extend(rustflags);
|
2022-12-14 12:30:46 -06:00
|
|
|
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
|
2024-08-10 09:16:53 -05:00
|
|
|
build_cmd.arg("--release");
|
2024-05-11 08:56:59 -05:00
|
|
|
build_cmd.arg("--features").arg("backtrace panic-unwind compiler-builtins-no-f16-f128");
|
2023-06-15 12:56:01 -05:00
|
|
|
build_cmd.env("CARGO_PROFILE_RELEASE_DEBUG", "true");
|
2021-12-20 11:56:35 -06:00
|
|
|
build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
|
2023-02-09 05:38:16 -06:00
|
|
|
if compiler.triple.contains("apple") {
|
|
|
|
build_cmd.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed");
|
|
|
|
}
|
2021-07-07 04:14:20 -05:00
|
|
|
spawn_and_wait(build_cmd);
|
|
|
|
|
2022-12-14 12:30:46 -06:00
|
|
|
for entry in fs::read_dir(build_dir.join("deps")).unwrap() {
|
2021-07-07 04:14:20 -05:00
|
|
|
let entry = entry.unwrap();
|
|
|
|
if let Some(ext) = entry.path().extension() {
|
2022-07-25 09:07:57 -05:00
|
|
|
if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
|
2021-07-07 04:14:20 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
2023-01-24 11:56:42 -06:00
|
|
|
target_libs.libs.push(entry.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
target_libs
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_rtstartup(dirs: &Dirs, compiler: &Compiler) -> Option<SysrootTarget> {
|
2023-08-09 13:20:12 -05:00
|
|
|
if !config::get_bool("keep_sysroot") {
|
|
|
|
crate::prepare::prepare_stdlib(dirs, &compiler.rustc);
|
2023-06-15 12:56:01 -05:00
|
|
|
}
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
if !compiler.triple.ends_with("windows-gnu") {
|
|
|
|
return None;
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
2023-01-24 11:56:42 -06:00
|
|
|
|
|
|
|
RTSTARTUP_SYSROOT.ensure_fresh(dirs);
|
|
|
|
|
2023-06-15 12:56:01 -05:00
|
|
|
let rtstartup_src = STDLIB_SRC.to_path(dirs).join("library").join("rtstartup");
|
2023-01-24 11:56:42 -06:00
|
|
|
let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] };
|
|
|
|
|
|
|
|
for file in ["rsbegin", "rsend"] {
|
|
|
|
let obj = RTSTARTUP_SYSROOT.to_path(dirs).join(format!("{file}.o"));
|
|
|
|
let mut build_rtstartup_cmd = Command::new(&compiler.rustc);
|
|
|
|
build_rtstartup_cmd
|
|
|
|
.arg("--target")
|
|
|
|
.arg(&compiler.triple)
|
|
|
|
.arg("--emit=obj")
|
|
|
|
.arg("-o")
|
|
|
|
.arg(&obj)
|
|
|
|
.arg(rtstartup_src.join(format!("{file}.rs")));
|
|
|
|
spawn_and_wait(build_rtstartup_cmd);
|
|
|
|
target_libs.libs.push(obj.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(target_libs)
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|