forked from achingbrain/webtransport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
298 lines (282 loc) · 7.44 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { arch, argv, env, platform } from 'node:process'
import { spawn } from 'node:child_process'
import { rename, mkdtemp, rm, access } from 'node:fs/promises'
import { constants } from 'node:fs'
import path from 'node:path'
import pkg from './package.json' assert { type: 'json' }
import { tmpdir } from 'node:os'
const binplatform = platform + '_' + arch
console.log(
'Webtransport binary handler: We are working on platform ',
binplatform
)
const callGit = (args, opts) => {
// console.log('callgit', args)
return new Promise((resolve, reject) => {
const git = spawn('git', args, {
cwd: (opts && opts.cwd) || process.cwd(),
shell: true
})
const output = []
git.stdout.on('data', (data) => {
output.push(data)
})
git.stderr.on('data', (data) => {
console.error(data.toString())
})
git.on('close', (code) => {
if (code === 0) resolve(output.join(''))
else {
reject(output.join(''))
}
})
})
}
const extractthirdparty = async () => {
let tmppath
let fatal = false
console.error('Now extracting third party lib code from git....')
const destdir = process.cwd() + '/third_party'
let exists = false
try {
await access(destdir, constants.F_OK)
exists = true
} catch (error) {
// ok this is ok
}
if (exists) {
console.error(
'Destination dir already exists: ' + destdir + ', skip downloading.'
)
return
}
try {
console.log('tmpdir', tmpdir())
tmppath = await mkdtemp(path.join(tmpdir(), 'wtbuild-'))
} catch (error) {
console.error('mktmp dir', error)
throw new Error('Cannot generate tmp dir')
}
try {
await callGit(
[
'clone',
'--branch',
`v${pkg.version}`,
'--depth',
'1',
pkg.repository.url.replace('git+https', 'https')
],
{ cwd: tmppath }
)
const copath = tmppath + '/webtransport'
const submodules = [
'third_party/boringssl/src',
'third_party/abseil-cpp',
'third_party/quiche',
'third_party/zlib',
'third_party/googleurl',
'third_party/libevent',
'third_party/protobuf',
'third_party/icu'
]
for (let mod in submodules) {
const sub = submodules[mod]
await callGit(
[
'config',
'-f',
'.gitmodules',
'submodule.' + sub + '.shallow',
'true'
],
{ cwd: copath }
)
await callGit(['submodule', 'update', '--init', '--recursive', sub], {
cwd: copath
})
}
try {
await rm(destdir, { recursive: true, maxRetries: 10 })
} catch (err) {
console.log('destdir does not exist: ', err)
}
await rename(path.join(copath, '/third_party'), destdir)
} catch (error) {
console.error('failed to get third party code from git', error)
fatal = true
}
try {
await rm(tmppath, { recursive: true, maxRetries: 10 })
} catch (error) {
console.error('failed to remove temp dir ', error)
}
if (fatal) throw new Error('Cannot get thirdparty code')
console.error('Extracting third party lib code from git finished.')
}
const prebuild = (args) => {
return new Promise((resolve, reject) => {
const pb = 'npx'
// if (platform === 'win32') cmakejs = 'cmake-js.exe'
const proc = spawn(pb, ['prebuild', ...args], {
cwd: process.cwd(),
stdio: [null, 'inherit', 'inherit'],
shell: true
})
proc.on('close', (code) => {
console.log(`child process exited with code ${code}`)
if (code == 0) resolve()
else reject()
})
})
}
const prebuildInstall = async (args) => {
return new Promise((resolve, reject) => {
const pb = 'prebuild-install'
// if (platform === 'win32') cmakejs = 'cmake-js.exe'
const proc = spawn(pb, args, {
cwd: process.cwd(),
stdio: [null, 'inherit', 'inherit'],
shell: true
})
proc.on('close', (code) => {
if (code === 0) resolve(code)
else reject(code)
console.log(`child process exited with code ${code}`)
})
})
}
const buildTypes = async () => {
return new Promise((resolve, reject) => {
const tsc = 'tsc'
const proc = spawn(tsc, {
cwd: process.cwd(),
stdio: [null, 'inherit', 'inherit'],
shell: true
})
proc.on('close', (code) => {
if (code === 0) resolve(code)
else reject(code)
console.log(`child process exited with code ${code}`)
})
})
}
const execbuild = async (args) => {
return new Promise((resolve, reject) => {
const cmakejs = 'cmake-js'
// if (platform === 'win32') cmakejs = 'cmake-js.exe'
const proc = spawn(cmakejs, args, {
cwd: process.cwd(),
stdio: [null, 'inherit', 'inherit'],
shell: true
})
proc.on('close', (code) => {
if (code === 0) resolve(code)
else reject(code)
console.log(`child process exited with code ${code}`)
})
})
}
if (argv.length > 2) {
const target = pkg.binary.napi_versions
const platformargs = [
'--CDnapi_build_version=' + target,
'-O',
'build_' + binplatform
]
if (platform === 'win32') platformargs.push('-t', 'ClangCL')
const pbargs = []
const pbargspre = []
if (platform === 'win32') pbargs.push('-t', 'ClangCL')
if (env.BUILDARCH) {
pbargspre.push('--arch', env.BUILDARCH)
platformargs.push('--arch', env.BUILDARCH)
}
if (env.GH_TOKEN) pbargspre.push('--u', env.GH_TOKEN)
switch (argv[2]) {
case 'prebuild':
try {
await prebuild([
'-t',
'6',
'-r',
'napi',
'--strip',
...pbargspre,
'--backend',
'cmake-js',
'--',
...pbargs
])
} catch (error) {
console.log('prebuild failed')
process.exit(1)
}
break
case 'install': {
try {
const pbres = await prebuildInstall([
'-r',
'napi',
'-d',
'-t',
'6',
'--verbose'
])
if (pbres === 0) break
} catch (error) {
console.error(
'No prebuild available, building binary, this may take more than 20 minutes'
)
}
try {
// if we do not succeed, we have to build it ourselves
await extractthirdparty()
} catch (error) {
console.error('Building binary failed: ', error)
}
}
// eslint-disable-next-line no-fallthrough
case 'build':
try {
await execbuild(['build', ...platformargs])
await buildTypes()
} catch (error) {
console.error('Building binary failed: ', error)
process.exit(1)
}
break
case 'rebuild':
try {
await execbuild(['rebuild', ...platformargs])
await buildTypes()
} catch (error) {
console.error('ReBuilding binary failed: ', error)
process.exit(1)
}
break
case 'build-debug':
try {
await execbuild(['build', '-D', ...platformargs])
await buildTypes()
} catch (error) {
console.error('Building binary failed: ', error)
process.exit(1)
}
break
case 'rebuild-debug':
try {
await execbuild(['rebuild', '-D', ...platformargs])
await buildTypes()
} catch (error) {
console.error('ReBuilding binary failed: ', error)
process.exit(1)
}
break
default:
console.log('unsupported argument ', argv[2])
break
}
} else {
console.log('Please supply a task as argument')
}