Skip to content

Commit

Permalink
[photon-targeting] Fix JNI loading (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 authored Nov 13, 2024
1 parent 744e522 commit c7ed377
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 23 deletions.
24 changes: 18 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions photon-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
13 changes: 0 additions & 13 deletions photon-lib/src/generate/photonlib.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 2 additions & 0 deletions photon-targeting/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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????????");
}
}
}
5 changes: 4 additions & 1 deletion shared/javacpp/publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down

0 comments on commit c7ed377

Please sign in to comment.