forked from termux/termux-packages
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheden.js
More file actions
executable file
·456 lines (440 loc) · 15.8 KB
/
Copy patheden.js
File metadata and controls
executable file
·456 lines (440 loc) · 15.8 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env node
/*
* vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=javascript
*/
"use strict";
const os = require('os');
const yargs = require('yargs');
const path = require('path');
const fs = require('fs');
const child_process = require('child_process');
const mkdirp = require('mkdirp');
const readline = require('readline');
const EventEmitter = require('events');
const util = require('util');
const outdir = path.join(__dirname, 'out');
const buildorder_script = path.join(__dirname, 'scripts', 'buildorder.py');
const build_script = path.join(__dirname, 'build-package.sh');
const argv = yargs
.usage('Usage: $0 [options] <pkgs>')
.option('verbose', {
alias: 'v',
type: 'count',
describe: 'Verbosity level: {-v -vv}'
})
.option('arch', {
alias: 'a',
type: 'string',
describe: 'Target architecture',
default: 'aarch64',
choices: [ 'aarch64', 'arm', 'i686', 'x86_64' ]
})
.option('jobs', {
alias: 'j',
type: 'number',
describe: 'Number of build jobs',
default: os.cpus().length,
nargs: 1
})
.help('help')
.alias('h', 'help')
.alias('V', 'version')
.argv;
function WARN() { argv.verbose >= 0 && console.log.apply(console, arguments); }
function INFO() { argv.verbose >= 1 && console.log.apply(console, arguments); }
function DEBUG() { argv.verbose >= 2 && console.log.apply(console, arguments); }
/* Will throw exception if buildorder.py fails */
function get_pkg_deps(pkgs) {
pkgs = Array.isArray(pkgs) ? pkgs : [ pkgs ];
return new Promise((resolve, reject) => {
let deps = '';
const script = child_process.spawn(buildorder_script, pkgs);
script.stdout.on('data', (data) => {
deps += data;
});
script.on('close', (code) => {
if (code) {
WARN('%s returned exit code %s for "%s"',
buildorder_script, code, pkgs);
return reject(code);
} else {
/* Remove last element, which is an empty line */
return resolve(deps.split('\n').slice(0, -1));
}
});
});
}
/* Does not throw (can be awaited without try/catch) */
function is_finished(pkg) {
return new Promise((resolve, reject) => {
fs.access(path.join(outdir, pkg, 'status', '10-finish'), fs.constants.F_OK, (err) => {
if (err) {
return resolve(false);
} else {
return resolve(true);
}
});
});
}
/* Will throw if get_pkg_deps (and fundamentally buildorder.py) fails.
* Does not attempt to catch and act upon the error */
async function has_satisfied_deps(pkg) {
const deps = await get_pkg_deps(pkg);
DEBUG("deps for " + pkg + ":");
DEBUG(deps);
/* Discard pkg (last element) from the deps array */
const answers = await Promise.all(deps.slice(0, -1).map(dep => is_finished(dep)));
return answers.reduce((acc, cur) => { return acc && cur; }, true);
}
async function get_a_job(pkg_pool) {
if (!pkg_pool.length) {
return null;
}
for (let i = 0; i < pkg_pool.length; i++) {
let pkg = pkg_pool[i];
DEBUG("get_a_job: Trying pkg " + pkg);
const statusdir = path.join(outdir, pkg, 'status');
/* Try to access statusdir.
* If access fails, it means we can pick it up and build it.
* If it does not fail, then skip it, because its build
* is either in progress, or finished already.
*/
const inProgress = await new Promise((resolve, reject) => {
fs.access(statusdir, fs.constants.F_OK, (err) => {
/* Could access means there is a build in progress */
if (err) {
return resolve(false);
} else {
return resolve(true);
}
});
});
if (inProgress) {
DEBUG("get_a_job: skipping pkg " + pkg);
continue;
}
const satisfied = await has_satisfied_deps(pkg);
DEBUG('does ' + pkg + ' have satisfied dependencies? ' + satisfied);
if (satisfied === false) {
DEBUG("get_a_job: unsatisfied deps for " + pkg);
continue;
}
/* pkg is ready to build. Do not hand it over until we
* can secure its exclusive ownership by creating
* its statusdir
*/
return await new Promise((resolve, reject) => {
mkdirp(statusdir, (err) => {
if (err) {
if (err.code === 'EEXIST') {
WARN("Continuing a previous build for " + pkg);
} else {
return reject(err);
}
}
INFO("get_a_job: selected " + pkg);
return resolve(pkg);
});
});
};
/* Bottom of the sack,
* haven't found any package to build */
DEBUG("resolving to null");
return null;
}
/*
* "this" bound to Task object
* Task: {
* worker_idx: Number,
* pkg: String,
* arch: String
* };
*/
async function build_pkg(cancel_token) {
const statusdir = path.join(outdir, this.pkg, "status");
this.inotify_proc = child_process.spawn('inotifywait',
['-m', '-e', 'close', '--format', '%f', statusdir],
{ detached: true });
/* stdout */
readline.createInterface({ input: this.inotify_proc.stdout })
.on('line', (line) => {
/* Suppress inotify events on the status directory itself */
if (!line) return;
if ([ "01-fetch-extract", "02-handle-hostbuild", "03-patch",
"04-configure", "05-make", "06-install", "07-massage",
"08-update-sysroot", "09-create-debfile", "10-finish" ]
.includes(line) === false) {
return;
}
this.emit('build-step', line);
if (line === "10-finish") {
this.emit('build-finished');
}
});
/* stderr */
readline.createInterface({ input: this.inotify_proc.stderr })
.on('line', (line) => {
if (line === "Watches established.") {
/* Avoid race condition. Wait until inotifywait places its watches. */
this.emit('build-can-start');
} else if (line !== "Setting up watches.") {
WARN("inotifywait stderr: " + line);
}
});
this.inotify_proc.on('error', (err) => {
WARN("inotifywait error: " + err);
});
this.inotify_proc.on('close', (code, signal) => {
if (code) {
this.error = new Error("inotifywait closed with error code " +
code + " from signal " + signal);
cancel_token(this);
}
});
/* Worded as such in order to serialize the code. Event emitted by
* the inotifywait process. */
await new Promise((resolve, reject) => {
this.on('build-can-start', () => {
DEBUG(this.pkg + ": Build can start");
resolve(true);
});
});
INFO("Worker " + this.worker_idx + " starting build for " + this.pkg);
this.build_proc = child_process.spawn('bash',
[ '-x', build_script, '-a', this.arch, this.pkg ],
{ detached: true });
this.build_in_progress = true;
/* Redirect stdout and stderr streams to log files */
const out_log = fs.createWriteStream(path.join(outdir, this.pkg, 'out.log'));
const err_log = fs.createWriteStream(path.join(outdir, this.pkg, 'err.log'));
this.build_proc.stdout.pipe(out_log);
this.build_proc.stderr.pipe(err_log);
this.build_proc.on('error', (err) => {
WARN(this.pkg + " build process error: " + err);
});
this.build_proc.on('close', (code, signal) => {
if (code) {
this.error = new Error("Build process for " + this.pkg +
" failed with error code " + code +
" from signal " + signal);
cancel_token(this);
} else {
this.emit('build-finished');
}
});
this.on('build-step', (step) => {
this.step = step;
WARN("[1;49;49m%d (PID %d):[0;49;49m %s: step %s",
this.worker_idx.toString().padEnd(2),
this.build_proc.pid.toString().padEnd(4),
this.pkg,
this.step);
});
await new Promise((resolve, reject) => {
this.on('build-finished', () => {
resolve(true);
});
});
this.build_in_progress = false;
/* Tear down the inotify watches */
this.inotify_proc.kill('SIGTERM');
/* Assume that the build process is already dead, since it
* successfully created the 10-finish file */
INFO("Worker " + this.worker_idx + " finished build for " + this.pkg);
return this;
}
/* Will throw if "rm -rf ${statusdir}" fails */
async function cleanup() {
//if (!this.build_in_progress) {
//return;
//};
WARN("%s: cleaning up build for %s", this.worker_idx, this.pkg);
this.build_proc.kill('SIGTERM');
this.inotify_proc.kill('SIGTERM');
await new Promise((resolve, reject) => {
const statusdir = path.join(outdir, this.pkg, 'status');
child_process.spawn("rm", [ "-rf", statusdir ])
.on('close', (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
if (this.step !== "00-pre-extract") {
return;
}
/*
* If extract step is not complete, there's a high chance we
* interrupted the build while it was downloading the source tarball.
* Don't take the risk to leave a corrupted tarball in the cache.
*/
await new Promise((resolve, reject) => {
const tmpdir = path.join(outdir, this.pkg, 'tmp');
child_process.spawn("rm", [ "-rf", tmpdir ])
.on('close', (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
}
function Task(worker_idx, pkg, arch) {
this.worker_idx = worker_idx;
this.pkg = pkg;
this.arch = arch;
this.build_pkg = build_pkg.bind(this);
this.cleanup = cleanup.bind(this);
this.build_in_progress = false;
}
util.inherits(Task, EventEmitter);
async function fill_task_pool(task_pool, workers, pkg_pool, arch, jobs, cancel_token) {
if (!pkg_pool.length) {
DEBUG("empty package pool");
return { task_pool: task_pool, workers: workers };
}
while (task_pool.length < jobs) {
const indices = task_pool.map(task => task.worker_idx);
let new_worker_idx;
for (new_worker_idx = 0; new_worker_idx < jobs; new_worker_idx++) {
if (!indices.includes(new_worker_idx)) {
break;
}
}
try {
const new_pkg = await get_a_job(pkg_pool);
if (!new_pkg) { break; }
const task = new Task(new_worker_idx, new_pkg, arch);
DEBUG("New task:");
DEBUG(task);
task_pool.push(task);
workers.push(task.build_pkg(cancel_token));
pkg_pool.splice(pkg_pool.indexOf(task.pkg), 1);
} catch (reason) {
WARN("Failed to assign new package to the task pool, reason: ", reason);
};
}
DEBUG("Task pool: ", task_pool);
return { task_pool: task_pool, workers: workers };
}
async function main(pkg_pool, arch, jobs) {
let task_pool, workers;
let interrupt_count = 0;
let forced_exit_requested;
let cancel_token;
/* task_canceler is a promise whose role is to break the
* Promise.race() in case of exceptional situations, such as
* forced termination requested by the user, or exceptions
* thrown by the worker tasks
*/
//let task_canceler;
const task_canceler = new Promise((resolve, reject) => {
//task_canceler = new Promise((resolve, reject) => {
cancel_token = reject;
/* Set up signal handler */
const signal_handler = function(signal) {
WARN('Received signal %s', signal);
interrupt_count++;
if (interrupt_count === 1) {
WARN("Waiting for tasks to finish. Ctrl-c again to force termination.");
} else {
WARN("[1;31;49mForced termination requested, exiting...[0;49;49m");
resolve({ forced_exit_requested: true});
}
};
process.on('SIGINT', () => { signal_handler('SIGINT'); });
process.on('SIGTERM', () => { signal_handler('SIGTERM'); });
});
task_canceler.then((result) => {
forced_exit_requested = result;
}).catch((task) => {
WARN("[1;31;49mBuild failed for task %s.[0;49;49m View the %s file for more details.",
task.pkg, path.join(outdir, task.pkg, 'err.log'));
//process.exit(1);
});
INFO("Building the following packets:");
INFO(pkg_pool);
({ task_pool, workers } = await fill_task_pool([], [], pkg_pool, arch, jobs, cancel_token));
while (pkg_pool.length || task_pool.length) {
if (!task_pool.length) {
WARN("Following packages left to build: ", pkg_pool);
WARN("But task pool is empty (no task can get a hold of a job). Exiting");
break;
}
DEBUG("Workers: ", workers);
/* Append to the workers pool a promise that resolves only when
* (and if) forced termination is requested (two SIGTERM or SIGINT
* signals).
*/
//let task;
try {
var task = await Promise.race(workers.concat(task_canceler));
} catch (failed_task) {
WARN("Failed task caught: ", failed_task.pkg, failed_task.error);
await failed_task.cleanup();
if (!interrupt_count) {
interrupt_count = 1;
}
}
/* Act upon the forced termination signal by cleaning up
* the running workers' space
*/
if (task === forced_exit_requested) {
try {
await Promise.all(task_pool.map((task) => { return task.cleanup(); }));
} catch (err) {
WARN("[1;31;49mException while cleaning up: [0;49;49m", err);
}
return false;
}
WARN("Build completed for %s", task.pkg);
workers.splice(task_pool.indexOf(task), 1);
task_pool.splice(task_pool.indexOf(task), 1);
if (interrupt_count === 1) {
/* Do not refill task pool */
continue;
}
({ task_pool, workers } = await fill_task_pool(task_pool, workers, pkg_pool, arch, jobs, cancel_token));
DEBUG("Package pool: ", pkg_pool);
}
return (pkg_pool.length === 0);
}
process.on('unhandledRejection', (err) => {
WARN("Unhandled rejection: ", err);
process.exit(1);
});
fs.mkdir(outdir, 0o755, async (err) => {
if (err && err.code !== 'EEXIST') {
console.log(err);
process.exit(1);
}
try {
let pkg_args = argv._.map((pkg) => {
try {
fs.accessSync(pkg);
} catch (e) {
return "packages/" + pkg;
}
return pkg;
});
let pkg_pool = await get_pkg_deps(argv);
console.log(pkg_pool);
const finished = await Promise.all(pkg_pool.map((pkg) => is_finished(pkg)));
pkg_pool = pkg_pool.filter((pkg, index) => { return !finished[index]; });
INFO(pkg_pool);
const rc = await main(pkg_pool, argv.arch, argv.jobs);
if (rc === true) {
WARN("Build finished successfully");
process.exit(0);
} else {
WARN("Build failed");
process.exit(1);
}
} catch (err) {
WARN("Top-level error caught: ", err);
process.exit(1);
}
});