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