Skip to content

Commit

Permalink
feat: use prebuild toolchain for native modules (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
okhiroyuki authored Oct 28, 2023
1 parent 3fa75c9 commit 1840078
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,40 +130,48 @@ cdvPluginPostBuildExtras += { ->
String temp_cc_ver = '4.9';
String temp_dest_cpu;
String temp_v8_arch;
String temp_suffix;
String temp_toolchain_name;
String temp_binutils_prefix;
String temp_compiler_prefix;
switch ( temp_arch )
{
case 'arm':
temp_dest_cpu = "${temp_arch}"
temp_v8_arch = "${temp_arch}"
temp_suffix = "${temp_arch}-linux-androideabi"
temp_toolchain_name = "${temp_suffix}"
temp_binutils_prefix = "arm-linux-androideabi"
temp_compiler_prefix = "armv7a-linux-androideabi${_compileNativeModulesSdkVersion}"
break
case 'x86_64':
temp_dest_cpu = 'x64'
temp_v8_arch = 'x64'
temp_suffix = "${temp_arch}-linux-android"
temp_toolchain_name = "${temp_arch}"
temp_binutils_prefix = "x86_64-linux-android"
temp_compiler_prefix = "x86_64-linux-android${_compileNativeModulesSdkVersion}"
break
case 'arm64':
temp_dest_cpu = "${temp_arch}"
temp_v8_arch = "${temp_arch}"
temp_suffix = 'aarch64-linux-android'
temp_toolchain_name = 'aarch64'
temp_binutils_prefix = "aarch64-linux-android"
temp_compiler_prefix = "aarch64-linux-android${_compileNativeModulesSdkVersion}"
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 standalone_toolchain = "${rootProject.buildDir}/standalone-toolchains/${temp_toolchain_name}"
String npm_toolchain_add_to_path = "${rootProject.buildDir}/bin"
String npm_toolchain_ar = "${standalone_toolchain}/bin/${temp_suffix}-ar"
String npm_toolchain_cc = "${standalone_toolchain}/bin/${temp_suffix}-clang"
String npm_toolchain_cxx = "${standalone_toolchain}/bin/${temp_suffix}-clang++"
String npm_toolchain_link = "${standalone_toolchain}/bin/${temp_suffix}-clang++"
String toolchain_path = "${ndk_bundle_path}/toolchains/llvm/prebuilt/${temp_host_tag}"
String npm_toolchain_ar = "${toolchain_path}/bin/${temp_binutils_prefix}-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_gyp_defines = "target_arch=${temp_arch}"
npm_gyp_defines += " v8_target_arch=${temp_v8_arch}"
Expand Down Expand Up @@ -207,15 +215,7 @@ cdvPluginPostBuildExtras += { ->
}
}

task "MakeToolchain${abi_name}" (type:Exec) {
description = "Building a native toolchain to compile nodejs-mobile native modules for ${abi_name}."
executable = "${ndk_bundle_path}/build/tools/make-standalone-toolchain.sh"
args "--toolchain=${temp_toolchain_name}-${temp_cc_ver}", "--arch=${temp_arch}", "--install-dir=${standalone_toolchain}", "--stl=libc++", "--force", "--platform=android-22"
outputs.file "${standalone_toolchain}"
}

task "BuildNpmModules${abi_name}" (type:Exec) {
dependsOn "MakeToolchain${abi_name}"
dependsOn "CopyNodeProjectAssets${abi_name}"
inputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"
outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
Expand Down Expand Up @@ -244,7 +244,7 @@ cdvPluginPostBuildExtras += { ->
environment ('PATH', "${original_project_bin}" + System.getProperty("path.separator") + "${System.env.PATH}")
}

environment ('TOOLCHAIN',"${standalone_toolchain}")
environment ('TOOLCHAIN',"${toolchain_path}")
environment ('AR',"${npm_toolchain_ar}")
environment ('CC',"${npm_toolchain_cc}")
environment ('CXX',"${npm_toolchain_cxx}")
Expand Down

0 comments on commit 1840078

Please sign in to comment.