From d5695d01cadb6ea8a9d88ffe690615a5433e5c67 Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Fri, 12 Jul 2024 13:10:43 +0200 Subject: [PATCH] fix(local publication): triggering compilation of zenoh jni when publishing jvm to maven local (#128) --- .github/workflows/publish-jvm.yml | 2 +- README.md | 12 ++---- zenoh-kotlin/build.gradle.kts | 66 +++++++++++++++++++++++++++---- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/.github/workflows/publish-jvm.yml b/.github/workflows/publish-jvm.yml index bb3938e1a..69ef853e5 100644 --- a/.github/workflows/publish-jvm.yml +++ b/.github/workflows/publish-jvm.yml @@ -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 }} diff --git a/README.md b/README.md index 344f0e64a..e428db951 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/zenoh-kotlin/build.gradle.kts b/zenoh-kotlin/build.gradle.kts index bc02939c1..86b5fff89 100644 --- a/zenoh-kotlin/build.gradle.kts +++ b/zenoh-kotlin/build.gradle.kts @@ -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") @@ -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") } } @@ -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")) } } @@ -103,7 +110,7 @@ tasks.withType { 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") } } @@ -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("android") { namespace = "io.zenoh"