forked from michaelforney/samurai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
280 lines (257 loc) · 5.5 KB
/
parse.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "env.h"
#include "graph.h"
#include "parse.h"
#include "scan.h"
#include "util.h"
struct parseoptions parseopts;
static struct node **deftarg;
static size_t ndeftarg;
void
parseinit(void)
{
free(deftarg);
deftarg = NULL;
ndeftarg = 0;
}
static void
parselet(struct scanner *s, struct evalstring **val)
{
scanchar(s, '=');
*val = scanstring(s, false);
scannewline(s);
}
static void
parserule(struct scanner *s, struct environment *env)
{
struct rule *r;
char *var;
struct evalstring *val;
bool hascommand = false, hasrspfile = false, hasrspcontent = false;
r = mkrule(scanname(s));
scannewline(s);
while (scanindent(s)) {
var = scanname(s);
parselet(s, &val);
ruleaddvar(r, var, val);
if (!val)
continue;
if (strcmp(var, "command") == 0)
hascommand = true;
else if (strcmp(var, "rspfile") == 0)
hasrspfile = true;
else if (strcmp(var, "rspfile_content") == 0)
hasrspcontent = true;
}
if (!hascommand)
fatal("rule '%s' has no command", r->name);
if (hasrspfile != hasrspcontent)
fatal("rule '%s' has rspfile and no rspfile_content or vice versa", r->name);
envaddrule(env, r);
}
static void
parseedge(struct scanner *s, struct environment *env)
{
struct edge *e;
struct evalstring *str, **path;
char *name;
struct string *val;
struct node *n;
size_t i;
int p;
e = mkedge(env);
scanpaths(s);
e->outimpidx = npaths;
if (scanpipe(s, 1))
scanpaths(s);
e->nout = npaths;
if (e->nout == 0)
scanerror(s, "expected output path");
scanchar(s, ':');
name = scanname(s);
e->rule = envrule(env, name);
if (!e->rule)
fatal("undefined rule '%s'", name);
free(name);
scanpaths(s);
e->inimpidx = npaths - e->nout;
p = scanpipe(s, 1 | 2);
if (p == 1) {
scanpaths(s);
p = scanpipe(s, 2);
}
e->inorderidx = npaths - e->nout;
if (p == 2)
scanpaths(s);
e->nin = npaths - e->nout;
scannewline(s);
while (scanindent(s)) {
name = scanname(s);
parselet(s, &str);
val = enveval(env, str);
envaddvar(e->env, name, val);
}
e->out = xreallocarray(NULL, e->nout, sizeof(e->out[0]));
for (i = 0, path = paths; i < e->nout; ++path) {
val = enveval(e->env, *path);
canonpath(val);
n = mknode(val);
if (n->gen) {
if (!parseopts.dupbuildwarn)
fatal("multiple rules generate '%s'", n->path->s);
warn("multiple rules generate '%s'", n->path->s);
--e->nout;
if (i < e->outimpidx)
--e->outimpidx;
} else {
n->gen = e;
e->out[i] = n;
++i;
}
}
e->in = xreallocarray(NULL, e->nin, sizeof(e->in[0]));
for (i = 0; i < e->nin; ++i, ++path) {
val = enveval(e->env, *path);
canonpath(val);
n = mknode(val);
e->in[i] = n;
nodeuse(n, e);
}
npaths = 0;
val = edgevar(e, "pool", true);
if (val)
e->pool = poolget(val->s);
}
static void
parseinclude(struct scanner *s, struct environment *env, bool newscope)
{
struct evalstring *str;
struct string *path;
str = scanstring(s, true);
if (!str)
scanerror(s, "expected include path");
scannewline(s);
path = enveval(env, str);
if (newscope)
env = mkenv(env);
parse(path->s, env);
free(path);
}
static void
parsedefault(struct scanner *s, struct environment *env)
{
struct string *path;
struct node *n;
size_t i;
scanpaths(s);
deftarg = xreallocarray(deftarg, ndeftarg + npaths, sizeof(*deftarg));
for (i = 0; i < npaths; ++i) {
path = enveval(env, paths[i]);
canonpath(path);
n = nodeget(path->s, path->n);
if (!n)
fatal("unknown target '%s'", path->s);
free(path);
deftarg[ndeftarg++] = n;
}
scannewline(s);
npaths = 0;
}
static void
parsepool(struct scanner *s, struct environment *env)
{
struct pool *p;
struct evalstring *val;
struct string *str;
char *var, *end;
p = mkpool(scanname(s));
scannewline(s);
while (scanindent(s)) {
var = scanname(s);
parselet(s, &val);
if (strcmp(var, "depth") == 0) {
str = enveval(env, val);
p->maxjobs = strtol(str->s, &end, 10);
if (*end)
fatal("invalid pool depth '%s'", str->s);
free(str);
} else {
fatal("unexpected pool variable '%s'", var);
}
}
if (!p->maxjobs)
fatal("pool '%s' has no depth", p->name);
}
static void
checkversion(const char *ver)
{
int major, minor = 0;
if (sscanf(ver, "%d.%d", &major, &minor) < 1)
fatal("invalid ninja_required_version");
if (major > ninjamajor || (major == ninjamajor && minor > ninjaminor))
fatal("ninja_required_version %s is newer than %d.%d", ver, ninjamajor, ninjaminor);
}
void
parse(const char *name, struct environment *env)
{
struct scanner s;
char *var;
struct string *val;
struct evalstring *str;
scaninit(&s, name);
for (;;) {
switch (scankeyword(&s, &var)) {
case RULE:
parserule(&s, env);
break;
case BUILD:
parseedge(&s, env);
break;
case INCLUDE:
parseinclude(&s, env, false);
break;
case SUBNINJA:
parseinclude(&s, env, true);
break;
case DEFAULT:
parsedefault(&s, env);
break;
case POOL:
parsepool(&s, env);
break;
case VARIABLE:
parselet(&s, &str);
val = enveval(env, str);
if (strcmp(var, "ninja_required_version") == 0)
checkversion(val->s);
envaddvar(env, var, val);
break;
case EOF:
scanclose(&s);
return;
}
}
}
void
defaultnodes(void fn(struct node *))
{
struct edge *e;
struct node *n;
size_t i;
if (ndeftarg > 0) {
for (i = 0; i < ndeftarg; ++i)
fn(deftarg[i]);
} else {
/* by default build all nodes which are not used by any edges */
for (e = alledges; e; e = e->allnext) {
for (i = 0; i < e->nout; ++i) {
n = e->out[i];
if (n->nuse == 0)
fn(n);
}
}
}
}