Debian Codegraph @

debian/tests/test_modules/llparse-test-fixture/src/fixture.ts

raw symbols SCIP
1 import { Buffer } from 'buffer';
2 import { spawn } from 'child_process';
3 import * as crypto from 'crypto';
4 import * as fs from 'fs';
5 import * as os from 'os';
6 import * as path from 'path';
7
8 import { FixtureResult, IFixtureResultOptions } from './result';
9
10 export { FixtureResult, IFixtureResultOptions };
11
12 const CLANG = process.env.CLANG || 'clang';
13 const CFLAGS = process.env.CFLAGS || '';
14
15 const NATIVE_DIR = path.join(__dirname, '..', 'src', 'native');
16 const FIXTURE = path.join(NATIVE_DIR, 'fixture.c');
17
18 export interface IFixtureOptions {
19 readonly buildDir: string;
20 readonly clang?: string;
21 readonly extra?: ReadonlyArray<string>;
22 readonly maxParallel?: number;
23 }
24
25 export interface IFixtureBuildOptions {
26 readonly extra?: ReadonlyArray<string>;
27 }
28
29 export interface IFixtureArtifacts {
30 readonly header: string;
31 readonly c: string;
32 }
33
34 interface IFixtureInternalOptions {
35 readonly buildDir: string;
36 readonly clang: string;
37 readonly extra: ReadonlyArray<string>;
38 readonly maxParallel: number;
39 }
40
41 // Just a random value, really
42 export const ERROR_PAUSE = 0x7fa73caa;
43
44 export class Fixture {
45 private readonly options: IFixtureInternalOptions;
46
47 constructor(options: IFixtureOptions) {
48 this.options = {
49 buildDir: options.buildDir,
50 clang: options.clang === undefined ? CLANG : options.clang,
51 extra: options.extra || [],
52 maxParallel: options.maxParallel === undefined ?
53 os.cpus().length : options.maxParallel,
54 };
55
56 try {
57 fs.mkdirSync(this.options.buildDir, { recursive: true });
58 } catch (e) {
59 // no-op
60 }
61 }
62
63 public async build(artifacts: IFixtureArtifacts, name: string,
64 options: IFixtureBuildOptions = {}): Promise<FixtureResult> {
65 const BUILD_DIR = this.options.buildDir;
66
67 const hash = crypto.createHash('sha256');
68
69 const c = path.join(BUILD_DIR, name + '.c');
70 const header = path.join(BUILD_DIR, name + '.h');
71
72 hash.update('header');
73 hash.update(artifacts.header);
74 await fs.promises.writeFile(header, artifacts.header);
75
76 const commonArgs = [
77 '-g3', '-Os', '-fvisibility=hidden',
78 '-I', NATIVE_DIR,
79 '-include', header,
80 FIXTURE,
81 ];
82
83 // This is rather lame, but should work
84 if (CFLAGS) {
85 for (const flag of CFLAGS.split(/\s+/g)) {
86 commonArgs.push(flag);
87 }
88 }
89
90 const args = {
91 c: [ '-I', BUILD_DIR ],
92 };
93
94 hash.update('c');
95 hash.update(artifacts.c);
96 await fs.promises.writeFile(c, artifacts.c);
97 args.c.push(c);
98
99 for (const extra of this.options.extra) {
100 commonArgs.push(extra);
101 }
102 if (options.extra) {
103 for (const extra of options.extra) {
104 commonArgs.push(extra);
105 }
106 }
107 hash.update('common-args');
108 hash.update(commonArgs.join(' '));
109
110 const digest = hash.digest('hex');
111
112 const executables: string[] = [];
113
114 const out = path.join(BUILD_DIR, name + '-c.' + digest);
115 const link = path.join(BUILD_DIR, name + '-c');
116 if (!fs.existsSync(out)) {
117 await this.clang(commonArgs.concat(args.c, '-o', out));
118 }
119 try {
120 await fs.promises.unlink(link);
121 } catch (e) {
122 // no-op
123 }
124 await fs.promises.link(out, link);
125 executables.push(out);
126
127 return new FixtureResult(executables, this.options.maxParallel);
128 }
129
130 private async clang(args: ReadonlyArray<string>): Promise<void> {
131 const proc = spawn(CLANG, args, {
132 stdio: [null, 'pipe', 'pipe'],
133 });
134
135 const stdout: Buffer[] = [];
136 proc.stdout.on('data', (chunk: Buffer) => stdout.push(chunk));
137 const stderr: Buffer[] = [];
138 proc.stderr.on('data', (chunk: Buffer) => stderr.push(chunk));
139
140 const code = await (new Promise((resolve) => {
141 proc.once('exit', (exitCode) => resolve(exitCode!));
142 }) as Promise<number>);
143
144 if (code !== 0) {
145 if (stdout.length > 0) {
146 process.stderr.write(Buffer.concat(stdout).toString());
147 }
148 if (stderr.length > 0) {
149 process.stderr.write(Buffer.concat(stderr).toString());
150 }
151
152 const escapedArgs = args.map((arg) => JSON.stringify(arg));
153 throw new Error('clang exit code: ' + code +
154 `\narguments: ${escapedArgs.join(' ')}`);
155 }
156 }
157 }