From 1840078ea7651a2a1df85479bbcc1f6afa56c13a Mon Sep 17 00:00:00 2001 From: Hiroyuki Okada Date: Sat, 28 Oct 2023 09:15:45 +0900 Subject: [PATCH] feat: use prebuild toolchain for native modules (#35) --- src/android/build.gradle | 46 ++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/android/build.gradle b/src/android/build.gradle index 61cb4ac..5e899df 100644 --- a/src/android/build.gradle +++ b/src/android/build.gradle @@ -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}" @@ -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/" @@ -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}")