Debian Codegraph @

debian/tests/test_modules/llparse-test-fixture/lib/fixture.js

raw symbols SCIP
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Fixture = exports.ERROR_PAUSE = exports.FixtureResult = void 0;
4 const buffer_1 = require("buffer");
5 const child_process_1 = require("child_process");
6 const crypto = require("crypto");
7 const fs = require("fs");
8 const os = require("os");
9 const path = require("path");
10 const result_1 = require("./result");
11 Object.defineProperty(exports, "FixtureResult", { enumerable: true, get: function () { return result_1.FixtureResult; } });
12 const CLANG = process.env.CLANG || 'clang';
13 const CFLAGS = process.env.CFLAGS || '';
14 const NATIVE_DIR = path.join(__dirname, '..', 'src', 'native');
15 const FIXTURE = path.join(NATIVE_DIR, 'fixture.c');
16 // Just a random value, really
17 exports.ERROR_PAUSE = 0x7fa73caa;
18 class Fixture {
19 constructor(options) {
20 this.options = {
21 buildDir: options.buildDir,
22 clang: options.clang === undefined ? CLANG : options.clang,
23 extra: options.extra || [],
24 maxParallel: options.maxParallel === undefined ?
25 os.cpus().length : options.maxParallel,
26 };
27 try {
28 fs.mkdirSync(this.options.buildDir, { recursive: true });
29 }
30 catch (e) {
31 // no-op
32 }
33 }
34 async build(artifacts, name, options = {}) {
35 const BUILD_DIR = this.options.buildDir;
36 const hash = crypto.createHash('sha256');
37 const c = path.join(BUILD_DIR, name + '.c');
38 const header = path.join(BUILD_DIR, name + '.h');
39 hash.update('header');
40 hash.update(artifacts.header);
41 await fs.promises.writeFile(header, artifacts.header);
42 const commonArgs = [
43 '-g3', '-Os', '-fvisibility=hidden',
44 '-I', NATIVE_DIR,
45 '-include', header,
46 FIXTURE,
47 ];
48 // This is rather lame, but should work
49 if (CFLAGS) {
50 for (const flag of CFLAGS.split(/\s+/g)) {
51 commonArgs.push(flag);
52 }
53 }
54 const args = {
55 c: ['-I', BUILD_DIR],
56 };
57 hash.update('c');
58 hash.update(artifacts.c);
59 await fs.promises.writeFile(c, artifacts.c);
60 args.c.push(c);
61 for (const extra of this.options.extra) {
62 commonArgs.push(extra);
63 }
64 if (options.extra) {
65 for (const extra of options.extra) {
66 commonArgs.push(extra);
67 }
68 }
69 hash.update('common-args');
70 hash.update(commonArgs.join(' '));
71 const digest = hash.digest('hex');
72 const executables = [];
73 const out = path.join(BUILD_DIR, name + '-c.' + digest);
74 const link = path.join(BUILD_DIR, name + '-c');
75 if (!fs.existsSync(out)) {
76 await this.clang(commonArgs.concat(args.c, '-o', out));
77 }
78 try {
79 await fs.promises.unlink(link);
80 }
81 catch (e) {
82 // no-op
83 }
84 await fs.promises.link(out, link);
85 executables.push(out);
86 return new result_1.FixtureResult(executables, this.options.maxParallel);
87 }
88 async clang(args) {
89 const proc = child_process_1.spawn(CLANG, args, {
90 stdio: [null, 'pipe', 'pipe'],
91 });
92 const stdout = [];
93 proc.stdout.on('data', (chunk) => stdout.push(chunk));
94 const stderr = [];
95 proc.stderr.on('data', (chunk) => stderr.push(chunk));
96 const code = await new Promise((resolve) => {
97 proc.once('exit', (exitCode) => resolve(exitCode));
98 });
99 if (code !== 0) {
100 if (stdout.length > 0) {
101 process.stderr.write(buffer_1.Buffer.concat(stdout).toString());
102 }
103 if (stderr.length > 0) {
104 process.stderr.write(buffer_1.Buffer.concat(stderr).toString());
105 }
106 const escapedArgs = args.map((arg) => JSON.stringify(arg));
107 throw new Error('clang exit code: ' + code +
108 `\narguments: ${escapedArgs.join(' ')}`);
109 }
110 }
111 }
112 exports.Fixture = Fixture;
113 //# sourceMappingURL=fixture.js.map