diff --git a/android/package.json b/android/package.json index 9956fe286e3..d45f424b387 100644 --- a/android/package.json +++ b/android/package.json @@ -20,7 +20,7 @@ "v8": { "version": "7.8.279.23", "mode": "release", - "checksum": "3d68aa597fe26d142b7f1373906d82bad835849d13ef2fb53aac0e55c9f3d0fff87d5c28719cf1ccedbd8e807c3805ae5df0cd3d5411261c9e33fb1f3cb06d7f" + "checksum": "b7e3768e95463ea77842c54f6d46b87e9f79761ff2e351d770af23943e11e26bb9066eaa9966f621085ff79cba4a972e339038afefbf83d462917d708a2b0c43" }, "minSDKVersion": "19", "compileSDKVersion": "29", diff --git a/build/lib/android/V8Snapshots.h.ejs b/build/lib/android/V8Snapshots.h.ejs deleted file mode 100644 index 7399eafe4ee..00000000000 --- a/build/lib/android/V8Snapshots.h.ejs +++ /dev/null @@ -1,28 +0,0 @@ -<% if (blobs && blobs.length > 0) { -%> -#ifndef V8_SNAPSHOT_H -#define V8_SNAPSHOT_H - -#include - -<% function DEFINE_SNAPSHOT (target, blob) { -%> -<% if (!blob) return -%> -#ifdef __<%- target -%>__ -static const unsigned char snapshot_data[] = { -<% for (let i = 0; i < blob.length - 1; i++) { -%> -<%- blob.readUInt8(i) + ',' -%> -<% } -%> -<%- blob.readUInt8(blob.length - 1) %> -}; - -static const int snapshot_size = sizeof(snapshot_data); -static v8::StartupData snapshot = { (const char*) snapshot_data, snapshot_size }; -#endif -<% } -%> -<% if (blobs.x86) DEFINE_SNAPSHOT('i386', blobs.x86) -%> -<% if (blobs.arm) DEFINE_SNAPSHOT('arm', blobs.arm) -%> -<% if (blobs.arm64) DEFINE_SNAPSHOT('aarch64', blobs.arm64) -%> - -#endif -<% } else { -%> -// GENERATED AT BUILD TIME -<% } -%> \ No newline at end of file diff --git a/build/lib/android/snapshot.js b/build/lib/android/snapshot.js index cbc1a03b0ab..ff1850194a6 100644 --- a/build/lib/android/snapshot.js +++ b/build/lib/android/snapshot.js @@ -1,11 +1,8 @@ 'use strict'; -const promisify = require('util').promisify; -const exec = require('child_process').execFile; // eslint-disable-line security/detect-child-process -const os = require('os'); const fs = require('fs-extra'); const path = require('path'); -const ejs = require('ejs'); +const request = require('request-promise-native'); const ROOT_DIR = path.join(__dirname, '..', '..', '..'); const DIST_DIR = path.join(ROOT_DIR, 'dist'); @@ -13,102 +10,65 @@ const TMP_DIR = path.join(DIST_DIR, 'tmp'); const ANDROID_DIR = path.join(ROOT_DIR, 'android'); const ANDROID_PROPS = require(path.join(ANDROID_DIR, 'package.json')); // eslint-disable-line security/detect-non-literal-require const V8_PROPS = ANDROID_PROPS.v8; -const V8_LIB_DIR = path.join(ROOT_DIR, 'dist', 'android', 'libv8', V8_PROPS.version, V8_PROPS.mode, 'libs'); - -// Obtain target architectures -const TARGETS = []; -for (const target of ANDROID_PROPS.architectures) { - if (target === 'arm64-v8a') { - TARGETS.push('arm64'); - } else if (target === 'armeabi-v7a') { - TARGETS.push('arm'); - } else { - TARGETS.push(target); - } -} /** - * Runs mksnapshot to generate snapshots for each architecture - * @param {string} target targets to generate blob + * Generates empty snapshot blobs for each supported architecture + * and creates a header to include the snapshots at build time. * @returns {Promise} */ -async function generateBlob(target) { - const V8_LIB_TARGET_DIR = path.resolve(V8_LIB_DIR, target); - const MKSNAPSHOT_PATH = path.join(V8_LIB_TARGET_DIR, 'mksnapshot'); - const BLOB_PATH = path.join(V8_LIB_TARGET_DIR, 'blob.bin'); - const STARTUP_PATH = path.join(TMP_DIR, 'startup.js'); - const TI_MAIN_PATH = path.join(ROOT_DIR, 'common', 'Resources', 'ti.main.js'); - const TI_MAIN_PLATFORM_PATH = path.join(TMP_DIR, 'common', 'android', 'ti.main.js'); - const args = [ - '--startup_blob=' + BLOB_PATH, - STARTUP_PATH, - '--print-all-exceptions' - ]; - - // Snapshot already exists, skip... - if (await fs.exists(BLOB_PATH)) { - const { blobStat, commonStat } = await Promise.all([ fs.stat(BLOB_PATH), fs.stat(TI_MAIN_PATH) ]); - if (commonStat && blobStat && commonStat.mtime < blobStat.mtime) { - return; - } - } +async function build() { + const v8 = V8_PROPS.version; + const script = (await fs.readFile(path.join(TMP_DIR, 'common', 'android', 'ti.main.js'))).toString(); - // Load platform optimized 'common' bundle - const commonBundle = await fs.readFile(TI_MAIN_PLATFORM_PATH); + return new Promise(async resolve => { // eslint-disable-line no-async-promise-executor - // Generate 'startup.js' - const output = await promisify(ejs.renderFile)(path.join(__dirname, 'startup.ejs'), { script: commonBundle }, {}); - await fs.writeFile(STARTUP_PATH, output); + console.log('Attempting to request snapshot...'); - // Set correct permissions for 'mksnapshot' - await fs.chmod(MKSNAPSHOT_PATH, 0o755); + // Obtain snapshot `id` and start new snapshot generation. + const id = await request.post('http://52.10.192.87', { body: { v8, script }, json: true }); - console.warn(`Generating snapshot blob for ${target}...`); + async function getSnapshot() { - // Generate snapshot - return promisify(exec)(MKSNAPSHOT_PATH, args).catch(e => console.warn(`Could not generate blob for ${target}: ${e.message}`)); -} + // Request generated snapshot. + const header = await request.get(`http://52.10.192.87/snapshot/${id}`, { + simple: false, + resolveWithFullResponse: true + }); -/** - * Generates V8Snapshots.h header from architecture blobs - * @returns {Promise} - */ -async function generateHeader() { - const blobs = {}; + if (header.statusCode === 200) { + console.log('Writing snapshot...'); - // Load snapshots for each architecture - await Promise.all(TARGETS.map(async target => { - const V8_LIB_TARGET_DIR = path.resolve(V8_LIB_DIR, target); - const BLOB_PATH = path.join(V8_LIB_TARGET_DIR, 'blob.bin'); + // Overwrite stub snapshot header. + await fs.writeFile(path.join(ANDROID_DIR, 'runtime', 'v8', 'src', 'native', 'V8Snapshots.h'), header.body); - if (await fs.exists(BLOB_PATH)) { - blobs[target] = Buffer.from(await fs.readFile(BLOB_PATH, 'binary'), 'binary'); - } - })); + // Done. Resolve `build` promise. + resolve(); - console.log(`Generating V8Snapshots.h for ${Object.keys(blobs).join(', ')}...`); + } else if (header.statusCode === 418) { + console.log('Waiting for snapshot generation...'); - // Generate 'V8Snapshots.h' from template - const output = await promisify(ejs.renderFile)(path.join(__dirname, 'V8Snapshots.h.ejs'), { blobs }, {}); - return fs.writeFile(path.join(ANDROID_DIR, 'runtime', 'v8', 'src', 'native', 'V8Snapshots.h'), output); -} + // Snapshot server is still building, wait for next interval. + return false; -/** - * Generates empty snapshot blobs for each supported architecture - * and creates a header to include the snapshots at build time. - * NOTE: SNAPSHOT GENERATION IS ONLY SUPPORTED ON macOS - * @returns {Promise} - */ -async function build() { - // Only macOS is supports creating snapshots - if (os.platform() !== 'darwin') { - console.warn('Snapshot generation is only supported on macOS, skipping...'); - return; - } - - // Generate snapshots in parallel - await Promise.all(TARGETS.map(target => generateBlob(target))); - return generateHeader(); + } else { + console.error('Could not generate snapshot, skipping...'); + } + + // Prevent retry interval. + return true; + } + + // Attempt to grab generated snapshot. + if (!await getSnapshot()) { + + // Retry until snapshot generation finishes. + const interval = setInterval(async () => { + if (await getSnapshot()) { + clearInterval(interval); + } + }, 5000); + } + }); } module.exports = { build }; diff --git a/build/lib/android/startup.ejs b/build/lib/android/startup.ejs deleted file mode 100644 index b64994c2391..00000000000 --- a/build/lib/android/startup.ejs +++ /dev/null @@ -1,3 +0,0 @@ -this._startSnapshot = function (global) { -<%- script %> -} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7a3d7b436d6..0951b0794d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10598,6 +10598,24 @@ } } }, + "request-promise-core": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.3.tgz", + "integrity": "sha512-QIs2+ArIGQVp5ZYbWD5ZLCY29D5CfWizP8eWnm8FoGD1TX61veauETVQbrV60662V0oFBkrDOuaBI8XgtuyYAQ==", + "requires": { + "lodash": "^4.17.15" + } + }, + "request-promise-native": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/request-promise-native/-/request-promise-native-1.0.8.tgz", + "integrity": "sha512-dapwLGqkHtwL5AEbfenuzjTYg35Jd6KPytsC2/TLkVMz8rm+tNt72MGUWT1RP/aYawMpN6HqbNGBQaRcBtjQMQ==", + "requires": { + "request-promise-core": "1.1.3", + "stealthy-require": "^1.1.1", + "tough-cookie": "^2.3.3" + } + }, "require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -11265,6 +11283,11 @@ } } }, + "stealthy-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz", + "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=" + }, "stream-buffers": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/stream-buffers/-/stream-buffers-2.2.0.tgz", diff --git a/package.json b/package.json index b126df9ceb8..f8b043d98f3 100644 --- a/package.json +++ b/package.json @@ -118,6 +118,7 @@ "p-limit": "^2.2.0", "pngjs": "^3.4.0", "request": "^2.87.0", + "request-promise-native": "^1.0.8", "semver": "^6.3.0", "sprintf": "0.1.5", "temp": "0.9.0",