• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import * as fs from 'fs';
17import * as path from 'path';
18
19import {
20  isLinux,
21  isMac,
22  isWindows,
23} from '../utils';
24import { PluginDriver } from '../plugins/plugins_driver';
25import {
26  BUILD_MODE,
27  KOALA_WRAPPER_PATH_FROM_SDK,
28  PANDA_SDK_PATH_FROM_SDK,
29  PROJECT_BUILD_CONFIG_FILE
30} from '../pre_define';
31import {
32  LogData,
33  LogDataFactory,
34  Logger
35} from '../logger';
36import { ErrorCode } from '../error_code';
37import { BuildConfig } from '../types';
38
39export function processBuildConfig(projectConfig: BuildConfig): BuildConfig {
40  let buildConfig: BuildConfig = {
41    ...projectConfig,
42    isBuildConfigModified: false
43  };
44  let buildSdkPath: string = buildConfig.buildSdkPath as string;
45  buildConfig.pandaSdkPath = buildConfig.pandaSdkPath ?? path.resolve(buildSdkPath, PANDA_SDK_PATH_FROM_SDK);
46  /**
47   * ets2panda guys require remove debug param
48   * it contain some bugs.
49   */
50  buildConfig.buildMode = BUILD_MODE.RELEASE;
51  checkCacheProjectConfig(buildConfig);
52  initPlatformSpecificConfig(buildConfig);
53  initBuildEnv(buildConfig);
54  initKoalaWrapper(buildConfig);
55  PluginDriver.getInstance().initPlugins(buildConfig);
56
57  return buildConfig;
58}
59
60function checkCacheProjectConfig(buildConfig: BuildConfig): void {
61  const cachePath = buildConfig.cachePath as string;
62  const projectionConfigPath = path.join(cachePath, PROJECT_BUILD_CONFIG_FILE);
63  const logger: Logger = Logger.getInstance();
64
65  if (!fs.existsSync(cachePath)) {
66    fs.mkdirSync(cachePath, { recursive: true });
67  }
68
69  if (!fs.existsSync(projectionConfigPath)) {
70    fs.writeFileSync(projectionConfigPath, JSON.stringify(buildConfig, null, '\t'));
71  } else {
72    const existingConfig = JSON.parse(fs.readFileSync(projectionConfigPath, 'utf8'));
73    if (!areConfigsEqual(existingConfig, buildConfig)) {
74      buildConfig.isBuildConfigModified = true;
75      fs.writeFileSync(projectionConfigPath, JSON.stringify(buildConfig, null, '\t'));
76    } else {
77      buildConfig.isBuildConfigModified = false;
78      logger.printInfo('projectionConfig.json is up to date.');
79    }
80  }
81}
82
83function areConfigsEqual(config1: BuildConfig, config2: BuildConfig): boolean {
84  const { isBuildConfigModified: _, compileFiles: __, ...rest1 } = config1;
85  const { isBuildConfigModified: ___, compileFiles: ____, ...rest2 } = config2;
86  return JSON.stringify(rest1) === JSON.stringify(rest2);
87}
88
89function initPlatformSpecificConfig(buildConfig: BuildConfig): void {
90  const pandaSdkPath: string = path.resolve(buildConfig.pandaSdkPath as string);
91  const logger: Logger = Logger.getInstance();
92  if (isWindows()) {
93    buildConfig.abcLinkerPath = path.join(pandaSdkPath, 'bin', 'ark_link.exe');
94    buildConfig.dependencyAnalyzerPath = path.join(pandaSdkPath, 'bin', 'dependency_analyzer.exe');
95  }
96
97  if (isMac() || isLinux()) {
98    buildConfig.abcLinkerPath = path.join(pandaSdkPath, 'bin', 'ark_link');
99    buildConfig.dependencyAnalyzerPath = path.join(pandaSdkPath, 'bin', 'dependency_analyzer');
100  }
101
102  if (!fs.existsSync(buildConfig.abcLinkerPath as string)) {
103    const logData: LogData = LogDataFactory.newInstance(
104      ErrorCode.BUILDSYSTEM_ARK_LINK_NOT_FOUND_FAIL,
105      'Ark_link not found in path.',
106      '',
107      buildConfig.abcLinkerPath as string
108    );
109    logger.printError(logData);
110  }
111
112  if (!buildConfig.frameworkMode && !fs.existsSync(buildConfig.dependencyAnalyzerPath as string)) {
113    const logData: LogData = LogDataFactory.newInstance(
114      ErrorCode.BUILDSYSTEM_Dependency_Analyzer_NOT_FOUND_FAIL,
115      'Dependency_analyzer not found in path.',
116      '',
117      buildConfig.dependencyAnalyzerPath as string
118    );
119    logger.printError(logData);
120  }
121}
122
123export function initBuildEnv(buildConfig: BuildConfig): void {
124  const pandaSdkPath: string = path.resolve(buildConfig.pandaSdkPath as string);
125  const currentPath: string | undefined = process.env.PATH;
126  const logger: Logger = Logger.getInstance();
127
128  let pandaLibPath: string = path.resolve(pandaSdkPath, 'lib');
129
130  process.env.PATH = `${currentPath}${path.delimiter}${pandaLibPath}`;
131  if (isMac()) {
132    process.env.DYLD_LIBRARY_PATH = `${currentPath}${path.delimiter}${pandaLibPath}`;
133  }
134  logger.printInfo(`Updated PATH: ${process.env.PATH}`);
135}
136
137function initKoalaWrapper(buildConfig: BuildConfig): void {
138  let koalaWrapperPath: string = path.resolve(buildConfig.buildSdkPath as string, KOALA_WRAPPER_PATH_FROM_SDK);
139  const { arkts, arktsGlobal } = require(koalaWrapperPath);
140  buildConfig.arkts = arkts;
141  buildConfig.arktsGlobal = arktsGlobal;
142  buildConfig.arktsGlobal.es2panda._SetUpSoPath(buildConfig.pandaSdkPath);
143}
144