From 5aa54c5a2d51d10088fbf98ce3af90edd6c0db04 Mon Sep 17 00:00:00 2001 From: xuhangqi Date: Fri, 1 Aug 2025 16:09:12 +0800 Subject: [PATCH] Adapt multi file Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQF66 Signed-off-by: xuhangqi Change-Id: Ie441597dc128f39dcf0cffaec5639bc9342351ba --- .../build_system/src/build/base_mode.ts | 14 +++++ .../build_system/src/build/compile_worker.ts | 10 ++-- ets2panda/public/es2panda_lib.cpp | 52 ++++++++++++++++++- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index a79857d9c7..ea7302f5d0 100644 --- a/ets2panda/driver/build_system/src/build/base_mode.ts +++ b/ets2panda/driver/build_system/src/build/base_mode.ts @@ -391,6 +391,20 @@ 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)) { + for (const sourceFilePath of this.buildConfig.compileFiles) { + const filePathFromModuleRoot: string = path.relative(this.moduleRootPath, sourceFilePath); + + const declEtsOutputPath: string = changeFileExtension( + path.join(this.declgenV2OutPath as string, filePathFromModuleRoot), + DECL_ETS_SUFFIX + ); + ensurePathExists(declEtsOutputPath); + + arkts.generateStaticDeclarationsFromContext(declEtsOutputPath); + } + } + ast = arkts.EtsScript.fromContext(); PluginDriver.getInstance().getPluginContext().setArkTSAst(ast); PluginDriver.getInstance().runPluginHook(PluginHook.CHECKED); diff --git a/ets2panda/driver/build_system/src/build/compile_worker.ts b/ets2panda/driver/build_system/src/build/compile_worker.ts index c10889518b..a93ea46d22 100644 --- a/ets2panda/driver/build_system/src/build/compile_worker.ts +++ b/ets2panda/driver/build_system/src/build/compile_worker.ts @@ -87,19 +87,15 @@ process.on('message', (message: { PluginDriver.getInstance().runPluginHook(PluginHook.PARSED); arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_CHECKED, arktsGlobal.compilerContext.peer); - if (buildConfig.hasMainModule && (buildConfig.byteCodeHar || buildConfig.moduleType === OHOS_MODULE_TYPE.SHARED)) { - let filePathFromModuleRoot: string = path.relative(buildConfig.moduleRootPath, fileInfo.filePath); - let declEtsOutputPath: string = changeFileExtension( - path.join(buildConfig.declgenV2OutPath as string, filePathFromModuleRoot), + const filePathFromModuleRoot = path.relative(buildConfig.moduleRootPath, fileInfo.filePath); + const declEtsOutputPath = changeFileExtension( + path.join(buildConfig.declgenV2OutPath!, filePathFromModuleRoot), DECL_ETS_SUFFIX ); ensurePathExists(declEtsOutputPath); - - // Generate 1.2 declaration files(a temporary solution while binary import not pushed) arkts.generateStaticDeclarationsFromContext(declEtsOutputPath); } - PluginDriver.getInstance().runPluginHook(PluginHook.CHECKED); arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED, arktsGlobal.compilerContext.peer); diff --git a/ets2panda/public/es2panda_lib.cpp b/ets2panda/public/es2panda_lib.cpp index f9295c138b..25a77acc60 100644 --- a/ets2panda/public/es2panda_lib.cpp +++ b/ets2panda/public/es2panda_lib.cpp @@ -1285,7 +1285,48 @@ extern "C" __attribute__((unused)) int GenerateTsDeclarationsFromContext(es2pand : 1; } -// Will be removed after binary import support is fully implemented. +// #28937 Will be removed after binary import support is fully implemented. +__attribute__((unused)) static std::string GetFileNameByPath(const std::string &path) +{ + auto lastSlash = path.find_last_of("/\\"); + std::string filename = (lastSlash == std::string::npos) ? path : path.substr(lastSlash + 1); + + auto lastDot = filename.find_last_of('.'); + if (lastDot != std::string::npos) { + filename = filename.substr(0, lastDot); + } + + lastDot = filename.find_last_of('.'); + if (lastDot != std::string::npos) { + filename = filename.substr(0, lastDot); + } + + return filename; +} + +// #28937 Will be removed after binary import support is fully implemented. +__attribute__((unused)) static bool HandleMultiFileMode(Context *ctxImpl, const std::string &outputPath) +{ + std::string outputStem = GetFileNameByPath(outputPath); + auto &externalSources = ctxImpl->parserProgram->DirectExternalSources(); + + for (const auto &entry : externalSources) { + for (auto *prog : entry.second) { + if (prog == nullptr || !prog->IsGenAbcForExternal()) { + continue; + } + + if (prog->FileName().Mutf8() == outputStem) { + compiler::HandleGenerateDecl(*prog, *ctxImpl->diagnosticEngine, outputPath); + return !ctxImpl->diagnosticEngine->IsAnyError(); + } + } + } + + return false; +} + +// #28937 Will be removed after binary import support is fully implemented. extern "C" __attribute__((unused)) int GenerateStaticDeclarationsFromContext(es2panda_Context *ctx, const char *outputPath) { @@ -1293,8 +1334,15 @@ extern "C" __attribute__((unused)) int GenerateStaticDeclarationsFromContext(es2 if (ctxImpl->state != ES2PANDA_STATE_CHECKED) { return 1; } - compiler::HandleGenerateDecl(*ctxImpl->parserProgram, *ctxImpl->diagnosticEngine, outputPath); + // Multiple file mode + if (ctxImpl->config->options->IsSimultaneous()) { + bool success = HandleMultiFileMode(ctxImpl, outputPath); + return success ? 0 : 1; + } + + // Single file mode + compiler::HandleGenerateDecl(*ctxImpl->parserProgram, *ctxImpl->diagnosticEngine, outputPath); return ctxImpl->diagnosticEngine->IsAnyError() ? 1 : 0; } -- Gitee