From cc3305e88c37a48c24063262758d4eea0b3a9268 Mon Sep 17 00:00:00 2001 From: zenghang Date: Sun, 8 Jun 2025 16:21:44 +0800 Subject: [PATCH 1/2] aaaa Signed-off-by: zenghang Change-Id: I6c8eacfa6f998eb472d8da97f9dccf018c3bf8f2 --- .../build_system/src/build/base_mode.ts | 280 +++++++++++++----- 1 file changed, 200 insertions(+), 80 deletions(-) diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index e1d65f97f6..8162450535 100644 --- a/ets2panda/driver/build_system/src/build/base_mode.ts +++ b/ets2panda/driver/build_system/src/build/base_mode.ts @@ -254,7 +254,7 @@ export abstract class BaseMode { arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_CHECKED, arktsGlobal.compilerContext.peer); this.logger.printInfo('es2panda proceedToState checked'); - + if (this.hasMainModule && (this.byteCodeHar || this.moduleType === OHOS_MODULE_TYPE.SHARED)) { let filePathFromModuleRoot: string = path.relative(this.moduleRootPath, fileInfo.filePath); let declEtsOutputPath: string = changeFileExtension( @@ -294,6 +294,83 @@ export abstract class BaseMode { } } + public compileAll(filePaths: string[], moduleInfo: ModuleInfo): void { + const abcPath = path.join(this.cacheDir, moduleInfo.packageName + ".abc"); + let ets2pandaCmd: string[] = [ + '_', + '--extension', + 'ets', + '--arktsconfig', + moduleInfo.arktsConfigFile, + '--output', + abcPath, + ]; + + if (this.isDebug) { + ets2pandaCmd.push('--debug-info'); + } + ets2pandaCmd.push(filePaths[0]); + this.logger.printInfo('ets2pandaCmd: ' + ets2pandaCmd.join(' ')); + + let arktsGlobal = this.buildConfig.arktsGlobal; + let arkts = this.buildConfig.arkts; + let errorStatus = false; + try { + // arktsGlobal.filePath = fileInfo.filePath; + arktsGlobal.config = arkts.Config.create(ets2pandaCmd).peer; + //@ts-ignore + arktsGlobal.compilerContext = arkts.Context.createFromMutilFIle(filePaths); + PluginDriver.getInstance().getPluginContext().setArkTSProgram(arktsGlobal.compilerContext.program); + + arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED, arktsGlobal.compilerContext.peer); + this.logger.printInfo('es2panda proceedToState parsed'); + let ast = arkts.EtsScript.fromContext(); + PluginDriver.getInstance().getPluginContext().setArkTSAst(ast); + PluginDriver.getInstance().runPluginHook(PluginHook.PARSED); + this.logger.printInfo('plugin parsed finished'); + + arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_CHECKED, arktsGlobal.compilerContext.peer); + this.logger.printInfo('es2panda proceedToState checked'); + + // if (this.hasMainModule && (this.byteCodeHar || this.moduleType === OHOS_MODULE_TYPE.SHARED)) { + // let filePathFromModuleRoot: string = path.relative(this.moduleRootPath, fileInfo.filePath); + // let declEtsOutputPath: string = changeFileExtension( + // path.join(this.declgenV2OutPath as string, this.packageName, filePathFromModuleRoot), + // DECL_ETS_SUFFIX + // ); + // ensurePathExists(declEtsOutputPath); + + // // Generate 1.2 declaration files(a temporary solution while binary import not pushed) + // arkts.generateStaticDeclarationsFromContext(declEtsOutputPath); + // } + + ast = arkts.EtsScript.fromContext(); + PluginDriver.getInstance().getPluginContext().setArkTSAst(ast); + PluginDriver.getInstance().runPluginHook(PluginHook.CHECKED); + this.logger.printInfo('plugin checked finished'); + + arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED, arktsGlobal.compilerContext.peer); + this.logger.printInfo('es2panda bin generated'); + } catch (error) { + errorStatus = true; + if (error instanceof Error) { + const logData: LogData = LogDataFactory.newInstance( + ErrorCode.BUILDSYSTEM_COMPILE_ABC_FAIL, + 'Compile abc files failed.', + error.message + ); + this.logger.printError(logData); + } + } finally { + if (!errorStatus) { + // when error occur,wrapper will destroy context. + arktsGlobal.es2panda._DestroyContext(arktsGlobal.compilerContext.peer); + } + PluginDriver.getInstance().runPluginHook(PluginHook.CLEAN); + arkts.destroyConfig(arktsGlobal.config); + } + } + public mergeAbcFiles(): void { let linkerInputFile: string = path.join(this.cacheDir, LINKER_INPUT_FILE); let linkerInputContent: string = ''; @@ -435,20 +512,20 @@ export abstract class BaseMode { protected getMainModuleInfo(): ModuleInfo { return { - isMainModule: this.hasMainModule, - packageName: this.packageName, - moduleRootPath: this.moduleRootPath, - moduleType: this.moduleType, - sourceRoots: this.sourceRoots, - entryFile: '', - arktsConfigFile: path.resolve(this.cacheDir, this.packageName, ARKTSCONFIG_JSON_FILE), - dynamicDepModuleInfos: new Map(), - staticDepModuleInfos: new Map(), - compileFileInfos: [], - declgenV1OutPath: this.declgenV1OutPath, - declgenV2OutPath: this.declgenV2OutPath, - declgenBridgeCodePath: this.declgenBridgeCodePath, - byteCodeHar: this.byteCodeHar + isMainModule: this.hasMainModule, + packageName: this.packageName, + moduleRootPath: this.moduleRootPath, + moduleType: this.moduleType, + sourceRoots: this.sourceRoots, + entryFile: '', + arktsConfigFile: path.resolve(this.cacheDir, this.packageName, ARKTSCONFIG_JSON_FILE), + dynamicDepModuleInfos: new Map(), + staticDepModuleInfos: new Map(), + compileFileInfos: [], + declgenV1OutPath: this.declgenV1OutPath, + declgenV2OutPath: this.declgenV2OutPath, + declgenBridgeCodePath: this.declgenBridgeCodePath, + byteCodeHar: this.byteCodeHar }; } @@ -593,19 +670,19 @@ export abstract class BaseMode { } }); } - + private shouldSkipFile(file: string, moduleInfo: ModuleInfo, filePathFromModuleRoot: string, abcFilePath: string): boolean { const targetPath = this.enableDeclgenEts2Ts - ? changeFileExtension(path.join(moduleInfo.declgenBridgeCodePath as string, moduleInfo.packageName, filePathFromModuleRoot), TS_SUFFIX) - : abcFilePath; + ? changeFileExtension(path.join(moduleInfo.declgenBridgeCodePath as string, moduleInfo.packageName, filePathFromModuleRoot), TS_SUFFIX) + : abcFilePath; return !this.isFileChanged(file, targetPath); } protected collectCompileFiles(): void { - if (!this.enableDeclgenEts2Ts) { - this.collectDependentCompileFiles(); - return; - } + // if (!this.enableDeclgenEts2Ts) { + // this.collectDependentCompileFiles(); + // return; + // } this.entryFiles.forEach((file: string) => { // Skip the declaration files when compiling abc if (file.endsWith(DECL_ETS_SUFFIX)) { @@ -693,15 +770,60 @@ export abstract class BaseMode { this.generateModuleInfos(); const compilePromises: Promise[] = []; + let moduleToFile = new Map(); this.compileFiles.forEach((fileInfo: CompileFileInfo, file: string) => { - compilePromises.push(new Promise((resolve) => { - this.compile(fileInfo); - resolve(); - })); + if (!moduleToFile.has(fileInfo.packageName)) { + moduleToFile.set(fileInfo.packageName, []); + } + moduleToFile.get(fileInfo.packageName)?.push(fileInfo.filePath); }); - await Promise.all(compilePromises); + for (const [packageName, filePaths] of moduleToFile) { + if (filePaths.length === 0) { + continue; + } + //@ts-ignore + this.compileAll(filePaths, this.moduleInfos.get(packageName)); + } - this.mergeAbcFiles(); + + let linkerInputFile: string = path.join(this.cacheDir, LINKER_INPUT_FILE); + let linkerInputContent: string = ''; + + for (const [packageName, filePaths] of moduleToFile) { + if (filePaths.length === 0) { + continue; + } + const abcPath = path.join(this.cacheDir, packageName + ".abc"); + linkerInputContent += abcPath+ os.EOL; + } + + fs.writeFileSync(linkerInputFile, linkerInputContent); + + this.abcLinkerCmd.push('--output'); + this.abcLinkerCmd.push('"' + this.mergedAbcFile + '"'); + this.abcLinkerCmd.push('--'); + this.abcLinkerCmd.push('@' + '"' + linkerInputFile + '"'); + + let abcLinkerCmdStr: string = this.abcLinkerCmd.join(' '); + if (isMac()) { + const loadLibrary = 'DYLD_LIBRARY_PATH=' + '"' + process.env.DYLD_LIBRARY_PATH + '"'; + abcLinkerCmdStr = loadLibrary + ' ' + abcLinkerCmdStr; + } + this.logger.printInfo(abcLinkerCmdStr); + + ensurePathExists(this.mergedAbcFile); + try { + child_process.execSync(abcLinkerCmdStr).toString(); + } catch (error) { + if (error instanceof Error) { + const logData: LogData = LogDataFactory.newInstance( + ErrorCode.BUILDSYSTEM_LINK_ABC_FAIL, + 'Link abc files failed.', + error.message + ); + this.logger.printError(logData); + } + } } // -- runParallell code begins -- @@ -712,9 +834,7 @@ export abstract class BaseMode { }; public generatedependencyFileMap(): void { - if (this.enableDeclgenEts2Ts) { - return; - } + return; let dependencyInputFile: string = path.join(this.cacheDir, DEPENDENCY_INPUT_FILE); let dependencyInputContent: string = ''; this.entryFiles.forEach((entryFile: string) => { @@ -725,8 +845,8 @@ export abstract class BaseMode { this.dependencyAnalyzerCmd.push('@' + '"' + dependencyInputFile + '"'); for (const [packageName, module] of this.moduleInfos) { if (module.isMainModule) { - this.dependencyAnalyzerCmd.push('--arktsconfig=' + '"' + module.arktsConfigFile + '"'); - break; + this.dependencyAnalyzerCmd.push('--arktsconfig=' + '"' + module.arktsConfigFile + '"'); + break; } } this.dependencyAnalyzerCmd.push('--output=' + '"' + this.dependencyJsonFile + '"'); @@ -739,7 +859,7 @@ export abstract class BaseMode { ensurePathExists(this.dependencyJsonFile); try { - const output = child_process.execSync(dependencyAnalyzerCmdStr, { + const output = child_process.execSync(dependencyAnalyzerCmdStr, { stdio: 'pipe', encoding: 'utf-8' }); @@ -759,10 +879,10 @@ export abstract class BaseMode { const execError = error as child_process.ExecException; let fullErrorMessage = execError.message; if (execError.stderr) { - fullErrorMessage += `\nError output: ${execError.stderr}`; + fullErrorMessage += `\nError output: ${execError.stderr}`; } if (execError.stdout) { - fullErrorMessage += `\nOutput: ${execError.stdout}`; + fullErrorMessage += `\nOutput: ${execError.stdout}`; } const logData: LogData = LogDataFactory.newInstance( ErrorCode.BUILDSYSTEM_Dependency_Analyze_FAIL, @@ -815,7 +935,7 @@ export abstract class BaseMode { await this.dispatchTasks(); this.logger.printInfo('All declaration generation tasks complete.'); } catch (error) { - this.logger.printError(LogDataFactory.newInstance( + this.logger.printError(LogDataFactory.newInstance( ErrorCode.BUILDSYSTEM_DECLGEN_FAIL, 'Generate declaration files failed.' )); @@ -852,7 +972,7 @@ export abstract class BaseMode { const worker = cluster.fork(); this.setupWorkerMessageHandler(worker); - worker.send({ taskList: taskChunk, buildConfig: serializableConfig, moduleInfos: moduleInfosArray}); + worker.send({ taskList: taskChunk, buildConfig: serializableConfig, moduleInfos: moduleInfosArray }); const exitPromise = new Promise((resolve, reject) => { worker.on('exit', (status) => status === 0 ? resolve() : reject()); @@ -904,11 +1024,11 @@ export abstract class BaseMode { execPath, execArgs = [], } = options; - + if (clearExitListeners) { cluster.removeAllListeners('exit'); } - + cluster.setupPrimary({ exec: execPath, execArgv: execArgs, @@ -925,63 +1045,63 @@ export abstract class BaseMode { const allNodes = new Set(); for (const node in graph.dependencies) { - allNodes.add(node); - graph.dependencies[node].forEach(dep => allNodes.add(dep)); + allNodes.add(node); + graph.dependencies[node].forEach(dep => allNodes.add(dep)); } for (const node in graph.dependants) { - allNodes.add(node); - graph.dependants[node].forEach(dep => allNodes.add(dep)); + allNodes.add(node); + graph.dependants[node].forEach(dep => allNodes.add(dep)); } Array.from(allNodes).forEach(node => { - adjacencyList[node] = graph.dependencies[node] || []; - reverseAdjacencyList[node] = graph.dependants[node] || []; + adjacencyList[node] = graph.dependencies[node] || []; + reverseAdjacencyList[node] = graph.dependants[node] || []; }); const visited = new Set(); const order: string[] = []; function dfs(node: string): void { - visited.add(node); - for (const neighbor of adjacencyList[node]) { - if (!visited.has(neighbor)) { - dfs(neighbor); - } + visited.add(node); + for (const neighbor of adjacencyList[node]) { + if (!visited.has(neighbor)) { + dfs(neighbor); } - order.push(node); + } + order.push(node); } Array.from(allNodes).forEach(node => { - if (!visited.has(node)) { - dfs(node); - } + if (!visited.has(node)) { + dfs(node); + } }); visited.clear(); const components = new Map>(); function reverseDfs(node: string, component: Set): void { - visited.add(node); - component.add(node); - for (const neighbor of reverseAdjacencyList[node]) { - if (!visited.has(neighbor)) { - reverseDfs(neighbor, component); - } + visited.add(node); + component.add(node); + for (const neighbor of reverseAdjacencyList[node]) { + if (!visited.has(neighbor)) { + reverseDfs(neighbor, component); } + } } for (let i = order.length - 1; i >= 0; i--) { - const node = order[i]; - if (!visited.has(node)) { - const component = new Set(); - reverseDfs(node, component); - if (component.size > 1) { - const sortedFiles = Array.from(component).sort(); - const hashKey = createHash(sortedFiles.join('|')); - components.set(hashKey, component); - } - + const node = order[i]; + if (!visited.has(node)) { + const component = new Set(); + reverseDfs(node, component); + if (component.size > 1) { + const sortedFiles = Array.from(component).sort(); + const hashKey = createHash(sortedFiles.join('|')); + components.set(hashKey, component); } + + } } return components; @@ -1006,14 +1126,14 @@ export abstract class BaseMode { private getAbcJobId(file: string): string { return '1' + file; } - + private getExternalProgramJobId(file: string): string { return '0' + file; } private getJobDependants(fileDeps: string[], cycleFiles: Map): Set { let depJobList: Set = new Set(); - fileDeps.forEach((file) => { + fileDeps.forEach((file) => { if (!file.endsWith(DECL_ETS_SUFFIX)) { depJobList.add(this.getAbcJobId(file)); } @@ -1231,7 +1351,7 @@ export abstract class BaseMode { '--output', fileInfo.abcFilePath, ]; - + if (this.isDebug) { ets2pandaCmd.push('--debug-info'); } @@ -1314,10 +1434,10 @@ export abstract class BaseMode { } private assignTaskToIdleWorker( - workerInfo: WorkerInfo, - queues: Queues, - processingJobs: Set, - serializableConfig: Object, + workerInfo: WorkerInfo, + queues: Queues, + processingJobs: Set, + serializableConfig: Object, globalContextPtr: KPointer): void { let job: Job | undefined; let jobInfo: JobInfo | undefined; @@ -1331,7 +1451,7 @@ export abstract class BaseMode { buildConfig: serializableConfig, globalContextPtr: globalContextPtr }; - } + } else if (queues.abcQueue.length > 0) { job = queues.abcQueue.shift()!; jobInfo = { @@ -1396,6 +1516,6 @@ function createHash(str: string): string { return hash.digest('hex'); } - // -- runConcurrent code ends -- +// -- runConcurrent code ends -- let finishedJob: string[] = []; \ No newline at end of file -- Gitee From 9f743a5e2883fe99cdf59b740201f46bc833f460 Mon Sep 17 00:00:00 2001 From: zenghang Date: Thu, 12 Jun 2025 17:26:52 +0800 Subject: [PATCH 2/2] aaac Change-Id: I8dcdfdc787c63f653746e6dbcf48987661383325 --- .../build_system/src/build/base_mode.ts | 56 ++----------------- 1 file changed, 5 insertions(+), 51 deletions(-) diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index 8162450535..bdcfac39e6 100644 --- a/ets2panda/driver/build_system/src/build/base_mode.ts +++ b/ets2panda/driver/build_system/src/build/base_mode.ts @@ -295,7 +295,6 @@ export abstract class BaseMode { } public compileAll(filePaths: string[], moduleInfo: ModuleInfo): void { - const abcPath = path.join(this.cacheDir, moduleInfo.packageName + ".abc"); let ets2pandaCmd: string[] = [ '_', '--extension', @@ -303,13 +302,13 @@ export abstract class BaseMode { '--arktsconfig', moduleInfo.arktsConfigFile, '--output', - abcPath, + path.resolve(this.outputDir, MERGED_ABC_FILE) ]; if (this.isDebug) { ets2pandaCmd.push('--debug-info'); } - ets2pandaCmd.push(filePaths[0]); + ets2pandaCmd.push(this.buildConfig.compileFiles[0]); this.logger.printInfo('ets2pandaCmd: ' + ets2pandaCmd.join(' ')); let arktsGlobal = this.buildConfig.arktsGlobal; @@ -319,7 +318,7 @@ export abstract class BaseMode { // arktsGlobal.filePath = fileInfo.filePath; arktsGlobal.config = arkts.Config.create(ets2pandaCmd).peer; //@ts-ignore - arktsGlobal.compilerContext = arkts.Context.createFromMutilFIle(filePaths); + arktsGlobal.compilerContext = arkts.Context.createContextGenerateAbcForExternalSourceFiles(this.buildConfig.compileFiles.length, this.buildConfig.compileFiles);; PluginDriver.getInstance().getPluginContext().setArkTSProgram(arktsGlobal.compilerContext.program); arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED, arktsGlobal.compilerContext.peer); @@ -777,53 +776,8 @@ export abstract class BaseMode { } moduleToFile.get(fileInfo.packageName)?.push(fileInfo.filePath); }); - for (const [packageName, filePaths] of moduleToFile) { - if (filePaths.length === 0) { - continue; - } - //@ts-ignore - this.compileAll(filePaths, this.moduleInfos.get(packageName)); - } - - - let linkerInputFile: string = path.join(this.cacheDir, LINKER_INPUT_FILE); - let linkerInputContent: string = ''; - - for (const [packageName, filePaths] of moduleToFile) { - if (filePaths.length === 0) { - continue; - } - const abcPath = path.join(this.cacheDir, packageName + ".abc"); - linkerInputContent += abcPath+ os.EOL; - } - - fs.writeFileSync(linkerInputFile, linkerInputContent); - - this.abcLinkerCmd.push('--output'); - this.abcLinkerCmd.push('"' + this.mergedAbcFile + '"'); - this.abcLinkerCmd.push('--'); - this.abcLinkerCmd.push('@' + '"' + linkerInputFile + '"'); - - let abcLinkerCmdStr: string = this.abcLinkerCmd.join(' '); - if (isMac()) { - const loadLibrary = 'DYLD_LIBRARY_PATH=' + '"' + process.env.DYLD_LIBRARY_PATH + '"'; - abcLinkerCmdStr = loadLibrary + ' ' + abcLinkerCmdStr; - } - this.logger.printInfo(abcLinkerCmdStr); - - ensurePathExists(this.mergedAbcFile); - try { - child_process.execSync(abcLinkerCmdStr).toString(); - } catch (error) { - if (error instanceof Error) { - const logData: LogData = LogDataFactory.newInstance( - ErrorCode.BUILDSYSTEM_LINK_ABC_FAIL, - 'Link abc files failed.', - error.message - ); - this.logger.printError(logData); - } - } + //@ts-ignore + this.compileAll([], this.moduleInfos.get(this.packageName)); } // -- runParallell code begins -- -- Gitee