Skip to content

Commit

Permalink
chore: update mksnapshot to work with arm*-x64 zips (#27)
Browse files Browse the repository at this point in the history
* chore: update mksnapshot to work with arm*-x64 zips

* Read mksnapshot args if it is provided

* Update documentation
  • Loading branch information
John Kleinschmidt committed May 13, 2020
1 parent baeb2a2 commit fcba03d
Show file tree
Hide file tree
Showing 6 changed files with 966 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -40,7 +40,7 @@ jobs:

install-test-osx:
macos:
xcode: "9.0"
xcode: "11.1.0"
steps:
- checkout

Expand Down
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -41,3 +41,27 @@ ELECTRON_MIRROR="https://npm.taobao.org/mirrors/electron/"
# Example of requested URL: http://localhost:8080/1.2.0/mksnapshot-v1.2.0-darwin-x64.zip
ELECTRON_MIRROR="http://localhost:8080/"
```
## Generating snapshots for ARM hardware

If you need to generate snapshots for Linux on 32 bit ARM, Linux on ARM64, or Windows on ARM64 you will need to install a cross arch mksnapshot on an Intel x64 machine. To do so, set the npm config `arch` to the proper arch and then run `npm install --save-dev electron-mksnapshot`. For example:

### Linux on ARM64
From an Intel x64 Linux OS run:
```sh
npm config set arch arm64
npm install --save-dev electron-mksnapshot
```

### Linux on 32 bit ARM
From an Intel x64 Linux OS run:
```sh
npm config set arch armv7l
npm install --save-dev electron-mksnapshot
```

### Windows on ARM (64-bit)
From an Intel x64 Windows OS run:
```sh
npm config set arch arm64
npm install --save-dev electron-mksnapshot
```
29 changes: 24 additions & 5 deletions download-mksnapshot.js
Expand Up @@ -4,9 +4,25 @@ var electronDownload = require('electron-download')
var extractZip = require('extract-zip')
var versionToDownload = require('./package').version
var archToDownload = process.env.npm_config_arch
if (archToDownload === 'arm64') {
archToDownload = 'arm64-x64'

if (process.arch.indexOf('arm') === 0) {
console.log(`WARNING: mksnapshot does not run on ${process.arch}. Download
https://github.com/electron/electron/releases/download/v${versionToDownload}/mksnapshot-v${versionToDownload}-${process.platform}-${process.arch}-x64.zip
on a x64 ${process.platform} OS to generate ${archToDownload} snapshots.`)
process.exit(1)
}

if (archToDownload && archToDownload.indexOf('arm') === 0) {
if (process.platform !== 'darwin') {
archToDownload += '-x64'
} else {
console.log(`WARNING: mksnapshot for ${archToDownload} is not available on macOS. Download
https://github.com/electron/electron/releases/download/v${versionToDownload}/mksnapshot-v${versionToDownload}-linux-${archToDownload}-x64.zip
on a x64 Linux OS to generate ${archToDownload} snapshots.`)
process.exit(1)
}
}

function download (version, callback) {
electronDownload({
version: version,
Expand All @@ -23,9 +39,12 @@ function processDownload (err, zipPath) {
extractZip(zipPath, { dir: path.join(__dirname, 'bin') }, function (error) {
if (error != null) throw error
if (process.platform !== 'win32') {
fs.chmod(path.join(__dirname, 'bin', 'mksnapshot'), '755', function (error) {
if (error != null) throw error
})
var mksnapshotPath = path.join(__dirname, 'bin', 'mksnapshot')
if (fs.existsSync(mksnapshotPath)) {
fs.chmod(path.join(__dirname, 'bin', 'mksnapshot'), '755', function (error) {
if (error != null) throw error
})
}
}
})
}
Expand Down
49 changes: 42 additions & 7 deletions mksnapshot.js
Expand Up @@ -5,6 +5,11 @@ 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') {
Expand Down Expand Up @@ -38,11 +43,6 @@ if (outDirIdx > -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')
Expand All @@ -52,13 +52,48 @@ const mksnapshotDir = path.join(__dirname, 'bin')
// 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', workingDir)
const mksnapshotCommand = getBinaryPath('mksnapshot', mksnapshotBinaryDir)
const mksnapshotProcess = spawnSync(mksnapshotCommand, mksnapshotArgs, options)
if (mksnapshotProcess.status !== 0) {
let code = mksnapshotProcess.status
Expand All @@ -75,7 +110,7 @@ if (args.includes('--help')) {
fs.copyFileSync(path.join(workingDir, 'snapshot_blob.bin'),
path.join(outputDir, 'snapshot_blob.bin'))

const v8ContextGenCommand = getBinaryPath('v8_context_snapshot_generator', workingDir)
const v8ContextGenCommand = getBinaryPath('v8_context_snapshot_generator', mksnapshotBinaryDir)
const v8ContextGenArgs = [
`--output_file=${path.join(outputDir, 'v8_context_snapshot.bin')}`
]
Expand Down

0 comments on commit fcba03d

Please sign in to comment.