diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index e1d65f97f6fe1fe2db58868c9de4ba6bfe8dfc66..38a1bc1c130b7560ee713c6e98aa2ed818479541 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,82 @@ export abstract class BaseMode { } } + public compileAll(filePaths: string[], moduleInfo: ModuleInfo): void { + let ets2pandaCmd: string[] = [ + '_', + '--extension', + 'ets', + '--arktsconfig', + moduleInfo.arktsConfigFile, + '--output', + path.resolve(this.outputDir, MERGED_ABC_FILE) + ]; + ensurePathExists(path.resolve(this.outputDir, MERGED_ABC_FILE)); + if (this.isDebug) { + ets2pandaCmd.push('--debug-info'); + } + ets2pandaCmd.push(this.buildConfig.compileFiles[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.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); + 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 +511,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 +669,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 +769,15 @@ 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); - - this.mergeAbcFiles(); + //@ts-ignore + this.compileAll([], this.moduleInfos.get(this.packageName)); } // -- runParallell code begins -- @@ -712,9 +788,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 +799,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 +813,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 +833,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 +889,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 +926,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 +978,11 @@ export abstract class BaseMode { execPath, execArgs = [], } = options; - + if (clearExitListeners) { cluster.removeAllListeners('exit'); } - + cluster.setupPrimary({ exec: execPath, execArgv: execArgs, @@ -925,63 +999,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 +1080,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 +1305,7 @@ export abstract class BaseMode { '--output', fileInfo.abcFilePath, ]; - + if (this.isDebug) { ets2pandaCmd.push('--debug-info'); } @@ -1314,10 +1388,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 +1405,7 @@ export abstract class BaseMode { buildConfig: serializableConfig, globalContextPtr: globalContextPtr }; - } + } else if (queues.abcQueue.length > 0) { job = queues.abcQueue.shift()!; jobInfo = { @@ -1396,6 +1470,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