From 27f63bde2284f7c93756b9080759a2d1ab82ebf0 Mon Sep 17 00:00:00 2001 From: DeltaDizzy Date: Sun, 26 Jan 2025 13:16:33 -0500 Subject: [PATCH] fix java jni and bazel --- libdatalog/BUILD.bazel | 3 +- .../edu/wpi/first/datalog/DataLogJNI.java | 59 ++++++++++++++++++- .../edu/wpi/first/wpilibj/DataLogManager.java | 10 ++-- .../edu/wpi/first/wpilibj/DriverStation.java | 10 ++-- .../first/wpilibj/sysid/SysIdRoutineLog.java | 4 +- 5 files changed, 70 insertions(+), 16 deletions(-) diff --git a/libdatalog/BUILD.bazel b/libdatalog/BUILD.bazel index 1d1dcec7ab6..b9f57737ed1 100644 --- a/libdatalog/BUILD.bazel +++ b/libdatalog/BUILD.bazel @@ -1,5 +1,4 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("//shared/bazel/rules/gen:gen-resources.bzl", "generate_resources") cc_library( name = "libdatalog.static", @@ -15,5 +14,5 @@ cc_library( visibility = ["//visibility:public"], deps = [ "//wpiutil:wpiutil.static", - ] + ], ) diff --git a/libdatalog/src/main/java/edu/wpi/first/datalog/DataLogJNI.java b/libdatalog/src/main/java/edu/wpi/first/datalog/DataLogJNI.java index fab19c25ae4..eff7521de0e 100644 --- a/libdatalog/src/main/java/edu/wpi/first/datalog/DataLogJNI.java +++ b/libdatalog/src/main/java/edu/wpi/first/datalog/DataLogJNI.java @@ -4,16 +4,71 @@ package edu.wpi.first.datalog; -import edu.wpi.first.util.WPIUtilJNI; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.concurrent.atomic.AtomicBoolean; + +import edu.wpi.first.util.RuntimeLoader; /** * DataLog wpiutil JNI Functions. * * @see "wpiutil/DataLog.h" */ -public class DataLogJNI extends WPIUtilJNI { +public class DataLogJNI { + static boolean libraryLoaded = false; + + /** Sets whether JNI should be loaded in the static block. */ + public static class Helper { + private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); + + /** + * Returns true if the JNI should be loaded in the static block. + * + * @return True if the JNI should be loaded in the static block. + */ + public static boolean getExtractOnStaticLoad() { + return extractOnStaticLoad.get(); + } + + /** + * Sets whether the JNI should be loaded in the static block. + * + * @param load Whether the JNI should be loaded in the static block. + */ + public static void setExtractOnStaticLoad(boolean load) { + extractOnStaticLoad.set(load); + } + + /** Utility class. */ + private Helper() {} + } + + static { + if (Helper.getExtractOnStaticLoad()) { + try { + RuntimeLoader.loadLibrary("datalogjni"); + } catch (Exception ex) { + ex.printStackTrace(); + System.exit(1); + } + libraryLoaded = true; + } + } + + /** + * Force load the library. + * + * @throws IOException if the library failed to load + */ + public static synchronized void forceLoad() throws IOException { + if (libraryLoaded) { + return; + } + RuntimeLoader.loadLibrary("datalogjni"); + libraryLoaded = true; + } + /** * Create a new Data Log background writer. The log will be initially created with a temporary * filename. diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DataLogManager.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DataLogManager.java index e78df005a1f..33fd32e0bfa 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DataLogManager.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DataLogManager.java @@ -4,17 +4,17 @@ package edu.wpi.first.wpilibj; +import edu.wpi.first.datalog.DataLog; +import edu.wpi.first.datalog.DataLogBackgroundWriter; +import edu.wpi.first.datalog.FileLogger; +import edu.wpi.first.datalog.IntegerLogEntry; +import edu.wpi.first.datalog.StringLogEntry; import edu.wpi.first.hal.FRCNetComm.tInstances; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; import edu.wpi.first.networktables.NetworkTableInstance; -import edu.wpi.first.datalog.FileLogger; import edu.wpi.first.util.WPIUtilJNI; import edu.wpi.first.util.concurrent.Event; -import edu.wpi.first.datalog.DataLog; -import edu.wpi.first.datalog.DataLogBackgroundWriter; -import edu.wpi.first.datalog.IntegerLogEntry; -import edu.wpi.first.datalog.StringLogEntry; import java.io.File; import java.io.IOException; import java.nio.file.Files; diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java index a29d6c661b5..eed127ce555 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java @@ -4,6 +4,11 @@ package edu.wpi.first.wpilibj; +import edu.wpi.first.datalog.BooleanArrayLogEntry; +import edu.wpi.first.datalog.BooleanLogEntry; +import edu.wpi.first.datalog.DataLog; +import edu.wpi.first.datalog.FloatArrayLogEntry; +import edu.wpi.first.datalog.IntegerArrayLogEntry; import edu.wpi.first.hal.AllianceStationID; import edu.wpi.first.hal.ControlWord; import edu.wpi.first.hal.DriverStationJNI; @@ -16,11 +21,6 @@ import edu.wpi.first.networktables.StringTopic; import edu.wpi.first.util.EventVector; import edu.wpi.first.util.WPIUtilJNI; -import edu.wpi.first.datalog.BooleanArrayLogEntry; -import edu.wpi.first.datalog.BooleanLogEntry; -import edu.wpi.first.datalog.DataLog; -import edu.wpi.first.datalog.FloatArrayLogEntry; -import edu.wpi.first.datalog.IntegerArrayLogEntry; import java.nio.ByteBuffer; import java.util.Map; import java.util.Optional; diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/sysid/SysIdRoutineLog.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/sysid/SysIdRoutineLog.java index 468e586bdcc..f80332a26be 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/sysid/SysIdRoutineLog.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/sysid/SysIdRoutineLog.java @@ -12,6 +12,8 @@ import static edu.wpi.first.units.Units.Second; import static edu.wpi.first.units.Units.Volts; +import edu.wpi.first.datalog.DoubleLogEntry; +import edu.wpi.first.datalog.StringLogEntry; import edu.wpi.first.units.measure.Angle; import edu.wpi.first.units.measure.AngularAcceleration; import edu.wpi.first.units.measure.AngularVelocity; @@ -20,8 +22,6 @@ import edu.wpi.first.units.measure.LinearAcceleration; import edu.wpi.first.units.measure.LinearVelocity; import edu.wpi.first.units.measure.Voltage; -import edu.wpi.first.datalog.DoubleLogEntry; -import edu.wpi.first.datalog.StringLogEntry; import edu.wpi.first.wpilibj.DataLogManager; import java.util.HashMap; import java.util.Map;