Skip to content

Commit

Permalink
fix(local publication): triggering compilation of zenoh jni when publ…
Browse files Browse the repository at this point in the history
…ishing jvm to maven local
  • Loading branch information
DariusIMP committed Jul 12, 2024
1 parent 1b0b604 commit d04df8e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ and in case of targetting Android you'll also need:
To publish a library for a JVM project into Maven local, run

```bash
gradle publishJvmPublicationToMavenLocal
gradle -Prelease=true publishJvmPublicationToMavenLocal
```

This will first, trigger the compilation of Zenoh-JNI, and second publish the library into maven local, containing the native library
This will first, trigger the compilation of Zenoh-JNI in release (if you want debug, specify `-Prelease=false`), and second publish the library into maven local, containing the native library
as a resource that will be loaded during runtime.

:warning: The native library will be compiled against the default rustup target on your machine, so although it may work fine
Expand Down Expand Up @@ -263,13 +263,7 @@ gradle zenoh-kotlin:dokkaHtml

## Running the tests

To run the tests, we must first build the native library
```bash
cd zenoh-jni
cargo build
```

and then run:
To run the tests, run:

```bash
gradle jvmTest
Expand Down
51 changes: 47 additions & 4 deletions zenoh-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ plugins {

val androidEnabled = project.findProperty("android")?.toString()?.toBoolean() == true

val release = project.findProperty("release")?.toString()?.toBoolean() == true
var buildMode = if (release) BuildMode.RELEASE else BuildMode.DEBUG

if (androidEnabled) {
apply(plugin = "com.android.library")
apply(plugin = "org.mozilla.rust-android-gradle.rust-android")
Expand All @@ -39,7 +42,7 @@ kotlin {
kotlinOptions.jvmTarget = "11"
}
testRuns["test"].executionTask.configure {
val zenohPaths = "../zenoh-jni/target/debug"
val zenohPaths = "../zenoh-jni/target/$buildMode"
jvmArgs("-Djava.library.path=$zenohPaths")
}
}
Expand Down Expand Up @@ -71,13 +74,14 @@ kotlin {
}
}
val jvmMain by getting {
resources.srcDir("../zenoh-jni/target/release").include(arrayListOf("*.dylib", "*.so", "*.dll"))
resources.srcDir("../zenoh-jni/target/$buildMode").include(arrayListOf("*.dylib", "*.so", "*.dll"))

// The line below is intended to load the native libraries that are crosscompiled on GitHub actions when publishing a JVM package.
resources.srcDir("../jni-libs").include("*/**")
}

val jvmTest by getting {
resources.srcDir("../zenoh-jni/target/debug").include(arrayListOf("*.dylib", "*.so", "*.dll"))
resources.srcDir("../zenoh-jni/target/$buildMode").include(arrayListOf("*.dylib", "*.so", "*.dll"))
}
}

Expand All @@ -103,7 +107,7 @@ tasks.withType<Test> {
doFirst {
// The line below is added for the Android Unit tests which are equivalent to the JVM tests.
// For them to work we need to specify the path to the native library as a system property and not as a jvmArg.
systemProperty("java.library.path", "../zenoh-jni/target/debug")
systemProperty("java.library.path", "../zenoh-jni/target/$buildMode")
}
}

Expand All @@ -114,6 +118,45 @@ tasks.whenObjectAdded {
}
}

tasks.named("compileKotlinJvm") {
dependsOn("buildZenohJni")
}

tasks.register("buildZenohJni") {
buildZenohJNI(buildMode)
}

fun buildZenohJNI(mode: BuildMode = BuildMode.DEBUG) {
val cargoCommand = mutableListOf("cargo", "build")

if (mode == BuildMode.RELEASE) {
cargoCommand.add("--release")
}

val result = project.exec {
commandLine(*(cargoCommand.toTypedArray()), "--manifest-path", "../zenoh-jni/Cargo.toml")
}

if (result.exitValue != 0) {
throw GradleException("Failed to build Zenoh-JNI.")
}

Thread.sleep(1000)
}

enum class BuildMode {
DEBUG {
override fun toString(): String {
return "debug"
}
},
RELEASE {
override fun toString(): String {
return "release"
}
}
}

fun Project.configureAndroid() {
extensions.configure<com.android.build.gradle.LibraryExtension>("android") {
namespace = "io.zenoh"
Expand Down

0 comments on commit d04df8e

Please sign in to comment.