forked from JaneaSystems/nodejs-mobile-cordova
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.gradle
326 lines (292 loc) · 16.3 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
buildscript {
repositories {
jcenter()
}
}
def _nodeMinSdkVersion = 24
ext.cdvMinSdkVersion = _nodeMinSdkVersion
def _compileNativeModulesSdkVersion=_nodeMinSdkVersion;
android {
ndkVersion "24.0.8215888"
defaultConfig {
externalNativeBuild {
cmake {
cppFlags ""
arguments "-DANDROID_STL=c++_shared"
}
}
}
externalNativeBuild {
cmake {
path "libs/cdvnodejsmobile/CMakeLists.txt"
}
}
lintOptions {
abortOnError false
}
packagingOptions {
pickFirst 'lib/armeabi-v7a/libnode.so'
pickFirst 'lib/arm64-v8a/libnode.so'
pickFirst 'lib/x86_64/libnode.so'
}
}
import org.gradle.internal.os.OperatingSystem;
import org.gradle.util.GradleVersion;
cdvPluginPostBuildExtras += { ->
// Set it to the supported architectures.
logger.info("nodejs-mobile-cordova were set [\"armeabi-v7a\", \"arm64-v8a\", \"x86_64\"] for ABI filters.");
android.defaultConfig.ndk.abiFilters = ["armeabi-v7a", "arm64-v8a", "x86_64"] as Set<String>;
// gzip files will cause errors with aapt. Remove them for improved npm compatibility.
android.aaptOptions.ignoreAssetsPattern += ":!*.gz";
// The MacOS builder helper script is not needed inside the APK.
android.aaptOptions.ignoreAssetsPattern += ":!build-native-modules-MacOS-helper-script.sh";
// The file that caches the value of NODEJS_MOBILE_BUILD_NATIVE_MODULES is not needed inside the APK.
android.aaptOptions.ignoreAssetsPattern += ":!NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt";
if (GradleVersion.current() < GradleVersion.version("4.0")) {
android.sourceSets.main.jniLibs.srcDirs += 'libs/cdvnodejsmobile/libnode/bin/';
}
String projectWWW; // www assets folder from the Application project.
if ( file("${project.projectDir}/src/main/assets/www/").exists() ) {
// www folder for cordova-android >= 7
projectWWW = "${project.projectDir}/src/main/assets/www";
} else if (file("${project.projectDir}/assets/www/").exists()) {
// www folder for cordova-android < 7
projectWWW = "${project.projectDir}/assets/www";
} else {
throw new GradleException('nodejs-mobile-cordova couldn\'t find the www folder in the Android project.');
}
String shouldRebuildNativeModules = System.getenv('NODEJS_MOBILE_BUILD_NATIVE_MODULES');
if (shouldRebuildNativeModules==null) {
// If the environment variable is not set right now, check if it has been saved to a file during
// the cordova prepare phase.
def nativeModulesPreferenceFile = file("${projectWWW}/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt");
if (nativeModulesPreferenceFile.exists()) {
shouldRebuildNativeModules=nativeModulesPreferenceFile.text;
}
}
if (shouldRebuildNativeModules==null) {
// If build native modules preference is not set, try to find .gyp files to turn it on.
shouldRebuildNativeModules="0";
def gyp_files_tree = fileTree(
dir: "${projectWWW}/nodejs-project/",
include: "**/*.gyp"
);
gyp_files_tree.visit { gypFile ->
if (!gypFile.isDirectory()) {
// It's a .gyp file.
shouldRebuildNativeModules="1";
gypFile.stopVisiting();
}
}
}
if ("1".equals(shouldRebuildNativeModules)) {
String npmCommandName = 'npm';
if (OperatingSystem.current().isMacOsX()) {
// On macOS, npm's location may not be in the PATH environment variable if gradle is being run
// by Android Studio. We need npm to build native modules, so we use a helper script that
// is created by a Cordova prepare hook to run npm with the PATH members that were available
// during the prepare event.
try {
def commandResult = exec {
commandLine 'command', '-v', 'npm'
ignoreExitValue = true
}
if ( commandResult.getExitValue() != 0 ) {
// If npm is not found by command, use the helper script.
logger.warn("Couldn't find npm in the PATH for building native modules. Will try to use a helper script.");
npmCommandName = '../build-native-modules-MacOS-helper-script.sh';
}
} catch ( Exception e ) {
throw new GradleException('Something went wrong looking for npm by running "command".', e)
}
}
android.defaultConfig.ndk.abiFilters.each { abi_name ->
String temp_arch = {
switch (abi_name) {
case 'armeabi-v7a':
'arm'
break
case 'arm64-v8a':
'arm64'
break
default:
abi_name
break
}
}()
String temp_cc_ver = '4.9';
String temp_dest_cpu;
String temp_v8_arch;
String temp_binutils_prefix;
String temp_compiler_prefix;
String cargo_build_target;
switch ( temp_arch )
{
case 'arm':
temp_dest_cpu = "${temp_arch}"
temp_v8_arch = "${temp_arch}"
temp_binutils_prefix = "arm-linux-androideabi"
temp_compiler_prefix = "armv7a-linux-androideabi${_compileNativeModulesSdkVersion}"
cargo_build_target = 'arm-linux-androideabi'
break
case 'x86_64':
temp_dest_cpu = 'x64'
temp_v8_arch = 'x64'
temp_binutils_prefix = "x86_64-linux-android"
temp_compiler_prefix = "x86_64-linux-android${_compileNativeModulesSdkVersion}"
cargo_build_target = "x86_64-linux-android"
break
case 'arm64':
temp_dest_cpu = "${temp_arch}"
temp_v8_arch = "${temp_arch}"
temp_binutils_prefix = "aarch64-linux-android"
temp_compiler_prefix = "aarch64-linux-android${_compileNativeModulesSdkVersion}"
cargo_build_target = "aarch64-linux-android"
break
default:
throw new GradleException("Unsupported architecture for nodejs-mobile native modules: ${temp_arch}")
break
}
String temp_host_tag
if (OperatingSystem.current().isMacOsX()) {
temp_host_tag = 'darwin-x86_64'
} else if (OperatingSystem.current().isLinux()) {
temp_host_tag = 'linux-x86_64'
} else {
throw new GradleException("Unsupported operating system for nodejs-mobile native builds: ${OperatingSystem.current().getName()}")
}
String ndk_bundle_path = android.ndkDirectory
String toolchain_path = "${ndk_bundle_path}/toolchains/llvm/prebuilt/${temp_host_tag}"
String npm_toolchain_ar = "${toolchain_path}/bin/llvm-ar"
String npm_toolchain_cc = "${toolchain_path}/bin/${temp_compiler_prefix}-clang"
String npm_toolchain_cxx = "${toolchain_path}/bin/${temp_compiler_prefix}-clang++"
String npm_toolchain_link = "${toolchain_path}/bin/${temp_compiler_prefix}-clang++"
String npm_toolchain_ranlib = "${toolchain_path}/bin/llvm-ranlib"
String cargo_target_triple = cargo_build_target.toUpperCase().replaceAll('-', '_')
String npm_gyp_defines = "target_arch=${temp_arch}"
npm_gyp_defines += " v8_target_arch=${temp_v8_arch}"
npm_gyp_defines += " android_target_arch=${temp_arch}"
if (OperatingSystem.current().isMacOsX()) {
npm_gyp_defines += " host_os=mac OS=android"
} else if (OperatingSystem.current().isLinux()) {
npm_gyp_defines += " host_os=linux OS=android"
} else {
throw new GradleException("Unsupported opperating system for nodejs-mobile native builds: ${OperatingSystem.current().getName()}")
}
task "CopyNodeProjectAssets${abi_name}" {
description = "Copying node assets to build native modules for ${abi_name}."
inputs.files fileTree (
dir: "${projectWWW}/nodejs-project/"
).exclude({
details -> // We shouldn't need to rebuild native code if there are only changes in the Node.js project javascript files.
!details.isDirectory() &&
details.getPath().endsWith('.js') &&
!details.getPath().startsWith('node_modules/')
})
outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"
doLast {
delete "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
copy {
from "${projectWWW}/nodejs-project/"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
// Symlinks to binaries are resolved by cordova prepare and Gradle during the copy, causing build time errors.
// The original project's .bin folder will be added to the path while building in the BuildNpmModules tasks.
exclude "**/.bin"
}
if (OperatingSystem.current().isMacOsX()) {
// Copy the helper script for calling npm when building in Android Studio on macOS.
copy {
from "${projectWWW}/build-native-modules-MacOS-helper-script.sh"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
}
}
new File("${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp").text = "${new Date().format('yyyy-MM-dd HH:mm:ss')}"
}
}
task "BuildNpmModules${abi_name}" (type:Exec) {
dependsOn "CopyNodeProjectAssets${abi_name}"
inputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"
outputs.dir "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
description = "Building native modules for ${abi_name}."
workingDir "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
String npm_gyp_path_to_use = "${workingDir}/node_modules/nodejs-mobile-gyp/bin/node-gyp.js"
environment ('npm_config_node_engine', 'v8' )
environment ('npm_config_nodedir', "${project.projectDir}/libs/cdvnodejsmobile/libnode/" )
environment ('npm_config_node_gyp', npm_gyp_path_to_use)
environment ('npm_config_arch', temp_arch)
environment ('npm_config_platform', 'android')
environment ('npm_config_format', 'make-android')
environment ('PROJECT_DIR', "${rootProject.projectDir}")
environment ('NODEJS_MOBILE_GYP', npm_gyp_path_to_use)
// Adds the original project .bin to the path. It's a workaround
// to correctly build some modules that depend on symlinked modules,
// like node-pre-gyp.
String original_project_bin = "${rootProject.projectDir}/../../www/nodejs-project/node_modules/.bin";
if(file(original_project_bin).exists()) {
environment ('PATH', "${original_project_bin}" + System.getProperty("path.separator") + "${System.env.PATH}")
}
environment ('CARGO_BUILD_TARGET', "${cargo_build_target}")
environment ("CARGO_TARGET_${cargo_target_triple}_AR", "${npm_toolchain_ar}")
environment ("CARGO_TARGET_${cargo_target_triple}_LINKER", "${npm_toolchain_link}")
environment ('TOOLCHAIN', "${toolchain_path}")
environment ('AR', "${npm_toolchain_ar}")
environment ('CC', "${npm_toolchain_cc}")
environment ('CXX', "${npm_toolchain_cxx}")
environment ('LINK', "${npm_toolchain_link}")
environment ('RANLIB', "${npm_toolchain_ranlib}")
environment ('GYP_DEFINES', "${npm_gyp_defines}")
commandLine npmCommandName, '--verbose', '--foreground-scripts', 'rebuild', '--build-from-source'
}
task "CopyBuiltNpmAssets${abi_name}" (type:Sync) {
dependsOn "BuildNpmModules${abi_name}"
description = "Copying node assets with build native modules for ${abi_name}."
from "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
into "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/"
includeEmptyDirs = false
include '**/*.node'
}
task "GenerateNodeNativeAssetsLists${abi_name}" {
dependsOn "CopyBuiltNpmAssets${abi_name}"
description "Generates a list for runtime copying"
inputs.dir "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/"
outputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list"
outputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list"
doLast{
if(!(new File("${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/")).exists()) {
// If the native assets folder doesn't exist from the copy task, skip the creation of the file.list
return;
}
delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list"
delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list"
ArrayList<String> file_list_arr = new ArrayList<String>();
ArrayList<String> dir_list_arr = new ArrayList<String>();
String file_list = "";
String dir_list = "";
def assets_tree = fileTree(dir: "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/" )
assets_tree.visit { assetFile ->
if (assetFile.isDirectory()) {
dir_list_arr.add("${assetFile.relativePath}\n");
} else {
file_list_arr.add("${assetFile.relativePath}\n");
}
}
//Ensure both files are ordered similarly across builds.
Collections.sort(file_list_arr);
Collections.sort(dir_list_arr);
def file_list_path = new File( "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list")
for (String file : file_list_arr){
file_list += file;
}
file_list_path.write file_list
def dir_list_path = new File( "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list")
for (String dir : dir_list_arr){
dir_list += dir;
}
dir_list_path.write dir_list
}
}
tasks.getByPath(":${(project.getParent()==null)?"":(project.name+":")}preBuild").dependsOn "GenerateNodeNativeAssetsLists${abi_name}";
project.android.sourceSets.main.assets.srcDirs+="${rootProject.buildDir}/nodejs-native-assets/";
}
}
};