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 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 15 | #include "rule.h" |
| 16 | |
| 17 | #include "log.h" |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 18 | #include "parser.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 19 | #include "stringprintf.h" |
| 20 | #include "strutil.h" |
| 21 | #include "value.h" |
| 22 | |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 23 | namespace { |
| 24 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 25 | // Strip leading sequences of './' from file names, so that ./file |
| 26 | // and file are considered to be the same file. |
| 27 | // From http://www.gnu.org/software/make/manual/make.html#Features |
| 28 | StringPiece TrimLeadingCurdir(StringPiece s) { |
| 29 | if (s.substr(0, 2) != "./") |
| 30 | return s; |
| 31 | return s.substr(2); |
| 32 | } |
| 33 | |
| 34 | static void ParseInputs(Rule* r, StringPiece s) { |
| 35 | bool is_order_only = false; |
| 36 | for (StringPiece input : WordScanner(s)) { |
| 37 | if (input == "|") { |
| 38 | is_order_only = true; |
| 39 | continue; |
| 40 | } |
| 41 | input = Intern(TrimLeadingCurdir(input)); |
| 42 | if (is_order_only) { |
| 43 | r->order_only_inputs.push_back(input); |
| 44 | } else { |
| 45 | r->inputs.push_back(input); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 50 | bool IsPatternRule(StringPiece s) { |
| 51 | return s.find('%') != string::npos; |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 56 | Rule::Rule() |
| 57 | : is_double_colon(false), |
| 58 | is_suffix_rule(false), |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 59 | cmd_lineno(0) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 60 | } |
| 61 | |
Shinichiro Hamaji | 784b995 | 2015-06-23 14:29:32 +0900 | [diff] [blame] | 62 | void ParseRule(Loc& loc, StringPiece line, bool is_assign, |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 63 | Rule** out_rule, RuleVar* rule_var) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 64 | size_t index = line.find(':'); |
| 65 | if (index == string::npos) { |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 66 | ERROR("%s:%d: *** missing separator.", LOCF(loc)); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | StringPiece first = line.substr(0, index); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 70 | vector<StringPiece> outputs; |
| 71 | for (StringPiece tok : WordScanner(first)) { |
| 72 | outputs.push_back(Intern(TrimLeadingCurdir(tok))); |
| 73 | } |
| 74 | |
| 75 | CHECK(!outputs.empty()); |
| 76 | const bool is_first_pattern = IsPatternRule(outputs[0]); |
| 77 | if (is_first_pattern) { |
| 78 | if (outputs.size() > 1) { |
| 79 | // TODO: Multiple output patterns are not supported yet. |
| 80 | ERROR("%s:%d: *** mixed implicit and normal rules: deprecated syntax", |
| 81 | LOCF(loc)); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 85 | bool is_double_colon = false; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 86 | index++; |
| 87 | if (line.get(index) == ':') { |
| 88 | is_double_colon = true; |
| 89 | index++; |
| 90 | } |
| 91 | |
| 92 | StringPiece rest = line.substr(index); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 93 | size_t equal_index = rest.find('='); |
Shinichiro Hamaji | 784b995 | 2015-06-23 14:29:32 +0900 | [diff] [blame] | 94 | if (equal_index != string::npos || is_assign) { |
| 95 | if (equal_index == string::npos) |
| 96 | equal_index = rest.size(); |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 97 | rule_var->outputs.swap(outputs); |
| 98 | ParseAssignStatement(rest, equal_index, |
| 99 | &rule_var->lhs, &rule_var->rhs, &rule_var->op); |
| 100 | *out_rule = NULL; |
| 101 | return; |
| 102 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 103 | |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 104 | Rule* rule = new Rule(); |
| 105 | *out_rule = rule; |
| 106 | rule->loc = loc; |
| 107 | rule->is_double_colon = is_double_colon; |
| 108 | if (is_first_pattern) { |
| 109 | rule->output_patterns.swap(outputs); |
| 110 | } else { |
| 111 | rule->outputs.swap(outputs); |
| 112 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 113 | |
| 114 | index = rest.find(':'); |
| 115 | if (index == string::npos) { |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 116 | ParseInputs(rule, rest); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 117 | return; |
| 118 | } |
Shinichiro Hamaji | 9b16bda | 2015-06-19 14:25:17 +0900 | [diff] [blame] | 119 | |
| 120 | if (is_first_pattern) { |
| 121 | ERROR("%s:%d: *** mixed implicit and normal rules: deprecated syntax", |
| 122 | LOCF(loc)); |
| 123 | } |
| 124 | |
| 125 | StringPiece second = rest.substr(0, index); |
| 126 | StringPiece third = rest.substr(index+1); |
| 127 | |
| 128 | for (StringPiece tok : WordScanner(second)) { |
| 129 | rule->output_patterns.push_back(tok); |
| 130 | } |
| 131 | |
| 132 | if (rule->output_patterns.empty()) { |
| 133 | ERROR("%s:%d: *** missing target pattern.", LOCF(loc)); |
| 134 | } |
| 135 | if (rule->output_patterns.size() > 1) { |
| 136 | ERROR("%s:%d: *** multiple target patterns.", LOCF(loc)); |
| 137 | } |
| 138 | if (!IsPatternRule(rule->output_patterns[0])) { |
| 139 | ERROR("%s:%d: *** target pattern contains no '%%'.", LOCF(loc)); |
| 140 | } |
| 141 | ParseInputs(rule, third); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | string Rule::DebugString() const { |
| 145 | vector<string> v; |
| 146 | v.push_back(StringPrintf("outputs=[%s]", JoinStrings(outputs, ",").c_str())); |
| 147 | v.push_back(StringPrintf("inputs=[%s]", JoinStrings(inputs, ",").c_str())); |
| 148 | if (!order_only_inputs.empty()) { |
| 149 | v.push_back(StringPrintf("order_only_inputs=[%s]", |
| 150 | JoinStrings(order_only_inputs, ",").c_str())); |
| 151 | } |
| 152 | if (is_double_colon) |
| 153 | v.push_back("is_double_colon"); |
| 154 | if (is_suffix_rule) |
| 155 | v.push_back("is_suffix_rule"); |
| 156 | if (!cmds.empty()) { |
| 157 | v.push_back(StringPrintf("cmds=[%s]", JoinValues(cmds, ",").c_str())); |
| 158 | } |
| 159 | return JoinStrings(v, " "); |
| 160 | } |