UNPKG

4.34 kBJavaScriptView Raw
1const chalk = require('chalk')
2const path = require('path')
3const shelljs = require('shelljs')
4const program = require('commander')
5const os = require('os')
6const fs = require('fs-extra')
7
8/**
9 * 调用示例:
10 * node scripts/pluginCompile.js --build /home/abeir/workspace/syberos/build --pdk /home/abeir/Syberos-Pdk \
11 * --target target-armv7tnhl-xuanwu --pro /home/abeir/workspace/syberos/syberh-plugins/audio \
12 * --qmakeArgs SYBERH_APP=/home/abeir/workspace/syberos/test/example/platforms/syberos,SOPID=com.syberos.example
13 */
14
15program
16 .option('--build [buildPath]', '编译输出目录')
17 .option('--pdk [pdkPath]', 'Syberos-Pdk所在位置')
18 .option('--target [targetName]', 'target完整名称:target-armv7tnhl-xuanwu')
19 .option('--pro [proPath]', '编译的项目pro文件位置')
20 .option('--qmakeArgs [qmakeArgs]', '额外的qmake编译参数', (v) => v.split(','), [])
21 .option('--num [processNum]', '并发编译进程数,为空时使用cpu数')
22 .option('--debug', '编译debug版本')
23 .option('--exConfig [exConfig]', '扩展参数')
24 .parse(process.argv)
25
26const qmakePath = '/usr/lib/qt5/bin/qmake'
27
28const compileConfig = {
29 // 编译输出目录
30 buildPath: program.build,
31 // Syberos-Pdk所在位置
32 pdkPath: program.pdk,
33 // target完整名称:target-armv7tnhl-xuanwu
34 targetName: program.target,
35 // 编译的项目pro文件位置
36 proPath: program.pro,
37 // 额外的qmake编译参数
38 qmakeArgs: program.qmakeArgs,
39 // 并发编译进程数,为空时使用cpu数
40 processNum: program.num,
41 // 是否编译debug版本
42 debug: !!program.debug,
43 // 扩展参数(字符串)
44 exConfig: program.exConfig
45}
46
47class PluginCompiler {
48 constructor(conf) {
49 this.conf = { ...conf }
50 }
51
52 build () {
53 this.verbose('PluginCompiler.build() conf: %j', this.conf)
54
55 const errorMessage = this.checkConf()
56 if (errorMessage) {
57 console.log(chalk.redBright(errorMessage))
58 return
59 }
60
61 const currentDir = shelljs.pwd()
62
63 if (!fs.existsSync(this.conf.buildPath)) {
64 fs.mkdirsSync(this.conf.buildPath)
65 }
66 shelljs.cd(this.conf.buildPath)
67
68 const kchrootPath = path.join(this.conf.pdkPath, 'sdk/script/kchroot')
69 const qmakeArgs = this.composeQmakeArgs()
70
71 const setup1 = `${kchrootPath} 'sb2 -t ${this.conf.targetName} -R' '${qmakePath} ${this.conf.proPath} -r -spec linux-g++ ${qmakeArgs}'`
72 const setup2 = `${kchrootPath} 'sb2 -t ${this.conf.targetName} -R' '/usr/bin/make -j${this.conf.processNum}'`
73
74 shelljs.exec(setup1)
75 shelljs.exec(setup2)
76
77 // console.log('============')
78 // console.log(this.conf.buildPath)
79 // console.log('------------')
80 // console.log(setup1)
81 // console.log('------------')
82 // console.log(setup2)
83 // console.log('============')
84
85 shelljs.cd(currentDir)
86 }
87
88 composeQmakeArgs () {
89 const qmakeArgs = []
90 // debug
91 if (this.conf.debug) {
92 qmakeArgs.push('CONFIG+=qml_debug')
93 } else {
94 qmakeArgs.push('CONFIG+=release')
95 }
96 // EX_CONFIG
97 if (this.conf.exConfig) {
98 qmakeArgs.push('EX_CONFIG=' + this.conf.exConfig)
99 }
100
101 if (this.conf.qmakeArgs && this.conf.qmakeArgs.length > 0) {
102 qmakeArgs.push(...this.conf.qmakeArgs)
103 }
104 if (qmakeArgs.length > 0) {
105 return qmakeArgs.join(' ')
106 }
107 return ''
108 }
109
110 cpuNum () {
111 return os.cpus().length
112 }
113
114 verbose (msg, ...args) {
115 console.log(msg, ...args)
116 }
117
118 checkConf () {
119 if (!this.conf) {
120 return this.failMessage('编译配置')
121 }
122 if (!this.conf.buildPath) {
123 return this.failMessage('buildPath')
124 }
125 if (!this.conf.pdkPath) {
126 return this.failMessage('pdkPath')
127 }
128 if (!fs.existsSync(this.conf.pdkPath)) {
129 return '未找到Syberos-Pdk所在位置: ' + this.conf.pdkPath
130 }
131 if (!this.conf.targetName) {
132 return this.failMessage('targetName')
133 }
134 if (!this.conf.proPath) {
135 return this.failMessage('proPath')
136 }
137 if (!fs.existsSync(this.conf.proPath)) {
138 return '未找到pro文件: ' + this.conf.proPath
139 }
140 if (!this.conf.processNum || this.conf.processNum <= 0) {
141 this.conf.processNum = this.cpuNum()
142 }
143 return null
144 }
145
146 failMessage (value) {
147 return `未设置${value}参数`
148 }
149}
150
151const compiler = new PluginCompiler(compileConfig)
152compiler.build()