forked from google/kati
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.cc
231 lines (199 loc) · 6.5 KB
/
command.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2015 Google Inc. All rights reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build ignore
#include "command.h"
#include <unordered_map>
#include <unordered_set>
#include "dep.h"
#include "eval.h"
#include "log.h"
#include "strutil.h"
#include "var.h"
namespace {
class AutoVar : public Var {
public:
virtual const char* Flavor() const override {
return "undefined";
}
virtual VarOrigin Origin() const override {
return VarOrigin::AUTOMATIC;
}
virtual void AppendVar(Evaluator*, Value*) override { CHECK(false); }
virtual StringPiece String() const override {
ERROR("$(value %s) is not implemented yet", sym_);
return "";
}
virtual string DebugString() const override {
return string("AutoVar(") + sym_ + ")";
}
protected:
AutoVar(CommandEvaluator* ce, const char* sym) : ce_(ce), sym_(sym) {}
virtual ~AutoVar() = default;
CommandEvaluator* ce_;
const char* sym_;
};
#define DECLARE_AUTO_VAR_CLASS(name) \
class name : public AutoVar { \
public: \
name(CommandEvaluator* ce, const char* sym) \
: AutoVar(ce, sym) {} \
virtual ~name() = default; \
virtual void Eval(Evaluator* ev, string* s) const override; \
}
DECLARE_AUTO_VAR_CLASS(AutoAtVar);
DECLARE_AUTO_VAR_CLASS(AutoLessVar);
DECLARE_AUTO_VAR_CLASS(AutoHatVar);
DECLARE_AUTO_VAR_CLASS(AutoPlusVar);
DECLARE_AUTO_VAR_CLASS(AutoStarVar);
class AutoSuffixDVar : public AutoVar {
public:
AutoSuffixDVar(CommandEvaluator* ce, const char* sym, Var* wrapped)
: AutoVar(ce, sym), wrapped_(wrapped) {
}
virtual ~AutoSuffixDVar() = default;
virtual void Eval(Evaluator* ev, string* s) const override;
private:
Var* wrapped_;
};
class AutoSuffixFVar : public AutoVar {
public:
AutoSuffixFVar(CommandEvaluator* ce, const char* sym, Var* wrapped)
: AutoVar(ce, sym), wrapped_(wrapped) {}
virtual ~AutoSuffixFVar() = default;
virtual void Eval(Evaluator* ev, string* s) const override;
private:
Var* wrapped_;
};
void AutoAtVar::Eval(Evaluator*, string* s) const {
*s += ce_->current_dep_node()->output.str();
}
void AutoLessVar::Eval(Evaluator*, string* s) const {
auto& ai = ce_->current_dep_node()->actual_inputs;
if (!ai.empty())
*s += ai[0].str();
}
void AutoHatVar::Eval(Evaluator*, string* s) const {
unordered_set<StringPiece> seen;
WordWriter ww(s);
for (Symbol ai : ce_->current_dep_node()->actual_inputs) {
if (seen.insert(ai.str()).second)
ww.Write(ai.str());
}
}
void AutoPlusVar::Eval(Evaluator*, string* s) const {
WordWriter ww(s);
for (Symbol ai : ce_->current_dep_node()->actual_inputs) {
ww.Write(ai.str());
}
}
void AutoStarVar::Eval(Evaluator*, string* s) const {
AppendString(StripExt(ce_->current_dep_node()->output.str()), s);
}
void AutoSuffixDVar::Eval(Evaluator* ev, string* s) const {
string buf;
wrapped_->Eval(ev, &buf);
WordWriter ww(s);
for (StringPiece tok : WordScanner(buf)) {
ww.Write(Dirname(tok));
}
}
void AutoSuffixFVar::Eval(Evaluator* ev, string* s) const {
string buf;
wrapped_->Eval(ev, &buf);
WordWriter ww(s);
for (StringPiece tok : WordScanner(buf)) {
ww.Write(Basename(tok));
}
}
void ParseCommandPrefixes(StringPiece* s, bool* echo, bool* ignore_error) {
*s = TrimLeftSpace(*s);
while (true) {
char c = s->get(0);
if (c == '@') {
*echo = false;
} else if (c == '-') {
*ignore_error = true;
} else {
break;
}
*s = TrimLeftSpace(s->substr(1));
}
}
} // namespace
CommandEvaluator::CommandEvaluator(Evaluator* ev)
: ev_(ev) {
Vars* vars = ev_->mutable_vars();
#define INSERT_AUTO_VAR(name, sym) do { \
Var* v = new name(this, sym); \
(*vars)[Intern(sym)] = v; \
(*vars)[Intern(sym"D")] = new AutoSuffixDVar(this, sym"D", v); \
(*vars)[Intern(sym"F")] = new AutoSuffixFVar(this, sym"F", v); \
} while (0)
INSERT_AUTO_VAR(AutoAtVar, "@");
INSERT_AUTO_VAR(AutoLessVar, "<");
INSERT_AUTO_VAR(AutoHatVar, "^");
INSERT_AUTO_VAR(AutoPlusVar, "+");
INSERT_AUTO_VAR(AutoStarVar, "*");
}
void CommandEvaluator::Eval(DepNode* n, vector<Command*>* commands) {
ev_->set_loc(n->loc);
ev_->set_current_scope(n->rule_vars);
current_dep_node_ = n;
for (Value* v : n->cmds) {
const string&& cmds_buf = v->Eval(ev_);
StringPiece cmds = cmds_buf;
bool global_echo = true;
bool global_ignore_error = false;
ParseCommandPrefixes(&cmds, &global_echo, &global_ignore_error);
if (cmds == "")
continue;
while (true) {
size_t lf_cnt;
size_t index = FindEndOfLine(cmds, 0, &lf_cnt);
if (index == cmds.size())
index = string::npos;
StringPiece cmd = TrimLeftSpace(cmds.substr(0, index));
cmds = cmds.substr(index + 1);
bool echo = global_echo;
bool ignore_error = global_ignore_error;
ParseCommandPrefixes(&cmd, &echo, &ignore_error);
if (!cmd.empty()) {
Command* command = new Command(n->output);
command->cmd = cmd.as_string();
command->echo = echo;
command->ignore_error = ignore_error;
commands->push_back(command);
}
if (index == string::npos)
break;
}
continue;
}
if (!ev_->delayed_output_commands().empty()) {
vector<Command*> output_commands;
for (const string& cmd : ev_->delayed_output_commands()) {
Command* c = new Command(n->output);
c->cmd = cmd;
c->echo = false;
c->ignore_error = false;
output_commands.push_back(c);
}
// Prepend |output_commands|.
commands->swap(output_commands);
copy(output_commands.begin(), output_commands.end(),
back_inserter(*commands));
ev_->clear_delayed_output_commands();
}
ev_->set_current_scope(NULL);
}