about summary refs log tree commit diff
diff options
context:
space:
mode:
authorantoyo <antoyo@users.noreply.github.com>2024-04-24 19:25:38 -0400
committerGitHub <noreply@github.com>2024-04-24 19:25:38 -0400
commit01b0fb7b56cfe1c78e728c09153846bab6d01a1b (patch)
treeac412652a06986afe852da2dac7e9acb34fe20d0
parent41839175b0ed5446c6b0564a04ecf0a6b83737f5 (diff)
parent04932ea22f51c6b4ed31463a5a6f4243f6b1b1e7 (diff)
downloadrust-01b0fb7b56cfe1c78e728c09153846bab6d01a1b.tar.gz
rust-01b0fb7b56cfe1c78e728c09153846bab6d01a1b.zip
Merge pull request #494 from darcagn/custom_rustlib
Modify build_system's prepare stage to allow for custom sysroot source path
-rw-r--r--build_system/src/prepare.rs66
-rw-r--r--doc/tips.md8
2 files changed, 56 insertions, 18 deletions
diff --git a/build_system/src/prepare.rs b/build_system/src/prepare.rs
index 9f405daa687..a085d863616 100644
--- a/build_system/src/prepare.rs
+++ b/build_system/src/prepare.rs
@@ -5,29 +5,41 @@ use crate::utils::{
 };
 
 use std::fs;
-use std::path::Path;
+use std::path::{Path, PathBuf};
 
 fn prepare_libcore(
     sysroot_path: &Path,
     libgccjit12_patches: bool,
     cross_compile: bool,
+    sysroot_source: Option<String>,
 ) -> Result<(), String> {
-    let rustc_path = match get_rustc_path() {
-        Some(path) => path,
-        None => return Err("`rustc` path not found".to_string()),
-    };
+    let rustlib_dir: PathBuf;
 
-    let parent = match rustc_path.parent() {
-        Some(path) => path,
-        None => return Err(format!("No parent for `{}`", rustc_path.display())),
-    };
+    if let Some(path) = sysroot_source {
+        rustlib_dir = Path::new(&path)
+            .canonicalize()
+            .map_err(|error| format!("Failed to canonicalize path: {:?}", error))?;
+        if !rustlib_dir.is_dir() {
+            return Err(format!("Custom sysroot path {:?} not found", rustlib_dir));
+        }
+    } else {
+        let rustc_path = match get_rustc_path() {
+            Some(path) => path,
+            None => return Err("`rustc` path not found".to_string()),
+        };
+
+        let parent = match rustc_path.parent() {
+            Some(path) => path,
+            None => return Err(format!("No parent for `{}`", rustc_path.display())),
+        };
 
-    let rustlib_dir = parent
-        .join("../lib/rustlib/src/rust")
-        .canonicalize()
-        .map_err(|error| format!("Failed to canonicalize path: {:?}", error))?;
-    if !rustlib_dir.is_dir() {
-        return Err("Please install `rust-src` component".to_string());
+        rustlib_dir = parent
+            .join("../lib/rustlib/src/rust")
+            .canonicalize()
+            .map_err(|error| format!("Failed to canonicalize path: {:?}", error))?;
+        if !rustlib_dir.is_dir() {
+            return Err("Please install `rust-src` component".to_string());
+        }
     }
 
     let sysroot_dir = sysroot_path.join("sysroot_src");
@@ -151,6 +163,7 @@ struct PrepareArg {
     cross_compile: bool,
     only_libcore: bool,
     libgccjit12_patches: bool,
+    sysroot_source: Option<String>,
 }
 
 impl PrepareArg {
@@ -158,12 +171,23 @@ impl PrepareArg {
         let mut only_libcore = false;
         let mut cross_compile = false;
         let mut libgccjit12_patches = false;
+        let mut sysroot_source = None;
 
-        for arg in std::env::args().skip(2) {
+        let mut args = std::env::args().skip(2);
+        while let Some(arg) = args.next() {
             match arg.as_str() {
                 "--only-libcore" => only_libcore = true,
                 "--cross" => cross_compile = true,
                 "--libgccjit12-patches" => libgccjit12_patches = true,
+                "--sysroot-source" => {
+                    if let Some(path) = args.next() {
+                        sysroot_source = Some(path);
+                    } else {
+                        return Err(
+                            "Expected a value after `--sysroot-source`, found nothing".to_string()
+                        );
+                    }
+                }
                 "--help" => {
                     Self::usage();
                     return Ok(None);
@@ -171,7 +195,7 @@ impl PrepareArg {
                 a => return Err(format!("Unknown argument `{a}`")),
             }
         }
-        Ok(Some(Self { cross_compile, only_libcore, libgccjit12_patches }))
+        Ok(Some(Self { cross_compile, only_libcore, libgccjit12_patches, sysroot_source }))
     }
 
     fn usage() {
@@ -182,6 +206,7 @@ impl PrepareArg {
     --only-libcore           : Only setup libcore and don't clone other repositories
     --cross                  : Apply the patches needed to do cross-compilation
     --libgccjit12-patches    : Apply patches needed for libgccjit12
+    --sysroot-source         : Specify custom path for sysroot source
     --help                   : Show this help"#
         )
     }
@@ -193,7 +218,12 @@ pub fn run() -> Result<(), String> {
         None => return Ok(()),
     };
     let sysroot_path = get_sysroot_dir();
-    prepare_libcore(&sysroot_path, args.libgccjit12_patches, args.cross_compile)?;
+    prepare_libcore(
+        &sysroot_path,
+        args.libgccjit12_patches,
+        args.cross_compile,
+        args.sysroot_source,
+    )?;
 
     if !args.only_libcore {
         cargo_install("hyperfine")?;
diff --git a/doc/tips.md b/doc/tips.md
index 6cc81871d02..86c22db186e 100644
--- a/doc/tips.md
+++ b/doc/tips.md
@@ -35,6 +35,14 @@ COLLECT_NO_DEMANGLE=1
  * Build the stage2 compiler (`rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2`).
  * Clean and rebuild the codegen with `debug-current` in the file `rust-toolchain`.
 
+### How to use a custom sysroot source path
+
+If you wish to build a custom sysroot, pass the path of your sysroot source to `--sysroot-source` during the `prepare` step, like so:
+
+```
+./y.sh prepare --sysroot-source /path/to/custom/source
+```
+
 ### How to use [mem-trace](https://github.com/antoyo/mem-trace)
 
 `rustc` needs to be built without `jemalloc` so that `mem-trace` can overload `malloc` since `jemalloc` is linked statically, so a `LD_PRELOAD`-ed library won't a chance to intercept the calls to `malloc`.