Skip to content

Commit

Permalink
Merge pull request #10 from electron/3-0-x
Browse files Browse the repository at this point in the history
build: update mksnapshot for 3-0-x
  • Loading branch information
John Kleinschmidt committed Dec 10, 2018
2 parents 53f8337 + c5354e0 commit ad2f2c0
Show file tree
Hide file tree
Showing 7 changed files with 2,342 additions and 33 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
@@ -1,7 +1,7 @@
language: node_js

node_js:
- "6"
- "10"

branches:
only:
Expand All @@ -15,3 +15,6 @@ notifications:
email:
on_success: never
on_failure: change

script:
- npm test
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -22,8 +22,12 @@ an `electron-mksnapshot` dependency of `~2.0.0` in your `package.json` file.

```sh
npm install --save-dev electron-mksnapshot
mksnapshot --help
mksnapshot.js file.js (--output_dir OUTPUT_DIR).
```
Running mksnapshot.js will generate both a snapshot_blob.bin and v8_context_snapshot.bin files which
are needed to use custom snapshots in Electron.
If an output directory isn't specified, the current directory will be used.
(Additional mksnapshot args except for --startup_blob are supported, run mksnapshot --help to see options)

## Custom Mirror

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -7,7 +7,7 @@ branches:
- master

environment:
nodejs_version: "6"
nodejs_version: "10"

cache:
- node_modules -> package.json
Expand Down
107 changes: 89 additions & 18 deletions mksnapshot.js
@@ -1,30 +1,101 @@
#!/usr/bin/env node

var ChildProcess = require('child_process')
var path = require('path')
const fs = require('fs')
const { spawnSync } = require('child_process')
const path = require('path')
const temp = require('temp').track()
const workingDir = temp.mkdirSync('mksnapshot-workdir')

var command = path.join(__dirname, 'bin', 'mksnapshot')
var args = process.argv.slice(2)
var options = {
cwd: process.cwd(),
/*
* Copy mksnapshot files to temporary working directory because
* v8_context_snapshot_generator expects to run everything from the same
* directory.
*/
function copyMksnapshotFiles (mksnapshotDir, workingDir) {
const mksnapshotFiles = fs.readdirSync(mksnapshotDir)
mksnapshotFiles.forEach(file => {
fs.copyFileSync(path.join(mksnapshotDir, file), path.join(workingDir, file))
})
}

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)
} else {
mksnapshotArgs = mksnapshotArgs.concat(['--startup_blob', 'snapshot_blob.bin'])
}
if (!mksnapshotArgs.includes('--turbo_instruction_scheduling')) {
mksnapshotArgs.push('--turbo_instruction_scheduling')
}

const mksnapshotDir = path.join(__dirname, 'bin')
copyMksnapshotFiles(mksnapshotDir, workingDir)

const options = {
cwd: workingDir,
env: process.env,
stdio: 'inherit'
}

var mksnapshotProcess = ChildProcess.spawn(command, args, options)
mksnapshotProcess.on('exit', function (code, signal) {
if (code == null && signal === 'SIGILL') {
const mksnapshotCommand = getBinaryPath('mksnapshot', workingDir)
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)
})

var killMksnapshot = function () {
try {
mksnapshotProcess.kill()
} catch (ignored) {
}
}
if (args.includes('--help')) {
process.exit(0)
}

fs.copyFileSync(path.join(workingDir, 'snapshot_blob.bin'),
path.join(outputDir, 'snapshot_blob.bin'))

process.on('exit', killMksnapshot)
process.on('SIGTERM', killMksnapshot)
const v8ContextGenCommand = getBinaryPath('v8_context_snapshot_generator', workingDir)
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)

0 comments on commit ad2f2c0

Please sign in to comment.