Files
allwpilib/fields/CMakeLists.txt

75 lines
2.3 KiB
CMake
Raw Normal View History

2025-11-07 19:55:39 -05:00
project(fields)
include(CompileWarnings)
include(GenResources)
if(WITH_JAVA)
include(UseJava)
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-11 02:21:00 -04:00
file(GLOB AVAJE_JARS "${WPILIB_BINARY_DIR}/wpiutil/thirdparty/avaje/*.jar")
file(GLOB_RECURSE JAVA_SOURCES src/main/java/*.java)
file(
GLOB_RECURSE JAVA_FRC_RESOURCES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
src/main/native/resources/frc/*.json
src/main/native/resources/frc/*.png
src/main/native/resources/frc/*.jpg
)
file(
GLOB_RECURSE JAVA_FTC_RESOURCES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
src/main/native/resources/ftc/*.json
src/main/native/resources/ftc/*.png
src/main/native/resources/ftc/*.jpg
)
add_jar(
field_images_jar
SOURCES ${JAVA_SOURCES}
RESOURCES
NAMESPACE "org/wpilib/fields/frc" ${JAVA_FRC_RESOURCES}
NAMESPACE "org/wpilib/fields/ftc" ${JAVA_FTC_RESOURCES}
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-11 02:21:00 -04:00
INCLUDE_JARS ${AVAJE_JARS}
OUTPUT_DIR ${WPILIB_BINARY_DIR}/${java_lib_dest}
2025-11-07 19:55:39 -05:00
OUTPUT_NAME fields
)
set_property(TARGET field_images_jar PROPERTY FOLDER "java")
install_jar(field_images_jar DESTINATION ${java_lib_dest})
2025-11-07 19:57:55 -05:00
install_jar_exports(TARGETS field_images_jar FILE fields_jar.cmake DESTINATION share/fields)
endif()
2024-05-24 13:48:05 -04:00
generate_resources(
src/main/native/resources/org/wpilib/fields/*
${CMAKE_CURRENT_BINARY_DIR}/generated/main/cpp
2024-05-24 13:48:05 -04:00
FIELDS
wpi::fields
2024-05-24 13:48:05 -04:00
field_images_resources_src
)
2025-11-07 19:55:39 -05:00
add_library(fields ${field_images_resources_src} src/main/native/cpp/fields.cpp)
set_target_properties(fields PROPERTIES DEBUG_POSTFIX "d")
2025-11-07 19:55:39 -05:00
set_property(TARGET fields PROPERTY FOLDER "libraries")
target_compile_features(fields PUBLIC cxx_std_23)
if(MSVC)
2025-11-07 19:55:39 -05:00
target_compile_options(fields PUBLIC /bigobj)
endif()
2025-11-07 19:55:39 -05:00
wpilib_target_warnings(fields)
2025-11-07 19:55:39 -05:00
install(DIRECTORY src/main/native/include/ DESTINATION "${include_dest}/fields")
target_include_directories(
2025-11-07 19:55:39 -05:00
fields
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/include>
2025-11-07 19:55:39 -05:00
$<INSTALL_INTERFACE:${include_dest}/fields>
)
2025-11-07 19:55:39 -05:00
install(TARGETS fields EXPORT fields)
export(TARGETS fields FILE fields.cmake NAMESPACE fields::)
2025-11-07 19:55:39 -05:00
configure_file(fields-config.cmake.in ${WPILIB_BINARY_DIR}/fields-config.cmake)
install(FILES ${WPILIB_BINARY_DIR}/fields-config.cmake DESTINATION share/fields)
install(EXPORT fields DESTINATION share/fields)