1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "manifest_parser.h"
21 #include "disk_interface.h"
28 ManifestParser::ManifestParser(State* state, FileReader* file_reader,
29 DupeEdgeAction dupe_edge_action)
30 : state_(state), file_reader_(file_reader),
31 dupe_edge_action_(dupe_edge_action), quiet_(false) {
32 env_ = &state->bindings_;
35 bool ManifestParser::Load(const string& filename, string* err, Lexer* parent) {
36 METRIC_RECORD(".ninja parse");
39 if (file_reader_->ReadFile(filename, &contents, &read_err) != FileReader::Okay) {
40 *err = "loading '" + filename + "': " + read_err;
42 parent->Error(string(*err), err);
46 // The lexer needs a nul byte at the end of its input, to know when it's done.
47 // It takes a StringPiece, and StringPiece's string constructor uses
48 // string::data(). data()'s return value isn't guaranteed to be
49 // null-terminated (although in practice - libc++, libstdc++, msvc's stl --
50 // it is, and C++11 demands that too), so add an explicit nul byte.
51 contents.resize(contents.size() + 1);
53 return Parse(filename, contents, err);
56 bool ManifestParser::Parse(const string& filename, const string& input,
58 lexer_.Start(filename, input);
61 Lexer::Token token = lexer_.ReadToken();
76 if (!ParseDefault(err))
83 if (!ParseLet(&name, &let_value, err))
85 string value = let_value.Evaluate(env_);
86 // Check ninja_required_version immediately so we can exit
87 // before encountering any syntactic surprises.
88 if (name == "ninja_required_version")
89 CheckNinjaVersion(value);
90 env_->AddBinding(name, value);
94 if (!ParseFileInclude(false, err))
98 if (!ParseFileInclude(true, err))
102 return lexer_.Error(lexer_.DescribeLastError(), err);
109 return lexer_.Error(string("unexpected ") + Lexer::TokenName(token),
113 return false; // not reached
117 bool ManifestParser::ParsePool(string* err) {
119 if (!lexer_.ReadIdent(&name))
120 return lexer_.Error("expected pool name", err);
122 if (!ExpectToken(Lexer::NEWLINE, err))
125 if (state_->LookupPool(name) != NULL)
126 return lexer_.Error("duplicate pool '" + name + "'", err);
130 while (lexer_.PeekToken(Lexer::INDENT)) {
133 if (!ParseLet(&key, &value, err))
136 if (key == "depth") {
137 string depth_string = value.Evaluate(env_);
138 depth = atol(depth_string.c_str());
140 return lexer_.Error("invalid pool depth", err);
142 return lexer_.Error("unexpected variable '" + key + "'", err);
147 return lexer_.Error("expected 'depth =' line", err);
149 state_->AddPool(new Pool(name, depth));
154 bool ManifestParser::ParseRule(string* err) {
156 if (!lexer_.ReadIdent(&name))
157 return lexer_.Error("expected rule name", err);
159 if (!ExpectToken(Lexer::NEWLINE, err))
162 if (env_->LookupRuleCurrentScope(name) != NULL)
163 return lexer_.Error("duplicate rule '" + name + "'", err);
165 Rule* rule = new Rule(name); // XXX scoped_ptr
167 while (lexer_.PeekToken(Lexer::INDENT)) {
170 if (!ParseLet(&key, &value, err))
173 if (Rule::IsReservedBinding(key)) {
174 rule->AddBinding(key, value);
176 // Die on other keyvals for now; revisit if we want to add a
178 return lexer_.Error("unexpected variable '" + key + "'", err);
182 if (rule->bindings_["rspfile"].empty() !=
183 rule->bindings_["rspfile_content"].empty()) {
184 return lexer_.Error("rspfile and rspfile_content need to be "
185 "both specified", err);
188 if (rule->bindings_["command"].empty())
189 return lexer_.Error("expected 'command =' line", err);
195 bool ManifestParser::ParseLet(string* key, EvalString* value, string* err) {
196 if (!lexer_.ReadIdent(key))
197 return lexer_.Error("expected variable name", err);
198 if (!ExpectToken(Lexer::EQUALS, err))
200 if (!lexer_.ReadVarValue(value, err))
205 bool ManifestParser::ParseDefault(string* err) {
207 if (!lexer_.ReadPath(&eval, err))
210 return lexer_.Error("expected target name", err);
213 string path = eval.Evaluate(env_);
215 unsigned int slash_bits; // Unused because this only does lookup.
216 if (!CanonicalizePath(&path, &slash_bits, &path_err))
217 return lexer_.Error(path_err, err);
218 if (!state_->AddDefault(path, &path_err))
219 return lexer_.Error(path_err, err);
222 if (!lexer_.ReadPath(&eval, err))
224 } while (!eval.empty());
226 if (!ExpectToken(Lexer::NEWLINE, err))
232 bool ManifestParser::ParseEdge(string* err) {
233 vector<EvalString> ins, outs;
237 if (!lexer_.ReadPath(&out, err))
240 return lexer_.Error("expected path", err);
246 if (!lexer_.ReadPath(&out, err))
248 } while (!out.empty());
251 // Add all implicit outs, counting how many as we go.
252 int implicit_outs = 0;
253 if (lexer_.PeekToken(Lexer::PIPE)) {
256 if (!lexer_.ReadPath(&out, err))
265 if (!ExpectToken(Lexer::COLON, err))
269 if (!lexer_.ReadIdent(&rule_name))
270 return lexer_.Error("expected build command name", err);
272 const Rule* rule = env_->LookupRule(rule_name);
274 return lexer_.Error("unknown build rule '" + rule_name + "'", err);
277 // XXX should we require one path here?
279 if (!lexer_.ReadPath(&in, err))
286 // Add all implicit deps, counting how many as we go.
288 if (lexer_.PeekToken(Lexer::PIPE)) {
291 if (!lexer_.ReadPath(&in, err))
300 // Add all order-only deps, counting how many as we go.
302 if (lexer_.PeekToken(Lexer::PIPE2)) {
305 if (!lexer_.ReadPath(&in, err))
314 if (!ExpectToken(Lexer::NEWLINE, err))
317 // Bindings on edges are rare, so allocate per-edge envs only when needed.
318 bool has_indent_token = lexer_.PeekToken(Lexer::INDENT);
319 BindingEnv* env = has_indent_token ? new BindingEnv(env_) : env_;
320 while (has_indent_token) {
323 if (!ParseLet(&key, &val, err))
326 env->AddBinding(key, val.Evaluate(env_));
327 has_indent_token = lexer_.PeekToken(Lexer::INDENT);
330 Edge* edge = state_->AddEdge(rule);
333 string pool_name = edge->GetBinding("pool");
334 if (!pool_name.empty()) {
335 Pool* pool = state_->LookupPool(pool_name);
337 return lexer_.Error("unknown pool name '" + pool_name + "'", err);
341 edge->outputs_.reserve(outs.size());
342 for (vector<EvalString>::iterator i = outs.begin(); i != outs.end(); ++i) {
343 string path = i->Evaluate(env);
345 unsigned int slash_bits;
346 if (!CanonicalizePath(&path, &slash_bits, &path_err))
347 return lexer_.Error(path_err, err);
348 if (!state_->AddOut(edge, path, slash_bits)) {
349 if (dupe_edge_action_ == kDupeEdgeActionError) {
350 lexer_.Error("multiple rules generate " + path + " [-w dupbuild=err]",
353 } else if (!quiet_) {
354 Warning("multiple rules generate %s. "
355 "builds involving this target will not be correct; "
356 "continuing anyway [-w dupbuild=warn]",
361 if (edge->outputs_.empty()) {
362 // All outputs of the edge are already created by other edges. Don't add
363 // this edge. Do this check before input nodes are connected to the edge.
364 state_->edges_.pop_back();
368 edge->implicit_outs_ = implicit_outs;
370 edge->inputs_.reserve(ins.size());
371 for (vector<EvalString>::iterator i = ins.begin(); i != ins.end(); ++i) {
372 string path = i->Evaluate(env);
374 unsigned int slash_bits;
375 if (!CanonicalizePath(&path, &slash_bits, &path_err))
376 return lexer_.Error(path_err, err);
377 state_->AddIn(edge, path, slash_bits);
379 edge->implicit_deps_ = implicit;
380 edge->order_only_deps_ = order_only;
382 // Multiple outputs aren't (yet?) supported with depslog.
383 string deps_type = edge->GetBinding("deps");
384 if (!deps_type.empty() && edge->outputs_.size() > 1) {
385 return lexer_.Error("multiple outputs aren't (yet?) supported by depslog; "
386 "bring this up on the mailing list if it affects you",
393 bool ManifestParser::ParseFileInclude(bool new_scope, string* err) {
395 if (!lexer_.ReadPath(&eval, err))
397 string path = eval.Evaluate(env_);
399 ManifestParser subparser(state_, file_reader_, dupe_edge_action_);
401 subparser.env_ = new BindingEnv(env_);
403 subparser.env_ = env_;
406 if (!subparser.Load(path, err, &lexer_))
409 if (!ExpectToken(Lexer::NEWLINE, err))
415 bool ManifestParser::ExpectToken(Lexer::Token expected, string* err) {
416 Lexer::Token token = lexer_.ReadToken();
417 if (token != expected) {
418 string message = string("expected ") + Lexer::TokenName(expected);
419 message += string(", got ") + Lexer::TokenName(token);
420 message += Lexer::TokenErrorHint(expected);
421 return lexer_.Error(message, err);