Skip to content

Commit

Permalink
Externals/Gradle: Revise build.gradle.kts
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
wooksong committed May 2, 2024
1 parent 07a2670 commit 10d47fe
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 63 deletions.
153 changes: 91 additions & 62 deletions externals/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 ->
Expand All @@ -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<Downloadable>()

// 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"
Expand All @@ -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")
}
}
Expand Down
2 changes: 1 addition & 1 deletion nnstreamer-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Expand Down

0 comments on commit 10d47fe

Please sign in to comment.