Skip to content

Commit

Permalink
obey builddir for placing .n2_db
Browse files Browse the repository at this point in the history
  • Loading branch information
evmar committed Dec 20, 2023
1 parent 4bae87a commit 8aaa403
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::io::BufReader;
use std::io::Read;
use std::io::Write;
use std::mem::MaybeUninit;
use std::path::Path;

const VERSION: u32 = 1;

Expand Down Expand Up @@ -111,7 +112,7 @@ pub struct Writer {
}

impl Writer {
fn create(path: &str) -> std::io::Result<Self> {
fn create(path: &Path) -> std::io::Result<Self> {
let f = std::fs::File::create(path)?;
let mut w = Writer {
ids: IdMap::default(),
Expand Down Expand Up @@ -333,7 +334,7 @@ impl<'a> Reader<'a> {
}

/// Opens or creates an on-disk database, loading its state into the provided Graph.
pub fn open(path: &str, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Result<Writer> {
pub fn open(path: &Path, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Result<Writer> {
match std::fs::OpenOptions::new()
.read(true)
.append(true)
Expand Down
13 changes: 11 additions & 2 deletions src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::{
{db, eval, graph, parse, trace},
};
use anyhow::{anyhow, bail};
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::PathBuf;
use std::{borrow::Cow, path::Path};

/// A variable lookup environment for magic $in/$out variables.
struct BuildImplicitVars<'a> {
Expand Down Expand Up @@ -49,6 +49,7 @@ struct Loader {
/// rule name -> list of (key, val)
rules: HashMap<String, SmallMap<String, eval::EvalString<String>>>,
pools: SmallMap<String, usize>,
builddir: Option<String>,
}

impl parse::Loader for Loader {
Expand Down Expand Up @@ -196,6 +197,7 @@ impl Loader {
}
};
}
self.builddir = parser.vars.get("builddir").cloned();
Ok(())
}
}
Expand All @@ -221,7 +223,14 @@ pub fn read(build_filename: &str) -> anyhow::Result<State> {
})?;
let mut hashes = graph::Hashes::default();
let db = trace::scope("db::open", || {
db::open(".n2_db", &mut loader.graph, &mut hashes)
let mut db_path = PathBuf::from(".n2_db");
if let Some(builddir) = &loader.builddir {
db_path = Path::new(&builddir).join(db_path);
if let Some(parent) = db_path.parent() {
std::fs::create_dir_all(parent)?;
}
};
db::open(&db_path, &mut loader.graph, &mut hashes)
})
.map_err(|err| anyhow!("load .n2_db: {}", err))?;
Ok(State {
Expand Down
19 changes: 19 additions & 0 deletions tests/e2e/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,22 @@ build out3: phony out2
space.read("outfile")?;
Ok(())
}

// builddir controls where .n2_db is written.
#[test]
fn builddir() -> anyhow::Result<()> {
let space = TestSpace::new()?;
space.write(
"build.ninja",
&[
"builddir = foo",
TOUCH_RULE,
"build $builddir/bar: touch",
"",
]
.join("\n"),
)?;
space.run_expect(&mut n2_command(vec!["foo/bar"]))?;
space.read("foo/.n2_db")?;
Ok(())
}

0 comments on commit 8aaa403

Please sign in to comment.