Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle/Externals: Add tflite option #12

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ build
# Ignore 3rd party binaries
/externals/downloadable
/externals/gst-1.0-android-universal
/externals/tensorflow-lite
75 changes: 52 additions & 23 deletions externals/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ tasks {
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 {
Expand All @@ -51,41 +56,65 @@ tasks {
}
}

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"

fileList.add(Triple(tfliteTarFileName, tfliteAndroidPath, tfliteUrl))
}

register("prepareDownloadable") {
if (project.hasProperty("dir.tfliteAndroid")) {
prepareTflite()
}

if (!downloadablePath.isDirectory()) {
downloadablePath.createDirectories()
}

//gstreamer-1.0-android-universal
val gstXzFileName = "$gstTarFileName.xz"
val gstAndroidUniversalUrl = URL("https://gstreamer.freedesktop.org/pkg/android/$gstVersion/$gstXzFileName")

if (!Path("$downloadablePath/$gstTarFileName").isRegularFile()) {
println("Could not find $gstTarFileName")
println("...downloading from https://gstreamer.freedesktop.org/pkg/android")
println("This step may take some time to complete...")
downloadFile(gstAndroidUniversalUrl, "$downloadablePath/$gstXzFileName")

try {
File("$downloadablePath/$gstXzFileName").inputStream().buffered().use { bufferedIn ->
XZCompressorInputStream(bufferedIn).toFile("$downloadablePath/$gstTarFileName")
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")
}
} catch (e: java.io.IOException) {
println("Failed to decompress $downloadablePath/$xzFileName")
} finally {
Path("$downloadablePath/xzFileName").deleteIfExists()
}
} catch (e: java.io.IOException) {
println("Failed to decompress $downloadablePath/$gstXzFileName")
} finally {
Path("$downloadablePath/$gstXzFileName").deleteIfExists()
}
}
}

register("copyFromTar", Copy::class) {
from(tarTree("${downloadablePath.toUri()}/$gstTarFileName"))
into(gstAndroidPath)

dependsOn("prepareDownloadable")
register("copyFromTar") {
doLast {
Path("$downloadablePath/$gstTarFileName").deleteIfExists()
for (file in fileList) {
val tarFileName = file.first
val androidPath = file.second
if (!Path("$androidPath").isDirectory()) {
copy {
from(tarTree("${downloadablePath.toUri()}/$tarFileName"))
into(androidPath)
}
}
Path("$downloadablePath/$tarFileName").deleteIfExists()
}
}

dependsOn("prepareDownloadable")
}

register("initGitSubmodules", GitTask::class) {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ org.gradle.jvmargs=-Xmx4096M -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF
dir.externals=externals
dir.downloadable=downloadable
dir.gstAndroid=gst-1.0-android-universal
dir.tfliteAndroid=tensorflow-lite
dir.nnstreamer=nnstreamer
dir.mlApi=ml-api
url.repo.nnstreamer=https://github.com/nnstreamer/nnstreamer
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
gstreamer = "1.24.0"
tensorflowLite = "2.8.1"
tukaani-xz-plugin = "1.9"
commons-compress-lib = "1.26.1"
xyz-simple-git-plugin = "2.0.3"
Expand Down
31 changes: 28 additions & 3 deletions nnstreamer-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ android {
val externalDirPath by rootProject.extra {
project.rootDir.toPath().resolve(properties["dir.externals"].toString())
}
val relativePathExternals = projectDir.toPath().relativize(externalDirPath)

namespace = "org.nnsuite.nnstreamer"
compileSdk = libs.versions.android.compile.sdk.get().toInt()
Expand All @@ -31,6 +30,10 @@ android {
"ML_API_ROOT=$externalDirPath/ml-api"
)
targets("nnstreamer-native")

if (project.hasProperty("dir.tfliteAndroid")) {
arguments("TFLITE_ROOT_ANDROID=$externalDirPath/tensorflow-lite/tensorflow-lite")
}
}
}
}
Expand All @@ -45,7 +48,7 @@ android {
}
externalNativeBuild {
ndkBuild {
path=file("$relativePathExternals/ml-api/java/android/nnstreamer/src/main/jni/Android.mk")
path=file(project.projectDir.toPath().resolve("src/main/jni/Android.mk"))
}
}

Expand All @@ -59,7 +62,7 @@ android {

val genNnsSrc = tasks.register("genNnsSrc", Copy::class) {
val srcDirPath = externalDirPath.resolve("ml-api/java/android/nnstreamer/src/main/java/org/nnsuite/nnstreamer")
val outDirPath = project.projectDir.toPath().resolve("src/main/java/org/nnsuite/nnstreamer").apply {
val outDirPath = project.projectDir.toPath().resolve("src/main/java/org/nnsuite/nnstreamer/java").apply {
createDirectories()
}

Expand Down Expand Up @@ -89,8 +92,30 @@ android {
}

tasks {
register("genJniSrc", Copy::class) {
val srcDirPath = externalDirPath.resolve("ml-api/java/android/nnstreamer/src/main/jni")
val outDirPath = project.projectDir.toPath().resolve("src/main/jni").apply {
createDirectories()
}

group = BasePlugin.BUILD_GROUP
description = "Generates NNStreamer JNI sources"

from(srcDirPath)
into(outDirPath)

if (project.hasProperty("dir.tfliteAndroid")) {
val tfliteVersion = libs.versions.tensorflowLite.get()
filter { line: String ->
line.replace("TFLITE_VERSION := 2.8.1", "TFLITE_VERSION := $tfliteVersion")
}
}
filteringCharset = "UTF-8"
}

named("preBuild") {
dependsOn("genNnsSrc")
dependsOn("genJniSrc")
}
}
}