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