Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

Commit

Permalink
feat: support V8 snapshots on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitarTachev committed Sep 27, 2019
1 parent f555118 commit 2e9b753
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
3 changes: 1 addition & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const { isAndroid } = require("../projectHelpers");

function shouldSnapshot(config) {
const platformSupportsSnapshot = isAndroid(config.platform);
const osSupportsSnapshot = os.type() !== "Windows_NT";

return config.release && platformSupportsSnapshot && osSupportsSnapshot;
return config.release && platformSupportsSnapshot;
}

function convertToUnixPath(relativePath) {
Expand Down
13 changes: 3 additions & 10 deletions snapshot/android/project-snapshot-generator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { dirname, isAbsolute, join, resolve } = require("path");
const { dirname, isAbsolute, join, resolve, sep } = require("path");
const { existsSync, readFileSync, writeFileSync } = require("fs");

const shelljs = require("shelljs");
Expand Down Expand Up @@ -102,22 +102,15 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function (projectRoot) {

// Copy the libs to the specified destination in the platforms folder
shelljs.mkdir("-p", libsDestinationPath);
shelljs.cp("-R", join(buildPath, "ndk-build/libs") + "/", libsDestinationPath);
shelljs.cp("-R", join(buildPath, "ndk-build/libs") + sep, libsDestinationPath);
} else {
// useLibs = false
const blobsSrcPath = join(buildPath, "snapshots/blobs");
const blobsDestinationPath = resolve(appPath, "../snapshots");
const appPackageJsonPath = join(appPath, "package.json");

// Copy the blobs in the prepared app folder
shelljs.cp("-R", blobsSrcPath + "/", resolve(appPath, "../snapshots"));

/*
Rename TNSSnapshot.blob files to snapshot.blob files. The xxd tool uses the file name for the name of the static array.
This is why the *.blob files are initially named TNSSnapshot.blob.
After the xxd step, they must be renamed to snapshot.blob, because this is the filename that the Android runtime is looking for.
*/
shelljs.exec("find " + blobsDestinationPath + " -name '*.blob' -execdir mv {} snapshot.blob ';'");
shelljs.cp("-R", blobsSrcPath + sep, resolve(appPath, "../snapshots"));

// Update the package.json file
const appPackageJson = shelljs.test("-e", appPackageJsonPath) ? JSON.parse(readFileSync(appPackageJsonPath, 'utf8')) : {};
Expand Down
21 changes: 14 additions & 7 deletions snapshot/android/snapshot-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ SnapshotGenerator.prototype.generate = function (options) {
}

SnapshotGenerator.prototype.getSnapshotToolCommand = function (snapshotToolPath, inputFilePath, outputPath, toolParams) {
return `${snapshotToolPath} ${inputFilePath} --startup_blob ${join(outputPath, `${SNAPSHOT_BLOB_NAME}.blob`)} ${toolParams}`;
return `${snapshotToolPath} ${inputFilePath} --startup_blob ${outputPath} ${toolParams}`;
}

SnapshotGenerator.prototype.getXxdCommand = function (srcOutputDir, xxdLocation) {
// https://github.com/NativeScript/docker-images/tree/master/v8-snapshot/bin
return `${xxdLocation || ""}xxd -i ${SNAPSHOT_BLOB_NAME}.blob > ${join(srcOutputDir, `${SNAPSHOT_BLOB_NAME}.c`)}`;
return `/bin/xxd -i ${SNAPSHOT_BLOB_NAME}.blob > ${srcOutputDir}`;
}

SnapshotGenerator.prototype.getPathInDocker = function (mappedLocalDir, mappedDockerDir, targetPath) {
Expand Down Expand Up @@ -309,11 +309,12 @@ SnapshotGenerator.prototype.buildCSource = function (androidArch, blobInputDir,
if (snapshotInDocker) {
const blobsInputInDocker = `/blobs/${androidArch}`
const srcOutputDirInDocker = `/dist/src/${androidArch}`;
const buildCSourceCommand = this.getXxdCommand(srcOutputDirInDocker, "/bin/");
const outputPathInDocker = this.getPathInDocker(srcOutputDir, srcOutputDirInDocker, join(srcOutputDir, `${SNAPSHOT_BLOB_NAME}.c`));
const buildCSourceCommand = this.getXxdCommand(outputPathInDocker);
command = `docker run --rm -v "${blobInputDir}:${blobsInputInDocker}" -v "${srcOutputDir}:${srcOutputDirInDocker}" ${SNAPSHOTS_DOCKER_IMAGE} /bin/sh -c "cd ${blobsInputInDocker} && ${buildCSourceCommand}"`;
}
else {
command = this.getXxdCommand(srcOutputDir);
command = this.getXxdCommand(join(srcOutputDir, `${SNAPSHOT_BLOB_NAME}.c`));
}
shellJsExecuteInDir(blobInputDir, function () {
shelljs.exec(command);
Expand Down Expand Up @@ -345,11 +346,11 @@ SnapshotGenerator.prototype.runMksnapshotTool = function (tool, mksnapshotParams
const toolPathInAppDir = this.copySnapshotTool(snapshotToolsPath, toolPath, toolsTempFolder);
const toolPathInDocker = this.getPathInDocker(inputFileDir, appDirInDocker, toolPathInAppDir);
const inputFilePathInDocker = this.getPathInDocker(inputFileDir, appDirInDocker, inputFile);
const outputPathInDocker = this.getPathInDocker(blobOutputDir, blobOutputDirInDocker, blobOutputDir);
const outputPathInDocker = this.getPathInDocker(blobOutputDir, blobOutputDirInDocker, join(blobOutputDir, `${SNAPSHOT_BLOB_NAME}.blob`));
const toolCommandInDocker = this.getSnapshotToolCommand(toolPathInDocker, inputFilePathInDocker, outputPathInDocker, toolParams);
command = `docker run --rm -v "${inputFileDir}:${appDirInDocker}" -v "${blobOutputDir}:${blobOutputDirInDocker}" ${SNAPSHOTS_DOCKER_IMAGE} /bin/sh -c "${toolCommandInDocker}"`;
} else {
command = this.getSnapshotToolCommand(toolPath, inputFile, blobOutputDir, toolParams);
command = this.getSnapshotToolCommand(toolPath, inputFile, join(blobOutputDir, `${SNAPSHOT_BLOB_NAME}.blob`), toolParams);
}

// Generate .blob file
Expand All @@ -370,6 +371,12 @@ SnapshotGenerator.prototype.runMksnapshotTool = function (tool, mksnapshotParams
if (buildCSource) {
this.buildCSource(androidArch, blobOutputDir, snapshotInDocker)
}

/*
Rename TNSSnapshot.blob files to snapshot.blob files. The xxd tool uses the file name for the name of the static array.
This is why the *.blob files are initially named TNSSnapshot.blob.
After the xxd step, they must be renamed to snapshot.blob, because this is the filename that the Android runtime is looking for.
*/
shelljs.mv(join(blobOutputDir, `${SNAPSHOT_BLOB_NAME}.blob`), join(blobOutputDir, `snapshot.blob`));
});
}

0 comments on commit 2e9b753

Please sign in to comment.