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

Commit 5431bd7

Browse files
committed
feat: support useLibs though env.compileSnapshot and calculate the NDK path internally
1 parent 516885d commit 5431bd7

9 files changed

+103
-16
lines changed

Diff for: androidProjectHelpers.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ const getMksnapshotParams = (projectDir) => {
4747
}
4848
};
4949

50+
const getRuntimeNdkRevision = (projectDir) => {
51+
try {
52+
const androidSettingsJSON = getAndroidSettingsJson(projectDir);
53+
if (androidSettingsJSON !== null) {
54+
return androidSettingsJSON.ndkRevision;
55+
} else {
56+
return null;
57+
}
58+
} catch (e) {
59+
return null;
60+
}
61+
};
62+
5063
const getAndroidSettingsJson = projectDir => {
5164
const androidSettingsJsonPath = resolve(projectDir, PLATFORMS_ANDROID, "settings.json");
5265
if (existsSync(androidSettingsJsonPath)) {
@@ -62,5 +75,6 @@ module.exports = {
6275
ANDROID_CONFIGURATIONS_PATH,
6376
getAndroidRuntimeVersion,
6477
getAndroidV8Version,
65-
getMksnapshotParams
78+
getMksnapshotParams,
79+
getRuntimeNdkRevision
6680
};

Diff for: lib/utils.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ function isWinOS() {
1515
return os.type() === "Windows_NT";
1616
}
1717

18+
function warn(message) {
19+
if (message) {
20+
console.log(`\x1B[33;1m${message}\x1B[0m`);
21+
}
22+
}
23+
1824
module.exports = {
1925
shouldSnapshot,
2026
convertToUnixPath,
21-
isWinOS
27+
isWinOS,
28+
warn
2229
};

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"minimatch": "3.0.4",
5959
"nativescript-hook": "0.2.4",
6060
"nativescript-worker-loader": "~0.9.0",
61+
"properties-reader": "0.3.1",
6162
"proxy-lib": "0.4.0",
6263
"raw-loader": "~0.5.1",
6364
"request": "2.88.0",

Diff for: snapshot/android/project-snapshot-generator.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
ANDROID_CONFIGURATIONS_PATH,
1717
getAndroidRuntimeVersion,
1818
getAndroidV8Version,
19+
getRuntimeNdkRevision,
1920
getMksnapshotParams
2021
} = require("../../androidProjectHelpers");
2122

@@ -155,6 +156,7 @@ const getV8VersionsMap = runtimeVersion =>
155156
}
156157
});
157158

159+
// TODO: remove?
158160
ProjectSnapshotGenerator.prototype.getV8Version = function (generationOptions) {
159161
return new Promise((resolve, reject) => {
160162
const maybeV8Version = generationOptions.v8Version;
@@ -229,6 +231,7 @@ ProjectSnapshotGenerator.prototype.generate = function (generationOptions) {
229231
let shouldRethrow = false;
230232

231233
const mksnapshotParams = getMksnapshotParams(this.options.projectRoot);
234+
const recommendedAndroidNdkRevision = getRuntimeNdkRevision(this.options.projectRoot);
232235

233236
return this.getV8Version(generationOptions).then(v8Version => {
234237
shouldRethrow = true;
@@ -254,7 +257,8 @@ ProjectSnapshotGenerator.prototype.generate = function (generationOptions) {
254257
inputFiles: generationOptions.inputFiles || [join(this.options.projectRoot, "__snapshot.js")],
255258
androidNdkPath,
256259
mksnapshotParams: mksnapshotParams,
257-
snapshotInDocker: generationOptions.snapshotInDocker
260+
snapshotInDocker: generationOptions.snapshotInDocker,
261+
recommendedAndroidNdkRevision
258262
};
259263

260264
return generator.generate(options).then(() => {

Diff for: snapshot/android/snapshot-generator.js

+53-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const fs = require("fs");
22
const { dirname, relative, join, EOL } = require("path");
33
const child_process = require("child_process");
4-
const { convertToUnixPath } = require("../../lib/utils");
5-
4+
const { convertToUnixPath, warn } = require("../../lib/utils");
5+
const PropertiesReader = require('properties-reader');
66
const shelljs = require("shelljs");
77

88
const { createDirectory, downloadFile, getHostOS, getHostOSArch, CONSTANTS, has32BitArch, isMacOSCatalinaOrHigher, isSubPath } = require("./utils");
@@ -192,8 +192,9 @@ SnapshotGenerator.prototype.setupDocker = function () {
192192
child_process.execSync(`docker pull ${SNAPSHOTS_DOCKER_IMAGE}`);
193193
}
194194

195-
SnapshotGenerator.prototype.buildSnapshotLibs = function (androidNdkBuildPath, targetArchs) {
195+
SnapshotGenerator.prototype.buildSnapshotLibs = function (androidNdkPath, recommendedAndroidNdkRevision, targetArchs) {
196196
// Compile *.c files to produce *.so libraries with ndk-build tool
197+
const androidNdkBuildPath = this.getAndroidNdkBuildPath(androidNdkPath, recommendedAndroidNdkRevision);
197198
const ndkBuildPath = join(this.buildPath, "ndk-build");
198199
const androidArchs = targetArchs.map(arch => this.convertToAndroidArchName(arch));
199200
console.log("Building native libraries for " + androidArchs.join());
@@ -207,6 +208,54 @@ SnapshotGenerator.prototype.buildSnapshotLibs = function (androidNdkBuildPath, t
207208
return join(ndkBuildPath, "libs");
208209
}
209210

211+
SnapshotGenerator.prototype.getAndroidNdkBuildPath = function (androidNdkPath, recommendedAndroidNdkRevision) {
212+
const ndkBuildExecutableName = "ndk-build";
213+
// fallback for Android Runtime < 6.2.0 with the 6.1.0 value
214+
recommendedAndroidNdkRevision = recommendedAndroidNdkRevision || "20.0.5594570";
215+
let androidNdkBuildPath = "";
216+
if (androidNdkPath) {
217+
// specified by the user
218+
const localNdkRevision = this.getAndroidNdkRevision(androidNdkPath);
219+
androidNdkBuildPath = join(androidNdkPath, ndkBuildExecutableName);
220+
if (!fs.existsSync(androidNdkBuildPath)) {
221+
throw new Error(`The provided Android NDK path does not contain ${ndkBuildExecutableName} executable.`);
222+
} else if (localNdkRevision !== recommendedAndroidNdkRevision) {
223+
warn(`The provided Android NDK is v${localNdkRevision} while the recommended one is v${recommendedAndroidNdkRevision}`);
224+
}
225+
} else {
226+
// available globally
227+
let hasAndroidNdkInPath = true;
228+
androidNdkBuildPath = ndkBuildExecutableName;
229+
try {
230+
child_process.execSync(`${androidNdkBuildPath} --version`);
231+
console.log(`Cannot determine the version of the global Android NDK. The recommended versions is v${recommendedAndroidNdkRevision}`);
232+
} catch (_) {
233+
hasAndroidNdkInPath = false;
234+
}
235+
236+
if (!hasAndroidNdkInPath) {
237+
// installed in ANDROID_HOME
238+
const androidHome = process.env.ANDROID_HOME;
239+
androidNdkBuildPath = join(androidHome, "ndk", recommendedAndroidNdkRevision, ndkBuildExecutableName);
240+
if (!fs.existsSync(androidNdkBuildPath)) {
241+
throw new Error(`Android NDK v${recommendedAndroidNdkRevision} is not installed. You can find installation instructions in this article: https://developer.android.com/studio/projects/install-ndk#specific-version`);
242+
}
243+
}
244+
}
245+
246+
return androidNdkBuildPath;
247+
}
248+
249+
SnapshotGenerator.prototype.getAndroidNdkRevision = function (androidNdkPath) {
250+
const ndkPropertiesFile = join(androidNdkPath, "source.properties");
251+
if (fs.existsSync(ndkPropertiesFile)) {
252+
const properties = PropertiesReader(ndkPropertiesFile);
253+
return properties.get("Pkg.Revision");
254+
} else {
255+
return null;
256+
}
257+
}
258+
210259
SnapshotGenerator.prototype.buildIncludeGradle = function () {
211260
shelljs.cp(INCLUDE_GRADLE_PATH, join(this.buildPath, "include.gradle"));
212261
}
@@ -236,8 +285,7 @@ SnapshotGenerator.prototype.generate = function (options) {
236285
).then(() => {
237286
this.buildIncludeGradle();
238287
if (options.useLibs) {
239-
const androidNdkBuildPath = options.androidNdkPath ? join(options.androidNdkPath, "ndk-build") : "ndk-build";
240-
this.buildSnapshotLibs(androidNdkBuildPath, options.targetArchs);
288+
this.buildSnapshotLibs(options.androidNdkPath, options.recommendedAndroidNdkRevision, options.targetArchs);
241289
}
242290
return this.buildPath;
243291
});

Diff for: templates/webpack.angular.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ module.exports = env => {
5252
unitTesting, // --env.unitTesting
5353
verbose, // --env.verbose
5454
snapshotInDocker, // --env.snapshotInDocker
55-
skipSnapshotTools // --env.skipSnapshotTools
55+
skipSnapshotTools, // --env.skipSnapshotTools
56+
compileSnapshot // --env.compileSnapshot
5657
} = env;
5758

59+
const useLibs = compileSnapshot;
5860
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
5961
const externals = nsWebpack.getConvertedExternals(env.externals);
6062
const appFullPath = resolve(projectRoot, appPath);
@@ -315,7 +317,8 @@ module.exports = env => {
315317
projectRoot,
316318
webpackConfig: config,
317319
snapshotInDocker,
318-
skipSnapshotTools
320+
skipSnapshotTools,
321+
useLibs
319322
}));
320323
}
321324

Diff for: templates/webpack.javascript.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ module.exports = env => {
4545
unitTesting, // --env.unitTesting,
4646
verbose, // --env.verbose
4747
snapshotInDocker, // --env.snapshotInDocker
48-
skipSnapshotTools // --env.skipSnapshotTools
48+
skipSnapshotTools, // --env.skipSnapshotTools
49+
compileSnapshot // --env.compileSnapshot
4950
} = env;
5051

52+
const useLibs = compileSnapshot;
5153
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
5254
const externals = nsWebpack.getConvertedExternals(env.externals);
5355
const appFullPath = resolve(projectRoot, appPath);
@@ -253,7 +255,8 @@ module.exports = env => {
253255
projectRoot,
254256
webpackConfig: config,
255257
snapshotInDocker,
256-
skipSnapshotTools
258+
skipSnapshotTools,
259+
useLibs
257260
}));
258261
}
259262

Diff for: templates/webpack.typescript.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ module.exports = env => {
4747
unitTesting, // --env.unitTesting,
4848
verbose, // --env.verbose
4949
snapshotInDocker, // --env.snapshotInDocker
50-
skipSnapshotTools // --env.skipSnapshotTools
50+
skipSnapshotTools, // --env.skipSnapshotTools
51+
compileSnapshot // --env.compileSnapshot
5152
} = env;
53+
54+
const useLibs = compileSnapshot;
5255
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
5356
const externals = nsWebpack.getConvertedExternals(env.externals);
5457

@@ -285,7 +288,8 @@ module.exports = env => {
285288
projectRoot,
286289
webpackConfig: config,
287290
snapshotInDocker,
288-
skipSnapshotTools
291+
skipSnapshotTools,
292+
useLibs
289293
}));
290294
}
291295

Diff for: templates/webpack.vue.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ module.exports = env => {
4848
unitTesting, // --env.unitTesting
4949
verbose, // --env.verbose
5050
snapshotInDocker, // --env.snapshotInDocker
51-
skipSnapshotTools // --env.skipSnapshotTools
51+
skipSnapshotTools, // --env.skipSnapshotTools
52+
compileSnapshot // --env.compileSnapshot
5253
} = env;
5354

55+
const useLibs = compileSnapshot;
5456
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
5557
const externals = nsWebpack.getConvertedExternals(env.externals);
5658

@@ -300,7 +302,8 @@ module.exports = env => {
300302
projectRoot,
301303
webpackConfig: config,
302304
snapshotInDocker,
303-
skipSnapshotTools
305+
skipSnapshotTools,
306+
useLibs
304307
}));
305308
}
306309

0 commit comments

Comments
 (0)