about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbjorn3 <bjorn3@users.noreply.github.com>2021-06-18 13:27:56 +0200
committerbjorn3 <bjorn3@users.noreply.github.com>2021-06-19 13:58:32 +0200
commitad971bbed773cad7cb446b49b07c64504e9a79fb (patch)
tree8b400d2a0f493ef358613a14457f16553a21d96c
parentd71b12535e84a7c242cf217af49a0366b06c8691 (diff)
downloadrust-ad971bbed773cad7cb446b49b07c64504e9a79fb.tar.gz
rust-ad971bbed773cad7cb446b49b07c64504e9a79fb.zip
Rewrite build_sysroot.sh in rust
-rwxr-xr-xbuild_sysroot/build_sysroot.sh39
-rw-r--r--build_system/build_sysroot.rs66
-rwxr-xr-xy.rs2
3 files changed, 53 insertions, 54 deletions
diff --git a/build_sysroot/build_sysroot.sh b/build_sysroot/build_sysroot.sh
deleted file mode 100755
index 0354304e55b..00000000000
--- a/build_sysroot/build_sysroot.sh
+++ /dev/null
@@ -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}
diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs
index 60906a8f19a..9988b35b363 100644
--- a/build_system/build_sysroot.rs
+++ b/build_system/build_sysroot.rs
@@ -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()),
+        );
+    }
+}
diff --git a/y.rs b/y.rs
index 62debc117c3..27ab44f5870 100755
--- a/y.rs
+++ b/y.rs
@@ -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() {