-
Notifications
You must be signed in to change notification settings - Fork 0
/
mksnapshot.js
executable file
·128 lines (117 loc) · 4.23 KB
/
mksnapshot.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
#!/usr/bin/env node
const fs = require('fs-extra')
const { spawnSync } = require('child_process')
const path = require('path')
const temp = require('temp').track()
const workingDir = temp.mkdirSync('mksnapshot-workdir')
const crossArchDirs = [
'clang_x86_v8_arm',
'clang_x64_v8_arm64',
'win_clang_x64'
]
function getBinaryPath (binary, binaryPath) {
if (process.platform === 'win32') {
return path.join(binaryPath, `${binary}.exe`)
} else {
return path.join(binaryPath, binary)
}
}
const args = process.argv.slice(2)
if (args.length === 0 || args.includes('--help')) {
console.log('Usage: mksnapshot file.js (--output_dir OUTPUT_DIR). ' +
'Additional mksnapshot args except for --startup_blob are supported:')
args.push('--help')
}
const outDirIdx = args.indexOf('--output_dir')
let outputDir = process.cwd()
let mksnapshotArgs = args
if (outDirIdx > -1) {
mksnapshotArgs = args.slice(0, outDirIdx)
if (args.length >= (outDirIdx + 2)) {
outputDir = args[(outDirIdx + 1)]
if (args.length > (outDirIdx + 2)) {
mksnapshotArgs = mksnapshotArgs.concat(args.slice(outDirIdx + 2))
}
} else {
console.log('Error! Output directory argument given but directory not specified.')
process.exit(1)
}
}
if (args.includes('--startup_blob')) {
console.log('--startup_blob argument not supported. Use --output_dir to specify where to output snapshot_blob.bin')
process.exit(1)
}
const mksnapshotDir = path.join(__dirname, 'bin')
// Copy mksnapshot files to temporary working directory because
// v8_context_snapshot_generator expects to run everything from the same
// directory.
fs.copySync(mksnapshotDir, workingDir)
const argsFile = path.join(mksnapshotDir, 'mksnapshot_args')
let mksnapshotBinaryDir = workingDir
if (fs.existsSync(argsFile)) {
// Use args from args file if it is provided as these match what is used to generate the original snapshot
const mksnapshotArgsFile = fs.readFileSync(argsFile, 'utf8')
const newlineRegEx = /(\r\n|\r|\n)/g
const mksnapshotArgsFromFile = mksnapshotArgsFile.split(newlineRegEx).filter((arg) => {
return (!arg.match(newlineRegEx) && arg !== '')
})
const mksnapshotBinaryPath = path.parse(mksnapshotArgsFromFile[0])
if (mksnapshotBinaryPath.dir) {
mksnapshotBinaryDir = path.join(workingDir, mksnapshotBinaryPath.dir)
}
mksnapshotArgs = mksnapshotArgs.concat(mksnapshotArgsFromFile.slice(1))
} else {
mksnapshotArgs = mksnapshotArgs.concat(['--startup_blob', 'snapshot_blob.bin'])
if (!mksnapshotArgs.includes('--turbo_instruction_scheduling')) {
mksnapshotArgs.push('--turbo_instruction_scheduling')
}
if (!fs.existsSync(getBinaryPath('mksnapshot', mksnapshotBinaryDir))) {
const matchingDir = crossArchDirs.find((crossArchDir) => {
const candidatePath = path.join(mksnapshotBinaryDir, crossArchDir)
if (fs.existsSync(getBinaryPath('mksnapshot', candidatePath))) {
return true
}
})
if (matchingDir) {
mksnapshotBinaryDir = path.join(workingDir, matchingDir)
} else {
console.log('ERROR: Could not find mksnapshot')
process.exit(1)
}
}
}
const options = {
cwd: workingDir,
env: process.env,
stdio: 'inherit'
}
const mksnapshotCommand = getBinaryPath('mksnapshot', mksnapshotBinaryDir)
const mksnapshotProcess = spawnSync(mksnapshotCommand, mksnapshotArgs, options)
if (mksnapshotProcess.status !== 0) {
let code = mksnapshotProcess.status
if (code == null && mksnapshotProcess.signal === 'SIGILL') {
code = 1
}
console.log('Error running mksnapshot.')
process.exit(code)
}
if (args.includes('--help')) {
process.exit(0)
}
fs.copyFileSync(path.join(workingDir, 'snapshot_blob.bin'),
path.join(outputDir, 'snapshot_blob.bin'))
const v8ContextGenCommand = getBinaryPath('v8_context_snapshot_generator', mksnapshotBinaryDir)
const v8ContextGenArgs = [
`--output_file=${path.join(outputDir, 'v8_context_snapshot.bin')}`
]
const v8ContextGenOptions = {
cwd: mksnapshotDir,
env: process.env,
stdio: 'inherit'
}
const v8ContextGenProcess = spawnSync(v8ContextGenCommand, v8ContextGenArgs, v8ContextGenOptions)
if (v8ContextGenProcess.status !== 0) {
console.log('Error running the v8 context snapshot generator.', v8ContextGenProcess)
process.exit(v8ContextGenProcess.status)
}
process.exit(0)