Rewrite build_sysroot.sh in rust

This commit is contained in:
bjorn3 2021-06-18 13:27:56 +02:00
parent d71b12535e
commit ad971bbed7
3 changed files with 53 additions and 54 deletions

View File

@ -1,39 +0,0 @@
#!/usr/bin/env bash
# Requires the CHANNEL env var to be set to `debug` or `release.`
set -e
source ./config.sh
dir=$(pwd)
# Use rustc with cg_clif as hotpluggable backend instead of the custom cg_clif driver so that
# build scripts are still compiled using cg_llvm.
export RUSTC=$dir"/bin/cg_clif_build_sysroot"
export RUSTFLAGS=$RUSTFLAGS" --clif"
cd "$(dirname "$0")"
# Cleanup for previous run
# v Clean target dir except for build scripts and incremental cache
rm -r target/*/{debug,release}/{build,deps,examples,libsysroot*,native} 2>/dev/null || true
# We expect the target dir in the default location. Guard against the user changing it.
export CARGO_TARGET_DIR=target
# Build libs
export RUSTFLAGS="$RUSTFLAGS -Zforce-unstable-if-unmarked -Cpanic=abort"
export __CARGO_DEFAULT_LIB_METADATA="cg_clif"
if [[ "$1" != "--debug" ]]; then
sysroot_channel='release'
# FIXME Enable incremental again once rust-lang/rust#74946 is fixed
CARGO_INCREMENTAL=0 RUSTFLAGS="$RUSTFLAGS -Zmir-opt-level=3" cargo build --target "$TARGET_TRIPLE" --release
else
sysroot_channel='debug'
cargo build --target "$TARGET_TRIPLE"
fi
# Copy files to sysroot
ln "target/$TARGET_TRIPLE/$sysroot_channel/deps/"* "$dir/lib/rustlib/$TARGET_TRIPLE/lib/"
rm "$dir/lib/rustlib/$TARGET_TRIPLE/lib/"*.{rmeta,d}

View File

@ -1,6 +1,6 @@
use crate::utils::spawn_and_wait;
use crate::utils::try_hard_link;
use crate::SysrootKind;
use std::env;
use std::fs;
use std::path::Path;
use std::process::{self, Command};
@ -100,24 +100,14 @@ pub(crate) fn build_sysroot(
}
}
SysrootKind::Clif => {
let cwd = env::current_dir().unwrap();
let mut cmd = Command::new(cwd.join("build_sysroot").join("build_sysroot.sh"));
cmd.current_dir(target_dir).env("TARGET_TRIPLE", target_triple);
eprintln!("[BUILD] sysroot");
if !cmd.spawn().unwrap().wait().unwrap().success() {
process::exit(1);
}
build_clif_sysroot_for_triple(channel, target_dir, target_triple);
if host_triple != target_triple {
let mut cmd = Command::new(cwd.join("build_sysroot").join("build_sysroot.sh"));
cmd.current_dir(target_dir).env("TARGET_TRIPLE", host_triple);
eprintln!("[BUILD] sysroot");
if !cmd.spawn().unwrap().wait().unwrap().success() {
process::exit(1);
}
build_clif_sysroot_for_triple(channel, target_dir, host_triple);
}
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
// libstd.
for file in fs::read_dir(host_rustlib_lib).unwrap() {
let file = file.unwrap().path();
if file.file_name().unwrap().to_str().unwrap().contains("std-") {
@ -127,3 +117,49 @@ pub(crate) fn build_sysroot(
}
}
}
fn build_clif_sysroot_for_triple(channel: &str, target_dir: &Path, triple: &str) {
let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
// FIXME add option to skip this
// Cleanup the target dir with the exception of build scripts and the incremental cache
for dir in ["build", "deps", "examples", "native"] {
if build_dir.join(dir).exists() {
fs::remove_dir_all(build_dir.join(dir)).unwrap();
}
}
// Build sysroot
let mut build_cmd = Command::new("cargo");
build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
if channel == "release" {
build_cmd.arg("--release");
rustflags.push_str(" -Zmir-opt-level=3");
}
build_cmd.env("RUSTFLAGS", rustflags);
build_cmd
.env("RUSTC", target_dir.join("bin").join("cg_clif_build_sysroot").canonicalize().unwrap());
// FIXME Enable incremental again once rust-lang/rust#74946 is fixed
build_cmd.env("CARGO_INCREMENTAL", "0").env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
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() {
if ext == "rmeta" || ext == "d" || ext == "dSYM" {
continue;
}
} else {
continue;
};
try_hard_link(
entry.path(),
target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
);
}
}

2
y.rs
View File

@ -66,6 +66,8 @@ enum SysrootKind {
fn main() {
env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
// The target dir is expected in the default location. Guard against the user changing it.
env::set_var("CARGO_TARGET_DIR", "target");
let mut args = env::args().skip(1);
let command = match args.next().as_deref() {