Skip to content

Commit

Permalink
Merge pull request #494 from darcagn/custom_rustlib
Browse files Browse the repository at this point in the history
Modify build_system's prepare stage to allow for custom sysroot source path
  • Loading branch information
antoyo authored Apr 24, 2024
2 parents 4183917 + 04932ea commit 01b0fb7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
66 changes: 48 additions & 18 deletions build_system/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -151,27 +163,39 @@ struct PrepareArg {
cross_compile: bool,
only_libcore: bool,
libgccjit12_patches: bool,
sysroot_source: Option<String>,
}

impl PrepareArg {
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);
}
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() {
Expand All @@ -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"#
)
}
Expand All @@ -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")?;
Expand Down
8 changes: 8 additions & 0 deletions doc/tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit 01b0fb7

Please sign in to comment.