From f20dd609a9096b9ff9b3cad6c2496500a268237d Mon Sep 17 00:00:00 2001 From: ignaciogarcia Date: Fri, 28 Oct 2022 07:48:31 +0200 Subject: [PATCH] PEMA-107 Only run buildExternal for connected devices --- android/build.gradle | 15 +++-- .../plugins/set-pep-jniadapter-archs.gradle | 56 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 android/gradle/plugins/set-pep-jniadapter-archs.gradle diff --git a/android/build.gradle b/android/build.gradle index 770959b..0347761 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -8,7 +8,9 @@ def buildAutomatic = hasProperty('buildAutomatic') ? buildAutomatic : "true" def threadsToUse = 1 def pEpEngineDB = new File(new File(pEpEngineSrc), 'db') -def archsToCompile = "arm arm64 x86 x86_64" +ext.archsToCompile = "arm arm64 x86 x86_64" + +apply from: 'gradle/plugins/set-pep-jniadapter-archs.gradle' buildscript { repositories { @@ -117,9 +119,14 @@ android { } // call external build (GnuPG, GPGME, etc) - task buildExternal(type:Exec, dependsOn: 'genSources') { - workingDir 'external' - commandLine 'make', "-j${threadsToUse}", 'build', "archs=${archsToCompile}" + task buildExternal(dependsOn: ['genSources', 'setpEpJNIAdapterArchs']) { + doLast { + println("compiling for abis: ${project.archsToCompile}") + exec { + workingDir 'external' + commandLine 'make', "-j${threadsToUse}", 'build', "archs=${project.archsToCompile}" + } + } } task externalAssets(type:Exec) { diff --git a/android/gradle/plugins/set-pep-jniadapter-archs.gradle b/android/gradle/plugins/set-pep-jniadapter-archs.gradle new file mode 100644 index 0000000..150fd91 --- /dev/null +++ b/android/gradle/plugins/set-pep-jniadapter-archs.gradle @@ -0,0 +1,56 @@ +task setpEpJNIAdapterArchs { + + description = "Prepare pEpJNIAdapter to build with the first archs of each connected device." + + doLast { + def serialNos = getConnectedDevicesIds() + println("Connected devices: $serialNos") + def abis = serialNos.collect { serialNo -> + convertAbiName(execCommand("adb -s ${serialNo} shell getprop ro.product.cpu.abilist", true).trim().split(",")[0]) + }.toSet() + def sb = new StringBuilder() + for (String s : abis) { + sb.append(s) + sb.append(" ") + } + if (!abis.isEmpty()) { + project.archsToCompile = sb.toString().trim() + println("got archs to compile: ${project.archsToCompile}") + } + } +} + +private List getConnectedDevicesIds(boolean verbose = false) { + def lines = execCommand("adb devices", true, verbose).readLines() + lines.removeIf { it.trim().isEmpty() } + lines.remove(0) + if (lines.isEmpty()) return lines + return lines.collect { line -> + line.substring(0, line.indexOf("device")).trim() + } +} + +static String convertAbiName(name) { + if (name == "armeabi-v7a") return "arm" + else if (name == "arm64-v8a") return "arm64" + else return name +} + +private String execCommand(String command, boolean captureOutput = false, boolean verbose = false) { + def stdout = captureOutput ? new ByteArrayOutputStream() : null + exec { + if (verbose) { + println("running command: $command") + } + commandLine command.split(' ') + if (stdout != null) { + standardOutput = stdout + } + } + String out = null + if (stdout != null) { + out = stdout.toString() + stdout.close() + } + return out +}