diff options
author | Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> | 2023-06-28 19:13:10 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-28 20:13:10 -0400 |
commit | 4260313e13aa3ec057806979dfac3fe6fd856b19 (patch) | |
tree | 1c4ef9c1106daef53ba7c45bb0150aeca816b1b6 /lib/base-compiler.ts | |
parent | 55cd371404c956554cafc329250e073b252118dc (diff) | |
download | compiler-explorer-4260313e13aa3ec057806979dfac3fe6fd856b19.tar.gz compiler-explorer-4260313e13aa3ec057806979dfac3fe6fd856b19.zip |
Common utilities and type work (#5200)gh-8021
This PR refactors some common utilities out of lib/ and into shared/ and
eliminates some use of underscore.js, as well as general type
improvements done along the way.
Diffstat (limited to 'lib/base-compiler.ts')
-rw-r--r-- | lib/base-compiler.ts | 190 |
1 files changed, 105 insertions, 85 deletions
diff --git a/lib/base-compiler.ts b/lib/base-compiler.ts index 0f69187cb..0569481f6 100644 --- a/lib/base-compiler.ts +++ b/lib/base-compiler.ts @@ -37,6 +37,7 @@ import { CompilationCacheKey, CompilationInfo, CompilationResult, + CompileChildLibraries, CustomInputForTool, ExecutionOptions, bypassCompilationCache, @@ -105,6 +106,8 @@ import { } from '../types/compilation/compiler-overrides.interfaces.js'; import {LLVMIrBackendOptions} from '../types/compilation/ir.interfaces.js'; import {ParsedAsmResultLine} from '../types/asmresult/asmresult.interfaces.js'; +import {unique} from '../shared/common-utils.js'; +import {ClientOptionsType, OptionsHandlerLibrary, VersionInfo} from './options-handler.js'; const compilationTimeHistogram = new PromClient.Histogram({ name: 'ce_base_compiler_compilation_duration_seconds', @@ -261,8 +264,8 @@ export class BaseCompiler implements ICompiler { this.packager = new Packager(); } - copyAndFilterLibraries(allLibraries, filter) { - const filterLibAndVersion = _.map(filter, lib => { + copyAndFilterLibraries(allLibraries: Record<string, OptionsHandlerLibrary>, filter: string[]) { + const filterLibAndVersion = filter.map(lib => { const match = lib.match(/([\w-]*)\.([\w-]*)/i); if (match) { return { @@ -296,7 +299,7 @@ export class BaseCompiler implements ICompiler { } return true; - }); + }) as Record<string, VersionInfo>; copiedLibraries[libid] = libcopy; }); @@ -304,7 +307,7 @@ export class BaseCompiler implements ICompiler { return copiedLibraries; } - getSupportedLibraries(supportedLibrariesArr, allLibs) { + getSupportedLibraries(supportedLibrariesArr: string[], allLibs: Record<string, OptionsHandlerLibrary>) { if (supportedLibrariesArr.length > 0) { return this.copyAndFilterLibraries(allLibs, supportedLibrariesArr); } @@ -748,15 +751,15 @@ export class BaseCompiler implements ICompiler { }; } - getSortedStaticLibraries(libraries) { + getSortedStaticLibraries(libraries: CompileChildLibraries[]) { const dictionary = {}; - const links = _.uniq( - _.flatten( - _.map(libraries, selectedLib => { + const links = unique( + libraries + .map(selectedLib => { const foundVersion = this.findLibVersion(selectedLib); if (!foundVersion) return false; - return _.map(foundVersion.staticliblink, lib => { + return foundVersion.staticliblink.map(lib => { if (lib) { dictionary[lib] = foundVersion; return [lib, foundVersion.dependencies]; @@ -764,106 +767,105 @@ export class BaseCompiler implements ICompiler { return false; } }); - }), - ), + }) + .flat(3), ); const sortedlinks: string[] = []; - _.each(links, libToInsertName => { - const libToInsertObj = dictionary[libToInsertName]; + for (const libToInsertName of links) { + if (libToInsertName) { + const libToInsertObj = dictionary[libToInsertName]; - let idxToInsert = sortedlinks.length; - for (const [idx, libCompareName] of sortedlinks.entries()) { - const libCompareObj: LibraryVersion = dictionary[libCompareName]; + let idxToInsert = sortedlinks.length; + for (const [idx, libCompareName] of sortedlinks.entries()) { + const libCompareObj: LibraryVersion = dictionary[libCompareName]; - if ( - libToInsertObj && - libCompareObj && - _.intersection(libToInsertObj.dependencies, libCompareObj.staticliblink).length > 0 - ) { - idxToInsert = idx; - break; - } else if (libToInsertObj && libToInsertObj.dependencies.includes(libCompareName)) { - idxToInsert = idx; - break; - } else if (libCompareObj && libCompareObj.dependencies.includes(libToInsertName)) { - continue; - } else if ( - libToInsertObj && - libToInsertObj.staticliblink.includes(libToInsertName) && - libToInsertObj.staticliblink.includes(libCompareName) - ) { if ( - libToInsertObj.staticliblink.indexOf(libToInsertName) > - libToInsertObj.staticliblink.indexOf(libCompareName) + libToInsertObj && + libCompareObj && + _.intersection(libToInsertObj.dependencies, libCompareObj.staticliblink).length > 0 ) { - continue; - } else { idxToInsert = idx; - } - break; - } else if ( - libCompareObj && - libCompareObj.staticliblink.includes(libToInsertName) && - libCompareObj.staticliblink.includes(libCompareName) - ) { - if ( - libCompareObj.staticliblink.indexOf(libToInsertName) > - libCompareObj.staticliblink.indexOf(libCompareName) - ) { - continue; - } else { + break; + } else if (libToInsertObj && libToInsertObj.dependencies.includes(libCompareName)) { idxToInsert = idx; + break; + } else if (libCompareObj && libCompareObj.dependencies.includes(libToInsertName)) { + continue; + } else if ( + libToInsertObj && + libToInsertObj.staticliblink.includes(libToInsertName) && + libToInsertObj.staticliblink.includes(libCompareName) + ) { + if ( + libToInsertObj.staticliblink.indexOf(libToInsertName) > + libToInsertObj.staticliblink.indexOf(libCompareName) + ) { + continue; + } else { + idxToInsert = idx; + } + break; + } else if ( + libCompareObj && + libCompareObj.staticliblink.includes(libToInsertName) && + libCompareObj.staticliblink.includes(libCompareName) + ) { + if ( + libCompareObj.staticliblink.indexOf(libToInsertName) > + libCompareObj.staticliblink.indexOf(libCompareName) + ) { + continue; + } else { + idxToInsert = idx; + } + break; } - break; } - } - if (idxToInsert < sortedlinks.length) { - sortedlinks.splice(idxToInsert, 0, libToInsertName); - } else { - sortedlinks.push(libToInsertName); + if (idxToInsert < sortedlinks.length) { + sortedlinks.splice(idxToInsert, 0, libToInsertName); + } else { + sortedlinks.push(libToInsertName); + } } - }); + } return sortedlinks; } - getStaticLibraryLinks(libraries) { + getStaticLibraryLinks(libraries: CompileChildLibraries[]) { const linkFlag = this.compiler.linkFlag || '-l'; - return _.map(this.getSortedStaticLibraries(libraries), lib => { - if (lib) { - return linkFlag + lib; - } else { - return false; - } - }) as string[]; + return this.getSortedStaticLibraries(libraries) + .filter(lib => lib) + .map(lib => linkFlag + lib); } - getSharedLibraryLinks(libraries: any[]): string[] { + getSharedLibraryLinks(libraries: CompileChildLibraries[]): string[] { const linkFlag = this.compiler.linkFlag || '-l'; - return _.flatten( - _.map(libraries, selectedLib => { + return libraries + .map(selectedLib => { const foundVersion = this.findLibVersion(selectedLib); if (!foundVersion) return false; - return _.map(foundVersion.liblink, lib => { + return foundVersion.liblink.map(lib => { if (lib) { return linkFlag + lib; } else { return false; } }); - }), - ) as string[]; + }) + .flat() + .filter(link => link) as string[]; } - getSharedLibraryPaths(libraries) { - return _.flatten( - _.map(libraries, selectedLib => { + getSharedLibraryPaths(libraries: CompileChildLibraries[]) { + return libraries + .map(selectedLib => { const foundVersion = this.findLibVersion(selectedLib); if (!foundVersion) return false; @@ -872,11 +874,15 @@ export class BaseCompiler implements ICompiler { paths.push(`/app/${selectedLib.id}/lib`); } return paths; - }), - ) as string[]; + }) + .flat(); } - protected getSharedLibraryPathsAsArguments(libraries, libDownloadPath?: string, toolchainPath?: string) { + protected getSharedLibraryPathsAsArguments( + libraries: CompileChildLibraries[], + libDownloadPath?: string, + toolchainPath?: string, + ) { const pathFlag = this.compiler.rpathFlag || '-Wl,-rpath,'; const libPathFlag = this.compiler.libpathFlag || '-L'; @@ -1058,7 +1064,7 @@ export class BaseCompiler implements ICompiler { backendOptions: Record<string, any>, inputFilename: string, outputFilename: string, - libraries, + libraries: CompileChildLibraries[], overrides: ConfiguredOverrides, ) { let options = this.optionsForFilter(filters, outputFilename, userOptions); @@ -1086,9 +1092,9 @@ export class BaseCompiler implements ICompiler { let staticLibLinks: string[] = []; if (filters.binary) { - libLinks = this.getSharedLibraryLinks(libraries) || []; + libLinks = (this.getSharedLibraryLinks(libraries).filter(l => l) as string[]) || []; libPaths = this.getSharedLibraryPathsAsArguments(libraries, undefined, toolchainPath); - staticLibLinks = this.getStaticLibraryLinks(libraries) || []; + staticLibLinks = (this.getStaticLibraryLinks(libraries).filter(l => l) as string[]) || []; } userOptions = this.filterUserOptions(userOptions) || []; @@ -2041,7 +2047,16 @@ export class BaseCompiler implements ICompiler { } } - async doCompilation(inputFilename, dirPath, key, options, filters, backendOptions, libraries, tools) { + async doCompilation( + inputFilename, + dirPath, + key, + options, + filters, + backendOptions, + libraries: CompileChildLibraries[], + tools, + ) { const inputFilenameSafe = this.filename(inputFilename); const outputFilename = this.getOutputFilename(dirPath, this.outputFilebase, key); @@ -2565,7 +2580,7 @@ export class BaseCompiler implements ICompiler { bypassCache: BypassCache, tools, executionParameters, - libraries, + libraries: CompileChildLibraries[], files, ) { const optionsError = this.checkOptions(options); @@ -3093,8 +3108,13 @@ but nothing was dumped. Possible causes are: } } - initialiseLibraries(clientOptions) { - this.supportedLibraries = this.getSupportedLibraries(this.compiler.libsArr, clientOptions.libs[this.lang.id]); + initialiseLibraries(clientOptions: ClientOptionsType) { + // TODO: Awful cast here because of OptionsHandlerLibrary vs Library. These might really be the same types and + // OptionsHandlerLibrary should maybe be yeeted. + this.supportedLibraries = this.getSupportedLibraries( + this.compiler.libsArr, + clientOptions.libs[this.lang.id], + ) as any as Record<string, Library>; } async getTargetsAsOverrideValues(): Promise<CompilerOverrideOption[]> { @@ -3178,7 +3198,7 @@ but nothing was dumped. Possible causes are: return this.env.getPossibleToolchains(); } - async initialise(mtime: Date, clientOptions, isPrediscovered = false) { + async initialise(mtime: Date, clientOptions: ClientOptionsType, isPrediscovered = false) { this.mtime = mtime; if (this.buildenvsetup) { |