1 | const chalk = require('chalk')
|
2 | const path = require('path')
|
3 | const shelljs = require('shelljs')
|
4 | const program = require('commander')
|
5 | const os = require('os')
|
6 | const fs = require('fs-extra')
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | program
|
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 |
|
26 | const qmakePath = '/usr/lib/qt5/bin/qmake'
|
27 |
|
28 | const compileConfig = {
|
29 |
|
30 | buildPath: program.build,
|
31 |
|
32 | pdkPath: program.pdk,
|
33 |
|
34 | targetName: program.target,
|
35 |
|
36 | proPath: program.pro,
|
37 |
|
38 | qmakeArgs: program.qmakeArgs,
|
39 |
|
40 | processNum: program.num,
|
41 |
|
42 | debug: !!program.debug,
|
43 |
|
44 | exConfig: program.exConfig
|
45 | }
|
46 |
|
47 | class 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 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | shelljs.cd(currentDir)
|
86 | }
|
87 |
|
88 | composeQmakeArgs () {
|
89 | const qmakeArgs = []
|
90 |
|
91 | if (this.conf.debug) {
|
92 | qmakeArgs.push('CONFIG+=qml_debug')
|
93 | } else {
|
94 | qmakeArgs.push('CONFIG+=release')
|
95 | }
|
96 |
|
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 |
|
151 | const compiler = new PluginCompiler(compileConfig)
|
152 | compiler.build()
|