mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
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.
288 lines
11 KiB
CMake
288 lines
11 KiB
CMake
project(wpiutil)
|
|
|
|
include(SubDirList)
|
|
include(GenResources)
|
|
include(CompileWarnings)
|
|
include(AddTest)
|
|
include(DownloadAndCheck)
|
|
|
|
file(GLOB wpiutil_jni_src src/main/native/cpp/jni/WPIUtilJNI.cpp)
|
|
|
|
# Java bindings
|
|
if(WITH_JAVA)
|
|
include(UseJava)
|
|
|
|
if(NOT EXISTS "${WPILIB_BINARY_DIR}/wpiutil/thirdparty/avaje/avaje-jsonb-3.11.jar")
|
|
set(BASE_URL "https://search.maven.org/remotecontent?filepath=")
|
|
set(JAR_ROOT "${WPILIB_BINARY_DIR}/wpiutil/thirdparty/avaje")
|
|
|
|
message(STATUS "Downloading Avaje jarfiles...")
|
|
|
|
download_and_check(
|
|
"${BASE_URL}io/avaje/avaje-jsonb/3.11/avaje-jsonb-3.11.jar"
|
|
"${JAR_ROOT}/avaje-jsonb-3.11.jar"
|
|
)
|
|
download_and_check(
|
|
"${BASE_URL}io/avaje/avaje-json-core/3.11/avaje-json-core-3.11.jar"
|
|
"${JAR_ROOT}/avaje-json-core-3.11.jar"
|
|
)
|
|
download_and_check(
|
|
"${BASE_URL}io/avaje/avaje-jsonb-inject-plugin/3.11/avaje-jsonb-inject-plugin-3.11.jar"
|
|
"${JAR_ROOT}/avaje-jsonb-inject-plugin-3.11.jar"
|
|
)
|
|
download_and_check(
|
|
"${BASE_URL}io/avaje/avaje-jsonb-generator/3.11/avaje-jsonb-generator-3.11.jar"
|
|
"${JAR_ROOT}/avaje-jsonb-generator-3.11.jar"
|
|
)
|
|
download_and_check(
|
|
"${BASE_URL}io/avaje/avaje-spi-service/2.16/avaje-spi-service-2.16.jar"
|
|
"${JAR_ROOT}/avaje-spi-service-2.16.jar"
|
|
)
|
|
download_and_check(
|
|
"${BASE_URL}io/avaje/avaje-spi-core/2.16/avaje-spi-core-2.16.jar"
|
|
"${JAR_ROOT}/avaje-spi-core-2.16.jar"
|
|
)
|
|
|
|
message(STATUS "All files downloaded.")
|
|
endif()
|
|
|
|
file(GLOB AVAJE_JARS ${WPILIB_BINARY_DIR}/wpiutil/thirdparty/avaje/*.jar)
|
|
|
|
if(NOT EXISTS "${WPILIB_BINARY_DIR}/wpiutil/thirdparty/quickbuf/quickbuf-runtime-1.4.jar")
|
|
set(BASE_URL "https://search.maven.org/remotecontent?filepath=")
|
|
set(JAR_ROOT "${WPILIB_BINARY_DIR}/wpiutil/thirdparty/quickbuf")
|
|
|
|
message(STATUS "Downloading Quickbuf jarfile...")
|
|
download_and_check(
|
|
"${BASE_URL}us/hebi/quickbuf/quickbuf-runtime/1.4/quickbuf-runtime-1.4.jar"
|
|
"${JAR_ROOT}/quickbuf-runtime-1.4.jar"
|
|
)
|
|
|
|
message(STATUS "Downloaded.")
|
|
endif()
|
|
|
|
file(GLOB QUICKBUF_JAR ${WPILIB_BINARY_DIR}/wpiutil/thirdparty/quickbuf/*.jar)
|
|
|
|
set(CMAKE_JNI_TARGET true)
|
|
|
|
file(GLOB_RECURSE JAVA_SOURCES src/main/java/*.java)
|
|
|
|
add_jar(
|
|
wpiutil_jar
|
|
${JAVA_SOURCES}
|
|
INCLUDE_JARS ${AVAJE_JARS} ${QUICKBUF_JAR}
|
|
OUTPUT_NAME wpiutil
|
|
OUTPUT_DIR ${WPILIB_BINARY_DIR}/${java_lib_dest}
|
|
GENERATE_NATIVE_HEADERS wpiutil_jni_headers
|
|
)
|
|
set_property(TARGET wpiutil_jar PROPERTY FOLDER "java")
|
|
|
|
install_jar(wpiutil_jar DESTINATION ${java_lib_dest})
|
|
install_jar_exports(TARGETS wpiutil_jar FILE wpiutil_jar.cmake DESTINATION share/wpiutil)
|
|
|
|
add_library(wpiutiljni ${wpiutil_jni_src})
|
|
wpilib_target_warnings(wpiutiljni)
|
|
target_link_libraries(wpiutiljni PUBLIC wpiutil)
|
|
|
|
set_property(TARGET wpiutiljni PROPERTY FOLDER "libraries")
|
|
|
|
target_link_libraries(wpiutiljni PRIVATE wpiutil_jni_headers)
|
|
add_dependencies(wpiutiljni wpiutil_jar)
|
|
|
|
install(TARGETS wpiutiljni EXPORT wpiutiljni)
|
|
export(TARGETS wpiutiljni FILE wpiutiljni.cmake NAMESPACE wpiutiljni::)
|
|
endif()
|
|
|
|
if(WITH_JAVA_SOURCE)
|
|
include(UseJava)
|
|
include(CreateSourceJar)
|
|
add_source_jar(
|
|
wpiutil_src_jar
|
|
BASE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/src/main/java
|
|
OUTPUT_NAME wpiutil-sources
|
|
OUTPUT_DIR ${WPILIB_BINARY_DIR}/${java_lib_dest}
|
|
)
|
|
set_property(TARGET wpiutil_src_jar PROPERTY FOLDER "java")
|
|
|
|
install_jar(wpiutil_src_jar DESTINATION ${java_lib_dest})
|
|
endif()
|
|
|
|
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
|
find_package(Threads REQUIRED)
|
|
|
|
if(NOT MSVC AND NOT APPLE AND NOT ANDROID)
|
|
find_library(ATOMIC NAMES atomic libatomic.so.1)
|
|
if(ATOMIC)
|
|
message(STATUS "Found libatomic: ${ATOMIC}")
|
|
else()
|
|
message(STATUS "libatomic not found. If build fails, install libatomic")
|
|
endif()
|
|
endif()
|
|
|
|
generate_resources(src/main/native/resources generated/main/cpp WPI wpi wpiutil_resources_src)
|
|
|
|
file(
|
|
GLOB_RECURSE wpiutil_native_src
|
|
src/main/native/cpp/*.cpp
|
|
src/main/native/thirdparty/debugging/src/*.cpp
|
|
src/main/native/thirdparty/double-conversion/src/*.cpp
|
|
src/main/native/thirdparty/json/src/*.cpp
|
|
src/main/native/thirdparty/llvm/cpp/*.cpp
|
|
src/main/native/thirdparty/mpack/src/*.cpp
|
|
src/main/native/thirdparty/nanopb/src/*.cpp
|
|
src/main/native/thirdparty/upb/src/*.c
|
|
)
|
|
list(REMOVE_ITEM wpiutil_native_src ${wpiutil_jni_src})
|
|
file(GLOB_RECURSE wpiutil_unix_src src/main/native/unix/*.cpp)
|
|
file(GLOB_RECURSE wpiutil_linux_src src/main/native/linux/*.cpp)
|
|
file(GLOB_RECURSE wpiutil_macos_src src/main/native/macOS/*.cpp)
|
|
file(GLOB_RECURSE wpiutil_windows_src src/main/native/windows/*.cpp)
|
|
|
|
file(GLOB fmtlib_native_src src/main/native/thirdparty/fmtlib/src/*.cpp)
|
|
|
|
add_library(wpiutil ${wpiutil_native_src} ${wpiutil_resources_src})
|
|
set_target_properties(wpiutil PROPERTIES DEBUG_POSTFIX "d")
|
|
|
|
set_property(TARGET wpiutil PROPERTY FOLDER "libraries")
|
|
|
|
target_compile_features(wpiutil PUBLIC cxx_std_20)
|
|
if(MSVC)
|
|
target_compile_options(
|
|
wpiutil
|
|
PUBLIC /permissive- /Zc:preprocessor /Zc:__cplusplus /Zc:throwingNew /MP /bigobj /utf-8
|
|
)
|
|
target_compile_definitions(wpiutil PRIVATE -D_CRT_SECURE_NO_WARNINGS)
|
|
endif()
|
|
wpilib_target_warnings(wpiutil)
|
|
target_link_libraries(wpiutil Threads::Threads ${CMAKE_DL_LIBS})
|
|
|
|
if(ATOMIC)
|
|
target_link_libraries(wpiutil ${ATOMIC})
|
|
endif()
|
|
|
|
if(NOT USE_SYSTEM_FMTLIB)
|
|
target_sources(wpiutil PRIVATE ${fmtlib_native_src})
|
|
install(
|
|
DIRECTORY src/main/native/thirdparty/fmtlib/include/
|
|
DESTINATION "${include_dest}/wpiutil"
|
|
)
|
|
target_include_directories(
|
|
wpiutil
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/fmtlib/include>
|
|
)
|
|
else()
|
|
find_package(fmt CONFIG REQUIRED)
|
|
target_link_libraries(wpiutil fmt::fmt)
|
|
if(MSVC)
|
|
get_target_property(fmt_includes fmt::fmt INTERFACE_INCLUDE_DIRECTORIES)
|
|
foreach(dir ${fmt_includes})
|
|
target_compile_options(wpiutil PUBLIC /external:I "${dir}")
|
|
endforeach()
|
|
target_compile_options(wpiutil PUBLIC /external:W0)
|
|
endif()
|
|
endif()
|
|
|
|
if(MSVC)
|
|
target_sources(wpiutil PRIVATE ${wpiutil_windows_src})
|
|
else()
|
|
target_sources(wpiutil PRIVATE ${wpiutil_unix_src})
|
|
if(APPLE)
|
|
target_sources(wpiutil PRIVATE ${wpiutil_macos_src})
|
|
else()
|
|
target_sources(wpiutil PRIVATE ${wpiutil_linux_src})
|
|
endif()
|
|
endif()
|
|
|
|
install(
|
|
DIRECTORY
|
|
src/main/native/include/
|
|
src/main/native/thirdparty/argparse/include/
|
|
src/main/native/thirdparty/debugging/include/
|
|
src/main/native/thirdparty/double-conversion/include/
|
|
src/main/native/thirdparty/expected/include/
|
|
src/main/native/thirdparty/json/include/
|
|
src/main/native/thirdparty/llvm/include/
|
|
src/main/native/thirdparty/mpack/include/
|
|
src/main/native/thirdparty/nanopb/include/
|
|
src/main/native/thirdparty/sigslot/include/
|
|
src/main/native/thirdparty/upb/include/
|
|
DESTINATION "${include_dest}/wpiutil"
|
|
)
|
|
target_include_directories(
|
|
wpiutil
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/argparse/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/debugging/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/double-conversion/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/expected/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/json/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/llvm/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/mpack/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/nanopb/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/sigslot/include>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/upb/include>
|
|
$<INSTALL_INTERFACE:${include_dest}/wpiutil>
|
|
)
|
|
|
|
install(TARGETS wpiutil EXPORT wpiutil)
|
|
export(TARGETS wpiutil FILE wpiutil.cmake NAMESPACE wpiutil::)
|
|
|
|
configure_file(wpiutil-config.cmake.in ${WPILIB_BINARY_DIR}/wpiutil-config.cmake)
|
|
install(FILES ${WPILIB_BINARY_DIR}/wpiutil-config.cmake DESTINATION share/wpiutil)
|
|
install(EXPORT wpiutil DESTINATION share/wpiutil)
|
|
|
|
add_executable(wpiutildev src/dev/native/cpp/main.cpp)
|
|
wpilib_target_warnings(wpiutildev)
|
|
target_link_libraries(wpiutildev wpiutil)
|
|
|
|
subdir_list(wpiutil_examples "${CMAKE_CURRENT_SOURCE_DIR}/examples")
|
|
foreach(example ${wpiutil_examples})
|
|
file(GLOB wpiutil_example_src examples/${example}/*.cpp)
|
|
if(wpiutil_example_src)
|
|
add_executable(wpiutil_${example} ${wpiutil_example_src})
|
|
wpilib_target_warnings(wpiutil_${example})
|
|
target_link_libraries(wpiutil_${example} wpiutil)
|
|
set_property(TARGET wpiutil_${example} PROPERTY FOLDER "examples")
|
|
endif()
|
|
endforeach()
|
|
|
|
if(WITH_TESTS)
|
|
file(GLOB_RECURSE wpiutil_testlib_src src/test/native/include/*.h)
|
|
add_library(wpiutil_testlib INTERFACE ${wpiutil_test_src})
|
|
target_include_directories(wpiutil_testlib INTERFACE src/test/native/include)
|
|
|
|
# Not using wpilib_add_test until we migrate more tests
|
|
file(GLOB_RECURSE test_src src/test/native/cpp/*.cpp)
|
|
file(GLOB catch2_test_src src/test/native/cpp/catch2main.cpp src/test/native/cpp/TestCatch2.cpp)
|
|
list(REMOVE_ITEM test_src ${catch2_test_src})
|
|
add_executable(wpiutil_test ${test_src})
|
|
set_property(TARGET wpiutil_test PROPERTY FOLDER "tests")
|
|
wpilib_target_warnings(wpiutil_test)
|
|
if(BUILD_SHARED_LIBS)
|
|
target_compile_definitions(wpiutil_test PRIVATE -DGTEST_LINKED_AS_SHARED_LIBRARY)
|
|
endif()
|
|
if(MSVC)
|
|
target_compile_options(wpiutil_test PRIVATE /wd4101 /wd4251)
|
|
endif()
|
|
add_test(NAME wpiutil COMMAND wpiutil_test)
|
|
target_include_directories(wpiutil_test PRIVATE src/generated/test/native/cpp)
|
|
file(GLOB_RECURSE wpiutil_nanopb_test_src src/generated/test/native/cpp/*.cpp)
|
|
target_sources(wpiutil_test PRIVATE ${wpiutil_nanopb_test_src})
|
|
target_link_libraries(wpiutil_test wpiutil googletest wpiutil_testlib)
|
|
|
|
add_executable(wpiutil_catch2_test ${catch2_test_src})
|
|
set_property(TARGET wpiutil_catch2_test PROPERTY FOLDER "tests")
|
|
wpilib_target_warnings(wpiutil_catch2_test)
|
|
if(MSVC)
|
|
target_compile_options(wpiutil_catch2_test PRIVATE /wd4101 /wd4251)
|
|
endif()
|
|
target_link_libraries(wpiutil_catch2_test wpiutil catch2 wpiutil_testlib)
|
|
catch_discover_tests(wpiutil_catch2_test)
|
|
if(MSVC)
|
|
target_compile_options(wpiutil_test PRIVATE /utf-8)
|
|
target_compile_options(wpiutil_catch2_test PRIVATE /utf-8)
|
|
endif()
|
|
endif()
|