-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_run.c
More file actions
362 lines (320 loc) · 9.78 KB
/
Copy pathnode_run.c
File metadata and controls
362 lines (320 loc) · 9.78 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#ifndef _AMALGAMATE_
#include "script.h"
#include "spawn.h"
#include "env.h"
#include "log.h"
#include "spawn.h"
#include "utils.h"
#include "log.h"
#include "paths.h"
#include <unistd.h>
#include <string.h>
#endif
struct _run_spawn_data {
struct node *n;
const char *cmd;
};
static void _run_stdout_handler(char *buf, size_t buflen, struct spawn *s) {
fprintf(stdout, "%s", buf);
}
static void _run_stderr_handler(char *buf, size_t buflen, struct spawn *s) {
fprintf(stderr, "%s", buf);
}
struct _run_on_resolve_ctx {
struct node_resolve *r;
struct node_foreach *fe;
struct ulist consumes; // sizeof(char*)
struct ulist consumes_foreach; // sizeof(char*)
bool fe_consumed; // Foreach variable is consumed
};
static void _run_on_resolve_shell(struct node_resolve *r, struct node *nn_) {
struct node *n = r->n;
struct xstr *xstr = xstr_create_empty();
for (struct node *nn = nn_; nn; nn = nn->next) {
if (nn != nn_) {
xstr_cat2(xstr, " ", 1);
}
const char *v = node_value(nn);
if (is_vlist(v)) {
struct vlist_iter iter;
vlist_iter_init(v, &iter);
while (vlist_iter_next(&iter)) {
if (xstr_size(xstr)) {
xstr_cat2(xstr, " ", 1);
}
xstr_cat2(xstr, iter.item, iter.len);
}
} else {
xstr_cat(xstr, v);
}
}
const char *shell = "/bin/sh";
struct unit *unit = unit_peek();
struct spawn *s = spawn_create(shell, &(struct _run_spawn_data) {
.cmd = shell,
.n = n
});
spawn_env_path_prepend(s, unit->dir);
spawn_env_path_prepend(s, unit->cache_dir);
spawn_set_stdout_handler(s, _run_stdout_handler);
spawn_set_stderr_handler(s, _run_stderr_handler);
spawn_arg_add(s, "-c");
spawn_arg_add(s, xstr_ptr(xstr));
if (g_env.check.log) {
xstr_printf(g_env.check.log, "%s: %s\n", n->name, shell);
}
int rc = spawn_do(s);
if (rc) {
node_fatal(rc, n, "%s", shell);
} else {
int code = spawn_exit_code(s);
if (code != 0) {
node_fatal(AK_ERROR_EXTERNAL_COMMAND, n, "%s: %d", shell, code);
}
}
spawn_destroy(s);
xstr_destroy(xstr);
}
static void _run_on_resolve_exec(struct node_resolve *r, struct node *ncmd) {
struct node *n = r->n;
const char *cmd = node_value(ncmd);
if (!cmd) {
node_fatal(AK_ERROR_FAIL, n, "No run command specified");
}
struct unit *unit = unit_peek();
struct spawn *s = spawn_create(cmd, &(struct _run_spawn_data) {
.cmd = cmd,
.n = n
});
spawn_env_path_prepend(s, unit->dir);
spawn_env_path_prepend(s, unit->cache_dir);
spawn_set_stdout_handler(s, _run_stdout_handler);
spawn_set_stderr_handler(s, _run_stderr_handler);
for (struct node *nn = ncmd->next; nn; nn = nn->next) {
if (node_is_can_be_value(nn)) {
spawn_arg_add(s, node_value(nn));
}
}
if (g_env.check.log) {
xstr_printf(g_env.check.log, "%s: %s\n", n->name, cmd);
}
int rc = spawn_do(s);
if (rc) {
node_fatal(rc, n, "%s", cmd);
} else {
int code = spawn_exit_code(s);
if (code != 0) {
node_fatal(AK_ERROR_EXTERNAL_COMMAND, n, "%s: %d", cmd, code);
}
}
spawn_destroy(s);
}
static void _run_on_resolve_do(struct node_resolve *r, struct node *n) {
for (struct node *nn = n->child; nn; nn = nn->next) {
if (strcmp(nn->value, "exec") == 0) {
_run_on_resolve_exec(r, nn->child);
} else if (strcmp(nn->value, "shell") == 0) {
_run_on_resolve_shell(r, nn->child);
}
}
}
static void _run_on_resolve(struct node_resolve *r) {
struct node *n = r->n;
struct _run_on_resolve_ctx *ctx = r->user_data;
if (ctx->fe) {
if (ctx->fe_consumed) { // Foreach variable is consumed by run
struct ulist flist_ = { .usize = sizeof(char*) };
struct ulist *flist = &ctx->consumes_foreach;
if (r->resolve_outdated.num) {
for (int i = 0; i < r->resolve_outdated.num; ++i) {
struct resolve_outdated *u = ulist_get(&r->resolve_outdated, i);
if (u->flags != 'f') {
flist = &ctx->consumes_foreach; // Fallback to all consumed
break;
}
char *path = pool_strdup(r->pool, u->path);
ulist_push(&flist_, &path);
flist = &flist_;
}
}
struct unit *unit = unit_peek();
for (int i = 0; i < flist->num; ++i) {
char *p, *fi = *(char**) ulist_get(flist, i);
if (n->flags & NODE_FLG_IN_CACHE) {
p = path_relativize_cwd(unit->cache_dir, fi, unit->cache_dir);
} else if (n->flags & NODE_FLG_IN_SRC) {
p = path_relativize_cwd(unit->dir, fi, unit->dir);
} else {
p = fi;
}
ctx->fe->value = p;
_run_on_resolve_do(r, n);
ctx->fe->value = 0;
if (p != fi) {
free(p);
}
}
ulist_destroy_keep(&flist_);
} else {
struct vlist_iter iter;
vlist_iter_init(ctx->fe->items, &iter);
while (vlist_iter_next(&iter)) {
char buf[iter.len + 1];
memcpy(buf, iter.item, iter.len);
buf[iter.len] = '\0';
ctx->fe->value = buf;
_run_on_resolve_do(r, n);
ctx->fe->value = 0;
}
}
} else {
_run_on_resolve_do(r, n);
}
struct deps deps;
int rc = deps_open(r->deps_path_tmp, 0, &deps);
if (rc) {
node_fatal(rc, n, "Failed to open dependency file: %s", r->deps_path_tmp);
}
node_add_unit_deps(n, &deps);
for (int i = 0; i < r->node_val_deps.num; ++i) {
struct node *nv = *(struct node**) ulist_get(&r->node_val_deps, i);
const char *val = node_value(nv);
if (val) {
deps_add(&deps, DEPS_TYPE_NODE_VALUE, 0, val, i);
}
}
for (int i = 0; i < ctx->consumes.num; ++i) {
const char *path = *(const char**) ulist_get(&ctx->consumes, i);
deps_add(&deps, DEPS_TYPE_FILE, 0, path, 0);
}
for (int i = 0; i < ctx->consumes_foreach.num; ++i) {
const char *path = *(const char**) ulist_get(&ctx->consumes_foreach, i);
deps_add(&deps, DEPS_TYPE_FILE, 'f', path, 0);
}
node_products_add_as_deps(n, &deps);
deps_close(&deps);
}
static bool _run_setup_foreach(struct node *n) {
struct node_foreach *fe = node_find_parent_foreach(n);
if (!fe) {
return false;
}
struct node *pn = node_find_direct_child(n, NODE_TYPE_BAG, "produces");
if (pn && pn->child) {
struct vlist_iter iter;
vlist_iter_init(fe->items, &iter);
while (vlist_iter_next(&iter)) {
char buf[iter.len + 1];
memcpy(buf, iter.item, iter.len);
buf[iter.len] = '\0';
fe->value = buf;
for (struct node *nn = pn->child; nn; nn = nn->next) {
if (node_is_can_be_value(nn)) {
const char *value = node_value(nn);
if (g_env.verbose) {
node_info(n, "Product: %s", value);
}
node_product_add(n, value, 0);
}
}
fe->value = 0;
}
}
return true;
}
static void _run_setup(struct node *n) {
if (_run_setup_foreach(n)) {
return;
}
struct node *nn = node_find_direct_child(n, NODE_TYPE_BAG, "produces");
if (nn && nn->child) {
for (nn = nn->child; nn; nn = nn->next) {
if (node_is_can_be_value(nn)) {
const char *value = node_value(nn);
if (g_env.verbose) {
node_info(n, "Product: %s", value);
}
node_product_add(n, value, 0);
}
}
}
}
static void _run_on_consumed_resolved(const char *path_, void *d) {
struct _run_on_resolve_ctx *ctx = d;
const char *path = pool_strdup(ctx->r->pool, path_);
ulist_push(&ctx->consumes, &path);
}
static void _run_on_consumed_resolved_foreach(const char *path_, void *d) {
struct _run_on_resolve_ctx *ctx = d;
const char *path = pool_strdup(ctx->r->pool, path_);
ulist_push(&ctx->consumes_foreach, &path);
}
static void _run_on_resolve_init(struct node_resolve *r) {
struct _run_on_resolve_ctx *ctx = r->user_data;
ctx->r = r;
struct node *nn = node_find_direct_child(r->n, NODE_TYPE_BAG, "consumes");
if (ctx->fe) {
ctx->fe->value = 0;
ctx->fe->access_cnt = 0;
}
if (nn && nn->child) {
node_consumes_resolve(r->n, nn->child, 0, _run_on_consumed_resolved, ctx);
}
if (ctx->fe && ctx->fe->access_cnt > 0) {
ctx->fe_consumed = true;
struct vlist_iter iter;
struct ulist paths = { .usize = sizeof(char*) };
vlist_iter_init(ctx->fe->items, &iter);
while (vlist_iter_next(&iter)) {
char buf[iter.len + 1];
memcpy(buf, iter.item, iter.len);
char *p = pool_strndup(r->pool, buf, iter.len);
ulist_push(&paths, &p);
}
node_consumes_resolve(r->n, 0, &paths, _run_on_consumed_resolved_foreach, ctx);
ulist_destroy_keep(&paths);
}
}
static void _run_build(struct node *n) {
struct _run_on_resolve_ctx ctx = {
.consumes = { .usize = sizeof(char*) },
.consumes_foreach = { .usize = sizeof(char*) },
.fe = node_find_parent_foreach(n),
};
struct node_resolve r = {
.n = n,
.path = n->vfile,
.user_data = &ctx,
.on_init = _run_on_resolve_init,
.on_resolve = _run_on_resolve,
.node_val_deps = { .usize = sizeof(struct node*) },
};
r.force_outdated = n->post_build != 0
|| node_find_direct_child(n, NODE_TYPE_VALUE, "always") != 0;
for (struct node *nn = n->child; nn; nn = nn->next) {
if (strcmp(nn->value, "exec") == 0 || strcmp(nn->value, "shell") == 0) {
for (struct node *cn = nn->child; cn; cn = cn->next) {
if (node_is_value_may_be_dep_saved(cn, 0)) {
ulist_push(&r.node_val_deps, &cn);
}
}
}
}
node_resolve(&r);
ulist_destroy_keep(&ctx.consumes);
ulist_destroy_keep(&ctx.consumes_foreach);
}
int node_run_setup(struct node *n) {
if (strcmp("run-on-install", n->value) == 0) {
if (g_env.install.enabled) {
n->flags |= NODE_FLG_IN_CACHE;
n->setup = _run_setup;
n->post_build = _run_build;
}
} else {
n->flags |= NODE_FLG_IN_CACHE;
n->setup = _run_setup;
n->build = _run_build;
}
return 0;
}