forked from michaelforney/samurai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.c
324 lines (289 loc) · 5.86 KB
/
tool.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "arg.h"
#include "env.h"
#include "graph.h"
#include "tool.h"
#include "util.h"
static int
cleanpath(struct string *path)
{
if (path) {
if (remove(path->s) == 0) {
printf("remove %s\n", path->s);
} else if (errno != ENOENT) {
warn("remove %s:", path->s);
return -1;
}
}
return 0;
}
static int
cleanedge(struct edge *e)
{
int ret = 0;
size_t i;
for (i = 0; i < e->nout; ++i) {
if (cleanpath(e->out[i]->path) < 0)
ret = -1;
}
if (cleanpath(edgevar(e, "rspfile", false)) < 0)
ret = -1;
if (cleanpath(edgevar(e, "depfile", false)) < 0)
ret = -1;
return ret;
}
static int
cleantarget(struct node *n)
{
int ret = 0;
size_t i;
if (!n->gen || n->gen->rule == &phonyrule)
return 0;
if (cleanpath(n->path) < 0)
ret = -1;
for (i = 0; i < n->gen->nin; ++i) {
if (cleantarget(n->gen->in[i]) < 0)
ret = -1;
}
return ret;
}
static int
clean(int argc, char *argv[])
{
int ret = 0;
bool cleangen = false, cleanrule = false;
struct edge *e;
struct node *n;
struct rule *r;
ARGBEGIN {
case 'g':
cleangen = true;
break;
case 'r':
cleanrule = true;
break;
default:
fprintf(stderr, "usage: %s ... -t clean [-gr] [targets...]\n", argv0);
return 2;
} ARGEND
if (cleanrule) {
if (!argc)
fatal("expected a rule to clean");
for (; *argv; ++argv) {
r = envrule(rootenv, *argv);
if (!r) {
warn("unknown rule '%s'", *argv);
ret = 1;
continue;
}
for (e = alledges; e; e = e->allnext) {
if (e->rule != r)
continue;
if (cleanedge(e) < 0)
ret = 1;
}
}
} else if (argc > 0) {
for (; *argv; ++argv) {
n = nodeget(*argv, 0);
if (!n) {
warn("unknown target '%s'", *argv);
ret = 1;
continue;
}
if (cleantarget(n) < 0)
ret = 1;
}
} else {
for (e = alledges; e; e = e->allnext) {
if (e->rule == &phonyrule)
continue;
if (!cleangen && edgevar(e, "generator", true))
continue;
if (cleanedge(e) < 0)
ret = 1;
}
}
return ret;
}
static void
printjson(const char *s, size_t n, bool join)
{
size_t i;
char c;
for (i = 0; i < n; ++i) {
c = s[i];
switch (c) {
case '"':
case '\\':
putchar('\\');
break;
case '\n':
if (join)
c = ' ';
break;
case '\0':
return;
}
putchar(c);
}
}
static int
compdb(int argc, char *argv[])
{
char dir[PATH_MAX], *p;
struct edge *e;
struct string *cmd, *rspfile, *content;
bool expandrsp = false, first = true;
int i;
size_t off;
ARGBEGIN {
case 'x':
expandrsp = true;
break;
default:
fprintf(stderr, "usage: %s ... -t compdb [-x] [rules...]\n", argv0);
return 2;
} ARGEND
if (!getcwd(dir, sizeof(dir)))
fatal("getcwd:");
putchar('[');
for (e = alledges; e; e = e->allnext) {
if (e->nin == 0)
continue;
for (i = 0; i < argc; ++i) {
if (strcmp(e->rule->name, argv[i]) == 0) {
if (first)
first = false;
else
putchar(',');
printf("\n {\n \"directory\": \"");
printjson(dir, -1, false);
printf("\",\n \"command\": \"");
cmd = edgevar(e, "command", true);
rspfile = expandrsp ? edgevar(e, "rspfile", true) : NULL;
p = rspfile ? strstr(cmd->s, rspfile->s) : NULL;
if (!p || p == cmd->s || p[-1] != '@') {
printjson(cmd->s, cmd->n, false);
} else {
off = p - cmd->s;
printjson(cmd->s, off - 1, false);
content = edgevar(e, "rspfile_content", true);
printjson(content->s, content->n, true);
off += rspfile->n;
printjson(cmd->s + off, cmd->n - off, false);
}
printf("\",\n \"file\": \"");
printjson(e->in[0]->path->s, -1, false);
printf("\",\n \"output\": \"");
printjson(e->out[0]->path->s, -1, false);
printf("\"\n }");
break;
}
}
}
puts("\n]");
fflush(stdout);
if (ferror(stdout))
fatal("write failed");
return 0;
}
static void
targetsdepth(struct node *n, size_t depth, size_t indent)
{
struct edge *e = n->gen;
size_t i;
for (i = 0; i < indent; ++i)
printf(" ");
if (e) {
printf("%s: %s\n", n->path->s, e->rule->name);
if (depth != 1) {
for (i = 0; i < e->nin; ++i)
targetsdepth(e->in[i], depth - 1, indent + 1);
}
} else {
puts(n->path->s);
}
}
static void
targetsusage(void)
{
fprintf(stderr,
"usage: %s ... -t targets [depth [maxdepth]]\n"
" %s ... -t targets rule [rulename]\n"
" %s ... -t targets all\n",
argv0, argv0, argv0);
exit(2);
}
static int
targets(int argc, char *argv[])
{
struct edge *e;
size_t depth = 1, i;
char *end, *mode, *name;
if (argc > 3)
targetsusage();
mode = argv[1];
if (!mode || strcmp(mode, "depth") == 0) {
if (argc == 3) {
depth = strtol(argv[2], &end, 10);
if (*end)
targetsusage();
}
for (e = alledges; e; e = e->allnext) {
for (i = 0; i < e->nout; ++i) {
if (e->out[i]->nuse == 0)
targetsdepth(e->out[i], depth, 0);
}
}
} else if (strcmp(mode, "rule") == 0) {
name = argv[2];
for (e = alledges; e; e = e->allnext) {
if (!name) {
for (i = 0; i < e->nin; ++i) {
if (!e->in[i]->gen)
puts(e->in[i]->path->s);
}
} else if (strcmp(e->rule->name, name) == 0) {
for (i = 0; i < e->nout; ++i)
puts(e->out[i]->path->s);
}
}
} else if (strcmp(mode, "all") == 0 && argc == 2) {
for (e = alledges; e; e = e->allnext) {
for (i = 0; i < e->nout; ++i)
printf("%s: %s\n", e->out[i]->path->s, e->rule->name);
}
} else {
targetsusage();
}
return 0;
}
static const struct tool tools[] = {
{"clean", clean},
{"compdb", compdb},
{"targets", targets},
};
const struct tool *
toolget(const char *name)
{
const struct tool *t;
size_t i;
t = NULL;
for (i = 0; i < LEN(tools); ++i) {
if (strcmp(name, tools[i].name) == 0) {
t = &tools[i];
break;
}
}
if (!t)
fatal("unknown tool '%s'", name);
return t;
}