• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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*/
15const path = require("path");
16const { Logger } = require("./logger");
17const { Tool } = require("./tool");
18const childProcess = require("child_process");
19const fs = require("fs");
20const { AnalyzeMake } = require("./analyze_make");
21
22class AnalyzeCMake {
23    constructor() {
24
25    }
26
27    static analyze(compileFile, cmakeArgs) {//在工程目录创建一个buildTmp目录,执行cmake初始化工程,执行make得到命令行序列
28        if (!fs.existsSync(path.join(Tool.OHOS_PROJECT_PATH, Tool.OHOS_PRODUCT_OUTPUT_PATH, "build.ninja"))) {
29            Logger.err("param ohos need to looks like out/rk3568");
30            return;
31        }
32
33        let buildTmp = path.join(compileFile.dir, "build_tmp");//cmake编译的临时目录
34        if (fs.existsSync(buildTmp)) {
35            fs.rmSync(buildTmp, { recursive: true, force: true });//��
36        }
37        fs.mkdirSync(buildTmp);
38        Tool.pushd(buildTmp);
39
40        let ohosToolchainCmake = Tool.getCMakeToolchain();
41        let ohosToolchainCmakeData = fs.readFileSync(ohosToolchainCmake, { encoding: "utf8" });
42        while (ohosToolchainCmakeData.indexOf("CC_REPLACE_OHOS_ROOT") >= 0) {
43            ohosToolchainCmakeData = ohosToolchainCmakeData.replace("CC_REPLACE_OHOS_ROOT",
44                Tool.swapPath(Tool.OHOS_PROJECT_PATH, true));
45        }
46        while (ohosToolchainCmakeData.indexOf("CC_REPLACE_OHOS_TARGET") >= 0) {
47            ohosToolchainCmakeData = ohosToolchainCmakeData.replace("CC_REPLACE_OHOS_TARGET",
48                Tool.OHOS_PRODUCT_OUTPUT_PATH);
49        }
50        ohosToolchainCmake = path.join(buildTmp, "ohos.toolchain.cmake");
51        fs.writeFileSync(ohosToolchainCmake, ohosToolchainCmakeData);
52
53        ohosToolchainCmake=Tool.swapPath(ohosToolchainCmake);
54        let args = ["..",
55            "-DCMAKE_TOOLCHAIN_FILE=%s".format(ohosToolchainCmake),
56            "-G",
57            "Unix Makefiles",
58            "-DCMAKE_MAKE_PROGRAM=%s".format(Tool.swapPath(Tool.getMakeRaw())),
59        ];
60        if (cmakeArgs.length > 0) {
61            args.push(...cmakeArgs.split(","));
62        }
63
64        let ret = childProcess.spawn(Tool.swapPath(Tool.getCMake()), args);
65        ret.stdout.on('data', (data) => {
66            Logger.info(data.toString());
67        });
68        ret.stderr.on('data', (data) => {
69            Logger.err(data.toString(), true);
70        });
71        ret.on('close', (code) => {
72            if (code == 0) {
73                Logger.info("------------------------cmake ok");
74                //调用make生成命令行
75                AnalyzeMake.analyze(path.join(buildTmp, "Makefile"));
76            }
77            else Logger.err("cmake fail");
78        })
79    }
80}
81
82module.exports = {
83    AnalyzeCMake
84}