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