-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathload.rs
286 lines (261 loc) · 9.46 KB
/
load.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! Graph loading: runs .ninja parsing and constructs the build graph from it.
use crate::{
canon::{canon_path, canon_path_fast},
eval::{EvalPart, EvalString},
graph::{FileId, RspFile},
parse::Statement,
scanner,
smallmap::SmallMap,
{db, eval, graph, parse, trace},
};
use anyhow::{anyhow, bail};
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> {
graph: &'a graph::Graph,
build: &'a graph::Build,
}
impl<'a> BuildImplicitVars<'a> {
fn file_list(&self, ids: &[FileId], sep: char) -> String {
let mut out = String::new();
for &id in ids {
if !out.is_empty() {
out.push(sep);
}
out.push_str(&self.graph.file(id).name);
}
out
}
}
impl<'a> eval::Env for BuildImplicitVars<'a> {
fn get_var(&self, var: &str) -> Option<EvalString<Cow<str>>> {
let string_to_evalstring =
|s: String| Some(EvalString::new(vec![EvalPart::Literal(Cow::Owned(s))]));
match var {
"in" => string_to_evalstring(self.file_list(self.build.explicit_ins(), ' ')),
"in_newline" => string_to_evalstring(self.file_list(self.build.explicit_ins(), '\n')),
"out" => string_to_evalstring(self.file_list(self.build.explicit_outs(), ' ')),
"out_newline" => string_to_evalstring(self.file_list(self.build.explicit_outs(), '\n')),
_ => None,
}
}
}
/// Internal state used while loading.
#[derive(Default)]
pub struct Loader {
graph: graph::Graph,
default: Vec<FileId>,
/// rule name -> list of (key, val)
rules: HashMap<String, SmallMap<String, eval::EvalString<String>>>,
pools: SmallMap<String, usize>,
builddir: Option<String>,
}
impl Loader {
pub fn new() -> Self {
let mut loader = Loader::default();
loader.rules.insert("phony".to_owned(), SmallMap::default());
loader
}
/// Convert a path string to a FileId. For performance reasons
/// this requires an owned 'path' param.
fn path(&mut self, mut path: String) -> FileId {
// Perf: this is called while parsing build.ninja files. We go to
// some effort to avoid allocating in the common case of a path that
// refers to a file that is already known.
let len = canon_path_fast(&mut path);
path.truncate(len);
self.graph.files.id_from_canonical(path)
}
fn evaluate_path(&mut self, path: EvalString<&str>, envs: &[&dyn eval::Env]) -> FileId {
self.path(path.evaluate(envs))
}
fn evaluate_paths(
&mut self,
paths: Vec<EvalString<&str>>,
envs: &[&dyn eval::Env],
) -> Vec<FileId> {
paths
.into_iter()
.map(|path| self.evaluate_path(path, envs))
.collect()
}
fn add_build(
&mut self,
filename: std::rc::Rc<PathBuf>,
env: &eval::Vars,
b: parse::Build,
) -> anyhow::Result<()> {
let ins = graph::BuildIns {
ids: self.evaluate_paths(b.ins, &[&b.vars, env]),
explicit: b.explicit_ins,
implicit: b.implicit_ins,
order_only: b.order_only_ins,
// validation is implied by the other counts
};
let outs = graph::BuildOuts {
ids: self.evaluate_paths(b.outs, &[&b.vars, env]),
explicit: b.explicit_outs,
};
let mut build = graph::Build::new(
graph::FileLoc {
filename,
line: b.line,
},
ins,
outs,
);
let rule = match self.rules.get(b.rule) {
Some(r) => r,
None => bail!("unknown rule {:?}", b.rule),
};
let implicit_vars = BuildImplicitVars {
graph: &self.graph,
build: &build,
};
// temp variable in order to not move all of b into the closure
let build_vars = &b.vars;
let lookup = |key: &str| -> Option<String> {
// Look up `key = ...` binding in build and rule block.
// See "Variable scope" in the design notes.
Some(match build_vars.get(key) {
Some(val) => val.evaluate(&[env]),
None => rule.get(key)?.evaluate(&[&implicit_vars, build_vars, env]),
})
};
let cmdline = lookup("command");
let desc = lookup("description");
let depfile = lookup("depfile");
let parse_showincludes = match lookup("deps").as_deref() {
None => false,
Some("gcc") => false,
Some("msvc") => true,
Some(other) => bail!("invalid deps attribute {:?}", other),
};
let pool = lookup("pool");
let rspfile_path = lookup("rspfile");
let rspfile_content = lookup("rspfile_content");
let rspfile = match (rspfile_path, rspfile_content) {
(None, None) => None,
(Some(path), Some(content)) => Some(RspFile {
path: std::path::PathBuf::from(path),
content,
}),
_ => bail!("rspfile and rspfile_content need to be both specified"),
};
build.cmdline = cmdline;
build.desc = desc;
build.depfile = depfile;
build.parse_showincludes = parse_showincludes;
build.rspfile = rspfile;
build.pool = pool;
self.graph.add_build(build)
}
fn read_file(&mut self, id: FileId) -> anyhow::Result<()> {
let path = self.graph.file(id).path().to_path_buf();
let bytes = match trace::scope("read file", || scanner::read_file_with_nul(&path)) {
Ok(b) => b,
Err(e) => bail!("read {}: {}", path.display(), e),
};
self.parse(path, &bytes)
}
fn evaluate_and_read_file(
&mut self,
file: EvalString<&str>,
envs: &[&dyn eval::Env],
) -> anyhow::Result<()> {
let evaluated = self.evaluate_path(file, envs);
self.read_file(evaluated)
}
pub fn parse(&mut self, path: PathBuf, bytes: &[u8]) -> anyhow::Result<()> {
let filename = std::rc::Rc::new(path);
let mut parser = parse::Parser::new(&bytes);
loop {
let stmt = match parser
.read()
.map_err(|err| anyhow!(parser.format_parse_error(&filename, err)))?
{
None => break,
Some(s) => s,
};
match stmt {
Statement::Include(id) => trace::scope("include", || {
self.evaluate_and_read_file(id, &[&parser.vars])
})?,
// TODO: implement scoping for subninja
Statement::Subninja(id) => trace::scope("subninja", || {
self.evaluate_and_read_file(id, &[&parser.vars])
})?,
Statement::Default(defaults) => {
let evaluated = self.evaluate_paths(defaults, &[&parser.vars]);
self.default.extend(evaluated);
}
Statement::Rule(rule) => {
let mut vars: SmallMap<String, eval::EvalString<String>> = SmallMap::default();
for (name, val) in rule.vars.into_iter() {
// TODO: We should not need to call .into_owned() here
// if we keep the contents of all included files in
// memory.
vars.insert(name.to_owned(), val.into_owned());
}
self.rules.insert(rule.name.to_owned(), vars);
}
Statement::Build(build) => self.add_build(filename.clone(), &parser.vars, build)?,
Statement::Pool(pool) => {
self.pools.insert(pool.name.to_string(), pool.depth);
}
};
}
self.builddir = parser.vars.get("builddir").cloned();
Ok(())
}
}
/// State loaded by read().
pub struct State {
pub graph: graph::Graph,
pub db: db::Writer,
pub hashes: graph::Hashes,
pub default: Vec<FileId>,
pub pools: SmallMap<String, usize>,
}
/// Load build.ninja/.n2_db and return the loaded build graph and state.
pub fn read(build_filename: &str) -> anyhow::Result<State> {
let mut loader = Loader::new();
trace::scope("loader.read_file", || {
let id = loader
.graph
.files
.id_from_canonical(canon_path(build_filename));
loader.read_file(id)
})?;
let mut hashes = graph::Hashes::default();
let db = trace::scope("db::open", || {
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 {
graph: loader.graph,
db,
hashes,
default: loader.default,
pools: loader.pools,
})
}
/// Parse a single file's content.
#[cfg(test)]
pub fn parse(name: &str, mut content: Vec<u8>) -> anyhow::Result<graph::Graph> {
content.push(0);
let mut loader = Loader::new();
trace::scope("loader.read_file", || {
loader.parse(PathBuf::from(name), &content)
})?;
Ok(loader.graph)
}