1 // Copyright 2015 Google Inc. All Rights Reserved.
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include "dyndep.h"
16
17 #include <assert.h>
18 #include <stdio.h>
19
20 #include "debug_flags.h"
21 #include "disk_interface.h"
22 #include "dyndep_parser.h"
23 #include "graph.h"
24 #include "state.h"
25 #include "util.h"
26
LoadDyndeps(Node * node,std::string * err) const27 bool DyndepLoader::LoadDyndeps(Node* node, std::string* err) const {
28 DyndepFile ddf;
29 return LoadDyndeps(node, &ddf, err);
30 }
31
LoadDyndeps(Node * node,DyndepFile * ddf,std::string * err) const32 bool DyndepLoader::LoadDyndeps(Node* node, DyndepFile* ddf,
33 std::string* err) const {
34 // We are loading the dyndep file now so it is no longer pending.
35 node->set_dyndep_pending(false);
36
37 // Load the dyndep information from the file.
38 EXPLAIN("loading dyndep file '%s'", node->path().c_str());
39 if (!LoadDyndepFile(node, ddf, err))
40 return false;
41
42 // Update each edge that specified this node as its dyndep binding.
43 std::vector<Edge*> const& out_edges = node->out_edges();
44 for (std::vector<Edge*>::const_iterator oe = out_edges.begin();
45 oe != out_edges.end(); ++oe) {
46 Edge* const edge = *oe;
47 if (edge->dyndep_ != node)
48 continue;
49
50 DyndepFile::iterator ddi = ddf->find(edge);
51 if (ddi == ddf->end()) {
52 *err = ("'" + edge->outputs_[0]->path() + "' "
53 "not mentioned in its dyndep file "
54 "'" + node->path() + "'");
55 return false;
56 }
57
58 ddi->second.used_ = true;
59 Dyndeps const& dyndeps = ddi->second;
60 if (!UpdateEdge(edge, &dyndeps, err)) {
61 return false;
62 }
63 }
64
65 // Reject extra outputs in dyndep file.
66 for (DyndepFile::const_iterator oe = ddf->begin(); oe != ddf->end();
67 ++oe) {
68 if (!oe->second.used_) {
69 Edge* const edge = oe->first;
70 *err = ("dyndep file '" + node->path() + "' mentions output "
71 "'" + edge->outputs_[0]->path() + "' whose build statement "
72 "does not have a dyndep binding for the file");
73 return false;
74 }
75 }
76
77 return true;
78 }
79
UpdateEdge(Edge * edge,Dyndeps const * dyndeps,std::string * err) const80 bool DyndepLoader::UpdateEdge(Edge* edge, Dyndeps const* dyndeps,
81 std::string* err) const {
82 // Add dyndep-discovered bindings to the edge.
83 // We know the edge already has its own binding
84 // scope because it has a "dyndep" binding.
85 if (dyndeps->restat_)
86 edge->env_->AddBinding("restat", "1");
87
88 // Add the dyndep-discovered outputs to the edge.
89 edge->outputs_.insert(edge->outputs_.end(),
90 dyndeps->implicit_outputs_.begin(),
91 dyndeps->implicit_outputs_.end());
92 edge->implicit_outs_ += dyndeps->implicit_outputs_.size();
93
94 // Add this edge as incoming to each new output.
95 for (std::vector<Node*>::const_iterator i =
96 dyndeps->implicit_outputs_.begin();
97 i != dyndeps->implicit_outputs_.end(); ++i) {
98 if ((*i)->in_edge() != NULL) {
99 *err = "multiple rules generate " + (*i)->path();
100 return false;
101 }
102 (*i)->set_in_edge(edge);
103 }
104
105 // Add the dyndep-discovered inputs to the edge.
106 edge->inputs_.insert(edge->inputs_.end() - edge->order_only_deps_,
107 dyndeps->implicit_inputs_.begin(),
108 dyndeps->implicit_inputs_.end());
109 edge->implicit_deps_ += dyndeps->implicit_inputs_.size();
110
111 // Add this edge as outgoing from each new input.
112 for (std::vector<Node*>::const_iterator i =
113 dyndeps->implicit_inputs_.begin();
114 i != dyndeps->implicit_inputs_.end(); ++i)
115 (*i)->AddOutEdge(edge);
116
117 return true;
118 }
119
LoadDyndepFile(Node * file,DyndepFile * ddf,std::string * err) const120 bool DyndepLoader::LoadDyndepFile(Node* file, DyndepFile* ddf,
121 std::string* err) const {
122 DyndepParser parser(state_, disk_interface_, ddf);
123 return parser.Load(file->path(), err);
124 }
125