2021-06-08 07:33:25 -05:00
|
|
|
use std::fs;
|
2021-07-04 11:15:13 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-06-08 07:33:25 -05:00
|
|
|
use std::process::{self, Command};
|
|
|
|
|
2022-01-09 12:30:07 -06:00
|
|
|
use super::rustc_info::{get_file_name, get_rustc_version};
|
|
|
|
use super::utils::{spawn_and_wait, try_hard_link};
|
|
|
|
use super::SysrootKind;
|
2021-06-18 06:38:50 -05:00
|
|
|
|
2021-06-08 07:33:25 -05:00
|
|
|
pub(crate) fn build_sysroot(
|
|
|
|
channel: &str,
|
|
|
|
sysroot_kind: SysrootKind,
|
|
|
|
target_dir: &Path,
|
2021-07-04 11:15:13 -05:00
|
|
|
cg_clif_build_dir: PathBuf,
|
2021-06-08 07:33:25 -05:00
|
|
|
host_triple: &str,
|
|
|
|
target_triple: &str,
|
|
|
|
) {
|
|
|
|
if target_dir.exists() {
|
|
|
|
fs::remove_dir_all(target_dir).unwrap();
|
|
|
|
}
|
|
|
|
fs::create_dir_all(target_dir.join("bin")).unwrap();
|
|
|
|
fs::create_dir_all(target_dir.join("lib")).unwrap();
|
|
|
|
|
|
|
|
// Copy the backend
|
2021-07-04 11:15:13 -05:00
|
|
|
let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
|
2022-02-13 11:33:30 -06:00
|
|
|
let cg_clif_dylib_path = target_dir
|
|
|
|
.join(if cfg!(windows) {
|
|
|
|
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
|
|
|
|
// binaries.
|
|
|
|
"bin"
|
|
|
|
} else {
|
|
|
|
"lib"
|
|
|
|
})
|
|
|
|
.join(&cg_clif_dylib);
|
|
|
|
try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path);
|
|
|
|
|
2022-02-13 12:31:49 -06:00
|
|
|
// Build and copy rustc and cargo wrappers
|
|
|
|
for wrapper in ["rustc-clif", "cargo-clif"] {
|
|
|
|
let mut build_cargo_wrapper_cmd = Command::new("rustc");
|
|
|
|
build_cargo_wrapper_cmd
|
|
|
|
.arg(PathBuf::from("scripts").join(format!("{wrapper}.rs")))
|
|
|
|
.arg("-o")
|
|
|
|
.arg(target_dir.join(wrapper))
|
|
|
|
.arg("-g");
|
|
|
|
spawn_and_wait(build_cargo_wrapper_cmd);
|
|
|
|
}
|
2021-06-08 07:33:25 -05:00
|
|
|
|
2022-01-09 12:30:07 -06:00
|
|
|
let default_sysroot = super::rustc_info::get_default_sysroot();
|
2021-06-08 07:33:25 -05:00
|
|
|
|
|
|
|
let rustlib = target_dir.join("lib").join("rustlib");
|
|
|
|
let host_rustlib_lib = rustlib.join(host_triple).join("lib");
|
|
|
|
let target_rustlib_lib = rustlib.join(target_triple).join("lib");
|
|
|
|
fs::create_dir_all(&host_rustlib_lib).unwrap();
|
|
|
|
fs::create_dir_all(&target_rustlib_lib).unwrap();
|
|
|
|
|
|
|
|
if target_triple == "x86_64-pc-windows-gnu" {
|
|
|
|
if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
|
|
|
|
eprintln!(
|
|
|
|
"The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
|
|
|
|
to compile a sysroot for it.",
|
|
|
|
);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
for file in fs::read_dir(
|
|
|
|
default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
let file = file.unwrap().path();
|
|
|
|
if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
|
|
|
|
continue; // only copy object files
|
|
|
|
}
|
|
|
|
try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match sysroot_kind {
|
|
|
|
SysrootKind::None => {} // Nothing to do
|
|
|
|
SysrootKind::Llvm => {
|
|
|
|
for file in fs::read_dir(
|
|
|
|
default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
let file = file.unwrap().path();
|
|
|
|
let file_name_str = file.file_name().unwrap().to_str().unwrap();
|
2021-07-26 09:11:24 -05:00
|
|
|
if (file_name_str.contains("rustc_")
|
|
|
|
&& !file_name_str.contains("rustc_std_workspace_")
|
|
|
|
&& !file_name_str.contains("rustc_demangle"))
|
2021-06-08 07:33:25 -05:00
|
|
|
|| 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;
|
|
|
|
}
|
|
|
|
try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if target_triple != host_triple {
|
|
|
|
for file in fs::read_dir(
|
|
|
|
default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
let file = file.unwrap().path();
|
|
|
|
try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SysrootKind::Clif => {
|
2022-02-13 11:33:30 -06:00
|
|
|
build_clif_sysroot_for_triple(
|
|
|
|
channel,
|
|
|
|
target_dir,
|
|
|
|
host_triple,
|
|
|
|
&cg_clif_dylib_path,
|
|
|
|
None,
|
|
|
|
);
|
2021-06-08 07:33:25 -05:00
|
|
|
|
|
|
|
if host_triple != target_triple {
|
2021-06-29 13:37:06 -05:00
|
|
|
// When cross-compiling it is often necessary to manually pick the right linker
|
|
|
|
let linker = if target_triple == "aarch64-unknown-linux-gnu" {
|
|
|
|
Some("aarch64-linux-gnu-gcc")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-02-13 11:33:30 -06:00
|
|
|
build_clif_sysroot_for_triple(
|
|
|
|
channel,
|
|
|
|
target_dir,
|
|
|
|
target_triple,
|
|
|
|
&cg_clif_dylib_path,
|
|
|
|
linker,
|
|
|
|
);
|
2021-06-08 07:33:25 -05:00
|
|
|
}
|
|
|
|
|
2021-06-18 06:27:56 -05:00
|
|
|
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
|
|
|
|
// libstd.
|
2021-06-08 07:33:25 -05:00
|
|
|
for file in fs::read_dir(host_rustlib_lib).unwrap() {
|
|
|
|
let file = file.unwrap().path();
|
2022-04-29 12:47:43 -05:00
|
|
|
let filename = file.file_name().unwrap().to_str().unwrap();
|
|
|
|
if filename.contains("std-") && !filename.contains(".rlib") {
|
2021-06-08 07:33:25 -05:00
|
|
|
try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 06:27:56 -05:00
|
|
|
|
2021-06-29 13:37:06 -05:00
|
|
|
fn build_clif_sysroot_for_triple(
|
|
|
|
channel: &str,
|
|
|
|
target_dir: &Path,
|
|
|
|
triple: &str,
|
2022-02-13 11:33:30 -06:00
|
|
|
cg_clif_dylib_path: &Path,
|
2021-06-29 13:37:06 -05:00
|
|
|
linker: Option<&str>,
|
|
|
|
) {
|
2021-07-02 12:14:02 -05:00
|
|
|
match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) {
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
|
|
|
|
eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
Ok(source_version) => {
|
|
|
|
let rustc_version = get_rustc_version();
|
|
|
|
if source_version != rustc_version {
|
|
|
|
eprintln!("The patched sysroot source is outdated");
|
|
|
|
eprintln!("Source version: {}", source_version.trim());
|
|
|
|
eprintln!("Rustc version: {}", rustc_version.trim());
|
|
|
|
eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:27:56 -05:00
|
|
|
let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
|
|
|
|
|
2022-01-09 12:30:07 -06:00
|
|
|
if !super::config::get_bool("keep_sysroot") {
|
2022-02-13 11:46:53 -06: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.
|
|
|
|
if build_dir.join("deps").exists() {
|
|
|
|
fs::remove_dir_all(build_dir.join("deps")).unwrap();
|
2021-06-18 06:27:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build sysroot
|
|
|
|
let mut build_cmd = Command::new("cargo");
|
|
|
|
build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
|
2022-02-13 11:33:30 -06:00
|
|
|
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
|
|
|
|
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
|
2021-06-18 06:27:56 -05:00
|
|
|
if channel == "release" {
|
|
|
|
build_cmd.arg("--release");
|
|
|
|
rustflags.push_str(" -Zmir-opt-level=3");
|
|
|
|
}
|
2021-06-29 13:37:06 -05:00
|
|
|
if let Some(linker) = linker {
|
|
|
|
use std::fmt::Write;
|
|
|
|
write!(rustflags, " -Clinker={}", linker).unwrap();
|
|
|
|
}
|
2021-06-18 06:27:56 -05:00
|
|
|
build_cmd.env("RUSTFLAGS", rustflags);
|
2021-11-23 07:25:28 -06:00
|
|
|
build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
|
2021-06-18 06:27:56 -05:00
|
|
|
spawn_and_wait(build_cmd);
|
|
|
|
|
|
|
|
// Copy all relevant files to the sysroot
|
|
|
|
for entry in
|
|
|
|
fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
if let Some(ext) = entry.path().extension() {
|
2022-06-28 04:55:37 -05:00
|
|
|
if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
|
2021-06-18 06:27:56 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
try_hard_link(
|
|
|
|
entry.path(),
|
|
|
|
target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|