2021-06-08 12:33:25 +00:00
|
|
|
use std::fs;
|
2022-11-28 17:37:30 +00:00
|
|
|
use std::path::Path;
|
2021-06-08 12:33:25 +00:00
|
|
|
use std::process::{self, Command};
|
|
|
|
|
2022-12-01 14:54:37 +00:00
|
|
|
use super::path::{Dirs, RelPath};
|
2022-07-31 10:15:56 +00:00
|
|
|
use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
|
2022-10-26 14:51:03 +00:00
|
|
|
use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
|
2022-01-09 18:30:07 +00:00
|
|
|
use super::SysrootKind;
|
2021-06-18 11:38:50 +00:00
|
|
|
|
2022-12-01 13:30:03 +00:00
|
|
|
static DIST_DIR: RelPath = RelPath::DIST;
|
|
|
|
static BIN_DIR: RelPath = RelPath::DIST.join("bin");
|
|
|
|
static LIB_DIR: RelPath = RelPath::DIST.join("lib");
|
|
|
|
static RUSTLIB_DIR: RelPath = LIB_DIR.join("rustlib");
|
|
|
|
|
2021-06-08 12:33:25 +00:00
|
|
|
pub(crate) fn build_sysroot(
|
2022-12-01 14:54:37 +00:00
|
|
|
dirs: &Dirs,
|
2021-06-08 12:33:25 +00:00
|
|
|
channel: &str,
|
|
|
|
sysroot_kind: SysrootKind,
|
2022-08-28 16:46:09 +00:00
|
|
|
cg_clif_dylib_src: &Path,
|
2023-01-14 12:53:33 +00:00
|
|
|
bootstrap_host_compiler: &Compiler,
|
2023-01-14 13:04:40 +00:00
|
|
|
target_triple: String,
|
|
|
|
) -> Compiler {
|
2022-07-30 09:32:54 +00:00
|
|
|
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
|
|
|
|
|
2022-12-01 14:54:37 +00:00
|
|
|
DIST_DIR.ensure_fresh(dirs);
|
|
|
|
BIN_DIR.ensure_exists(dirs);
|
|
|
|
LIB_DIR.ensure_exists(dirs);
|
2021-06-08 12:33:25 +00:00
|
|
|
|
2023-01-14 13:04:40 +00:00
|
|
|
let is_native = bootstrap_host_compiler.triple == target_triple;
|
|
|
|
|
2021-06-08 12:33:25 +00:00
|
|
|
// Copy the backend
|
2022-12-01 13:30:03 +00:00
|
|
|
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
|
|
|
|
}
|
2022-12-01 14:54:37 +00:00
|
|
|
.to_path(dirs)
|
2022-12-01 13:30:03 +00:00
|
|
|
.join(get_file_name("rustc_codegen_cranelift", "dylib"));
|
2022-08-28 16:46:09 +00:00
|
|
|
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
|
2022-02-13 17:33:30 +00:00
|
|
|
|
2022-02-13 18:31:49 +00:00
|
|
|
// Build and copy rustc and cargo wrappers
|
2022-10-28 12:51:15 +00:00
|
|
|
for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] {
|
2022-07-30 22:04:59 +00:00
|
|
|
let wrapper_name = get_wrapper_file_name(wrapper, "bin");
|
2022-07-30 09:32:54 +00:00
|
|
|
|
2022-02-13 18:31:49 +00:00
|
|
|
let mut build_cargo_wrapper_cmd = Command::new("rustc");
|
|
|
|
build_cargo_wrapper_cmd
|
2022-12-01 14:54:37 +00:00
|
|
|
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
|
2022-02-13 18:31:49 +00:00
|
|
|
.arg("-o")
|
2022-12-01 14:54:37 +00:00
|
|
|
.arg(DIST_DIR.to_path(dirs).join(wrapper_name))
|
2022-02-13 18:31:49 +00:00
|
|
|
.arg("-g");
|
|
|
|
spawn_and_wait(build_cargo_wrapper_cmd);
|
|
|
|
}
|
2021-06-08 12:33:25 +00:00
|
|
|
|
2022-01-09 18:30:07 +00:00
|
|
|
let default_sysroot = super::rustc_info::get_default_sysroot();
|
2021-06-08 12:33:25 +00:00
|
|
|
|
2023-01-14 12:53:33 +00:00
|
|
|
let host_rustlib_lib =
|
|
|
|
RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.triple).join("lib");
|
2023-01-14 13:04:40 +00:00
|
|
|
let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&target_triple).join("lib");
|
2021-06-08 12:33:25 +00:00
|
|
|
fs::create_dir_all(&host_rustlib_lib).unwrap();
|
|
|
|
fs::create_dir_all(&target_rustlib_lib).unwrap();
|
|
|
|
|
|
|
|
if target_triple == "x86_64-pc-windows-gnu" {
|
2023-01-14 13:04:40 +00:00
|
|
|
if !default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib").exists() {
|
2021-06-08 12:33:25 +00:00
|
|
|
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(
|
2023-01-14 13:04:40 +00:00
|
|
|
default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib"),
|
2021-06-08 12:33:25 +00:00
|
|
|
)
|
|
|
|
.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(
|
2023-01-14 12:53:33 +00:00
|
|
|
default_sysroot
|
|
|
|
.join("lib")
|
|
|
|
.join("rustlib")
|
|
|
|
.join(&bootstrap_host_compiler.triple)
|
|
|
|
.join("lib"),
|
2021-06-08 12:33:25 +00:00
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
let file = file.unwrap().path();
|
|
|
|
let file_name_str = file.file_name().unwrap().to_str().unwrap();
|
2021-07-26 14:11:24 +00:00
|
|
|
if (file_name_str.contains("rustc_")
|
|
|
|
&& !file_name_str.contains("rustc_std_workspace_")
|
|
|
|
&& !file_name_str.contains("rustc_demangle"))
|
2021-06-08 12:33:25 +00: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()));
|
|
|
|
}
|
|
|
|
|
2023-01-14 13:04:40 +00:00
|
|
|
if !is_native {
|
2021-06-08 12:33:25 +00:00
|
|
|
for file in fs::read_dir(
|
2023-01-14 13:04:40 +00:00
|
|
|
default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib"),
|
2021-06-08 12:33:25 +00:00
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
let file = file.unwrap().path();
|
|
|
|
try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SysrootKind::Clif => {
|
2023-01-13 12:16:11 +00:00
|
|
|
build_clif_sysroot_for_triple(
|
|
|
|
dirs,
|
|
|
|
channel,
|
2023-01-14 12:53:33 +00:00
|
|
|
bootstrap_host_compiler.clone(),
|
2023-01-13 12:16:11 +00:00
|
|
|
&cg_clif_dylib_path,
|
|
|
|
);
|
2021-06-08 12:33:25 +00:00
|
|
|
|
2023-01-14 13:04:40 +00:00
|
|
|
if !is_native {
|
2022-12-01 14:54:37 +00:00
|
|
|
build_clif_sysroot_for_triple(
|
|
|
|
dirs,
|
|
|
|
channel,
|
2023-01-13 12:16:11 +00:00
|
|
|
{
|
2023-01-14 12:53:33 +00:00
|
|
|
let mut bootstrap_target_compiler = bootstrap_host_compiler.clone();
|
2023-01-14 13:04:40 +00:00
|
|
|
bootstrap_target_compiler.triple = target_triple.clone();
|
2023-01-14 12:53:33 +00:00
|
|
|
bootstrap_target_compiler.set_cross_linker_and_runner();
|
|
|
|
bootstrap_target_compiler
|
2023-01-13 12:16:11 +00:00
|
|
|
},
|
2022-12-01 14:54:37 +00:00
|
|
|
&cg_clif_dylib_path,
|
|
|
|
);
|
2021-06-08 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 11:27:56 +00:00
|
|
|
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
|
|
|
|
// libstd.
|
2021-06-08 12:33:25 +00:00
|
|
|
for file in fs::read_dir(host_rustlib_lib).unwrap() {
|
|
|
|
let file = file.unwrap().path();
|
2022-04-29 17:47:43 +00:00
|
|
|
let filename = file.file_name().unwrap().to_str().unwrap();
|
|
|
|
if filename.contains("std-") && !filename.contains(".rlib") {
|
2022-12-01 14:54:37 +00:00
|
|
|
try_hard_link(&file, LIB_DIR.to_path(dirs).join(file.file_name().unwrap()));
|
2021-06-08 12:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-14 13:04:40 +00:00
|
|
|
|
|
|
|
let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple);
|
|
|
|
if !is_native {
|
|
|
|
target_compiler.set_cross_linker_and_runner();
|
|
|
|
}
|
|
|
|
target_compiler
|
2021-06-08 12:33:25 +00:00
|
|
|
}
|
2021-06-18 11:27:56 +00:00
|
|
|
|
2023-01-05 17:26:54 +00:00
|
|
|
pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot");
|
|
|
|
pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot");
|
|
|
|
pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version");
|
|
|
|
pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src");
|
2023-01-13 15:58:39 +00:00
|
|
|
pub(crate) static STANDARD_LIBRARY: CargoProject =
|
|
|
|
CargoProject::new(&BUILD_SYSROOT, "build_sysroot");
|
2022-10-26 14:51:03 +00:00
|
|
|
|
2021-06-29 18:37:06 +00:00
|
|
|
fn build_clif_sysroot_for_triple(
|
2022-12-01 14:54:37 +00:00
|
|
|
dirs: &Dirs,
|
2021-06-29 18:37:06 +00:00
|
|
|
channel: &str,
|
2023-01-13 12:16:11 +00:00
|
|
|
mut compiler: Compiler,
|
2022-02-13 17:33:30 +00:00
|
|
|
cg_clif_dylib_path: &Path,
|
2021-06-29 18:37:06 +00:00
|
|
|
) {
|
2022-12-01 14:54:37 +00:00
|
|
|
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
|
2021-07-02 17:14:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:16:11 +00:00
|
|
|
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel);
|
2021-06-18 11:27:56 +00:00
|
|
|
|
2022-01-09 18:30:07 +00:00
|
|
|
if !super::config::get_bool("keep_sysroot") {
|
2022-02-13 17:46:53 +00: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 11:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build sysroot
|
2023-01-13 12:16:11 +00:00
|
|
|
let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
|
2022-02-13 17:33:30 +00:00
|
|
|
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
|
2022-12-01 14:54:37 +00:00
|
|
|
rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap()));
|
2021-06-18 11:27:56 +00:00
|
|
|
if channel == "release" {
|
|
|
|
rustflags.push_str(" -Zmir-opt-level=3");
|
|
|
|
}
|
2023-01-13 12:16:11 +00:00
|
|
|
compiler.rustflags += &rustflags;
|
2022-12-01 14:54:37 +00:00
|
|
|
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
|
2022-10-26 14:51:03 +00:00
|
|
|
if channel == "release" {
|
|
|
|
build_cmd.arg("--release");
|
|
|
|
}
|
2021-11-23 13:25:28 +00:00
|
|
|
build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
|
2021-06-18 11:27:56 +00:00
|
|
|
spawn_and_wait(build_cmd);
|
|
|
|
|
|
|
|
// Copy all relevant files to the sysroot
|
2022-11-28 17:37:30 +00:00
|
|
|
for entry in fs::read_dir(build_dir.join("deps")).unwrap() {
|
2021-06-18 11:27:56 +00:00
|
|
|
let entry = entry.unwrap();
|
|
|
|
if let Some(ext) = entry.path().extension() {
|
2022-06-28 09:55:37 +00:00
|
|
|
if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
|
2021-06-18 11:27:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
try_hard_link(
|
|
|
|
entry.path(),
|
2023-01-13 12:16:11 +00:00
|
|
|
RUSTLIB_DIR.to_path(dirs).join(&compiler.triple).join("lib").join(entry.file_name()),
|
2021-06-18 11:27:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|