From 7a5bc100e460bab833366e9b7f82c843fc686b2c Mon Sep 17 00:00:00 2001 From: dongchao Date: Fri, 13 Jun 2025 00:04:47 +0800 Subject: [PATCH] Fix driver codecheck Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICEUHH Signed-off-by: dongchao Change-Id: I0ad334d2a64931bcb2802fd29a7a93a4474c2250 --- .../build_system/src/build/base_mode.ts | 167 +++++++++--------- 1 file changed, 83 insertions(+), 84 deletions(-) diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index 97e25efc5d..027e8cf3bd 100644 --- a/ets2panda/driver/build_system/src/build/base_mode.ts +++ b/ets2panda/driver/build_system/src/build/base_mode.ts @@ -24,10 +24,7 @@ import cluster, { Cluster, Worker, } from 'cluster'; -import { - Worker as ThreadWorker, - workerData -} from 'worker_threads'; +import { Worker as ThreadWorker } from 'worker_threads'; import { ABC_SUFFIX, ARKTSCONFIG_JSON_FILE, @@ -39,7 +36,6 @@ import { MERGED_ABC_FILE, TS_SUFFIX, DEPENDENCY_INPUT_FILE, - PROJECT_BUILD_CONFIG_FILE } from '../pre_define'; import { changeDeclgenFileExtension, @@ -73,46 +69,47 @@ import { } from '../types'; import { ArkTSConfigGenerator } from './generate_arktsconfig'; import { SetupClusterOptions } from '../types'; -import { create } from 'domain'; -import { emitKeypressEvents } from 'readline'; + export abstract class BaseMode { - buildConfig: BuildConfig; - entryFiles: Set; - allFiles: Map; - compileFiles: Map; - outputDir: string; - cacheDir: string; - pandaSdkPath: string; - buildSdkPath: string; - packageName: string; - sourceRoots: string[]; - moduleRootPath: string; - moduleType: string; - dependentModuleList: DependentModuleConfig[]; - moduleInfos: Map; - mergedAbcFile: string; - dependencyJsonFile: string; - abcLinkerCmd: string[]; - dependencyAnalyzerCmd: string[]; - logger: Logger; - isDebug: boolean; - enableDeclgenEts2Ts: boolean; - declgenV1OutPath: string | undefined; - declgenV2OutPath: string | undefined; - declgenBridgeCodePath: string | undefined; - hasMainModule: boolean; - abcFiles: Set; - hashCache: Record; - hashCacheFile: string; - isCacheFileExists: boolean; - dependencyFileMap: DependencyFileConfig | null; - isBuildConfigModified: boolean | undefined; - hasCleanWorker: boolean; - byteCodeHar: boolean; + public buildConfig: BuildConfig; + public entryFiles: Set; + public allFiles: Map; + public compileFiles: Map; + public outputDir: string; + public cacheDir: string; + public pandaSdkPath: string; + public buildSdkPath: string; + public packageName: string; + public sourceRoots: string[]; + public moduleRootPath: string; + public moduleType: string; + public dependentModuleList: DependentModuleConfig[]; + public moduleInfos: Map; + public mergedAbcFile: string; + public dependencyJsonFile: string; + public abcLinkerCmd: string[]; + public dependencyAnalyzerCmd: string[]; + public logger: Logger; + public isDebug: boolean; + public enableDeclgenEts2Ts: boolean; + public declgenV1OutPath: string | undefined; + public declgenV2OutPath: string | undefined; + public declgenBridgeCodePath: string | undefined; + public hasMainModule: boolean; + public abcFiles: Set; + public hashCacheFile: string; + public isCacheFileExists: boolean; + public hashCache: Record; + public dependencyFileMap: DependencyFileConfig | null; + public isBuildConfigModified: boolean | undefined; + public hasCleanWorker: boolean; + public byteCodeHar: boolean; constructor(buildConfig: BuildConfig) { this.buildConfig = buildConfig; this.entryFiles = new Set(buildConfig.compileFiles as string[]); + this.allFiles = new Map(); + this.compileFiles = new Map(); this.outputDir = buildConfig.loaderOutPath as string; this.cacheDir = buildConfig.cachePath as string; this.pandaSdkPath = buildConfig.pandaSdkPath as string; @@ -121,50 +118,45 @@ export abstract class BaseMode { this.sourceRoots = buildConfig.sourceRoots as string[]; this.moduleRootPath = buildConfig.moduleRootPath as string; this.moduleType = buildConfig.moduleType as string; - this.byteCodeHar = buildConfig.byteCodeHar as boolean; this.dependentModuleList = buildConfig.dependentModuleList; + this.moduleInfos = new Map(); + this.mergedAbcFile = path.resolve(this.outputDir, MERGED_ABC_FILE); + this.dependencyJsonFile = path.resolve(this.cacheDir, DEPENDENCY_JSON_FILE); + this.abcLinkerCmd = ['"' + this.buildConfig.abcLinkerPath + '"']; + this.dependencyAnalyzerCmd = ['"' + this.buildConfig.dependencyAnalyzerPath + '"']; + this.logger = Logger.getInstance(); this.isDebug = buildConfig.buildMode as string === BUILD_MODE.DEBUG; + this.enableDeclgenEts2Ts = buildConfig.enableDeclgenEts2Ts as boolean; + this.declgenV1OutPath = buildConfig.declgenV1OutPath as string | undefined; + this.declgenV2OutPath = buildConfig.declgenV2OutPath as string | undefined; + this.declgenBridgeCodePath = buildConfig.declgenBridgeCodePath as string | undefined; this.hasMainModule = buildConfig.hasMainModule; this.abcFiles = new Set(); this.hashCacheFile = path.join(this.cacheDir, 'hash_cache.json'); - this.hashCache = this.loadHashCache(); this.isCacheFileExists = fs.existsSync(this.hashCacheFile); + this.hashCache = this.loadHashCache(); this.dependencyFileMap = null; this.isBuildConfigModified = buildConfig.isBuildConfigModified as boolean | undefined; - - this.enableDeclgenEts2Ts = buildConfig.enableDeclgenEts2Ts as boolean; - this.declgenV1OutPath = buildConfig.declgenV1OutPath as string | undefined; - this.declgenV2OutPath = buildConfig.declgenV2OutPath as string | undefined; - this.declgenBridgeCodePath = buildConfig.declgenBridgeCodePath as string | undefined; - - this.moduleInfos = new Map(); - this.compileFiles = new Map(); - this.allFiles = new Map(); - this.mergedAbcFile = path.resolve(this.outputDir, MERGED_ABC_FILE); - this.dependencyJsonFile = path.resolve(this.cacheDir, DEPENDENCY_JSON_FILE); - this.abcLinkerCmd = ['"' + this.buildConfig.abcLinkerPath + '"']; - this.dependencyAnalyzerCmd = ['"' + this.buildConfig.dependencyAnalyzerPath + '"']; - - this.logger = Logger.getInstance(); this.hasCleanWorker = false; + this.byteCodeHar = buildConfig.byteCodeHar as boolean; } public declgen(fileInfo: CompileFileInfo): void { const source = fs.readFileSync(fileInfo.filePath, 'utf8'); - let moduleInfo: ModuleInfo = this.moduleInfos.get(fileInfo.packageName)!; - let filePathFromModuleRoot: string = path.relative(moduleInfo.moduleRootPath, fileInfo.filePath); - let declEtsOutputPath: string = changeDeclgenFileExtension( + const moduleInfo: ModuleInfo = this.moduleInfos.get(fileInfo.packageName)!; + const filePathFromModuleRoot: string = path.relative(moduleInfo.moduleRootPath, fileInfo.filePath); + const declEtsOutputPath: string = changeDeclgenFileExtension( path.join(moduleInfo.declgenV1OutPath as string, moduleInfo.packageName, filePathFromModuleRoot), DECL_ETS_SUFFIX ); - let etsOutputPath: string = changeDeclgenFileExtension( + const etsOutputPath: string = changeDeclgenFileExtension( path.join(moduleInfo.declgenBridgeCodePath as string, moduleInfo.packageName, filePathFromModuleRoot), TS_SUFFIX ); ensurePathExists(declEtsOutputPath); ensurePathExists(etsOutputPath); - let arktsGlobal: ArkTSGlobal = this.buildConfig.arktsGlobal; - let arkts: ArkTS = this.buildConfig.arkts; + const arktsGlobal: ArkTSGlobal = this.buildConfig.arktsGlobal; + const arkts: ArkTS = this.buildConfig.arkts; let errorStatus = false; try { arktsGlobal.filePath = fileInfo.filePath; @@ -219,7 +211,7 @@ export abstract class BaseMode { public compile(fileInfo: CompileFileInfo): void { ensurePathExists(fileInfo.abcFilePath); - let ets2pandaCmd: string[] = [ + const ets2pandaCmd: string[] = [ '_', '--extension', 'ets', @@ -235,8 +227,8 @@ export abstract class BaseMode { ets2pandaCmd.push(fileInfo.filePath); this.logger.printInfo('ets2pandaCmd: ' + ets2pandaCmd.join(' ')); - let arktsGlobal = this.buildConfig.arktsGlobal; - let arkts = this.buildConfig.arkts; + const arktsGlobal = this.buildConfig.arktsGlobal; + const arkts = this.buildConfig.arkts; let errorStatus = false; try { arktsGlobal.filePath = fileInfo.filePath; @@ -330,8 +322,8 @@ export abstract class BaseMode { } private getDependentModules(moduleInfo: ModuleInfo): Map[] { - let dynamicDepModules: Map = new Map(); - let staticDepModules: Map = new Map(); + const dynamicDepModules: Map = new Map(); + const staticDepModules: Map = new Map(); if (moduleInfo.isMainModule) { this.moduleInfos.forEach((module: ModuleInfo, packageName: string) => { @@ -377,13 +369,13 @@ export abstract class BaseMode { } protected generateArkTSConfigForModules(): void { - this.moduleInfos.forEach((moduleInfo: ModuleInfo, moduleRootPath: string) => { + this.moduleInfos.forEach((moduleInfo: ModuleInfo, _: string) => { ArkTSConfigGenerator.getInstance(this.buildConfig, this.moduleInfos).writeArkTSConfigFile(moduleInfo, this.enableDeclgenEts2Ts); }); } private collectDepModuleInfos(): void { - this.moduleInfos.forEach((moduleInfo) => { + this.moduleInfos.forEach((moduleInfo: ModuleInfo) => { let [dynamicDepModules, staticDepModules] = this.getDependentModules(moduleInfo); moduleInfo.dynamicDepModuleInfos = dynamicDepModules; moduleInfo.staticDepModuleInfos = staticDepModules; @@ -398,7 +390,7 @@ export abstract class BaseMode { ); this.logger.printError(logData); } - let mainModuleInfo: ModuleInfo = this.getMainModuleInfo(); + const mainModuleInfo: ModuleInfo = this.getMainModuleInfo(); this.moduleInfos.set(this.packageName, mainModuleInfo); this.dependentModuleList.forEach((module: DependentModuleConfig) => { if (!module.packageName || !module.modulePath || !module.sourceRoots || !module.entryFile) { @@ -408,7 +400,7 @@ export abstract class BaseMode { ); this.logger.printError(logData); } - let moduleInfo: ModuleInfo = { + const moduleInfo: ModuleInfo = { isMainModule: false, packageName: module.packageName, moduleRootPath: module.modulePath, @@ -483,7 +475,7 @@ export abstract class BaseMode { if (etsFileLastModified < abcFileLastModified) { const currentHash = getFileHash(etsFilePath); const cachedHash = this.hashCache[etsFilePath]; - if (cachedHash && currentHash === cachedHash) { + if (cachedHash && cachedHash === currentHash) { return false; } } @@ -512,7 +504,7 @@ export abstract class BaseMode { return; } let hasModule = false; - for (const [packageName, moduleInfo] of this.moduleInfos) { + for (const [_, moduleInfo] of this.moduleInfos) { if (!file.startsWith(moduleInfo.moduleRootPath)) { continue; } @@ -566,7 +558,7 @@ export abstract class BaseMode { compileFiles.forEach((file: string) => { let hasModule = false; - for (const [packageName, moduleInfo] of this.moduleInfos) { + for (const [_, moduleInfo] of this.moduleInfos) { if (!file.startsWith(moduleInfo.moduleRootPath)) { continue; } @@ -612,16 +604,23 @@ export abstract class BaseMode { return; } this.entryFiles.forEach((file: string) => { - for (const [packageName, moduleInfo] of this.moduleInfos) { + // Skip the declaration files when compiling abc + if (file.endsWith(DECL_ETS_SUFFIX)) { + return; + } + for (const [_, moduleInfo] of this.moduleInfos) { if (!file.startsWith(moduleInfo.moduleRootPath)) { continue; } - let filePathFromModuleRoot: string = path.relative(moduleInfo.moduleRootPath, file); - let filePathInCache: string = path.join(this.cacheDir, moduleInfo.packageName, filePathFromModuleRoot); - let abcFilePath: string = path.resolve(changeFileExtension(filePathInCache, ABC_SUFFIX)); + if (moduleInfo.moduleType === OHOS_MODULE_TYPE.HAR && moduleInfo.byteCodeHar) { + return; + } + const filePathFromModuleRoot: string = path.relative(moduleInfo.moduleRootPath, file); + const filePathInCache: string = path.join(this.cacheDir, moduleInfo.packageName, filePathFromModuleRoot); + const abcFilePath: string = path.resolve(changeFileExtension(filePathInCache, ABC_SUFFIX)); this.abcFiles.add(abcFilePath); this.hashCache[file] = getFileHash(file); - let fileInfo: CompileFileInfo = { + const fileInfo: CompileFileInfo = { filePath: path.resolve(file), dependentFiles: [], abcFilePath: abcFilePath, @@ -676,7 +675,7 @@ export abstract class BaseMode { this.generateModuleInfos(); const compilePromises: Promise[] = []; - this.compileFiles.forEach((fileInfo: CompileFileInfo, file: string) => { + this.compileFiles.forEach((fileInfo: CompileFileInfo, _: string) => { compilePromises.push(new Promise((resolve) => { this.declgen(fileInfo); resolve(); @@ -689,7 +688,7 @@ export abstract class BaseMode { this.generateModuleInfos(); const compilePromises: Promise[] = []; - this.compileFiles.forEach((fileInfo: CompileFileInfo, file: string) => { + this.compileFiles.forEach((fileInfo: CompileFileInfo, _: string) => { compilePromises.push(new Promise((resolve) => { this.compile(fileInfo); resolve(); @@ -711,7 +710,7 @@ export abstract class BaseMode { if (this.enableDeclgenEts2Ts) { return; } - let dependencyInputFile: string = path.join(this.cacheDir, DEPENDENCY_INPUT_FILE); + const dependencyInputFile: string = path.join(this.cacheDir, DEPENDENCY_INPUT_FILE); let dependencyInputContent: string = ''; this.entryFiles.forEach((entryFile: string) => { dependencyInputContent += entryFile + os.EOL; @@ -719,7 +718,7 @@ export abstract class BaseMode { fs.writeFileSync(dependencyInputFile, dependencyInputContent); this.dependencyAnalyzerCmd.push('@' + '"' + dependencyInputFile + '"'); - for (const [packageName, module] of this.moduleInfos) { + for (const [_, module] of this.moduleInfos) { if (module.isMainModule) { this.dependencyAnalyzerCmd.push('--arktsconfig=' + '"' + module.arktsConfigFile + '"'); break; -- Gitee