From eced5eb6a9b2e0d702ce919d3b3d99e202e963a5 Mon Sep 17 00:00:00 2001 From: zenghang Date: Wed, 18 Jun 2025 15:03:41 +0800 Subject: [PATCH] add declgen cache && run parallel Issue: ICG1VW Signed-off-by: zenghang Change-Id: Iae72c1de262c3950fd65189691d77fd545b259dd --- .../interop/run_declgen_standalone.ts | 43 ++++++++++++++++--- .../fast_build/ark_compiler/interop/type.ts | 3 +- compiler/src/fast_build/ark_compiler/utils.ts | 8 ++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/compiler/src/fast_build/ark_compiler/interop/run_declgen_standalone.ts b/compiler/src/fast_build/ark_compiler/interop/run_declgen_standalone.ts index 418b0b5ec..b1cb71d67 100644 --- a/compiler/src/fast_build/ark_compiler/interop/run_declgen_standalone.ts +++ b/compiler/src/fast_build/ark_compiler/interop/run_declgen_standalone.ts @@ -25,6 +25,7 @@ import { ArkTSEvolutionModule, BuildType, DeclFilesConfig, + DECLGEN_CACHE_FILE, Params, ProjectConfig, RunnerParms @@ -34,7 +35,8 @@ import path from 'path'; import * as ts from 'typescript'; import { EXTNAME_D_ETS, EXTNAME_JS } from '../common/ark_define'; import { getRealModulePath } from '../../system_api/api_check_utils'; -import { generateInteropDecls } from '../../../../node_modules/declgen/build/src/generateInteropDecls'; +import { generateInteropDecls } from 'declgen/build/src/generateInteropDecls'; +import { calculateFileHash } from '../utils'; export function run(param: Params): boolean { FileManager.init(param.dependentModuleMap); @@ -121,18 +123,35 @@ class DeclfileProductor { } runDeclgen(moduleInfo: ArkTSEvolutionModule): void { + const cachePath = `${moduleInfo.declgenV2OutPath}/.${DECLGEN_CACHE_FILE}`; + let existingCache = {}; + const filesToProcess = []; + + if (fs.existsSync(cachePath)) { + existingCache = JSON.parse(fs.readFileSync(cachePath, 'utf-8')); + } + let inputList = []; + let hashMap = {}; moduleInfo.dynamicFiles.forEach(path => { - inputList.push(toUnixPath(path)); + let unixPath = toUnixPath(path); + const fileHash = calculateFileHash(path); + if (!existingCache[unixPath] || existingCache[unixPath] !== fileHash) { + filesToProcess.push(unixPath); + hashMap[unixPath] = fileHash; + } }); + if (filesToProcess.length === 0) { + return; + } readDeaclareFiles().forEach(path => { - inputList.push(toUnixPath(path)); + filesToProcess.push(toUnixPath(path)); }); inputList = inputList.filter(filePath => !filePath.endsWith('.js')); const config: RunnerParms = { inputDirs: [], - inputFiles: inputList, + inputFiles: filesToProcess, outDir: moduleInfo.declgenV2OutPath, // use package name as folder name rootDir: moduleInfo.modulePath, @@ -140,12 +159,17 @@ class DeclfileProductor { customCompilerOptions: DeclfileProductor.compilerOptions, includePaths: [moduleInfo.modulePath] }; - if (fs.existsSync(config.outDir)) { - fs.rmSync(config.outDir, { recursive: true, force: true }); + if (!fs.existsSync(config.outDir)) { + fs.mkdirSync(config.outDir, { recursive: true }); } fs.mkdirSync(config.outDir, { recursive: true }); generateInteropDecls(config); processInteropUI(FileManager.arkTSModuleMap.get(moduleInfo.packageName)?.declgenV2OutPath); + const newCache = { + ...existingCache, + ...hashMap + }; + fs.writeFileSync(cachePath, JSON.stringify(newCache, null, 2)); } writeDeclFileInfo(moduleInfo: ArkTSEvolutionModule, mainModuleName: string): void { @@ -155,7 +179,9 @@ class DeclfileProductor { const declFilesConfigFile: string = toUnixPath(moduleInfo.declFilesPath); mkdirsSync(path.dirname(declFilesConfigFile)); - fs.writeFileSync(declFilesConfigFile, JSON.stringify(this.pkgDeclFilesConfig[moduleInfo.packageName], null, 2), 'utf-8'); + if (this.pkgDeclFilesConfig[moduleInfo.packageName]) { + fs.writeFileSync(declFilesConfigFile, JSON.stringify(this.pkgDeclFilesConfig[moduleInfo.packageName], null, 2), 'utf-8'); + } } addDeclFilesConfig(filePath: string, mainModuleName: string, bundleName: string, moduleInfo: ArkTSEvolutionModule): void { @@ -168,6 +194,9 @@ class DeclfileProductor { if (!this.pkgDeclFilesConfig[moduleInfo.packageName]) { this.pkgDeclFilesConfig[moduleInfo.packageName] = { packageName: moduleInfo.packageName, files: {} }; } + if (filePath.endsWith(EXTNAME_JS)) { + return; + } if (this.pkgDeclFilesConfig[moduleInfo.packageName].files[projectFilePath]) { return; } diff --git a/compiler/src/fast_build/ark_compiler/interop/type.ts b/compiler/src/fast_build/ark_compiler/interop/type.ts index fdb5abe79..17d7165f9 100644 --- a/compiler/src/fast_build/ark_compiler/interop/type.ts +++ b/compiler/src/fast_build/ark_compiler/interop/type.ts @@ -115,4 +115,5 @@ export interface FileInfo { recordName: string; baseUrl: string; abstractPath: string; -} \ No newline at end of file +} +export const DECLGEN_CACHE_FILE = 'declgen_cache.json'; diff --git a/compiler/src/fast_build/ark_compiler/utils.ts b/compiler/src/fast_build/ark_compiler/utils.ts index 9b2bde4be..a4ad63964 100644 --- a/compiler/src/fast_build/ark_compiler/utils.ts +++ b/compiler/src/fast_build/ark_compiler/utils.ts @@ -19,6 +19,7 @@ import path from 'path'; import ts from 'typescript'; import os from 'os'; import sourceMap from 'source-map'; +import * as crypto from 'node:crypto'; import { DEBUG, @@ -323,4 +324,11 @@ export function isSubPathOf(targetPath: string, parentDir: string): boolean { const resolvedParent = toUnixPath(path.resolve(parentDir)); const resolvedTarget = toUnixPath(path.resolve(targetPath)); return resolvedTarget === resolvedParent || resolvedTarget.startsWith(resolvedParent + '/'); +} + +export function calculateFileHash(filePath: string, algorithm: string = 'sha256'): string { + const fileBuffer = fs.readFileSync(filePath); + const hash = crypto.createHash(algorithm); + hash.update(fileBuffer); + return hash.digest('hex'); } \ No newline at end of file -- Gitee