From 34e24f4768f23c92c3eb658e53da14d1d18b4668 Mon Sep 17 00:00:00 2001 From: Kleene Date: Fri, 27 Jun 2025 17:23:37 +0800 Subject: [PATCH] dk interop with relative path Issue: ICICSH Signed-off-by: Kleene --- .../build_system/src/build/base_mode.ts | 23 +- .../build_system/src/build/build_mode.ts | 36 ++-- .../src/build/compile_thread_worker.ts | 4 +- .../build_system/src/build/compile_worker.ts | 4 +- .../src/build/generate_arktsconfig.ts | 10 +- .../src/plugins/AliasImportTransformer.ts | 201 ++++++++++++++++++ .../src/plugins/KitImportTransformer.ts | 187 ---------------- ets2panda/driver/build_system/src/types.ts | 3 +- 8 files changed, 256 insertions(+), 212 deletions(-) create mode 100644 ets2panda/driver/build_system/src/plugins/AliasImportTransformer.ts delete mode 100644 ets2panda/driver/build_system/src/plugins/KitImportTransformer.ts diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index 0dc2f94caa..c5a373a5d9 100644 --- a/ets2panda/driver/build_system/src/build/base_mode.ts +++ b/ets2panda/driver/build_system/src/build/base_mode.ts @@ -71,7 +71,7 @@ import { } from '../types'; import { ArkTSConfigGenerator } from './generate_arktsconfig'; import { SetupClusterOptions } from '../types'; -import { KitImportTransformer } from '../plugins/KitImportTransformer'; +import { AliasImportTransformer } from '../plugins/AliasImportTransformer'; export abstract class BaseMode { public buildConfig: BuildConfig; public entryFiles: Set; @@ -248,7 +248,7 @@ export abstract class BaseMode { if (this.buildConfig.aliasConfig?.size > 0) { // if aliasConfig is set, transform aliasName@kit.xxx to default@ohos.xxx through the plugin this.logger.printInfo('Transforming import statements with alias config'); - let transformAst = new KitImportTransformer(arkts, arktsGlobal.compilerContext.program, this.buildConfig.buildSdkPath,this.buildConfig.aliasConfig).transform(ast); + let transformAst = new AliasImportTransformer(arkts, arktsGlobal.compilerContext.program, this.buildConfig.buildSdkPath,this.buildConfig.aliasConfig).transform(ast); PluginDriver.getInstance().getPluginContext().setArkTSAst(transformAst); } else { PluginDriver.getInstance().getPluginContext().setArkTSAst(ast); @@ -689,6 +689,10 @@ export abstract class BaseMode { } protected collectCompileFiles(): void { + if (!this.enableDeclgenEts2Ts) { + this.collectDependentCompileFiles(); + return; + } this.entryFiles.forEach((file: string) => { // Skip the declaration files when compiling abc if (file.endsWith(DECL_ETS_SUFFIX)) { @@ -771,6 +775,21 @@ export abstract class BaseMode { public async run(): Promise { this.generateModuleInfos(); + const compilePromises: Promise[] = []; + this.compileFiles.forEach((fileInfo: CompileFileInfo, _: string) => { + compilePromises.push(new Promise((resolve) => { + this.compile(fileInfo); + resolve(); + })); + }); + await Promise.all(compilePromises); + + this.mergeAbcFiles(); + } + + public async runMultiFiles(): Promise { + this.generateModuleInfos(); + const compilePromises: Promise[] = []; let moduleToFile = new Map(); this.compileFiles.forEach((fileInfo: CompileFileInfo, file: string) => { diff --git a/ets2panda/driver/build_system/src/build/build_mode.ts b/ets2panda/driver/build_system/src/build/build_mode.ts index 0f14d3e8ba..281b8084c7 100644 --- a/ets2panda/driver/build_system/src/build/build_mode.ts +++ b/ets2panda/driver/build_system/src/build/build_mode.ts @@ -29,18 +29,30 @@ export class BuildMode extends BaseMode { } public async run(): Promise { - if (this.es2pandaMode === ES2PANDA_MODE.RUN_PARALLEL) { - // RUN_PARALLEL: Executes tasks using multiple processes - await super.runParallel(); - } else if (this.es2pandaMode === ES2PANDA_MODE.RUN_CONCURRENT) { - // RUN_CONCURRENT: Executes tasks using multiple threads with astcache - await super.runConcurrent(); - } else if (this.es2pandaMode === ES2PANDA_MODE.RUN) { - // RUN: Executes tasks sequentially in a single process and single thread - await super.run(); - } else { - // Default fallback: Uses parallel execution (same as RUN_PARALLEL) - await super.runParallel(); + switch (this.es2pandaMode) { + case ES2PANDA_MODE.RUN_PARALLEL: + // Executes tasks using multiple processes + await super.runParallel(); + break; + + case ES2PANDA_MODE.RUN_CONCURRENT: + // Executes tasks using multiple threads with astcache + await super.runConcurrent(); + break; + + case ES2PANDA_MODE.RUN: + // Executes tasks sequentially in a single process and single thread + await super.run(); + break; + + case ES2PANDA_MODE.RUN_WITH_MUTIL: + await super.runMultiFiles(); + break; + + default: + // Default fallback: Uses parallel execution + await super.runParallel(); + break; } } } \ No newline at end of file diff --git a/ets2panda/driver/build_system/src/build/compile_thread_worker.ts b/ets2panda/driver/build_system/src/build/compile_thread_worker.ts index 987221aaf9..9bbfc6ca76 100644 --- a/ets2panda/driver/build_system/src/build/compile_thread_worker.ts +++ b/ets2panda/driver/build_system/src/build/compile_thread_worker.ts @@ -37,7 +37,7 @@ import { Logger } from '../logger'; import { ErrorCode } from '../error_code'; -import { KitImportTransformer } from '../plugins/KitImportTransformer'; +import { AliasImportTransformer } from '../plugins/AliasImportTransformer'; const { workerId } = workerData; @@ -76,7 +76,7 @@ function compileAbc(jobInfo: JobInfo): void { if (config.aliasConfig?.size > 0) { // if aliasConfig is set, transform aliasName@kit.xxx to default@ohos.xxx through the plugin let ast = arkts.EtsScript.fromContext(); - let transformAst = new KitImportTransformer(arkts, arktsGlobal.compilerContext.program, config.buildSdkPath, config.aliasConfig).transform(ast); + let transformAst = new AliasImportTransformer(arkts, arktsGlobal.compilerContext.program, config.buildSdkPath, config.aliasConfig).transform(ast); PluginDriver.getInstance().getPluginContext().setArkTSAst(transformAst); } PluginDriver.getInstance().runPluginHook(PluginHook.PARSED); diff --git a/ets2panda/driver/build_system/src/build/compile_worker.ts b/ets2panda/driver/build_system/src/build/compile_worker.ts index d6eb4dcad3..48cb3ef74d 100644 --- a/ets2panda/driver/build_system/src/build/compile_worker.ts +++ b/ets2panda/driver/build_system/src/build/compile_worker.ts @@ -36,6 +36,7 @@ import { Logger } from '../logger'; import { ErrorCode } from '../error_code'; +import { AliasImportTransformer } from '../plugins/AliasImportTransformer'; process.on('message', (message: { taskList: CompileFileInfo[]; @@ -52,7 +53,6 @@ process.on('message', (message: { PluginDriver.getInstance().initPlugins(buildConfig); const koalaWrapperPath = process.env.KOALA_WRAPPER_PATH ?? path.resolve(buildConfig.buildSdkPath, KOALA_WRAPPER_PATH_FROM_SDK); let { arkts, arktsGlobal } = require(koalaWrapperPath); - const { KitImportTransformer } = require("../plugins/KitImportTransformer"); for (const fileInfo of taskList) { let errorStatus = false; @@ -80,7 +80,7 @@ process.on('message', (message: { if (buildConfig.aliasConfig?.size > 0) { // if aliasConfig is set, transform aliasName@kit.xxx to default@ohos.xxx through the plugin let ast = arkts.EtsScript.fromContext(); - let transformAst = new KitImportTransformer(arkts, arktsGlobal.compilerContext.program, buildConfig.buildSdkPath, buildConfig.aliasConfig).transform(ast); + let transformAst = new AliasImportTransformer(arkts, arktsGlobal.compilerContext.program, buildConfig.buildSdkPath, buildConfig.aliasConfig).transform(ast); PluginDriver.getInstance().getPluginContext().setArkTSAst(transformAst); } PluginDriver.getInstance().runPluginHook(PluginHook.PARSED); diff --git a/ets2panda/driver/build_system/src/build/generate_arktsconfig.ts b/ets2panda/driver/build_system/src/build/generate_arktsconfig.ts index 01b17cab2c..486aadfbd9 100644 --- a/ets2panda/driver/build_system/src/build/generate_arktsconfig.ts +++ b/ets2panda/driver/build_system/src/build/generate_arktsconfig.ts @@ -290,11 +290,9 @@ export class ArkTSConfigGenerator { const aliasForPkg: Map | undefined = this.aliasConfig?.get(moduleInfo.packageName); aliasForPkg?.forEach((aliasConfig, aliasName) => { - if (aliasConfig.isStatic || aliasConfig.originalAPIName.startsWith('@kit')) { - this.processStaticAlias(aliasName, aliasConfig); - } else { - this.processDynamicAlias(aliasName, aliasConfig, dependencySection); - } + if (!aliasConfig.isStatic) { + this.processStaticAlias(aliasName, aliasConfig); + } }); this.dynamicSDKPaths.forEach(basePath => { @@ -374,7 +372,7 @@ export class ArkTSConfigGenerator { private processStaticAlias(aliasName: string, aliasConfig: AliasConfig) { if (aliasConfig.originalAPIName in this.pathSection) { - this.pathSection[aliasName] = this.pathSection[aliasConfig.originalAPIName]; + this.pathSection[aliasName] = [getInteropFilePathByApi(aliasConfig.originalAPIName, this.dynamicSDKPaths)]; } } diff --git a/ets2panda/driver/build_system/src/plugins/AliasImportTransformer.ts b/ets2panda/driver/build_system/src/plugins/AliasImportTransformer.ts new file mode 100644 index 0000000000..b24e31991f --- /dev/null +++ b/ets2panda/driver/build_system/src/plugins/AliasImportTransformer.ts @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as fs from 'fs'; +import * as path from 'path'; +import { AliasConfig, ArkTS } from '../types'; +import { + Logger, + LogDataFactory +} from '../logger'; +import { ErrorCode } from '../error_code'; +import { KIT_CONFIGS_PATH_FROM_SDK } from '../pre_define'; + +export class AliasImportTransformer { + + private arkts: ArkTS; + private extraImports: ArkTS["ETSImportDeclaration"][] = []; + private sdkAliasConfig: Map>; + private buildSdkPath: string; + private program: object; + private logger: Logger; + + constructor(arkts: ArkTS, program: object, buildSdkPath: string, aliasMap: Map>) { + this.arkts = arkts; + this.buildSdkPath = buildSdkPath; + this.sdkAliasConfig = aliasMap; + this.program = program; + this.logger = Logger.getInstance(); + } + + public transform(astNode: ArkTS["AstNode"]): ArkTS["AstNode"] { + if (!this.arkts.isEtsScript(astNode)) { + return astNode; + } + + const newStatements: ArkTS["AstNode"][] = []; + const dynamicAliasNames = new Set(this.getDynamicAliasNames()); + if (astNode.statements.length === 0) { + return astNode; + } + for (const stmt of astNode.statements) { + if (this.arkts.isETSImportDeclaration(stmt) && dynamicAliasNames.has(stmt.source?.str)) { + const aliasInfo = this.getOriginalNameByAlias(stmt.source?.str); + if (aliasInfo?.originalAPIName.includes('kit')) { + this.splitKitImport(stmt); + }else{ + this.transformToDefaultAlais(stmt,aliasInfo); + } + continue; + } + newStatements.push(stmt); + } + + const finalStatements = [...this.extraImports, ...newStatements]; + + return this.arkts.factory.updateEtsScript(astNode, finalStatements); + } + + private splitKitImport(importNode: ArkTS["ETSImportDeclaration"]): void { + const kitName = importNode.source.str; + const symbolsJson = this.loadKitSymbolsJson(kitName); + if (!symbolsJson) { + return; + } + + const groupedSymbols = this.groupImportSpecifiersBySource(importNode, symbolsJson, kitName); + this.generateSplitImportDeclarations(groupedSymbols); + } + + private loadKitSymbolsJson(kitName: string): any | null { + let jsonFileName: string | undefined = this.getOriginalNameByAlias(kitName)?.originalAPIName; + if (!jsonFileName || jsonFileName === '') { + this.logger.printError(LogDataFactory.newInstance( + ErrorCode.BUILDSYSTEM_PLUGIN_ALIAS_CONFIG_PARSING_FAIL, + `json file: '${jsonFileName}' not found in kit config contents` + )); + return null; + } + const configPath = path.resolve(this.buildSdkPath, KIT_CONFIGS_PATH_FROM_SDK, `${jsonFileName}.json`); + + if (!fs.existsSync(configPath)) { + this.logger.printError(LogDataFactory.newInstance( + ErrorCode.BUILDSYSTEM_PLUGIN_ALIAS_CONFIG_PARSING_FAIL, + `Kit config file not found for ${kitName}`, + configPath + )); + return null; + } + + try { + return JSON.parse(fs.readFileSync(configPath, 'utf-8')); + } catch (error) { + this.logger.printError(LogDataFactory.newInstance( + ErrorCode.BUILDSYSTEM_PLUGIN_ALIAS_CONFIG_PARSING_FAIL, + `Failed to parse kit config JSON for ${kitName}`, + error instanceof Error ? error.message : String(error) + )); + return null; + } + } + + private groupImportSpecifiersBySource(importNode: ArkTS["ETSImportDeclaration"], symbolsJson: any, kitName: string): Map { + const grouped = new Map(); + + for (const specifier of importNode.specifiers) { + if (!this.arkts.isImportSpecifier(specifier)) { + continue; + } + + const symbolName = specifier.imported?.name; + if (!symbolName) { + continue; + } + + const symbolEntry = symbolsJson.symbols?.[symbolName]; + if (!symbolEntry?.source) { + this.logger.printWarn(`Symbol '${symbolName}' not found in ${kitName}.json`); + continue; + } + + const sourcePath = "default" + '/' + symbolEntry.source.replace(/\.d\.ts$/, ''); + if (!grouped.has(sourcePath)) { + grouped.set(sourcePath, []); + } + grouped.get(sourcePath)!.push(symbolName); + } + + return grouped; + } + + private generateSplitImportDeclarations(groupedSymbols: Map): void { + for (const [source, names] of groupedSymbols.entries()) { + const specifiers = names.map(name => + this.arkts.factory.createImportSpecifier( + this.arkts.factory.createIdentifier(name), + this.arkts.factory.createIdentifier(name) + ) + ); + + const importDecl = this.arkts.factory.createImportDeclaration( + this.arkts.factory.createStringLiteral(source), + specifiers, + this.arkts.Es2pandaImportKinds.IMPORT_KINDS_VALUE, + this.program, + this.arkts.Es2pandaImportFlags.IMPORT_FLAGS_NONE + ); + this.extraImports.push(importDecl); + } + } + + private getDynamicAliasNames(): Set { + const dynamicAliasNames = new Set(); + + if (this.sdkAliasConfig.size === 0) { + return dynamicAliasNames; + } + + for (const innerMap of this.sdkAliasConfig.values()) { + for (const [aliasName, aliasConfig] of innerMap.entries()) { + if (!aliasConfig.originalAPIName.startsWith('@kit')) { + continue; + } + if (!aliasConfig.isStatic) { + dynamicAliasNames.add(aliasName); + } + } + } + return dynamicAliasNames; + } + + private getOriginalNameByAlias(aliasName: string): AliasConfig | undefined { + for (const innerMap of this.sdkAliasConfig.values()) { + if (innerMap.has(aliasName)) { + return innerMap.get(aliasName)!; + } + } + return undefined; + } + private transformToDefaultAlais(importNode: ArkTS["ETSImportDeclaration"],aliasInfo:AliasConfig): void { + const importDecl = this.arkts.factory.createImportDeclaration( + this.arkts.factory.createStringLiteral('default/'+aliasInfo.originalAPIName), + importNode.specifiers, + this.arkts.Es2pandaImportKinds.IMPORT_KINDS_VALUE, + this.program, + this.arkts.Es2pandaImportFlags.IMPORT_FLAGS_NONE + ); + this.extraImports.push(importDecl); + } +} diff --git a/ets2panda/driver/build_system/src/plugins/KitImportTransformer.ts b/ets2panda/driver/build_system/src/plugins/KitImportTransformer.ts deleted file mode 100644 index 0f0f1ad077..0000000000 --- a/ets2panda/driver/build_system/src/plugins/KitImportTransformer.ts +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as fs from 'fs'; -import * as path from 'path'; -import { AliasConfig, ArkTS } from '../types'; -import { - Logger, - LogData, - LogDataFactory -} from '../logger'; -import { ErrorCode } from '../error_code'; -import { KIT_CONFIGS_PATH_FROM_SDK } from '../pre_define'; - -export class KitImportTransformer { - - private arkts: ArkTS; - private extraImports: ArkTS["ETSImportDeclaration"][] = []; - private sdkAliasConfig: Map>; - private buildSdkPath: string; - private program: object; - private logger: Logger; - - constructor(arkts: ArkTS, program: object, buildSdkPath: string, aliasMap: Map>) { - this.arkts = arkts; - this.buildSdkPath = buildSdkPath; - this.sdkAliasConfig = aliasMap; - this.program = program; - this.logger = Logger.getInstance(); - } - - public transform(astNode: ArkTS["AstNode"]): ArkTS["AstNode"] { - if (!this.arkts.isEtsScript(astNode)) { - return astNode; - } - - const newStatements: ArkTS["AstNode"][] = []; - const dynamicAliasNames = new Set(this.getDynamicAliasNames()); - if (astNode.statements.length === 0) { - return astNode; - } - for (const stmt of astNode.statements) { - if (this.arkts.isETSImportDeclaration(stmt) && dynamicAliasNames.has(stmt.source?.str)) { - this.splitKitImport(stmt); - continue; - } - newStatements.push(stmt); - } - - const finalStatements = [...this.extraImports, ...newStatements]; - - return this.arkts.factory.updateEtsScript(astNode, finalStatements); - } - - private splitKitImport(importNode: ArkTS["ETSImportDeclaration"]): void { - const kitName = importNode.source.str; - const symbolsJson = this.loadKitSymbolsJson(kitName); - if (!symbolsJson) { - return; - } - - const groupedSymbols = this.groupImportSpecifiersBySource(importNode, symbolsJson, kitName); - this.generateSplitImportDeclarations(groupedSymbols); - } - - private loadKitSymbolsJson(kitName: string): any | null { - let jsonFileName: string = this.getOriginalNameByAlias(kitName); - if (jsonFileName === '') { - this.logger.printError(LogDataFactory.newInstance( - ErrorCode.BUILDSYSTEM_PLUGIN_ALIAS_CONFIG_PARSING_FAIL, - `json file: '${jsonFileName}' not found in kit config contents` - )); - return null; - } - const configPath = path.resolve(this.buildSdkPath, KIT_CONFIGS_PATH_FROM_SDK, `${jsonFileName}.json`); - - if (!fs.existsSync(configPath)) { - this.logger.printError(LogDataFactory.newInstance( - ErrorCode.BUILDSYSTEM_PLUGIN_ALIAS_CONFIG_PARSING_FAIL, - `Kit config file not found for ${kitName}`, - configPath - )); - return null; - } - - try { - return JSON.parse(fs.readFileSync(configPath, 'utf-8')); - } catch (error) { - this.logger.printError(LogDataFactory.newInstance( - ErrorCode.BUILDSYSTEM_PLUGIN_ALIAS_CONFIG_PARSING_FAIL, - `Failed to parse kit config JSON for ${kitName}`, - error instanceof Error ? error.message : String(error) - )); - return null; - } - } - - private groupImportSpecifiersBySource(importNode: ArkTS["ETSImportDeclaration"], symbolsJson: any, kitName: string): Map { - const grouped = new Map(); - - for (const specifier of importNode.specifiers) { - if (!this.arkts.isImportSpecifier(specifier)) { - continue; - } - - const symbolName = specifier.imported?.name; - if (!symbolName) { - continue; - } - - const symbolEntry = symbolsJson.symbols?.[symbolName]; - if (!symbolEntry?.source) { - this.logger.printWarn(`Symbol '${symbolName}' not found in ${kitName}.json`); - continue; - } - - const sourcePath = "default" + symbolEntry.source.replace(/\.d\.ts$/, ''); - if (!grouped.has(sourcePath)) { - grouped.set(sourcePath, []); - } - grouped.get(sourcePath)!.push(symbolName); - } - - return grouped; - } - - private generateSplitImportDeclarations(groupedSymbols: Map): void { - for (const [source, names] of groupedSymbols.entries()) { - const specifiers = names.map(name => - this.arkts.factory.createImportSpecifier( - this.arkts.factory.createIdentifier(name), - this.arkts.factory.createIdentifier(name) - ) - ); - - const importDecl = this.arkts.factory.createImportDeclaration( - this.arkts.factory.createStringLiteral(source), - specifiers, - this.arkts.Es2pandaImportKinds.IMPORT_KINDS_VALUE, - this.program, - this.arkts.Es2pandaImportFlags.IMPORT_FLAGS_NONE - ); - this.extraImports.push(importDecl); - } - } - - private getDynamicAliasNames(): Set { - const dynamicAliasNames = new Set(); - - if (this.sdkAliasConfig.size === 0) { - return dynamicAliasNames; - } - - for (const innerMap of this.sdkAliasConfig.values()) { - for (const [aliasName, aliasConfig] of innerMap.entries()) { - if (!aliasConfig.originalAPIName.startsWith('@kit')) { - continue; - } - if (!aliasConfig.isStatic) { - dynamicAliasNames.add(aliasName); - } - } - } - return dynamicAliasNames; - } - - private getOriginalNameByAlias(aliasName: string): string { - for (const innerMap of this.sdkAliasConfig.values()) { - if (innerMap.has(aliasName)) { - return innerMap.get(aliasName)!.originalAPIName; - } - } - return ''; - } -} diff --git a/ets2panda/driver/build_system/src/types.ts b/ets2panda/driver/build_system/src/types.ts index d3012e4096..0cb5d291b8 100644 --- a/ets2panda/driver/build_system/src/types.ts +++ b/ets2panda/driver/build_system/src/types.ts @@ -294,5 +294,6 @@ export enum Es2pandaImportFlags { export enum ES2PANDA_MODE { RUN_PARALLEL = 0, RUN_CONCURRENT = 1, - RUN = 2 + RUN = 2, + RUN_WITH_MUTIL = 3 }; -- Gitee