From 9a3d6dc390267b8cbe06e1eea96c823bf856814d Mon Sep 17 00:00:00 2001 From: Wook Song Date: Thu, 2 May 2024 16:36:58 +0900 Subject: [PATCH] Externals/Gradle: Revise build.gradle.kts This patch modifies build.gradle.kts in the externals submodule to use the existing gradle tasks to download and extract archive files. Signed-off-by: Wook Song --- externals/build.gradle.kts | 153 +++++++++++++++++++------------- nnstreamer-api/build.gradle.kts | 2 +- 2 files changed, 92 insertions(+), 63 deletions(-) diff --git a/externals/build.gradle.kts b/externals/build.gradle.kts index 6c6ebe5..f2b58a1 100644 --- a/externals/build.gradle.kts +++ b/externals/build.gradle.kts @@ -1,14 +1,9 @@ -import org.apache.commons.compress.compressors.xz.XZCompressorInputStream - import java.io.FileOutputStream import java.net.URL import java.nio.channels.Channels import kotlin.io.path.* import kotlin.io.path.Path -import kotlin.io.path.createDirectories -import kotlin.io.path.isDirectory -import kotlin.io.path.isRegularFile - +import org.apache.commons.compress.compressors.xz.XZCompressorInputStream import xyz.ronella.gradle.plugin.simple.git.task.* apply { @@ -30,16 +25,6 @@ buildscript { } tasks { - val downloadablePath = Path("${projectDir}/${project.properties["dir.downloadable"].toString()}") - val gstVersion = libs.versions.gstreamer.get() - val gstTarFileName = "gstreamer-1.0-android-universal-$gstVersion.tar" - val gstAndroidPath = Path("${projectDir}/${project.properties["dir.gstAndroid"].toString()}") - val gstAndroidUniversalUrl = "https://gstreamer.freedesktop.org/pkg/android/$gstVersion" - - val fileList = mutableListOf( - Triple(gstTarFileName, gstAndroidPath, gstAndroidUniversalUrl) - ) - fun downloadFile(url: URL, outputFileName: String) { url.openStream().use { Channels.newChannel(it).use { rbc -> @@ -51,75 +36,119 @@ tasks { } fun XZCompressorInputStream.toFile(filePath: String) { - File(filePath).outputStream().use { fileOutput -> - this.copyTo(fileOutput) - } + File(filePath).outputStream().use { fileOutput -> this.copyTo(fileOutput) } } - fun prepareTflite() { - val tfliteVersion = libs.versions.tensorflowLite.get() - val tfliteTarFileName = "tensorflow-lite-$tfliteVersion.tar" - val tfliteAndroidPath = Path("${projectDir}/${project.properties["dir.tfliteAndroid"].toString()}") - val tfliteUrl = "https://raw.githubusercontent.com/nnstreamer/nnstreamer-android-resource/master/external" + val downloadablePath = Path("${projectDir}/${project.properties["dir.downloadable"].toString()}") - fileList.add(Triple(tfliteTarFileName, tfliteAndroidPath, tfliteUrl)) - } + //FIXME Revise project.properties + // - remove dir.gstAndroid/dir.tfliteAndroid if not necessary + // - remove dir.downloadable if not necessary + // - set enabled by using the value in local.properties? + data class Downloadable( + val tarFileName: String, + val targetDir: String, + val url: String, + val enabled: Boolean, + val downloadableFormat: String = "", + ) - register("prepareDownloadable") { - if (project.hasProperty("dir.tfliteAndroid")) { - prepareTflite() - } + val downloadables = mutableListOf() + // GStreamer Android Universal + val gstVersion = libs.versions.gstreamer.get() + val gstDownloadable = + Downloadable( + "gstreamer-1.0-android-universal-$gstVersion.tar", + project.properties["dir.gstAndroid"].toString(), + "https://gstreamer.freedesktop.org/pkg/android/$gstVersion", + true, + ".xz" + ) + downloadables.add(gstDownloadable) + + // TensorFlow Lite + val tfliteVersion = libs.versions.tensorflowLite.get() + val tfliteDownloadable = + Downloadable( + "tensorflow-lite-$tfliteVersion.tar", + project.properties["dir.tfliteAndroid"].toString(), + "https://raw.githubusercontent.com/nnstreamer/nnstreamer-android-resource/master/external", + true, + ".xz" + ) + downloadables.add(tfliteDownloadable) + + + register("prepareDownloadable") { if (!downloadablePath.isDirectory()) { + @OptIn(ExperimentalPathApi::class) downloadablePath.deleteRecursively() downloadablePath.createDirectories() } - for (file in fileList) { - val tarFileName = file.first - val androidPath = file.second - val url = file.third - val xzFileName = "$tarFileName.xz" - - if (!Path("$androidPath").isDirectory()) { - println("Could not find $androidPath") - println("...downloading from $url") - println("This step may take some time to complete...") - downloadFile(URL("$url/$xzFileName"), "$downloadablePath/$xzFileName") - - try { - File("$downloadablePath/$xzFileName").inputStream().buffered().use { bufferedIn -> - XZCompressorInputStream(bufferedIn).toFile("$downloadablePath/$tarFileName") + for (downloadble in downloadables) { + val (tarFileName, targetDir, url) = downloadble + val targetPath = projectDir.toPath().resolve(targetDir) + + if (targetPath.isDirectory()) { + continue + } + + println("Could not find $targetPath") + println("...downloading from $url") + println("This step may take some time to complete...") + + val downloadbleName = "$tarFileName${downloadble.downloadableFormat}" + downloadFile(URL("$url/$downloadbleName"), downloadablePath.resolve(downloadbleName).toString()) + + when { + downloadbleName.endsWith(".xz") -> { + try { + File("$downloadablePath/$downloadbleName").inputStream().buffered().use { bufferedIn -> + XZCompressorInputStream(bufferedIn).toFile("$downloadablePath/$tarFileName") + } + downloadablePath.resolve(downloadbleName).deleteIfExists() + } catch (e: java.io.IOException) { + println("Failed to decompress $downloadablePath/$downloadbleName") } - } catch (e: java.io.IOException) { - println("Failed to decompress $downloadablePath/$xzFileName") - } finally { - Path("$downloadablePath/$xzFileName").deleteIfExists() } } } } + register("copyFromTar") { doLast { - for (file in fileList) { - val tarFileName = file.first - val androidPath = file.second - if (!Path("$androidPath").isDirectory()) { - copy { - from(tarTree("${downloadablePath.toUri()}/$tarFileName")) - into(androidPath) + for (downloadble in downloadables) { + val (tarFileName, targetDir, _) = downloadble + val tarPath = downloadablePath.resolve(tarFileName) + val tree = tarTree(tarPath) + var intoPath = projectDir.toPath().resolve(targetDir) + + if (intoPath.exists()) { + continue + } + + tree.visit { + if (this.relativePath.startsWith(targetDir)) { + intoPath = projectDir.toPath() } + return@visit + } + + copy { + from(tree) + into(intoPath) } - Path("$downloadablePath/$tarFileName").deleteIfExists() + + tarPath.deleteIfExists() } } - dependsOn("prepareDownloadable") } - register("initGitSubmodules", GitTask::class) { - command = "submodule init" - } + //TODO Use a specific commit ID or TAG + register("initGitSubmodules", GitTask::class) { command = "submodule init" } register("updateGitSubmodules", GitTask::class) { command = "submodule update" @@ -129,7 +158,7 @@ tasks { register("checkoutGitSubmodules", GitTask::class) { command = "submodule foreach" - args = listOf("git checkout .") //The git command arguments + args = listOf("git checkout .") // The git command arguments dependsOn("updateGitSubmodules") } } diff --git a/nnstreamer-api/build.gradle.kts b/nnstreamer-api/build.gradle.kts index bd9849a..8b6692e 100644 --- a/nnstreamer-api/build.gradle.kts +++ b/nnstreamer-api/build.gradle.kts @@ -32,7 +32,7 @@ android { targets("nnstreamer-native") if (project.hasProperty("dir.tfliteAndroid")) { - arguments("TFLITE_ROOT_ANDROID=$externalDirPath/tensorflow-lite/tensorflow-lite") + arguments("TFLITE_ROOT_ANDROID=$externalDirPath/tensorflow-lite") } } }