Tsify compilation-env

This commit is contained in:
Jeremy
2023-01-24 00:54:54 -05:00
parent 526ea9590e
commit 4cf2be8f66
6 changed files with 44 additions and 21 deletions
+3
View File
@@ -162,6 +162,9 @@ rules:
unicorn/prefer-switch: off
unicorn/no-static-only-class: off
unicorn/no-process-exit: off
unicorn/no-useless-undefined:
- error
- checkArguments: false
'@typescript-eslint/no-empty-function': off
'@typescript-eslint/no-unused-vars': off
'@typescript-eslint/no-explicit-any': off
+8 -7
View File
@@ -57,6 +57,7 @@ import {Artifact, ToolResult, ToolTypeKey} from '../types/tool.interfaces';
import {BuildEnvSetupBase, getBuildEnvTypeByKey} from './buildenvsetup';
import {BuildEnvDownloadInfo} from './buildenvsetup/buildenv.interfaces';
import * as cfg from './cfg';
import {CompilationEnvironment} from './compilation-env';
import {CompilerArguments} from './compiler-arguments';
import {ClangParser, GCCParser} from './compilers/argument-parsers';
import {getDemanglerTypeByKey} from './demangler';
@@ -84,7 +85,7 @@ export class BaseCompiler implements ICompiler {
protected compiler: CompilerInfo & Record<string, any>; // TODO: Some missing types still present in Compiler type
public lang: Language;
protected compileFilename: string;
protected env: any;
protected env: CompilationEnvironment;
protected compilerProps: PropertyGetter;
protected alwaysResetLdPath: any;
protected delayCleanupTemp: any;
@@ -113,7 +114,7 @@ export class BaseCompiler implements ICompiler {
labelNames: [],
});
constructor(compilerInfo: CompilerInfo & Record<string, any>, env) {
constructor(compilerInfo: CompilerInfo & Record<string, any>, env: CompilationEnvironment) {
// Information about our compiler
this.compiler = compilerInfo;
this.lang = languages[compilerInfo.lang];
@@ -123,7 +124,7 @@ export class BaseCompiler implements ICompiler {
this.compileFilename = `example${this.lang.extensions[0]}`;
this.env = env;
// Partial application of compilerProps with the proper language id applied to it
this.compilerProps = _.partial(this.env.compilerProps, this.lang.id);
this.compilerProps = _.partial(this.env.compilerProps as any, this.lang.id);
this.compiler.supportsIntel = !!this.compiler.intelAsm;
this.alwaysResetLdPath = this.env.ceProps('alwaysResetLdPath');
@@ -316,12 +317,12 @@ export class BaseCompiler implements ICompiler {
}
const key = this.getCompilerCacheKey(compiler, args, options);
let result = await this.env.compilerCacheGet(key);
let result = await this.env.compilerCacheGet(key as any);
if (!result) {
result = await this.env.enqueue(async () => await exec.execute(compiler, args, options));
if (result.okToCache) {
this.env
.compilerCachePut(key, result)
.compilerCachePut(key as any, result, undefined)
.then(() => {
// Do nothing, but we don't await here.
})
@@ -2370,7 +2371,7 @@ export class BaseCompiler implements ICompiler {
result = this.postCompilationPreCacheHook(result);
if (result.okToCache) {
await this.env.cachePut(key, result);
await this.env.cachePut(key, result, undefined);
}
if (doExecute) {
@@ -2624,7 +2625,7 @@ but nothing was dumped. Possible causes are:
return result;
}
checkOptions(options) {
checkOptions(options: string[]) {
const error = this.env.findBadOptions(options);
if (error.length > 0) return `Bad options: ${error.join(', ')}`;
return null;
@@ -27,12 +27,31 @@ import child_process from 'child_process';
import fs from 'fs-extra';
import _ from 'underscore';
import {CacheableValue} from '../types/cache.interfaces';
import {BaseCache} from './cache/base';
import {Cache} from './cache/base.interfaces';
import {createCacheFromConfig} from './cache/from-config';
import {CompilationQueue, Job} from './compilation-queue';
import {FormattingHandler} from './handlers/formatting';
import {logger} from './logger';
import {CompilerProps} from './properties';
import {PropertyGetter} from './properties.interfaces';
export class CompilationEnvironment {
ceProps: PropertyGetter;
compilationQueue: CompilationQueue;
compilerProps: CompilerProps;
okOptions: RegExp;
badOptions: RegExp;
cache: Cache;
executableCache: Cache;
compilerCache: Cache;
reportCacheEvery: number;
multiarch: string | null;
baseEnv: Record<string, string | undefined>;
formatHandler: FormattingHandler;
constructor(compilerProps, compilationQueue, doCache) {
this.ceProps = compilerProps.ceProps;
this.compilationQueue = compilationQueue;
@@ -79,7 +98,7 @@ export class CompilationEnvironment {
this.formatHandler = new FormattingHandler(this.ceProps);
}
getEnv(needsMulti) {
getEnv(needsMulti: boolean) {
const env = {...this.baseEnv};
if (needsMulti && this.multiarch) {
env.LIBRARY_PATH = '/usr/lib/' + this.multiarch;
@@ -89,7 +108,7 @@ export class CompilationEnvironment {
return env;
}
async cacheGet(object) {
async cacheGet(object: CacheableValue) {
const result = await this.cache.get(BaseCache.hash(object));
if (this.cache.gets % this.reportCacheEvery === 0) {
this.cache.report();
@@ -98,12 +117,12 @@ export class CompilationEnvironment {
return JSON.parse(result.data);
}
async cachePut(object, result, creator) {
async cachePut(object: CacheableValue, result: object, creator: string | undefined) {
const key = BaseCache.hash(object);
return this.cache.put(key, JSON.stringify(result), creator);
}
async compilerCacheGet(object) {
async compilerCacheGet(object: CacheableValue) {
const key = BaseCache.hash(object);
const result = await this.compilerCache.get(key);
if (this.ceProps('logCompilerCacheAccesses', false)) {
@@ -113,12 +132,12 @@ export class CompilationEnvironment {
return JSON.parse(result.data);
}
async compilerCachePut(object, result, creator) {
async compilerCachePut(object: CacheableValue, result: object, creator: string | undefined) {
const key = BaseCache.hash(object);
return this.compilerCache.put(key, JSON.stringify(result), creator);
}
async executableGet(object, destinationFolder) {
async executableGet(object: CacheableValue, destinationFolder: string) {
const key = BaseCache.hash(object) + '_exec';
const result = await this.executableCache.get(key);
if (!result.hit) return null;
@@ -127,16 +146,16 @@ export class CompilationEnvironment {
return filepath;
}
executablePut(object, filepath) {
executablePut(object: CacheableValue, filepath: string) {
const key = BaseCache.hash(object) + '_exec';
return this.executableCache.put(key, fs.readFileSync(filepath));
}
enqueue(job) {
enqueue<T>(job: Job<T>) {
return this.compilationQueue.enqueue(job);
}
findBadOptions(options) {
return _.filter(options, option => !this.okOptions.test(option) || option.match(this.badOptions));
findBadOptions(options: string[]) {
return _.filter(options, option => !this.okOptions.test(option) || this.badOptions.test(option));
}
}
+1 -1
View File
@@ -42,7 +42,7 @@ const queueCompleted = new PromClient.Counter({
help: 'Total number of jobs completed',
});
type Job<TaskResultType> = () => PromiseLike<TaskResultType>;
export type Job<TaskResultType> = () => PromiseLike<TaskResultType>;
export class CompilationQueue {
private readonly _running: Set<number> = new Set();
+2 -2
View File
@@ -108,12 +108,12 @@ export class DosboxCompiler extends BaseCompiler {
}
const key = this.getCompilerCacheKey(compiler, args, options);
let result = await this.env.compilerCacheGet(key);
let result = await this.env.compilerCacheGet(key as any);
if (!result) {
result = await this.env.enqueue(async () => this.exec(compiler, args, options));
if (result.okToCache) {
this.env
.compilerCachePut(key, result)
.compilerCachePut(key as any, result, undefined)
.then(() => {
// Do nothing, but we don't await here.
})
+1 -1
View File
@@ -123,7 +123,7 @@ export class CompileHandler {
constructor(compilationEnvironment: CompilationEnvironment, awsProps: (name: string, def?: string) => string) {
this.compilerEnv = compilationEnvironment;
this.textBanner = this.compilerEnv.ceProps('textBanner');
this.textBanner = this.compilerEnv.ceProps<string>('textBanner');
this.proxy = Server.createProxyServer({});
this.awsProps = awsProps;
initialise(this.compilerEnv);