Merge pull request #494 from darcagn/custom_rustlib

Modify build_system's prepare stage to allow for custom sysroot source path
This commit is contained in:
antoyo 2024-04-24 19:25:38 -04:00 committed by GitHub
commit 01b0fb7b56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 18 deletions

View File

@ -5,29 +5,41 @@
};
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 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 parent = match rustc_path.parent() {
Some(path) => path,
None => return Err(format!("No parent for `{}`", rustc_path.display())),
};
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 @@ fn new() -> Result<Option<Self>, String> {
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 @@ fn new() -> Result<Option<Self>, String> {
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 @@ fn usage() {
--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")?;

View File

@ -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`.