Skip to content

Commit

Permalink
tsification, + small package upgrade (semver)
Browse files Browse the repository at this point in the history
  • Loading branch information
OfekShilon committed Sep 7, 2024
1 parent 9ef46fb commit 36a9abf
Show file tree
Hide file tree
Showing 38 changed files with 172 additions and 115 deletions.
38 changes: 18 additions & 20 deletions lib/base-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import {
import type {CompilerOutputOptions, ParseFiltersAndOutputOptions} from '../types/features/filters.interfaces.js';
import {InstructionSet} from '../types/instructionsets.js';
import type {Language} from '../types/languages.interfaces.js';
import type {Library, LibraryVersion, SelectedLibraryVersion} from '../types/libraries/libraries.interfaces.js';
import type {SelectedLibraryVersion} from '../types/libraries/libraries.interfaces.js';
import type {ResultLine} from '../types/resultline/resultline.interfaces.js';
import {type ToolResult, type ToolTypeKey} from '../types/tool.interfaces.js';

Expand Down Expand Up @@ -185,7 +185,7 @@ export class BaseCompiler implements ICompiler {
protected cmakeBaseEnv: Record<string, string>;
protected buildenvsetup: null | any;
protected externalparser: null | ExternalParserBase;
protected supportedLibraries?: Record<string, Library>;
protected supportedLibraries?: Record<string, OptionsHandlerLibrary>;
protected packager: Packager;
protected executionType: string;
protected sandboxType: string;
Expand Down Expand Up @@ -758,15 +758,15 @@ export class BaseCompiler implements ICompiler {
return options;
}

findLibVersion(selectedLib: SelectedLibraryVersion): false | LibraryVersion {
findLibVersion(selectedLib: SelectedLibraryVersion): false | VersionInfo {
if (!this.supportedLibraries) return false;

const foundLib = _.find(this.supportedLibraries, (o, libId) => libId === selectedLib.id);
if (!foundLib) return false;

const result: LibraryVersion | undefined = _.find(
const result: VersionInfo | undefined = _.find(
foundLib.versions,
(o: LibraryVersion, versionId: string): boolean => {
(o: VersionInfo, versionId: string): boolean => {
if (versionId === selectedLib.version) return true;
return !!(o.alias && o.alias.includes(selectedLib.version));
},
Expand All @@ -783,7 +783,7 @@ export class BaseCompiler implements ICompiler {
}

findAutodetectStaticLibLink(linkname: string): SelectedLibraryVersion | false {
const foundLib = _.findKey(this.supportedLibraries as Record<string, Library>, lib => {
const foundLib = _.findKey(this.supportedLibraries!, lib => {
return (
lib.versions.autodetect &&
lib.versions.autodetect.staticliblink &&
Expand All @@ -799,7 +799,7 @@ export class BaseCompiler implements ICompiler {
}

getSortedStaticLibraries(libraries: CompileChildLibraries[]) {
const dictionary = {};
const dictionary: Record<string, VersionInfo> = {};
const links = unique(
libraries
.map(selectedLib => {
Expand All @@ -826,7 +826,7 @@ export class BaseCompiler implements ICompiler {

let idxToInsert = sortedlinks.length;
for (const [idx, libCompareName] of sortedlinks.entries()) {
const libCompareObj: LibraryVersion = dictionary[libCompareName];
const libCompareObj: VersionInfo = dictionary[libCompareName];

if (
libToInsertObj &&
Expand Down Expand Up @@ -1719,16 +1719,16 @@ export class BaseCompiler implements ICompiler {
return maskedArgs;
}

async getRequiredLibraryVersions(libraries): Promise<Record<string, LibraryVersion>> {
const libraryDetails = {};
async getRequiredLibraryVersions(libraries): Promise<Record<string, VersionInfo>> {
const libraryDetails: Record<string, VersionInfo> = {};
_.each(libraries, selectedLib => {
const foundVersion = this.findLibVersion(selectedLib);
if (foundVersion) libraryDetails[selectedLib.id] = foundVersion;
});
return libraryDetails;
}

async setupBuildEnvironment(key: any, dirPath: string, binary: boolean): Promise<BuildEnvDownloadInfo[]> {
async setupBuildEnvironment(key: CacheKey, dirPath: string, binary: boolean): Promise<BuildEnvDownloadInfo[]> {
if (this.buildenvsetup) {
const libraryDetails = await this.getRequiredLibraryVersions(key.libraries);
return this.buildenvsetup.setup(key, dirPath, libraryDetails, binary);
Expand Down Expand Up @@ -1785,7 +1785,7 @@ export class BaseCompiler implements ICompiler {
};
}

async buildExecutableInFolder(key, dirPath: string): Promise<BuildResult> {
async buildExecutableInFolder(key: CacheKey, 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 @@ -1827,11 +1827,11 @@ export class BaseCompiler implements ICompiler {
});
}

async afterBuild(key, dirPath: string, buildResult: BuildResult): Promise<BuildResult> {
async afterBuild(key: CacheKey, dirPath: string, buildResult: BuildResult): Promise<BuildResult> {
return buildResult;
}

async getOrBuildExecutable(key, bypassCache: BypassCache) {
async getOrBuildExecutable(key: CacheKey, bypassCache: BypassCache) {
const dirPath = await this.newTempDir();

if (!bypassCompilationCache(bypassCache)) {
Expand Down Expand Up @@ -1864,7 +1864,7 @@ export class BaseCompiler implements ICompiler {
return compilationResult;
}

async loadPackageWithExecutable(key, dirPath) {
async loadPackageWithExecutable(key: CacheKey, dirPath: string) {
const compilationResultFilename = 'compilation-result.json';
try {
const startTime = process.hrtime.bigint();
Expand Down Expand Up @@ -1939,7 +1939,7 @@ export class BaseCompiler implements ICompiler {
executeParameters.args.unshift(outputFilename);
}

async handleInterpreting(key, executeParameters: ExecutableExecutionOptions): Promise<CompilationResult> {
async handleInterpreting(key: CacheKey, executeParameters: ExecutableExecutionOptions): Promise<CompilationResult> {
const source = key.source;
const dirPath = await this.newTempDir();
const outputFilename = this.getExecutableFilename(dirPath, this.outputFilebase);
Expand Down Expand Up @@ -1971,7 +1971,7 @@ export class BaseCompiler implements ICompiler {
}

async doExecution(
key,
key: CacheKey,
executeParameters: ExecutableExecutionOptions,
bypassCache: BypassCache,
): Promise<CompilationResult> {
Expand Down Expand Up @@ -3229,12 +3229,10 @@ but nothing was dumped. Possible causes are:
}

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[]> {
Expand Down
21 changes: 12 additions & 9 deletions lib/buildenvsetup/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import path from 'path';

import _ from 'underscore';

import {LibraryVersion} from '../../types/libraries/libraries.interfaces.js';
import {CacheKey} from '../../types/compilation/compilation.interfaces.js';
import {CompilerInfo} from '../../types/compiler.interfaces.js';
import {CompilationEnvironment} from '../compilation-env.js';
import {logger} from '../logger.js';
import {VersionInfo} from '../options-handler.js';
import * as utils from '../utils.js';

import type {BuildEnvDownloadInfo} from './buildenv.interfaces.js';
Expand All @@ -41,7 +44,7 @@ export class BuildEnvSetupBase {
public compilerSupportsX86: boolean;
public defaultLibCxx: string;

constructor(compilerInfo, env) {
constructor(compilerInfo: CompilerInfo, env: CompilationEnvironment) {
this.compiler = compilerInfo;
this.env = env;

Expand Down Expand Up @@ -99,9 +102,9 @@ export class BuildEnvSetupBase {
}

async setup(
key,
key: CacheKey,
dirPath: string,
selectedLibraries: Record<string, LibraryVersion>,
selectedLibraries: Record<string, VersionInfo>,
binary: boolean,
): Promise<BuildEnvDownloadInfo[]> {
return [];
Expand Down Expand Up @@ -143,7 +146,7 @@ export class BuildEnvSetupBase {
return false;
}

getLibcxx(key) {
getLibcxx(key: CacheKey): string {
const match = this.compiler.options.match(/-stdlib=(\S*)/i);
if (match) {
return match[1];
Expand All @@ -160,7 +163,7 @@ export class BuildEnvSetupBase {
}
}

getTarget(key): string {
getTarget(key: CacheKey): string {
if (!this.compilerSupportsX86) return '';
if (this.compilerArch) return this.compilerArch;

Expand All @@ -179,19 +182,19 @@ export class BuildEnvSetupBase {
return 'x86_64';
}

hasBinariesToLink(details: LibraryVersion) {
hasBinariesToLink(details: VersionInfo) {
return (
details.libpath.length === 0 &&
(details.staticliblink.length > 0 || details.liblink.length > 0) &&
details.version !== 'autodetect'
);
}

hasPackagedHeaders(details: LibraryVersion) {
hasPackagedHeaders(details: VersionInfo) {
return !!details.packagedheaders;
}

shouldDownloadPackage(details: LibraryVersion) {
shouldDownloadPackage(details: VersionInfo) {
return this.hasPackagedHeaders(details) || this.hasBinariesToLink(details);
}
}
2 changes: 1 addition & 1 deletion lib/buildenvsetup/ceconan-fortran.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class BuildEnvSetupCeConanFortranDirect extends BuildEnvSetupCeConanDirec
return 'ceconan-fortran';
}

override async getConanBuildProperties(key): Promise<ConanBuildProperties> {
override async getConanBuildProperties(/*key*/): Promise<ConanBuildProperties> {
const arch = this.getCompilerArch() || 'x86_64';
const libcxx = 'std';
const stdver = '';
Expand Down
36 changes: 24 additions & 12 deletions lib/buildenvsetup/ceconan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ import request from 'request';
import tar from 'tar-stream';
import _ from 'underscore';

import {LibraryVersion} from '../../types/libraries/libraries.interfaces.js';
import {CacheKey} from '../../types/compilation/compilation.interfaces.js';
import {CompilerInfo} from '../../types/compiler.interfaces.js';
import {logger} from '../logger.js';
import {VersionInfo} from '../options-handler.js';

import {BuildEnvSetupBase} from './base.js';
import type {BuildEnvDownloadInfo} from './buildenv.interfaces.js';
// import { CompilationEnvironment } from '../compilation-env.js';

export type ConanBuildProperties = {
os: string;
Expand All @@ -56,11 +59,11 @@ export class BuildEnvSetupCeConanDirect extends BuildEnvSetupBase {
return 'ceconan';
}

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

this.host = compilerInfo.buildenvsetup.props('host', false);
this.onlyonstaticliblink = compilerInfo.buildenvsetup.props('onlyonstaticliblink', false);
this.host = compilerInfo.buildenvsetup!.props('host', '');
this.onlyonstaticliblink = compilerInfo.buildenvsetup!.props('onlyonstaticliblink', '');
this.extractAllToRoot = false;

if (env.debug) request.debug = true;
Expand Down Expand Up @@ -121,7 +124,12 @@ export class BuildEnvSetupCeConanDirect extends BuildEnvSetupBase {
}
}

async downloadAndExtractPackage(libId, version, downloadPath, packageUrl): Promise<BuildEnvDownloadInfo> {
async downloadAndExtractPackage(
libId,
version,
downloadPath: string,
packageUrl: string,
): Promise<BuildEnvDownloadInfo> {
return new Promise((resolve, reject) => {
const startTime = process.hrtime.bigint();
const extract = tar.extract();
Expand Down Expand Up @@ -207,7 +215,7 @@ export class BuildEnvSetupCeConanDirect extends BuildEnvSetupBase {
});
}

async getConanBuildProperties(key): Promise<ConanBuildProperties> {
async getConanBuildProperties(key: CacheKey): Promise<ConanBuildProperties> {
const arch = this.getTarget(key);
const libcxx = this.getLibcxx(key);
const stdver = '';
Expand Down Expand Up @@ -239,7 +247,11 @@ export class BuildEnvSetupCeConanDirect extends BuildEnvSetupBase {
});
}

async download(key, dirPath, libraryDetails): Promise<BuildEnvDownloadInfo[]> {
async download(
key: CacheKey,
dirPath: string,
libraryDetails: Record<string, VersionInfo>,
): Promise<BuildEnvDownloadInfo[]> {
const allDownloads: Promise<BuildEnvDownloadInfo>[] = [];
const allLibraryBuilds: any = [];

Expand Down Expand Up @@ -285,18 +297,18 @@ export class BuildEnvSetupCeConanDirect extends BuildEnvSetupBase {
}

override async setup(
key,
dirPath,
libraryDetails: Record<string, LibraryVersion>,
binary,
key: CacheKey,
dirPath: string,
libraryDetails: Record<string, VersionInfo>,
binary: boolean,
): Promise<BuildEnvDownloadInfo[]> {
if (!this.host) return [];

if (this.onlyonstaticliblink && !binary) return [];

const librariesToDownload = _.pick(libraryDetails, details => {
return this.shouldDownloadPackage(details);
}) as Record<string, LibraryVersion>;
}) as Record<string, VersionInfo>;

return this.download(key, dirPath, librariesToDownload);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cache/multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {BaseCache} from './base.js';
export class MultiCache extends BaseCache {
private readonly upstream: BaseCache[];

constructor(cacheName, ...upstream) {
constructor(cacheName: string, ...upstream: any[]) {
super(cacheName, 'Multi', 'multi');
this.countersEnabled = false;
this.upstream = upstream;
Expand Down
2 changes: 1 addition & 1 deletion lib/cache/null.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {GetResult} from '../../types/cache.interfaces.js';
import {BaseCache} from './base.js';

export class NullCache extends BaseCache {
constructor(cacheName) {
constructor(cacheName: string) {
super(cacheName, 'Null', 'null');
}

Expand Down
11 changes: 9 additions & 2 deletions lib/cache/on-disk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ import {logger} from '../logger.js';
import {BaseCache} from './base.js';

// With thanks to https://gist.github.com/kethinov/6658166
function getAllFiles(root: string, dir?: string) {
type relFile = {name: string; fullPath: string};

function getAllFiles(root: string, dir?: string): Array<relFile> {
const actualDir = dir || root;
return fs.readdirSync(actualDir).reduce((files: Array<string>, file: string) => {
return fs.readdirSync(actualDir).reduce((files: Array<relFile>, file: string) => {
const fullPath = path.join(actualDir, file);
const name = path.relative(root, fullPath);
const isDirectory = fs.statSync(fullPath).isDirectory();
Expand Down Expand Up @@ -80,9 +82,14 @@ export class OnDiskCache extends BaseCache {
};
})
.filter(Boolean);

// Sort oldest first
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore filter(Boolean) should have sufficed but doesn't
info.sort((x, y) => x.sort - y.sort);
for (const i of info) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.cache.set(i.key, i.data);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cfg/cfg-parsers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class BaseCFGParser {
return null;
}

protected filterTextSection(data: AssemblyLine[]) {
protected filterTextSection(data: AssemblyLine[]): AssemblyLine[] {
let useCurrentSection = true;
const result: AssemblyLine[] = [];
for (const i in data) {
Expand Down
3 changes: 2 additions & 1 deletion lib/cfg/cfg-parsers/clang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export class ClangCFGParser extends BaseCFGParser {

override filterData(assembly: ResultLine[]) {
const jmpLabelRegex = /\.LBB\d+_\d+:/;
const isCode = x => x && x.text && (x.source !== null || jmpLabelRegex.test(x.text) || this.isFunctionName(x));
const isCode = (x: ResultLine) =>
x && x.text && (x.source !== null || jmpLabelRegex.test(x.text) || this.isFunctionName(x));

const removeComments = (x: ResultLine) => {
const pos_x86 = x.text.indexOf('# ');
Expand Down
Loading

0 comments on commit 36a9abf

Please sign in to comment.