-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Expand file tree
/
Copy pathdocker-native-build.js
More file actions
executable file
·227 lines (200 loc) · 5.87 KB
/
Copy pathdocker-native-build.js
File metadata and controls
executable file
·227 lines (200 loc) · 5.87 KB
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
#!/usr/bin/env node
// Local wrapper for running native docker builds.
//
// Usage: node scripts/docker-native-build.js [flags] [filter]
// --quick Use release-with-assertions profile (no LTO, faster)
// --host-target Share host target/ dir with container for caching
// --rebuild Force a fresh Docker image build (docker build --no-cache)
// --test Smoke-test built binaries (native arch only)
// filter Substring match on target name (e.g. "musl", "x86_64")
const { execFileSync } = require('child_process')
const path = require('path')
const fs = require('fs')
const os = require('os')
const REPO_ROOT = path.resolve(__dirname, '..')
const DOCKER_IMAGE = 'next-swc-builder:latest'
const TARGETS = [
{
target: 'x86_64-unknown-linux-gnu',
arch: 'x86_64',
abi: 'gnu',
napiPlatform: 'linux-x64-gnu',
},
{
target: 'aarch64-unknown-linux-gnu',
arch: 'aarch64',
abi: 'gnu',
napiPlatform: 'linux-arm64-gnu',
},
{
target: 'x86_64-unknown-linux-musl',
arch: 'x86_64',
abi: 'musl',
napiPlatform: 'linux-x64-musl',
},
{
target: 'aarch64-unknown-linux-musl',
arch: 'aarch64',
abi: 'musl',
napiPlatform: 'linux-arm64-musl',
},
]
// Map uname -m to our arch names
const HOST_ARCH =
os.arch() === 'arm64' || os.arch() === 'aarch64' ? 'aarch64' : 'x86_64'
// --- Parse args ---
const { parseArgs } = require('node:util')
const { values: flags, positionals } = parseArgs({
args: process.argv.slice(2),
options: {
quick: { type: 'boolean', default: false },
'host-target': { type: 'boolean', default: false },
rebuild: { type: 'boolean', default: false },
test: { type: 'boolean', default: false },
help: { type: 'boolean', short: 'h', default: false },
},
allowPositionals: true,
})
if (flags.help) {
console.log(
'Usage: node scripts/docker-native-build.js [--quick] [--host-target] [--rebuild] [--test] [filter]'
)
process.exit(0)
}
const quick = flags.quick
const hostTarget = flags['host-target']
const rebuild = flags.rebuild
const test = flags.test
const filter = positionals[0] || ''
// --- Filter targets ---
let targets = TARGETS
if (filter) {
targets = TARGETS.filter((t) => t.target.includes(filter))
}
if (targets.length === 0) {
console.error(`No targets match filter: "${filter}"`)
console.error('Available:', TARGETS.map((t) => t.target).join(', '))
process.exit(1)
}
function buildImage() {
console.log(`Building Docker image: ${DOCKER_IMAGE}`)
const ctx = fs.mkdtempSync(path.join(os.tmpdir(), 'next-swc-docker-'))
fs.copyFileSync(
path.join(REPO_ROOT, 'rust-toolchain.toml'),
path.join(ctx, 'rust-toolchain.toml')
)
try {
execFileSync(
'docker',
[
'build',
...(rebuild ? ['--no-cache'] : []),
'-t',
DOCKER_IMAGE,
'-f',
path.join(REPO_ROOT, 'scripts/native-builder.Dockerfile'),
ctx,
],
{ stdio: 'inherit' }
)
} finally {
fs.rmSync(ctx, { recursive: true, force: true })
}
}
buildImage()
// --- Build targets ---
const buildTask = quick
? 'build-native-release-with-assertions'
: 'build-native-release'
if (quick) {
console.log(
'Quick mode: using release-with-assertions profile (no LTO, 64 codegen units)'
)
}
console.log(
`Building ${targets.length} target(s): ${targets.map((t) => t.target).join(', ')}\n`
)
const HOME = os.homedir()
const VOLUMES = [
`${HOME}/.cargo/git:/root/.cargo/git`,
`${HOME}/.cargo/registry:/root/.cargo/registry`,
`${REPO_ROOT}:/build`,
]
for (const { target, arch, abi, napiPlatform } of targets) {
console.log('='.repeat(50))
console.log(`Building: ${target}`)
console.log(`Docker: ${DOCKER_IMAGE}`)
console.log(`Task: ${buildTask}`)
console.log('='.repeat(50))
// Clean only this target's previous build (preserve other targets' .node files)
const nativeDir = path.join(REPO_ROOT, 'packages/next-swc/native')
const nodeFile = path.join(nativeDir, `next-swc.${napiPlatform}.node`)
if (fs.existsSync(nodeFile)) fs.unlinkSync(nodeFile)
const ENV = {
CI: '1',
RUST_BACKTRACE: '1',
CARGO_TERM_COLOR: 'always',
CARGO_INCREMENTAL: '0',
TARGET: target,
ABI: abi,
ARCH: arch,
BUILD_TASK: buildTask,
}
const dockerArgs = [
'run',
'--rm',
...Object.entries(ENV).flatMap(([k, v]) => ['-e', `${k}=${v}`]),
...VOLUMES.flatMap((v) => ['-v', v]),
...(hostTarget ? [] : ['-v', '/build/target']),
'-w',
'/build',
'--entrypoint',
'bash',
DOCKER_IMAGE,
'-xeo',
'pipefail',
'scripts/docker-native-build.sh',
]
execFileSync('docker', dockerArgs, { stdio: 'inherit' })
console.log(`\nSuccessfully built: ${target}\n`)
}
// --- Smoke test ---
if (test) {
console.log('='.repeat(50))
console.log('Running smoke tests...')
console.log('='.repeat(50))
for (const { target, arch, abi, napiPlatform } of targets) {
// Skip cross-built binaries (would need qemu)
if (arch !== HOST_ARCH) {
console.log(`Skipping smoke test for ${target} (cross-built, needs qemu)`)
continue
}
const testImage = abi === 'musl' ? 'node:20-alpine' : 'node:20-slim'
const nodeFile = `./packages/next-swc/native/next-swc.${napiPlatform}.node`
console.log(`Testing ${target} in ${testImage}...`)
const testScript = [
`const b = require('${nodeFile}')`,
`const t = b.getTargetTriple()`,
`console.log('OK: getTargetTriple() =', t)`,
`if (!t.includes('linux')) { console.error('FAIL: expected linux in triple'); process.exit(1) }`,
].join('; ')
execFileSync(
'docker',
[
'run',
'--rm',
'-v',
`${REPO_ROOT}:/work`,
'-w',
'/work',
testImage,
'node',
'-e',
testScript,
],
{ stdio: 'inherit' }
)
console.log(`Smoke test passed: ${target}\n`)
}
}
console.log('All targets built successfully!')