Skip to content

Commit

Permalink
Refactor and reduce usage of RelPath
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Nov 21, 2024
1 parent c8ad57b commit ca1b498
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 49 deletions.
14 changes: 7 additions & 7 deletions build_system/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::Write;
use std::path::Path;
use std::process::Command;

use crate::path::{Dirs, RelPath};
use crate::path::Dirs;
use crate::prepare::GitRepo;
use crate::rustc_info::get_file_name;
use crate::utils::{Compiler, spawn_and_wait};
Expand Down Expand Up @@ -39,11 +39,11 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
};

eprintln!("[BENCH COMPILE] ebobby/simple-raytracer");
let cargo_clif = RelPath::DIST
.to_path(dirs)
let cargo_clif = dirs
.dist_dir
.join(get_file_name(&bootstrap_host_compiler.rustc, "cargo_clif", "bin").replace('_', "-"));
let manifest_path = SIMPLE_RAYTRACER_REPO.source_dir().to_path(dirs).join("Cargo.toml");
let target_dir = RelPath::BUILD.join("simple_raytracer").to_path(dirs);
let target_dir = dirs.build_dir.join("simple_raytracer");

let clean_cmd = format!(
"RUSTC=rustc cargo clean --manifest-path {manifest_path} --target-dir {target_dir}",
Expand All @@ -68,7 +68,7 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
target_dir = target_dir.display(),
);

let bench_compile_markdown = RelPath::DIST.to_path(dirs).join("bench_compile.md");
let bench_compile_markdown = dirs.dist_dir.join("bench_compile.md");

let bench_compile = hyperfine_command(
1,
Expand All @@ -92,7 +92,7 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {

eprintln!("[BENCH RUN] ebobby/simple-raytracer");

let bench_run_markdown = RelPath::DIST.to_path(dirs).join("bench_run.md");
let bench_run_markdown = dirs.dist_dir.join("bench_run.md");

let raytracer_cg_llvm = Path::new(".").join(get_file_name(
&bootstrap_host_compiler.rustc,
Expand Down Expand Up @@ -120,7 +120,7 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
],
&bench_run_markdown,
);
bench_run.current_dir(RelPath::BUILD.to_path(dirs));
bench_run.current_dir(&dirs.build_dir);
spawn_and_wait(bench_run);

if let Some(gha_step_summary) = gha_step_summary.as_mut() {
Expand Down
2 changes: 1 addition & 1 deletion build_system/build_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::rustc_info::get_file_name;
use crate::shared_utils::{rustflags_from_env, rustflags_to_cmd_env};
use crate::utils::{CargoProject, Compiler, LogGroup};

static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
static CG_CLIF: CargoProject = CargoProject::new(&RelPath::source("."), "cg_clif");

pub(crate) fn build_backend(
dirs: &Dirs,
Expand Down
16 changes: 8 additions & 8 deletions build_system/build_sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub(crate) fn build_sysroot(

eprintln!("[BUILD] sysroot {:?}", sysroot_kind);

let dist_dir = RelPath::DIST.to_path(dirs);
let dist_dir = &dirs.dist_dir;

ensure_empty_dir(&dist_dir);
ensure_empty_dir(dist_dir);
fs::create_dir_all(dist_dir.join("bin")).unwrap();
fs::create_dir_all(dist_dir.join("lib")).unwrap();

Expand Down Expand Up @@ -55,7 +55,7 @@ pub(crate) fn build_sysroot(
let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
let wrapper_path = dist_dir.join(&wrapper_name);
build_cargo_wrapper_cmd
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
.arg(dirs.source_dir.join("scripts").join(&format!("{wrapper}.rs")))
.arg("-o")
.arg(&wrapper_path)
.arg("-Cstrip=debuginfo");
Expand Down Expand Up @@ -85,7 +85,7 @@ pub(crate) fn build_sysroot(
&cg_clif_dylib_path,
sysroot_kind,
);
host.install_into_sysroot(&dist_dir);
host.install_into_sysroot(dist_dir);

if !is_native {
build_sysroot_for_triple(
Expand All @@ -99,7 +99,7 @@ pub(crate) fn build_sysroot(
&cg_clif_dylib_path,
sysroot_kind,
)
.install_into_sysroot(&dist_dir);
.install_into_sysroot(dist_dir);
}

let mut target_compiler = {
Expand Down Expand Up @@ -143,10 +143,10 @@ impl SysrootTarget {
}
}

static STDLIB_SRC: RelPath = RelPath::BUILD.join("stdlib");
static STDLIB_SRC: RelPath = RelPath::build("stdlib");
static STANDARD_LIBRARY: CargoProject =
CargoProject::new(&STDLIB_SRC.join("library/sysroot"), "stdlib_target");
static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup");
CargoProject::new(&RelPath::build("stdlib/library/sysroot"), "stdlib_target");
static RTSTARTUP_SYSROOT: RelPath = RelPath::build("rtstartup");

fn build_sysroot_for_triple(
dirs: &Dirs,
Expand Down
5 changes: 2 additions & 3 deletions build_system/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,11 @@ fn main() {
frozen,
};

path::RelPath::BUILD.ensure_exists(&dirs);
std::fs::create_dir_all(&dirs.build_dir).unwrap();

{
// Make sure we always explicitly specify the target dir
let target =
path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs);
let target = dirs.build_dir.join("target_dir_should_be_set_explicitly");
env::set_var("CARGO_TARGET_DIR", &target);
let _ = std::fs::remove_file(&target);
std::fs::File::create(target).unwrap();
Expand Down
31 changes: 9 additions & 22 deletions build_system/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Clone)]
Expand All @@ -15,45 +14,33 @@ pub(crate) struct Dirs {
pub(crate) enum PathBase {
Source,
Build,
Dist,
}

impl PathBase {
fn to_path(self, dirs: &Dirs) -> PathBuf {
match self {
PathBase::Source => dirs.source_dir.clone(),
PathBase::Build => dirs.build_dir.clone(),
PathBase::Dist => dirs.dist_dir.clone(),
}
}
}

#[derive(Debug, Copy, Clone)]
pub(crate) enum RelPath {
Base(PathBase),
Join(&'static RelPath, &'static str),
pub(crate) struct RelPath {
base: PathBase,
suffix: &'static str,
}

impl RelPath {
pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source);
pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build);
pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist);

pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts");
pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches");

pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath {
RelPath::Join(self, suffix)
pub(crate) const fn source(suffix: &'static str) -> RelPath {
RelPath { base: PathBase::Source, suffix }
}

pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
match self {
RelPath::Base(base) => base.to_path(dirs),
RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix),
}
pub(crate) const fn build(suffix: &'static str) -> RelPath {
RelPath { base: PathBase::Build, suffix }
}

pub(crate) fn ensure_exists(&self, dirs: &Dirs) {
fs::create_dir_all(self.to_path(dirs)).unwrap();
pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
self.base.to_path(dirs).join(self.suffix)
}
}
6 changes: 3 additions & 3 deletions build_system/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl GitRepo {

pub(crate) const fn source_dir(&self) -> RelPath {
match self.url {
GitRepoUrl::Github { user: _, repo } => RelPath::BUILD.join(repo),
GitRepoUrl::Github { user: _, repo } => RelPath::build(repo),
}
}

Expand Down Expand Up @@ -130,7 +130,7 @@ impl GitRepo {
}

let source_lockfile =
RelPath::PATCHES.to_path(dirs).join(format!("{}-lock.toml", self.patch_name));
dirs.source_dir.join("patches").join(format!("{}-lock.toml", self.patch_name));
let target_lockfile = download_dir.join("Cargo.lock");
if source_lockfile.exists() {
assert!(!target_lockfile.exists());
Expand Down Expand Up @@ -191,7 +191,7 @@ fn init_git_repo(repo_dir: &Path) {
}

fn get_patches(dirs: &Dirs, crate_name: &str) -> Vec<PathBuf> {
let mut patches: Vec<_> = fs::read_dir(RelPath::PATCHES.to_path(dirs))
let mut patches: Vec<_> = fs::read_dir(dirs.source_dir.join("patches"))
.unwrap()
.map(|entry| entry.unwrap().path())
.filter(|path| path.extension() == Some(OsStr::new("patch")))
Expand Down
8 changes: 4 additions & 4 deletions build_system/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::shared_utils::rustflags_from_env;
use crate::utils::{CargoProject, Compiler, LogGroup, ensure_empty_dir, spawn_and_wait};
use crate::{CodegenBackend, SysrootKind, build_sysroot, config};

static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example");
static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::build("example");

struct TestCase {
config: &'static str,
Expand Down Expand Up @@ -129,11 +129,11 @@ pub(crate) static REGEX_REPO: GitRepo = GitRepo::github(

static REGEX: CargoProject = CargoProject::new(&REGEX_REPO.source_dir(), "regex_target");

static PORTABLE_SIMD_SRC: RelPath = RelPath::BUILD.join("portable-simd");
static PORTABLE_SIMD_SRC: RelPath = RelPath::build("portable-simd");

static PORTABLE_SIMD: CargoProject = CargoProject::new(&PORTABLE_SIMD_SRC, "portable-simd_target");

static LIBCORE_TESTS_SRC: RelPath = RelPath::BUILD.join("coretests");
static LIBCORE_TESTS_SRC: RelPath = RelPath::build("coretests");

static LIBCORE_TESTS: CargoProject = CargoProject::new(&LIBCORE_TESTS_SRC, "coretests_target");

Expand Down Expand Up @@ -162,7 +162,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
&LIBCORE_TESTS_SRC.to_path(&runner.dirs),
);

let source_lockfile = RelPath::PATCHES.to_path(&runner.dirs).join("coretests-lock.toml");
let source_lockfile = runner.dirs.source_dir.join("patches/coretests-lock.toml");
let target_lockfile = LIBCORE_TESTS_SRC.to_path(&runner.dirs).join("Cargo.lock");
fs::copy(source_lockfile, target_lockfile).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion build_system/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl CargoProject {
}

pub(crate) fn target_dir(&self, dirs: &Dirs) -> PathBuf {
RelPath::BUILD.join(self.target).to_path(dirs)
dirs.build_dir.join(self.target)
}

#[must_use]
Expand Down

0 comments on commit ca1b498

Please sign in to comment.