blob: f3f520327fe93c8d7aa69fed6856d63ae483b1bf [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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 Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017#include "rule.h"
18
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090019#include "expr.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090020#include "log.h"
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090021#include "parser.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090022#include "stringprintf.h"
23#include "strutil.h"
Shinichiro Hamajie7992752015-06-29 18:38:35 +090024#include "symtab.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090025
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090026namespace {
27
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090028static void ParseInputs(Rule* r, StringPiece s) {
29 bool is_order_only = false;
30 for (StringPiece input : WordScanner(s)) {
31 if (input == "|") {
32 is_order_only = true;
33 continue;
34 }
Shinichiro Hamajie7992752015-06-29 18:38:35 +090035 Symbol input_sym = Intern(TrimLeadingCurdir(input));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090036 if (is_order_only) {
Shinichiro Hamajie7992752015-06-29 18:38:35 +090037 r->order_only_inputs.push_back(input_sym);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090038 } else {
Shinichiro Hamajie7992752015-06-29 18:38:35 +090039 r->inputs.push_back(input_sym);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090040 }
41 }
42}
43
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090044bool IsPatternRule(StringPiece s) {
45 return s.find('%') != string::npos;
46}
47
48} // namespace
49
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090050Rule::Rule()
51 : is_double_colon(false),
52 is_suffix_rule(false),
Shinichiro Hamaji0325b162016-02-19 14:55:09 +090053 cmd_lineno(0) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090054}
55
Shinichiro Hamaji2928f462015-06-23 20:24:53 +090056void ParseRule(Loc& loc, StringPiece line, char term,
Shinichiro Hamajiffc52c32015-06-23 16:51:07 +090057 Rule** out_rule, RuleVarAssignment* rule_var) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090058 size_t index = line.find(':');
59 if (index == string::npos) {
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090060 ERROR("%s:%d: *** missing separator.", LOCF(loc));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090061 }
62
63 StringPiece first = line.substr(0, index);
Shinichiro Hamajie7992752015-06-29 18:38:35 +090064 vector<Symbol> outputs;
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090065 for (StringPiece tok : WordScanner(first)) {
66 outputs.push_back(Intern(TrimLeadingCurdir(tok)));
67 }
68
Shinichiro Hamajie7992752015-06-29 18:38:35 +090069 const bool is_first_pattern = (
70 !outputs.empty() && IsPatternRule(outputs[0].str()));
Shinichiro Hamaji36b65822015-09-11 15:39:29 +090071 for (size_t i = 1; i < outputs.size(); i++) {
72 if (IsPatternRule(outputs[i].str()) != is_first_pattern) {
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090073 ERROR("%s:%d: *** mixed implicit and normal rules: deprecated syntax",
74 LOCF(loc));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090075 }
76 }
77
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090078 bool is_double_colon = false;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090079 index++;
80 if (line.get(index) == ':') {
81 is_double_colon = true;
82 index++;
83 }
84
85 StringPiece rest = line.substr(index);
Shinichiro Hamaji2928f462015-06-23 20:24:53 +090086 size_t term_index = rest.find_first_of("=;");
87 if ((term_index != string::npos && rest[term_index] == '=') ||
88 (term_index == string::npos && term == '=')) {
89 if (term_index == string::npos)
90 term_index = rest.size();
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090091 rule_var->outputs.swap(outputs);
Shinichiro Hamaji2928f462015-06-23 20:24:53 +090092 ParseAssignStatement(rest, term_index,
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090093 &rule_var->lhs, &rule_var->rhs, &rule_var->op);
94 *out_rule = NULL;
95 return;
96 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090097
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090098 Rule* rule = new Rule();
99 *out_rule = rule;
100 rule->loc = loc;
101 rule->is_double_colon = is_double_colon;
102 if (is_first_pattern) {
103 rule->output_patterns.swap(outputs);
104 } else {
105 rule->outputs.swap(outputs);
106 }
Shinichiro Hamaji2928f462015-06-23 20:24:53 +0900107 if (term_index != string::npos && term != ';') {
108 CHECK(rest[term_index] == ';');
109 // TODO: Maybe better to avoid Intern here?
110 rule->cmds.push_back(
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900111 NewLiteral(Intern(TrimLeftSpace(rest.substr(term_index + 1))).str()));
Shinichiro Hamaji2928f462015-06-23 20:24:53 +0900112 rest = rest.substr(0, term_index);
113 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900114
115 index = rest.find(':');
116 if (index == string::npos) {
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900117 ParseInputs(rule, rest);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900118 return;
119 }
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900120
121 if (is_first_pattern) {
122 ERROR("%s:%d: *** mixed implicit and normal rules: deprecated syntax",
123 LOCF(loc));
124 }
125
126 StringPiece second = rest.substr(0, index);
127 StringPiece third = rest.substr(index+1);
128
129 for (StringPiece tok : WordScanner(second)) {
Shinichiro Hamajifcbadb22016-01-26 16:38:52 +0900130 tok = TrimLeadingCurdir(tok);
Shinichiro Hamaji138c4d82015-09-28 13:53:46 +0900131 for (Symbol output : rule->outputs) {
132 if (!Pattern(tok).Match(output.str())) {
133 WARN("%s:%d: target `%s' doesn't match the target pattern",
134 LOCF(loc), output.c_str());
135 }
136 }
137
Shinichiro Hamaji2e23e4a2015-06-26 07:33:16 +0900138 rule->output_patterns.push_back(Intern(tok));
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900139 }
140
141 if (rule->output_patterns.empty()) {
142 ERROR("%s:%d: *** missing target pattern.", LOCF(loc));
143 }
144 if (rule->output_patterns.size() > 1) {
145 ERROR("%s:%d: *** multiple target patterns.", LOCF(loc));
146 }
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900147 if (!IsPatternRule(rule->output_patterns[0].str())) {
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +0900148 ERROR("%s:%d: *** target pattern contains no '%%'.", LOCF(loc));
149 }
150 ParseInputs(rule, third);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900151}
152
153string Rule::DebugString() const {
154 vector<string> v;
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900155 v.push_back(StringPrintf("outputs=[%s]", JoinSymbols(outputs, ",").c_str()));
156 v.push_back(StringPrintf("inputs=[%s]", JoinSymbols(inputs, ",").c_str()));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900157 if (!order_only_inputs.empty()) {
158 v.push_back(StringPrintf("order_only_inputs=[%s]",
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900159 JoinSymbols(order_only_inputs, ",").c_str()));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900160 }
Shinichiro Hamaji2e23e4a2015-06-26 07:33:16 +0900161 if (!output_patterns.empty()) {
162 v.push_back(StringPrintf("output_patterns=[%s]",
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900163 JoinSymbols(output_patterns, ",").c_str()));
Shinichiro Hamaji2e23e4a2015-06-26 07:33:16 +0900164 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900165 if (is_double_colon)
166 v.push_back("is_double_colon");
167 if (is_suffix_rule)
168 v.push_back("is_suffix_rule");
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900169 if (!cmds.empty()) {
170 v.push_back(StringPrintf("cmds=[%s]", JoinValues(cmds, ",").c_str()));
171 }
172 return JoinStrings(v, " ");
173}