From e30b3bee7d149ae80a95af3ca8eb9942e295f695 Mon Sep 17 00:00:00 2001 From: Shinichiro Hamaji Date: Fri, 19 Jun 2015 16:15:42 +0900 Subject: [C++] Merge explicit rules --- dep.cc | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'dep.cc') diff --git a/dep.cc b/dep.cc index 4325925..356c02d 100644 --- a/dep.cc +++ b/dep.cc @@ -107,32 +107,49 @@ class DepBuilder { return true; } + shared_ptr MergeRules(const Rule& old_rule, + const Rule& rule, + StringPiece output, + bool is_suffix_rule) { + if (old_rule.is_double_colon != rule.is_double_colon) { + ERROR("%s:%d: *** target file `%s' has both : and :: entries.", + LOCF(rule.loc), output.as_string().c_str()); + } + if (!old_rule.cmds.empty() && !rule.cmds.empty() && + !is_suffix_rule && !rule.is_double_colon) { + WARN("%s:%d: overriding commands for target `%s'", + LOCF(rule.cmd_loc()), output.as_string().c_str()); + WARN("%s:%d: ignoring old commands for target `%s'", + LOCF(old_rule.cmd_loc()), output.as_string().c_str()); + } + + shared_ptr r = make_shared(rule); + if (rule.is_double_colon) { + r->cmds.clear(); + for (Value* c : old_rule.cmds) + r->cmds.push_back(c); + for (Value* c : rule.cmds) + r->cmds.push_back(c); + } else if (!old_rule.cmds.empty() && rule.cmds.empty()) { + r->cmds = old_rule.cmds; + } + for (StringPiece p : old_rule.output_patterns) { + r->output_patterns.push_back(p); + } + return r; + } + void PopulateExplicitRule(shared_ptr rule) { for (StringPiece output : rule->outputs) { const bool is_suffix_rule = PopulateSuffixRule(rule, output); - // isSuffixRule := db.populateSuffixRule(rule, output) - - - /* - if oldRule, present := db.rules[output]; present { - r := mergeRules(oldRule, rule, output, isSuffixRule) - db.rules[output] = r - } else { - db.rules[output] = rule - if db.firstRule == nil && !strings.HasPrefix(output, ".") { - db.firstRule = rule - } - } - */ - auto p = rules_.insert(make_pair(output, rule)); if (p.second) { if (!first_rule_ && output.get(0) != '.') { first_rule_ = rule; } } else { - // TODO: merge - CHECK(false); + p.first->second = + MergeRules(*p.first->second, *rule, output, is_suffix_rule); } } } -- cgit v1.2.3