Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 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 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | #include "dep.h" |
| 18 | |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 19 | #include <algorithm> |
Shinichiro Hamaji | 485f912 | 2015-06-26 07:06:14 +0900 | [diff] [blame] | 20 | #include <iterator> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 21 | #include <memory> |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 22 | #include <unordered_map> |
| 23 | #include <unordered_set> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 24 | |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 25 | #include "eval.h" |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 26 | #include "fileutil.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 27 | #include "log.h" |
| 28 | #include "rule.h" |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 29 | #include "strutil.h" |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 30 | #include "symtab.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 31 | #include "var.h" |
| 32 | |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 33 | namespace { |
| 34 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 35 | static vector<DepNode*>* g_dep_node_pool; |
| 36 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 37 | static Symbol ReplaceSuffix(Symbol s, Symbol newsuf) { |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 38 | string r; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 39 | AppendString(StripExt(s.str()), &r); |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 40 | r += '.'; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 41 | AppendString(newsuf.str(), &r); |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 42 | return Intern(r); |
| 43 | } |
| 44 | |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 45 | class RuleTrie { |
| 46 | struct Entry { |
| 47 | Entry(shared_ptr<Rule> r, StringPiece s) |
| 48 | : rule(r), suffix(s) { |
| 49 | } |
| 50 | shared_ptr<Rule> rule; |
| 51 | StringPiece suffix; |
| 52 | }; |
| 53 | |
| 54 | public: |
| 55 | RuleTrie() {} |
| 56 | ~RuleTrie() { |
| 57 | for (auto& p : children_) |
| 58 | delete p.second; |
| 59 | } |
| 60 | |
| 61 | void Add(StringPiece name, shared_ptr<Rule> rule) { |
| 62 | if (name.empty() || name[0] == '%') { |
| 63 | rules_.push_back(Entry(rule, name)); |
| 64 | return; |
| 65 | } |
| 66 | const char c = name[0]; |
| 67 | auto p = children_.emplace(c, nullptr); |
| 68 | if (p.second) { |
| 69 | p.first->second = new RuleTrie(); |
| 70 | } |
| 71 | p.first->second->Add(name.substr(1), rule); |
| 72 | } |
| 73 | |
Shinichiro Hamaji | f772b17 | 2015-06-29 16:05:44 +0900 | [diff] [blame] | 74 | void Get(StringPiece name, vector<shared_ptr<Rule>>* rules) const { |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 75 | for (const Entry& ent : rules_) { |
| 76 | if ((ent.suffix.empty() && name.empty()) || |
| 77 | HasSuffix(name, ent.suffix.substr(1))) { |
| 78 | rules->push_back(ent.rule); |
| 79 | } |
| 80 | } |
Shinichiro Hamaji | f772b17 | 2015-06-29 16:05:44 +0900 | [diff] [blame] | 81 | if (name.empty()) |
| 82 | return; |
| 83 | auto found = children_.find(name[0]); |
| 84 | if (found != children_.end()) { |
| 85 | found->second->Get(name.substr(1), rules); |
| 86 | } |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | size_t size() const { |
| 90 | size_t r = rules_.size(); |
| 91 | for (const auto& c : children_) |
| 92 | r += c.second->size(); |
| 93 | return r; |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | vector<Entry> rules_; |
| 98 | unordered_map<char, RuleTrie*> children_; |
| 99 | }; |
| 100 | |
| 101 | } // namespace |
| 102 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 103 | DepNode::DepNode(Symbol o, bool p) |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 104 | : output(o), |
| 105 | has_rule(false), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 106 | is_phony(p), |
Colin Cross | 5b26db3 | 2015-09-29 16:51:02 -0700 | [diff] [blame^] | 107 | is_default_target(false), |
Shinichiro Hamaji | a7984ad | 2015-09-11 16:33:16 +0900 | [diff] [blame] | 108 | rule_vars(NULL), |
| 109 | output_pattern(Symbol::IsUninitialized()) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 110 | g_dep_node_pool->push_back(this); |
| 111 | } |
| 112 | |
| 113 | class DepBuilder { |
| 114 | public: |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 115 | DepBuilder(Evaluator* ev, |
| 116 | const vector<shared_ptr<Rule>>& rules, |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 117 | const unordered_map<Symbol, Vars*>& rule_vars) |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 118 | : ev_(ev), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 119 | rule_vars_(rule_vars), |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 120 | implicit_rules_(new RuleTrie()), |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 121 | first_rule_(NULL) { |
| 122 | PopulateRules(rules); |
Shinichiro Hamaji | b4969af | 2015-06-28 02:06:54 +0900 | [diff] [blame] | 123 | LOG_STAT("%zu variables", ev->mutable_vars()->size()); |
| 124 | LOG_STAT("%zu explicit rules", rules_.size()); |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 125 | LOG_STAT("%zu implicit rules", implicit_rules_->size()); |
Shinichiro Hamaji | b4969af | 2015-06-28 02:06:54 +0900 | [diff] [blame] | 126 | LOG_STAT("%zu suffix rules", suffix_rules_.size()); |
Shinichiro Hamaji | d40b6fe | 2015-06-29 17:08:42 +0900 | [diff] [blame] | 127 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 128 | auto found = rules_.find(Intern(".PHONY")); |
Shinichiro Hamaji | d40b6fe | 2015-06-29 17:08:42 +0900 | [diff] [blame] | 129 | if (found != rules_.end()) { |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 130 | for (Symbol input : found->second->inputs) { |
Shinichiro Hamaji | d40b6fe | 2015-06-29 17:08:42 +0900 | [diff] [blame] | 131 | phony_.insert(input); |
| 132 | } |
| 133 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 134 | } |
| 135 | |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 136 | ~DepBuilder() { |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 137 | } |
| 138 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 139 | void Build(vector<Symbol> targets, |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 140 | vector<DepNode*>* nodes) { |
Colin Cross | 5b26db3 | 2015-09-29 16:51:02 -0700 | [diff] [blame^] | 141 | if (!first_rule_) { |
| 142 | ERROR("*** No targets."); |
| 143 | } |
| 144 | CHECK(!first_rule_->outputs.empty()); |
Shinichiro Hamaji | 43defe0 | 2015-07-11 07:06:43 +0900 | [diff] [blame] | 145 | |
Colin Cross | 5b26db3 | 2015-09-29 16:51:02 -0700 | [diff] [blame^] | 146 | first_rule_->is_default_target = true; |
| 147 | |
| 148 | if (targets.empty()) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 149 | targets.push_back(first_rule_->outputs[0]); |
Shinichiro Hamaji | 319b649 | 2015-09-24 14:23:55 +0900 | [diff] [blame] | 150 | } |
| 151 | if (g_flags.gen_all_phony_targets) { |
| 152 | for (Symbol s : phony_) |
| 153 | targets.push_back(s); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 154 | } |
Shinichiro Hamaji | 7223e7b | 2015-09-28 15:17:27 +0900 | [diff] [blame] | 155 | if (g_flags.gen_all_targets) { |
| 156 | for (const auto& p : rules_) |
| 157 | targets.push_back(p.first); |
| 158 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 159 | |
| 160 | // TODO: LogStats? |
| 161 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 162 | for (Symbol target : targets) { |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 163 | cur_rule_vars_.reset(new Vars); |
| 164 | ev_->set_current_scope(cur_rule_vars_.get()); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 165 | DepNode* n = BuildPlan(target, Intern("")); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 166 | nodes->push_back(n); |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 167 | ev_->set_current_scope(NULL); |
| 168 | cur_rule_vars_.reset(NULL); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | private: |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 173 | bool Exists(Symbol target) { |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 174 | auto found = rules_.find(target); |
| 175 | if (found != rules_.end()) |
| 176 | return true; |
| 177 | if (phony_.count(target)) |
| 178 | return true; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 179 | return ::Exists(target.str()); |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 180 | } |
| 181 | |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 182 | void PopulateRules(const vector<shared_ptr<Rule>>& rules) { |
| 183 | for (shared_ptr<Rule> rule : rules) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 184 | if (rule->outputs.empty()) { |
| 185 | PopulateImplicitRule(rule); |
| 186 | } else { |
| 187 | PopulateExplicitRule(rule); |
| 188 | } |
| 189 | } |
Shinichiro Hamaji | 65657d9 | 2015-06-24 17:05:50 +0900 | [diff] [blame] | 190 | for (auto& p : suffix_rules_) { |
| 191 | reverse(p.second.begin(), p.second.end()); |
| 192 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 193 | } |
| 194 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 195 | bool PopulateSuffixRule(shared_ptr<Rule> rule, Symbol output) { |
| 196 | if (output.empty() || output.str()[0] != '.') |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 197 | return false; |
| 198 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 199 | const StringPiece rest = StringPiece(output.str()).substr(1); |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 200 | size_t dot_index = rest.find('.'); |
| 201 | // If there is only a single dot or the third dot, this is not a |
| 202 | // suffix rule. |
| 203 | if (dot_index == string::npos || |
| 204 | rest.substr(dot_index+1).find('.') != string::npos) { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | StringPiece input_suffix = rest.substr(0, dot_index); |
| 209 | StringPiece output_suffix = rest.substr(dot_index+1); |
| 210 | shared_ptr<Rule> r = make_shared<Rule>(*rule); |
| 211 | r->inputs.clear(); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 212 | r->inputs.push_back(Intern(input_suffix)); |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 213 | r->is_suffix_rule = true; |
| 214 | suffix_rules_[output_suffix].push_back(r); |
| 215 | return true; |
| 216 | } |
| 217 | |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 218 | void ApplyOutputPattern(const Rule& r, |
| 219 | Symbol output, |
| 220 | const vector<Symbol>& inputs, |
| 221 | vector<Symbol>* out_inputs) { |
| 222 | if (inputs.empty()) |
| 223 | return; |
| 224 | if (r.is_suffix_rule) { |
| 225 | for (Symbol input : inputs) { |
| 226 | out_inputs->push_back(ReplaceSuffix(output, input)); |
| 227 | } |
| 228 | return; |
| 229 | } |
| 230 | if (r.output_patterns.empty()) { |
| 231 | copy(inputs.begin(), inputs.end(), back_inserter(*out_inputs)); |
| 232 | return; |
| 233 | } |
| 234 | CHECK(r.output_patterns.size() == 1); |
| 235 | Pattern pat(r.output_patterns[0].str()); |
| 236 | for (Symbol input : inputs) { |
| 237 | string buf; |
| 238 | pat.AppendSubst(output.str(), input.str(), &buf); |
| 239 | out_inputs->push_back(Intern(buf)); |
| 240 | } |
| 241 | } |
| 242 | |
Shinichiro Hamaji | e30b3be | 2015-06-19 16:15:42 +0900 | [diff] [blame] | 243 | shared_ptr<Rule> MergeRules(const Rule& old_rule, |
| 244 | const Rule& rule, |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 245 | Symbol output, |
Shinichiro Hamaji | e30b3be | 2015-06-19 16:15:42 +0900 | [diff] [blame] | 246 | bool is_suffix_rule) { |
| 247 | if (old_rule.is_double_colon != rule.is_double_colon) { |
| 248 | ERROR("%s:%d: *** target file `%s' has both : and :: entries.", |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 249 | LOCF(rule.loc), output.str().c_str()); |
Shinichiro Hamaji | e30b3be | 2015-06-19 16:15:42 +0900 | [diff] [blame] | 250 | } |
| 251 | if (!old_rule.cmds.empty() && !rule.cmds.empty() && |
| 252 | !is_suffix_rule && !rule.is_double_colon) { |
Shinichiro Hamaji | ff4584d | 2015-06-24 17:45:14 +0900 | [diff] [blame] | 253 | WARN("%s:%d: warning: overriding commands for target `%s'", |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 254 | LOCF(rule.cmd_loc()), output.str().c_str()); |
Shinichiro Hamaji | ff4584d | 2015-06-24 17:45:14 +0900 | [diff] [blame] | 255 | WARN("%s:%d: warning: ignoring old commands for target `%s'", |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 256 | LOCF(old_rule.cmd_loc()), output.str().c_str()); |
Shinichiro Hamaji | e30b3be | 2015-06-19 16:15:42 +0900 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | shared_ptr<Rule> r = make_shared<Rule>(rule); |
| 260 | if (rule.is_double_colon) { |
| 261 | r->cmds.clear(); |
| 262 | for (Value* c : old_rule.cmds) |
| 263 | r->cmds.push_back(c); |
| 264 | for (Value* c : rule.cmds) |
| 265 | r->cmds.push_back(c); |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 266 | if (!rule.output_patterns.empty() && !old_rule.output_patterns.empty() && |
| 267 | rule.output_patterns != old_rule.output_patterns) { |
| 268 | ERROR("%s:%d: TODO: merging two double rules with output patterns " |
| 269 | "is not supported", LOCF(rule.loc)); |
| 270 | } |
Shinichiro Hamaji | e30b3be | 2015-06-19 16:15:42 +0900 | [diff] [blame] | 271 | } else if (!old_rule.cmds.empty() && rule.cmds.empty()) { |
| 272 | r->cmds = old_rule.cmds; |
| 273 | } |
Shinichiro Hamaji | 485f912 | 2015-06-26 07:06:14 +0900 | [diff] [blame] | 274 | |
Shinichiro Hamaji | 2e23e4a | 2015-06-26 07:33:16 +0900 | [diff] [blame] | 275 | // If the latter rule has a command (regardless of the commands in |
| 276 | // |old_rule|), inputs in the latter rule has a priority. |
Shinichiro Hamaji | 485f912 | 2015-06-26 07:06:14 +0900 | [diff] [blame] | 277 | if (rule.cmds.empty()) { |
| 278 | r->inputs = old_rule.inputs; |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 279 | ApplyOutputPattern(rule, output, rule.inputs, &r->inputs); |
Shinichiro Hamaji | 485f912 | 2015-06-26 07:06:14 +0900 | [diff] [blame] | 280 | r->order_only_inputs = old_rule.order_only_inputs; |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 281 | ApplyOutputPattern(rule, output, rule.order_only_inputs, |
| 282 | &r->order_only_inputs); |
| 283 | r->output_patterns = old_rule.output_patterns; |
Shinichiro Hamaji | 485f912 | 2015-06-26 07:06:14 +0900 | [diff] [blame] | 284 | } else { |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 285 | ApplyOutputPattern(old_rule, output, old_rule.inputs, &r->inputs); |
| 286 | ApplyOutputPattern(old_rule, output, old_rule.order_only_inputs, |
| 287 | &r->order_only_inputs); |
Shinichiro Hamaji | e30b3be | 2015-06-19 16:15:42 +0900 | [diff] [blame] | 288 | } |
| 289 | return r; |
| 290 | } |
| 291 | |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 292 | void PopulateExplicitRule(shared_ptr<Rule> orig_rule) { |
| 293 | for (Symbol output : orig_rule->outputs) { |
| 294 | const bool is_suffix_rule = PopulateSuffixRule(orig_rule, output); |
Shinichiro Hamaji | e36dd56 | 2015-06-27 16:34:48 +0900 | [diff] [blame] | 295 | |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 296 | shared_ptr<Rule> rule = make_shared<Rule>(*orig_rule); |
Shinichiro Hamaji | e36dd56 | 2015-06-27 16:34:48 +0900 | [diff] [blame] | 297 | rule->outputs.clear(); |
| 298 | rule->outputs.push_back(output); |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 299 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 300 | auto p = rules_.insert(make_pair(output, rule)); |
| 301 | if (p.second) { |
| 302 | if (!first_rule_ && output.get(0) != '.') { |
| 303 | first_rule_ = rule; |
| 304 | } |
| 305 | } else { |
Shinichiro Hamaji | e30b3be | 2015-06-19 16:15:42 +0900 | [diff] [blame] | 306 | p.first->second = |
| 307 | MergeRules(*p.first->second, *rule, output, is_suffix_rule); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 312 | void PopulateImplicitRule(shared_ptr<Rule> rule) { |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 313 | for (Symbol output_pattern : rule->output_patterns) { |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 314 | shared_ptr<Rule> r = make_shared<Rule>(*rule); |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 315 | r->output_patterns.clear(); |
| 316 | r->output_patterns.push_back(output_pattern); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 317 | implicit_rules_->Add(output_pattern.str(), r); |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 318 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 319 | } |
| 320 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 321 | shared_ptr<Rule> LookupRule(Symbol o) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 322 | auto found = rules_.find(o); |
| 323 | if (found != rules_.end()) |
| 324 | return found->second; |
| 325 | return NULL; |
| 326 | } |
| 327 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 328 | Vars* LookupRuleVars(Symbol o) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 329 | auto found = rule_vars_.find(o); |
| 330 | if (found != rule_vars_.end()) |
| 331 | return found->second; |
| 332 | return NULL; |
| 333 | } |
| 334 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 335 | bool CanPickImplicitRule(shared_ptr<Rule> rule, Symbol output) { |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 336 | CHECK(rule->output_patterns.size() == 1); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 337 | Pattern pat(rule->output_patterns[0].str()); |
| 338 | if (!pat.Match(output.str())) { |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 339 | return false; |
| 340 | } |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 341 | for (Symbol input : rule->inputs) { |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 342 | string buf; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 343 | pat.AppendSubst(output.str(), input.str(), &buf); |
| 344 | if (!Exists(Intern(buf))) |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 345 | return false; |
| 346 | } |
| 347 | return true; |
| 348 | } |
| 349 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 350 | Vars* MergeImplicitRuleVars(Symbol output, Vars* vars) { |
Shinichiro Hamaji | dca79a3 | 2015-06-24 15:27:33 +0900 | [diff] [blame] | 351 | auto found = rule_vars_.find(output); |
| 352 | if (found == rule_vars_.end()) |
| 353 | return vars; |
| 354 | if (vars == NULL) |
| 355 | return found->second; |
| 356 | // TODO: leak. |
| 357 | Vars* r = new Vars(*found->second); |
| 358 | for (auto p : *vars) { |
| 359 | (*r)[p.first] = p.second; |
| 360 | } |
| 361 | return r; |
| 362 | } |
| 363 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 364 | bool PickRule(Symbol output, |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 365 | shared_ptr<Rule>* out_rule, Vars** out_var) { |
| 366 | shared_ptr<Rule> rule = LookupRule(output); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 367 | Vars* vars = LookupRuleVars(output); |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 368 | *out_rule = rule; |
| 369 | *out_var = vars; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 370 | if (rule) { |
| 371 | if (!rule->cmds.empty()) { |
| 372 | return true; |
| 373 | } |
| 374 | } |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 375 | |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 376 | vector<shared_ptr<Rule>> irules; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 377 | implicit_rules_->Get(output.str(), &irules); |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 378 | for (auto iter = irules.rbegin(); iter != irules.rend(); ++iter) { |
| 379 | shared_ptr<Rule> irule = *iter; |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 380 | if (!CanPickImplicitRule(irule, output)) |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 381 | continue; |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 382 | if (rule) { |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 383 | shared_ptr<Rule> r = make_shared<Rule>(*rule); |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 384 | r->output_patterns = irule->output_patterns; |
Shinichiro Hamaji | d2c0fe1 | 2015-06-26 07:42:53 +0900 | [diff] [blame] | 385 | r->inputs.clear(); |
| 386 | r->inputs = irule->inputs; |
| 387 | copy(rule->inputs.begin(), rule->inputs.end(), |
| 388 | back_inserter(r->inputs)); |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 389 | r->cmds = irule->cmds; |
| 390 | r->loc = irule->loc; |
| 391 | r->cmd_lineno = irule->cmd_lineno; |
| 392 | *out_rule = r; |
| 393 | return true; |
| 394 | } |
| 395 | if (vars) { |
Shinichiro Hamaji | dca79a3 | 2015-06-24 15:27:33 +0900 | [diff] [blame] | 396 | CHECK(irule->output_patterns.size() == 1); |
| 397 | vars = MergeImplicitRuleVars(irule->output_patterns[0], vars); |
| 398 | *out_var = vars; |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 399 | } |
| 400 | *out_rule = irule; |
| 401 | return true; |
| 402 | } |
| 403 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 404 | StringPiece output_suffix = GetExt(output.str()); |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 405 | if (output_suffix.get(0) != '.') |
| 406 | return rule.get(); |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 407 | output_suffix = output_suffix.substr(1); |
Shinichiro Hamaji | 4a71131 | 2015-06-19 14:44:20 +0900 | [diff] [blame] | 408 | |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 409 | SuffixRuleMap::const_iterator found = suffix_rules_.find(output_suffix); |
| 410 | if (found == suffix_rules_.end()) |
| 411 | return rule.get(); |
| 412 | |
| 413 | for (shared_ptr<Rule> irule : found->second) { |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 414 | CHECK(irule->inputs.size() == 1); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 415 | Symbol input = ReplaceSuffix(output, irule->inputs[0]); |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 416 | if (!Exists(input)) |
| 417 | continue; |
| 418 | |
| 419 | if (rule) { |
| 420 | shared_ptr<Rule> r = make_shared<Rule>(*rule); |
| 421 | r->inputs.insert(r->inputs.begin(), input); |
| 422 | r->cmds = irule->cmds; |
| 423 | r->loc = irule->loc; |
| 424 | r->cmd_lineno = irule->cmd_lineno; |
| 425 | *out_rule = r; |
| 426 | return true; |
| 427 | } |
| 428 | if (vars) { |
Shinichiro Hamaji | dca79a3 | 2015-06-24 15:27:33 +0900 | [diff] [blame] | 429 | CHECK(irule->outputs.size() == 1); |
| 430 | vars = MergeImplicitRuleVars(irule->outputs[0], vars); |
| 431 | *out_var = vars; |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 432 | } |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 433 | *out_rule = irule; |
| 434 | return true; |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | return rule.get(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 438 | } |
| 439 | |
Shinichiro Hamaji | 8d50301 | 2015-07-03 16:26:15 +0900 | [diff] [blame] | 440 | DepNode* BuildPlan(Symbol output, Symbol needed_by UNUSED) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 441 | LOG("BuildPlan: %s for %s", |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 442 | output.c_str(), |
| 443 | needed_by.c_str()); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 444 | |
| 445 | auto found = done_.find(output); |
| 446 | if (found != done_.end()) { |
| 447 | return found->second; |
| 448 | } |
| 449 | |
Shinichiro Hamaji | 52dbb60 | 2015-06-19 15:57:07 +0900 | [diff] [blame] | 450 | DepNode* n = new DepNode(output, phony_.count(output)); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 451 | done_[output] = n; |
| 452 | |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 453 | shared_ptr<Rule> rule; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 454 | Vars* vars; |
| 455 | if (!PickRule(output, &rule, &vars)) { |
| 456 | return n; |
| 457 | } |
| 458 | |
Shinichiro Hamaji | a7984ad | 2015-09-11 16:33:16 +0900 | [diff] [blame] | 459 | if (rule->output_patterns.size() >= 1) { |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 460 | if (rule->output_patterns.size() != 1) { |
| 461 | fprintf(stderr, "hmm %s\n", rule->DebugString().c_str()); |
| 462 | } |
Shinichiro Hamaji | a7984ad | 2015-09-11 16:33:16 +0900 | [diff] [blame] | 463 | CHECK(rule->output_patterns.size() == 1); |
| 464 | n->output_pattern = rule->output_patterns[0]; |
| 465 | } |
| 466 | |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 467 | vector<unique_ptr<ScopedVar>> sv; |
| 468 | if (vars) { |
| 469 | for (const auto& p : *vars) { |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 470 | Symbol name = p.first; |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 471 | RuleVar* var = reinterpret_cast<RuleVar*>(p.second); |
| 472 | CHECK(var); |
| 473 | Var* new_var = var->v(); |
| 474 | if (var->op() == AssignOp::PLUS_EQ) { |
| 475 | Var* old_var = ev_->LookupVar(name); |
| 476 | if (old_var->IsDefined()) { |
| 477 | // TODO: This would be incorrect and has a leak. |
| 478 | shared_ptr<string> s = make_shared<string>(); |
| 479 | old_var->Eval(ev_, s.get()); |
Shinichiro Hamaji | 3c785c7 | 2015-06-23 19:22:34 +0900 | [diff] [blame] | 480 | if (!s->empty()) |
| 481 | *s += ' '; |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 482 | new_var->Eval(ev_, s.get()); |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 483 | new_var = new SimpleVar(*s, old_var->Origin()); |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 484 | } |
| 485 | } else if (var->op() == AssignOp::QUESTION_EQ) { |
| 486 | Var* old_var = ev_->LookupVar(name); |
| 487 | if (old_var->IsDefined()) { |
| 488 | continue; |
| 489 | } |
| 490 | } |
Shinichiro Hamaji | e978a89 | 2015-08-17 16:53:40 +0900 | [diff] [blame] | 491 | sv.emplace_back(new ScopedVar(cur_rule_vars_.get(), name, new_var)); |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 492 | } |
| 493 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 494 | |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 495 | ApplyOutputPattern(*rule, output, rule->inputs, &n->actual_inputs); |
| 496 | for (Symbol input : n->actual_inputs) { |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 497 | DepNode* c = BuildPlan(input, output); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 498 | n->deps.push_back(c); |
| 499 | } |
| 500 | |
Shinichiro Hamaji | 53ffb07 | 2015-09-26 12:07:22 +0900 | [diff] [blame] | 501 | vector<Symbol> order_only_inputs; |
| 502 | ApplyOutputPattern(*rule, output, rule->order_only_inputs, |
| 503 | &order_only_inputs); |
| 504 | for (Symbol input : order_only_inputs) { |
Shinichiro Hamaji | 704e4fe | 2015-06-24 17:37:47 +0900 | [diff] [blame] | 505 | DepNode* c = BuildPlan(input, output); |
Shinichiro Hamaji | 183dbb9 | 2015-07-06 17:21:39 +0900 | [diff] [blame] | 506 | n->order_onlys.push_back(c); |
Shinichiro Hamaji | 704e4fe | 2015-06-24 17:37:47 +0900 | [diff] [blame] | 507 | } |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 508 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 509 | n->has_rule = true; |
| 510 | n->cmds = rule->cmds; |
Colin Cross | 5b26db3 | 2015-09-29 16:51:02 -0700 | [diff] [blame^] | 511 | n->is_default_target = rule->is_default_target; |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 512 | if (cur_rule_vars_->empty()) { |
| 513 | n->rule_vars = NULL; |
| 514 | } else { |
| 515 | n->rule_vars = new Vars; |
| 516 | for (auto p : *cur_rule_vars_) { |
| 517 | n->rule_vars->insert(p); |
| 518 | } |
| 519 | } |
| 520 | n->loc = rule->loc; |
| 521 | if (!rule->cmds.empty() && rule->cmd_lineno) |
| 522 | n->loc.lineno = rule->cmd_lineno; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 523 | |
| 524 | return n; |
| 525 | } |
| 526 | |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 527 | Evaluator* ev_; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 528 | unordered_map<Symbol, shared_ptr<Rule>> rules_; |
| 529 | const unordered_map<Symbol, Vars*>& rule_vars_; |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 530 | unique_ptr<Vars> cur_rule_vars_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 531 | |
Shinichiro Hamaji | 2a04789 | 2015-06-29 13:56:41 +0900 | [diff] [blame] | 532 | unique_ptr<RuleTrie> implicit_rules_; |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 533 | typedef unordered_map<StringPiece, vector<shared_ptr<Rule>>> SuffixRuleMap; |
| 534 | SuffixRuleMap suffix_rules_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 535 | |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 536 | shared_ptr<Rule> first_rule_; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 537 | unordered_map<Symbol, DepNode*> done_; |
| 538 | unordered_set<Symbol> phony_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 539 | }; |
| 540 | |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 541 | void MakeDep(Evaluator* ev, |
| 542 | const vector<shared_ptr<Rule>>& rules, |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 543 | const unordered_map<Symbol, Vars*>& rule_vars, |
| 544 | const vector<Symbol>& targets, |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 545 | vector<DepNode*>* nodes) { |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 546 | DepBuilder db(ev, rules, rule_vars); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 547 | db.Build(targets, nodes); |
| 548 | } |
| 549 | |
| 550 | void InitDepNodePool() { |
| 551 | g_dep_node_pool = new vector<DepNode*>; |
| 552 | } |
| 553 | |
| 554 | void QuitDepNodePool() { |
| 555 | for (DepNode* n : *g_dep_node_pool) |
| 556 | delete n; |
| 557 | delete g_dep_node_pool; |
| 558 | } |