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 (#128)
  • Loading branch information
DariusIMP authored Jul 12, 2024
1 parent 1b0b604 commit d5695d0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-jvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,6 @@ jobs:
- name: Gradle Publish JVM Package
uses: gradle/gradle-build-action@v2
with:
arguments: publishJvmPublicationToGithubPackagesRepository ${{ env.PUB_MODE }}
arguments: publishJvmPublicationToGithubPackagesRepository -PgithubPublish=true ${{ env.PUB_MODE }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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
66 changes: 59 additions & 7 deletions zenoh-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ plugins {
}

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

var buildMode = if (release) BuildMode.RELEASE else BuildMode.DEBUG

if (androidEnabled) {
apply(plugin = "com.android.library")
Expand All @@ -39,7 +43,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 +75,16 @@ kotlin {
}
}
val jvmMain by getting {
resources.srcDir("../zenoh-jni/target/release").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("*/**")
if (githubPublish) {
// 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("*/**")
} else {
resources.srcDir("../zenoh-jni/target/$buildMode").include(arrayListOf("*.dylib", "*.so", "*.dll"))
}
}

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 +110,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 +121,51 @@ tasks.whenObjectAdded {
}
}

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

tasks.register("buildZenohJni") {
doLast {
if (!githubPublish) {
// This is intended for local publications. For publications done through GitHub workflows,
// the zenoh-jni build is achieved and loaded differently from the CI
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 d5695d0

Please # to comment.