From c7ed37789e4d0d096b22cfb4b72fc03aec158802 Mon Sep 17 00:00:00 2001 From: Gold856 <117957790+Gold856@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:37:51 -0500 Subject: [PATCH] [photon-targeting] Fix JNI loading (#1563) --- .github/workflows/build.yml | 24 +++++++++--- photon-lib/build.gradle | 1 + photon-lib/src/generate/photonlib.json.in | 13 ------- photon-targeting/build.gradle | 2 + .../jni/PhotonTargetingJniLoader.java | 6 +-- .../src/test/java/frc/robot/JniLoadTest.java | 39 +++++++++++++++++++ shared/javacpp/publish.gradle | 5 ++- 7 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 photonlib-java-examples/aimattarget/src/test/java/frc/robot/JniLoadTest.java diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8bc365eeb9..25f8c0f3cc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -39,8 +39,20 @@ jobs: name: built-client path: photon-client/dist/ build-examples: - name: "Build Examples" - runs-on: ubuntu-22.04 + + strategy: + fail-fast: false + matrix: + include: + - os: windows-2022 + architecture: x64 + - os: macos-14 + architecture: aarch64 + - os: ubuntu-22.04 + + name: "Photonlib - Build Examples - ${{ matrix.os }}" + runs-on: ${{ matrix.os }} + steps: - name: Checkout code uses: actions/checkout@v4 @@ -60,19 +72,19 @@ jobs: - name: Publish photonlib to maven local run: | chmod +x gradlew - ./gradlew publishtomavenlocal -x check + ./gradlew photon-targeting:publishtomavenlocal photon-lib:publishtomavenlocal -x check - name: Build Java examples working-directory: photonlib-java-examples run: | chmod +x gradlew ./gradlew copyPhotonlib -x check - ./gradlew build -x check + ./gradlew build - name: Build C++ examples working-directory: photonlib-cpp-examples run: | chmod +x gradlew ./gradlew copyPhotonlib -x check - ./gradlew build -x check + ./gradlew build build-gradle: name: "Gradle Build" runs-on: ubuntu-22.04 @@ -585,7 +597,7 @@ jobs: dispatch: name: dispatch - needs: [build-photonlib-vendorjson] + needs: [build-photonlib-vendorjson, release] runs-on: ubuntu-22.04 steps: - uses: peter-evans/repository-dispatch@v3 diff --git a/photon-lib/build.gradle b/photon-lib/build.gradle index 3e3fc666fe..5e79ba4cc3 100644 --- a/photon-lib/build.gradle +++ b/photon-lib/build.gradle @@ -88,6 +88,7 @@ model { } if(project.hasProperty('includePhotonTargeting')) { lib project: ':photon-targeting', library: 'photontargeting', linkage: 'shared' + lib project: ':photon-targeting', library: 'photontargetingJNI', linkage: 'shared' } } diff --git a/photon-lib/src/generate/photonlib.json.in b/photon-lib/src/generate/photonlib.json.in index 88d4e002d9..cb7dcb5bca 100644 --- a/photon-lib/src/generate/photonlib.json.in +++ b/photon-lib/src/generate/photonlib.json.in @@ -35,19 +35,6 @@ "linuxx86-64", "osxuniversal" ] - }, - { - "groupId": "org.photonvision", - "artifactId": "photontargeting-jni", - "version": "${photon_version}", - "skipInvalidPlatforms": true, - "isJar": true, - "validPlatforms": [ - "windowsx86-64", - "linuxathena", - "linuxx86-64", - "osxuniversal" - ] } ], "cppDependencies": [ diff --git a/photon-targeting/build.gradle b/photon-targeting/build.gradle index 6b35a95987..bcf2ad6933 100644 --- a/photon-targeting/build.gradle +++ b/photon-targeting/build.gradle @@ -146,6 +146,8 @@ model { } binaries.all { + lib library: nativeName, linkage: 'shared' + lib library: "${nativeName}JNI", linkage: 'shared' it.tasks.withType(CppCompile) { it.dependsOn generateProto } diff --git a/photon-targeting/src/main/java/org/photonvision/jni/PhotonTargetingJniLoader.java b/photon-targeting/src/main/java/org/photonvision/jni/PhotonTargetingJniLoader.java index a49e832066..32ef1abf6b 100644 --- a/photon-targeting/src/main/java/org/photonvision/jni/PhotonTargetingJniLoader.java +++ b/photon-targeting/src/main/java/org/photonvision/jni/PhotonTargetingJniLoader.java @@ -17,7 +17,6 @@ package org.photonvision.jni; -import edu.wpi.first.util.RuntimeDetector; import edu.wpi.first.util.RuntimeLoader; import java.io.File; import java.io.FileOutputStream; @@ -42,10 +41,11 @@ public static boolean load_() throws IOException, UnsatisfiedLinkError { var clazz = PhotonTargetingJniLoader.class; for (var libraryName : List.of("photontargeting", "photontargetingJNI")) { - if (RuntimeDetector.isAthena()) { - System.out.println("Detected rio - loading directly"); + try { RuntimeLoader.loadLibrary(libraryName); continue; + } catch (Exception e) { + System.out.println("Direct library load failed; falling back to extraction"); } var nativeLibName = System.mapLibraryName(libraryName); diff --git a/photonlib-java-examples/aimattarget/src/test/java/frc/robot/JniLoadTest.java b/photonlib-java-examples/aimattarget/src/test/java/frc/robot/JniLoadTest.java new file mode 100644 index 0000000000..65cd0f3856 --- /dev/null +++ b/photonlib-java-examples/aimattarget/src/test/java/frc/robot/JniLoadTest.java @@ -0,0 +1,39 @@ +/* + * MIT License + * + * Copyright (c) PhotonVision + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package frc.robot; + +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.photonvision.timesync.TimeSyncSingleton; + +public class JniLoadTest { + @Test + public void smoketest() { + if (!TimeSyncSingleton.load()) { + fail("Could not load TimeSync JNI????????"); + } + } +} diff --git a/shared/javacpp/publish.gradle b/shared/javacpp/publish.gradle index cbbd91ea91..cdd309c055 100644 --- a/shared/javacpp/publish.gradle +++ b/shared/javacpp/publish.gradle @@ -75,7 +75,10 @@ addTaskToCopyAllOutputs(cppHeadersZip) model { publishing { - def cppTaskList = createComponentZipTasks($.components, [nativeName], zipBaseName, Zip, project, includeStandardZipFormat) + def cppTaskList = createComponentZipTasks($.components, [ + nativeName, + "${nativeName}JNI" + ], zipBaseName, Zip, project, includeStandardZipFormat) // From https://github.com/wpilibsuite/allwpilib/blob/1c220ebc607daa8da7d983b8f17bc40323633cb2/shared/jni/publish.gradle#L80C9-L100C11 def jniTaskList = createComponentZipTasks($.components, ["${nativeName}JNI"], jniBaseName, Jar, project, { task, value ->