Skip to content

Commit

Permalink
Tsify lib/compilers (#4609)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Jan 29, 2023
1 parent 87203f7 commit 6a6efae
Show file tree
Hide file tree
Showing 81 changed files with 829 additions and 493 deletions.
39 changes: 24 additions & 15 deletions lib/base-compiler.ts
Expand Up @@ -60,7 +60,7 @@ 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';
import {BaseDemangler, getDemanglerTypeByKey} from './demangler';
import {LLVMIRDemangler} from './demangler/llvm';
import * as exec from './exec';
import {getExternalParserByKey} from './external-parsers';
Expand Down Expand Up @@ -94,7 +94,7 @@ const executionTimeHistogram = new PromClient.Histogram({
});

export class BaseCompiler implements ICompiler {
protected compiler: CompilerInfo & Record<string, any>; // TODO: Some missing types still present in Compiler type
protected compiler: CompilerInfo; // TODO: Some missing types still present in Compiler type
public lang: Language;
protected compileFilename: string;
protected env: CompilationEnvironment;
Expand All @@ -111,7 +111,7 @@ export class BaseCompiler implements ICompiler {
protected toolchainPath: any;
public possibleArguments: CompilerArguments;
protected possibleTools: ITool[];
protected demanglerClass: any;
protected demanglerClass: typeof BaseDemangler | null = null;
protected objdumperClass: any;
public outputFilebase: string;
protected mtime: Date | null = null;
Expand All @@ -126,7 +126,7 @@ export class BaseCompiler implements ICompiler {
labelNames: [],
});

constructor(compilerInfo: CompilerInfo & Record<string, any>, env: CompilationEnvironment) {
constructor(compilerInfo: CompilerInfo, env: CompilationEnvironment) {
// Information about our compiler
this.compiler = compilerInfo;
this.lang = languages[compilerInfo.lang];
Expand Down Expand Up @@ -300,7 +300,7 @@ export class BaseCompiler implements ICompiler {
});
}

optOutputRequested(options) {
optOutputRequested(options: string[]) {
return options.includes('-fsave-optimization-record');
}

Expand Down Expand Up @@ -401,7 +401,7 @@ export class BaseCompiler implements ICompiler {
return !!this.objdumperClass;
}

getObjdumpOutputFilename(defaultOutputFilename) {
getObjdumpOutputFilename(defaultOutputFilename: string): string {
return defaultOutputFilename;
}

Expand Down Expand Up @@ -533,7 +533,7 @@ export class BaseCompiler implements ICompiler {
return outputFilename.replace(path.extname(outputFilename), '.dump');
}

getGccDumpOptions(gccDumpOptions, outputFilename) {
getGccDumpOptions(gccDumpOptions, outputFilename: string) {
const addOpts = ['-fdump-passes'];

// Build dump options to append to the end of the -fdump command-line flag.
Expand Down Expand Up @@ -756,7 +756,7 @@ export class BaseCompiler implements ICompiler {
}) as string[];
}

getSharedLibraryLinks(libraries): string[] {
getSharedLibraryLinks(libraries: any[]): string[] {
const linkFlag = this.compiler.linkFlag || '-l';

return _.flatten(
Expand Down Expand Up @@ -1248,7 +1248,7 @@ export class BaseCompiler implements ICompiler {
return [{text: 'Internal error; unable to open output path'}];
}

getIrOutputFilename(inputFilename: string, filters: ParseFiltersAndOutputOptions): string {
getIrOutputFilename(inputFilename: string, filters?: ParseFiltersAndOutputOptions): string {
return inputFilename.replace(path.extname(inputFilename), '.ll');
}

Expand All @@ -1267,7 +1267,7 @@ export class BaseCompiler implements ICompiler {
}
}

getExecutableFilename(dirPath, outputFilebase, key?) {
getExecutableFilename(dirPath: string, outputFilebase: string, key?) {
return this.getOutputFilename(dirPath, outputFilebase, key);
}

Expand Down Expand Up @@ -1436,7 +1436,7 @@ export class BaseCompiler implements ICompiler {
result.artifacts.push(artifact);
}

protected async writeMultipleFiles(files, dirPath) {
protected async writeMultipleFiles(files: any[], dirPath: string) {
const filesToWrite: Promise<void>[] = [];

for (const file of files) {
Expand All @@ -1449,7 +1449,12 @@ export class BaseCompiler implements ICompiler {
return Promise.all(filesToWrite);
}

protected async writeAllFiles(dirPath, source, files, filters: ParseFiltersAndOutputOptions) {
protected async writeAllFiles(
dirPath: string,
source: string,
files: any[],
filters: ParseFiltersAndOutputOptions,
) {
if (!source) throw new Error(`File ${this.compileFilename} has no content or file is missing`);

const inputFilename = path.join(dirPath, this.compileFilename);
Expand Down Expand Up @@ -1479,7 +1484,7 @@ export class BaseCompiler implements ICompiler {
};
}

async buildExecutableInFolder(key, dirPath): Promise<BuildResult> {
async buildExecutableInFolder(key, dirPath: string): Promise<BuildResult> {
const writeSummary = await this.writeAllFiles(dirPath, key.source, key.files, key.filters);
const downloads = await this.setupBuildEnvironment(key, dirPath, true);

Expand Down Expand Up @@ -2237,7 +2242,10 @@ export class BaseCompiler implements ICompiler {

if (!bypassCache) {
const cacheRetreiveTimeStart = process.hrtime.bigint();
const result = await this.env.cacheGet(key);
// TODO: We should be able to eliminate this any cast. `key` should be cacheable (if it's not that's a big
// problem) Because key coantains a CompilerInfo which contains a function member it can't be assigned to a
// CacheableValue.
const result = await this.env.cacheGet(key as any);
if (result) {
const cacheRetreiveTimeEnd = process.hrtime.bigint();
result.retreivedFromCacheTime = (
Expand Down Expand Up @@ -2762,7 +2770,7 @@ but nothing was dumped. Possible causes are:
return this.compiler;
}

getDefaultFilters() {
getDefaultFilters(): ParseFiltersAndOutputOptions {
return {
binary: false,
execute: false,
Expand All @@ -2774,6 +2782,7 @@ but nothing was dumped. Possible causes are:
optOutput: false,
libraryCode: false,
trim: false,
binaryObject: false,
};
}
}
File renamed without changes.
18 changes: 12 additions & 6 deletions lib/compilers/ada.ts
Expand Up @@ -25,9 +25,8 @@

import path from 'path';

import fs from 'fs-extra';

import {CompilationResult, ExecutionOptions} from '../../types/compilation/compilation.interfaces';
import {CompilerInfo} from '../../types/compiler.interfaces';
import {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces';
import {BaseCompiler} from '../base-compiler';
import * as utils from '../utils';

Expand All @@ -36,7 +35,7 @@ export class AdaCompiler extends BaseCompiler {
return 'ada';
}

constructor(info, env) {
constructor(info: CompilerInfo, env) {
super(info, env);
this.compiler.supportsGccDump = true;
this.compiler.removeEmptyGccDump = true;
Expand All @@ -45,7 +44,7 @@ export class AdaCompiler extends BaseCompiler {
this.compiler.supportsGnatDebugViews = true;
}

override getExecutableFilename(dirPath) {
override getExecutableFilename(dirPath: string, outputFilebase: string, key?) {
// The name here must match the value used in the pragma Source_File
// in the user provided source.
return path.join(dirPath, 'example');
Expand All @@ -70,7 +69,14 @@ export class AdaCompiler extends BaseCompiler {
}
}

override prepareArguments(userOptions, filters, backendOptions, inputFilename, outputFilename, libraries) {
override prepareArguments(
userOptions: string[],
filters: ParseFiltersAndOutputOptions,
backendOptions: Record<string, any>,
inputFilename: string,
outputFilename: string,
libraries,
) {
backendOptions = backendOptions || {};

// super call is needed as it handles the GCC Dump files.
Expand Down
Expand Up @@ -22,6 +22,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import {CompilerInfo} from '../../types/compiler.interfaces';
import {BaseCompiler} from '../base-compiler';

// Plain compiler, which just runs the tool and returns whatever the output was
Expand All @@ -30,20 +31,26 @@ export class AnalysisTool extends BaseCompiler {
return 'analysis-tool';
}

constructor(info, env) {
constructor(info: CompilerInfo, env) {
// Default is to disable all "cosmetic" filters
if (!info.disabledFilters) info.disabledFilters = ['labels', 'directives', 'commentOnly', 'trim'];
super(info, env);
}

getDefaultFilters() {
override getDefaultFilters() {
// Disable everything but intel syntax
return {
intel: true,
commentOnly: false,
directives: false,
labels: false,
optOutput: false,
binary: false,
execute: false,
demangle: false,
libraryCode: false,
trim: false,
binaryObject: false,
};
}
}
17 changes: 9 additions & 8 deletions lib/compilers/assembly.ts
Expand Up @@ -28,6 +28,7 @@ import path from 'path';
import _ from 'underscore';

import {BuildResult} from '../../types/compilation/compilation.interfaces';
import {CompilerInfo} from '../../types/compiler.interfaces';
import {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces';
import {BaseCompiler} from '../base-compiler';
import {AsmRaw} from '../parsers/asm-raw';
Expand All @@ -40,7 +41,7 @@ export class AssemblyCompiler extends BaseCompiler {
return 'assembly';
}

constructor(info, env) {
constructor(info: CompilerInfo, env) {
super(info, env);
this.asm = new AsmRaw();
}
Expand All @@ -53,12 +54,12 @@ export class AssemblyCompiler extends BaseCompiler {
return BaseParser;
}

override optionsForFilter(filters) {
override optionsForFilter(filters: ParseFiltersAndOutputOptions, outputFilename: string, userOptions?: string[]) {
filters.binary = true;
return [];
}

getGeneratedOutputFilename(fn) {
getGeneratedOutputFilename(fn: string) {
const outputFolder = path.dirname(fn);
const files = fs.readdirSync(outputFolder);

Expand All @@ -72,7 +73,7 @@ export class AssemblyCompiler extends BaseCompiler {
return outputFilename;
}

override getOutputFilename(dirPath) {
override getOutputFilename(dirPath: string) {
return this.getGeneratedOutputFilename(path.join(dirPath, 'example.asm'));
}

Expand Down Expand Up @@ -126,11 +127,11 @@ export class AssemblyCompiler extends BaseCompiler {
return this.doBuildstepAndAddToResult(fullResult, 'ld', this.env.ceProps('ld'), options, execOptions);
}

override getExecutableFilename(dirPath) {
override getExecutableFilename(dirPath: string) {
return path.join(dirPath, 'ce-asm-executable');
}

override async buildExecutableInFolder(key, dirPath): Promise<BuildResult> {
override async buildExecutableInFolder(key, dirPath: string): Promise<BuildResult> {
const buildEnvironment = this.setupBuildEnvironment(key, dirPath, true);

const writeSummary = await this.writeAllFiles(dirPath, key.source, key.files, key.filters);
Expand Down Expand Up @@ -178,11 +179,11 @@ export class AssemblyCompiler extends BaseCompiler {
return fullResult;
}

override checkOutputFileAndDoPostProcess(asmResult, outputFilename, filters) {
override checkOutputFileAndDoPostProcess(asmResult, outputFilename, filters: ParseFiltersAndOutputOptions) {
return this.postProcess(asmResult, outputFilename, filters);
}

override getObjdumpOutputFilename(defaultOutputFilename) {
override getObjdumpOutputFilename(defaultOutputFilename: string): string {
return this.getGeneratedOutputFilename(defaultOutputFilename);
}

Expand Down
16 changes: 12 additions & 4 deletions lib/compilers/avrgcc6502.ts
Expand Up @@ -24,6 +24,9 @@

import path from 'path';

import {ExecutionOptions} from '../../types/compilation/compilation.interfaces';
import {CompilerInfo} from '../../types/compiler.interfaces';
import {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces';
import {BaseCompiler} from '../base-compiler';

export class AvrGcc6502Compiler extends BaseCompiler {
Expand All @@ -35,7 +38,7 @@ export class AvrGcc6502Compiler extends BaseCompiler {
return 'avrgcc6502';
}

constructor(compilerInfo, env) {
constructor(compilerInfo: CompilerInfo, env) {
super(compilerInfo, env);

this.avrgccpath = this.compilerProps<string>(`compiler.${this.compiler.id}.avrgccpath`);
Expand All @@ -44,7 +47,7 @@ export class AvrGcc6502Compiler extends BaseCompiler {
this.outputFilebase = 'example';
}

public override getOutputFilename(dirPath, outputFilebase, key) {
public override getOutputFilename(dirPath: string, outputFilebase: string, key?: any) {
let filename;
if (key && key.backendOptions && key.backendOptions.customOutputFilename) {
filename = key.backendOptions.customOutputFilename;
Expand All @@ -59,11 +62,16 @@ export class AvrGcc6502Compiler extends BaseCompiler {
}
}

protected override optionsForFilter(filters: object, outputFilename: string): string[] {
protected override optionsForFilter(filters: ParseFiltersAndOutputOptions, outputFilename: string): string[] {
return [`-I${this.avrlibstdcpppath}`];
}

public override async runCompiler(compiler, options, inputFilename, execOptions) {
public override async runCompiler(
compiler: string,
options: string[],
inputFilename: string,
execOptions: ExecutionOptions,
) {
if (!execOptions) {
execOptions = this.getDefaultExecOptions();
}
Expand Down
11 changes: 9 additions & 2 deletions lib/compilers/beebasm.ts
Expand Up @@ -26,6 +26,8 @@ import path from 'path';

import fs from 'fs-extra';

import {ExecutionOptions} from '../../types/compilation/compilation.interfaces';
import {CompilerInfo} from '../../types/compiler.interfaces';
import {ArtifactType} from '../../types/tool.interfaces';
import {BaseCompiler} from '../base-compiler';
import {AsmParserBeebAsm} from '../parsers/asm-parser-beebasm';
Expand All @@ -36,7 +38,7 @@ export class BeebAsmCompiler extends BaseCompiler {
return 'beebasm';
}

constructor(compilerInfo, env) {
constructor(compilerInfo: CompilerInfo, env) {
super(compilerInfo, env);

this.asm = new AsmParserBeebAsm(this.compilerProps);
Expand All @@ -50,7 +52,12 @@ export class BeebAsmCompiler extends BaseCompiler {
return [];
}

override async runCompiler(compiler, options, inputFilename, execOptions) {
override async runCompiler(
compiler: string,
options: string[],
inputFilename: string,
execOptions: ExecutionOptions,
) {
if (!execOptions) {
execOptions = this.getDefaultExecOptions();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compilers/carbon.ts
Expand Up @@ -37,7 +37,7 @@ export class CarbonCompiler extends BaseCompiler {
return 'carbon';
}

constructor(compilerInfo: CompilerInfo & Record<string, any>, env) {
constructor(compilerInfo: CompilerInfo, env) {
super(compilerInfo, env);
this.compiler.demangler = '';
this.demanglerClass = null;
Expand Down

0 comments on commit 6a6efae

Please sign in to comment.