Files
allwpilib/wpilibj/CMakeLists.txt
Gold856 2102a543d1 Change Java JSON to Avaje Jsonb (#8721)
Jackson is a very heavy library; it supports loads of features that we
don't need, and historically has caused issues due to long class loading
times (a little over 2 seconds to load AprilTagFieldLayout). This often
manifests as a help request in the form of "my robot disables when I do
X, but doesn't disable when doing X in subsequent attempts until code
restart." While SC has brought down Jackson loading times significantly,
with AprilTagFieldLayout loads taking only 330 milliseconds, that's
still a rather long delay, and while libraries should handle any JSON
loading ahead of time to prevent delays in auto/teleop, it would still
be good to make the worst case better to reduce user frustration.
Benchmarks indicate using [Avaje
Jsonb](https://github.com/avaje/avaje-jsonb) to load AprilTagFieldLayout
only takes ~70 ms, a fair chunk of which isn't actually in Avaje Jsonb
(~4 ms is spent on using getResourceAsStream to retrieve the JSON file,
~8 ms is spent on just loading the AprilTag class and its dependencies).

Note that all times listed are end-to-end, meaning nothing else was done
except for the operation being benchmarked, and doing arithmetic on them
can be flawed due to some classes being loaded twice, i.e.,
getResourceAsStream and `new AprilTag()` likely load some of the same
JDK classes and so subtracting both from the Avaje Jsonb load time is
likely slightly incorrect because class loading is being double counted.
For our purposes, it's likely accurate enough and is mostly just for
contextualization.

Benchmarks were run on a Raspberry Pi CM5 with 2 GB of RAM. Source code
for the
[results](https://github.com/user-attachments/files/26471452/benchmark.txt)
can be found in the "Fastjson2" commit
(2456d15ca8ebd17635e607cd40bf8816e77869a1).

Avaje Jsonb uses code generation via annotation processors to generate
the classes needed to do JSON serde and uses service providers to find
them, which will require downstream changes in robot projects, as the
different service providers in each library must be merged together for
Avaje Jsonb to function. We will use the Gradle shadow plugin, as its
already used by the installer and therefore adds zero additional
dependencies.
2026-04-10 23:21:00 -07:00

79 lines
2.4 KiB
CMake

project(wpilibj)
# Java bindings
if(WITH_JAVA)
include(UseJava)
set(OPENCV_JAVA_INSTALL_DIR ${OpenCV_INSTALL_PATH}/share/java/opencv4)
find_file(
OPENCV_JAR_FILE
NAMES opencv-${OpenCV_VERSION_MAJOR}${OpenCV_VERSION_MINOR}${OpenCV_VERSION_PATCH}.jar
PATHS
${OPENCV_JAVA_INSTALL_DIR}
${OpenCV_INSTALL_PATH}/bin
${OpenCV_INSTALL_PATH}/share/java
${OpenCV_INSTALL_PATH}/share/OpenCV/java
NO_DEFAULT_PATH
)
configure_file(
src/generate/WPILibVersion.java.in
generated/main/java/org/wpilib/system/WPILibVersion.java
)
file(GLOB_RECURSE JAVA_SOURCES src/main/java/*.java src/generated/main/java/*.java)
file(GLOB EJML_JARS "${WPILIB_BINARY_DIR}/wpimath/thirdparty/ejml/*.jar")
file(GLOB AVAJE_JARS "${WPILIB_BINARY_DIR}/wpiutil/thirdparty/avaje/*.jar")
add_jar(
wpilibj_jar
${JAVA_SOURCES}
${CMAKE_CURRENT_BINARY_DIR}/generated/main/java/org/wpilib/system/WPILibVersion.java
INCLUDE_JARS
hal_jar
ntcore_jar
${EJML_JARS}
${AVAJE_JARS}
${OPENCV_JAR_FILE}
cscore_jar
cameraserver_jar
wpimath_jar
wpiunits_jar
wpiutil_jar
datalog_jar
OUTPUT_NAME wpilibj
OUTPUT_DIR ${WPILIB_BINARY_DIR}/${java_lib_dest}
)
set_property(TARGET wpilibj_jar PROPERTY FOLDER "java")
install_jar(wpilibj_jar DESTINATION ${java_lib_dest})
install_jar_exports(TARGETS wpilibj_jar FILE wpilibj.cmake DESTINATION share/wpilibj)
install(FILES wpilibj-config.cmake DESTINATION share/wpilibj)
endif()
if(WITH_JAVA_SOURCE)
include(UseJava)
include(CreateSourceJar)
# Generate version file if it wasn't generated already
if(NOT WITH_JAVA)
configure_file(
src/generate/WPILibVersion.java.in
generated/main/java/org/wpilib/system/WPILibVersion.java
)
endif()
add_source_jar(
wpilibj_src_jar
BASE_DIRECTORIES
${CMAKE_CURRENT_SOURCE_DIR}/src/main/java
${CMAKE_CURRENT_SOURCE_DIR}/src/generated/main/java
${CMAKE_CURRENT_BINARY_DIR}/generated/main/java
OUTPUT_NAME wpilibj-sources
OUTPUT_DIR ${WPILIB_BINARY_DIR}/${java_lib_dest}
)
set_property(TARGET wpilibj_src_jar PROPERTY FOLDER "java")
install_jar(wpilibj_src_jar DESTINATION ${java_lib_dest})
endif()