Files
allwpilib/wpimath/BUILD.bazel

424 lines
12 KiB
Python
Raw Permalink Normal View History

load("@allwpilib_pip_deps//:requirements.bzl", "requirement")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@rules_java//java:defs.bzl", "java_binary")
load("@rules_pkg//:mappings.bzl", "pkg_files")
load("@rules_python//python:defs.bzl", "py_binary")
load("//shared/bazel/rules:cc_rules.bzl", "generate_def_windows", "third_party_cc_lib_helper", "wpilib_cc_library", "wpilib_cc_shared_library", "wpilib_cc_static_library")
load("//shared/bazel/rules:java_rules.bzl", "wpilib_java_junit5_test")
load("//shared/bazel/rules:jni_rules.bzl", "wpilib_jni_cc_library", "wpilib_jni_java_library")
load("//shared/bazel/rules:packaging.bzl", "package_default_jni_project", "pkg_files_headers")
load("//shared/bazel/rules/robotpy:build_info_gen.bzl", "generate_robotpy_native_wrapper_build_info", "generate_robotpy_pybind_build_info")
load("//shared/bazel/rules/robotpy:pytest_util.bzl", "robotpy_py_test")
load("//wpimath:generate.bzl", "generate_wpimath")
load("//wpimath:robotpy_native_build_info.bzl", "define_native_wrapper")
load("//wpimath:robotpy_pybind_build_info.bzl", "define_pybind_library", "publish_library_casters", "wpimath_extension")
load("//wpimath:robotpy_pybind_test_info.bzl", "wpimath_test_extension", define_test_pybind_library = "define_pybind_library")
filegroup(
name = "doxygen-files",
srcs = glob([
"src/generated/main/native/cpp/wpimath/protobuf/*.h",
"src/main/native/include/**/*",
"src/main/native/thirdparty/gcem/include/**/*",
"src/main/native/thirdparty/sleipnir/include/**/*",
]),
visibility = ["//visibility:public"],
)
filegroup(
name = "templates",
srcs = glob(["src/generate/main/java/*.jinja"]),
)
filegroup(
name = "proto_files",
srcs = glob(["src/main/proto/**/*.proto"]),
)
py_binary(
name = "generate_nanopb",
srcs = ["generate_nanopb.py"],
target_compatible_with = select({
"@rules_bzlmodrio_toolchains//constraints/is_roborio:roborio": ["@platforms//:incompatible"],
"@rules_bzlmodrio_toolchains//constraints/is_systemcore:systemcore": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
py_binary(
name = "generate_numbers",
srcs = ["generate_numbers.py"],
data = [":templates"],
target_compatible_with = select({
"@rules_bzlmodrio_toolchains//constraints/is_roborio:roborio": ["@platforms//:incompatible"],
"@rules_bzlmodrio_toolchains//constraints/is_systemcore:systemcore": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
deps = [requirement("jinja2")],
)
py_binary(
name = "generate_quickbuf",
srcs = ["generate_quickbuf.py"],
target_compatible_with = select({
"@rules_bzlmodrio_toolchains//constraints/is_roborio:roborio": ["@platforms//:incompatible"],
"@rules_bzlmodrio_toolchains//constraints/is_systemcore:systemcore": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
py_binary(
name = "generate_wpimath_py",
srcs = ["generate_wpimath.py"],
main = "generate_wpimath.py",
target_compatible_with = select({
"@rules_bzlmodrio_toolchains//constraints/is_roborio:roborio": ["@platforms//:incompatible"],
"@rules_bzlmodrio_toolchains//constraints/is_systemcore:systemcore": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
deps = [
":generate_nanopb",
":generate_numbers",
":generate_quickbuf",
],
)
generate_wpimath(
name = "generate_wpimath",
tags = ["pregeneration"],
)
write_source_files(
name = "write_wpimath",
diff_args = ["-u"],
files = {
"src/generated": ":generate_wpimath",
},
suggested_update_target = "//:write_all",
tags = ["pregeneration"],
visibility = ["//visibility:public"],
)
filegroup(
name = "generated_java",
srcs = glob(["src/generated/main/java/**/*.java"]),
)
alias(
name = "eigen-headers",
actual = "@eigen",
visibility = ["//visibility:public"],
)
pkg_files_headers(
name = "eigen-hdrs-pkg",
dep = "@eigen",
)
2025-06-13 23:53:09 -04:00
third_party_cc_lib_helper(
name = "gcem",
2025-06-13 23:53:09 -04:00
include_root = "src/main/native/thirdparty/gcem/include",
)
2025-06-13 23:53:09 -04:00
third_party_cc_lib_helper(
name = "sleipnir",
include_root = "src/main/native/thirdparty/sleipnir/include",
src_root = "src/main/native/thirdparty/sleipnir/src",
)
cc_library(
name = "nanopb-generated-headers",
hdrs = glob(["src/generated/main/native/cpp/**/*.h"]),
includes = ["src/generated/main/native/cpp"],
strip_include_prefix = "src/generated/main/native/cpp",
)
pkg_files(
name = "protobuf_hdr_pkg",
srcs = glob(["src/generated/main/native/cpp/wpimath/protobuf/*.npb.h"]),
strip_prefix = "src/generated/main/native/cpp/",
)
2025-06-13 23:53:09 -04:00
wpilib_cc_library(
name = "wpimath",
srcs = glob(
[
"src/main/native/cpp/**",
"src/generated/main/native/cpp/**/*.cpp",
],
exclude = ["src/main/native/cpp/jni/**"],
2025-06-13 23:53:09 -04:00
),
hdrs = glob(["src/main/native/include/**"]),
extra_hdr_pkg_files = [":protobuf_hdr_pkg"],
extra_src_pkg_files = [
":wpimath-java-jni-hdrs-pkg",
],
includes = [
"src/main/native/include",
"src/main/native/thirdparty/sleipnir/src",
],
local_defines = [
"WPILIB_EXPORTS",
"SLEIPNIR_EXPORTS",
],
strip_include_prefix = "src/main/native/include",
2025-06-13 23:53:09 -04:00
third_party_header_only_libraries = [
":eigen",
":gcem",
],
third_party_libraries = [
":sleipnir",
],
visibility = ["//visibility:public"],
deps = [
":nanopb-generated-headers",
"//wpiutil",
],
)
generate_def_windows(
name = "wpimath_def",
filters = [
".pb.obj",
".npb.obj",
],
deps = [
":wpimath",
],
)
wpilib_cc_shared_library(
name = "shared/wpimath",
auto_export_windows_symbols = False,
dynamic_deps = [
"//wpiutil:shared/wpiutil",
],
visibility = ["//visibility:public"],
win_def_file = ":wpimath_def",
deps = [
":wpimath",
],
)
wpilib_cc_static_library(
name = "static/wpimath",
static_deps = [
"//wpiutil:static/wpiutil",
],
visibility = ["//visibility:public"],
deps = [
":wpimath",
],
)
wpilib_jni_cc_library(
name = "wpimathjni",
srcs = glob(["src/main/native/cpp/jni/**"]),
java_dep = ":wpimath-java",
visibility = ["//visibility:public"],
deps = [
":wpimath",
],
)
wpilib_cc_shared_library(
name = "shared/wpimathjni",
auto_export_windows_symbols = False,
dynamic_deps = [
":shared/wpimath",
"//wpiutil:shared/wpiutil",
],
use_debug_name = False,
visibility = ["//visibility:public"],
deps = [":wpimathjni"],
)
wpilib_jni_java_library(
name = "wpimath-java",
srcs = [":generated_java"] + glob(["src/main/java/**/*.java"]),
exported_plugins = ["//javacPlugin:plugin"],
javacopts = [
"-Xep:UnicodeInCode:OFF",
],
maven_artifact_name = "wpimath-java",
2025-11-07 20:00:38 -05:00
maven_group_id = "org.wpilib.wpimath",
native_libs = [":wpimathjni"],
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
plugins = [
"//:avaje_jsonb_generator",
],
resource_strip_prefix = "wpimath/src/main/proto",
resources = glob(["src/main/proto/**"]),
visibility = ["//visibility:public"],
deps = [
"//wpiannotations",
"//wpiunits:wpiunits-java",
"//wpiutil:wpiutil-java",
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
"@maven//:io_avaje_avaje_json_core",
"@maven//:io_avaje_avaje_jsonb",
"@maven//:org_ejml_ejml_core",
"@maven//:org_ejml_ejml_ddense",
"@maven//:org_ejml_ejml_simple",
"@maven//:us_hebi_quickbuf_quickbuf_runtime",
],
)
cc_library(
name = "test_headers",
hdrs = glob([
"src/test/native/include/**",
]),
strip_include_prefix = "src/test/native/include",
)
cc_test(
name = "wpimath-cpp-test",
size = "small",
srcs = glob([
"src/test/native/cpp/**/*.cpp",
2025-11-07 19:56:21 -05:00
"src/test/native/cpp/**/*.hpp",
]),
tags = [
"no-raspi",
],
deps = [
":test_headers",
":wpimath",
"//thirdparty/googletest",
],
)
wpilib_java_junit5_test(
name = "wpimath-java-test",
srcs = glob(["src/test/java/**/*.java"]),
deps = [
":wpimath-java",
"//wpiunits:wpiunits-java",
"//wpiutil:wpiutil-java",
"@maven//:org_ejml_ejml_core",
"@maven//:org_ejml_ejml_ddense",
"@maven//:org_ejml_ejml_simple",
"@maven//:us_hebi_quickbuf_quickbuf_runtime",
],
)
cc_binary(
name = "DevMain-Cpp",
srcs = ["src/dev/native/cpp/main.cpp"],
deps = [
":wpimath",
],
)
java_binary(
name = "DevMain-Java",
2025-11-07 19:56:21 -05:00
srcs = ["src/dev/java/org/wpilib/math/DevMain.java"],
main_class = "org.wpilib.math.DevMain",
deps = [
":wpimath-java",
],
)
py_binary(
name = "ExponentialProfileModel",
srcs = ["algorithms/ExponentialProfileModel.py"],
2025-06-13 23:53:09 -04:00
target_compatible_with = select({
"@rules_bzlmodrio_toolchains//constraints/is_systemcore:systemcore": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
package_default_jni_project(
name = "wpimath",
maven_artifact_name = "wpimath-cpp",
2025-11-07 20:00:38 -05:00
maven_group_id = "org.wpilib.wpimath",
)
generate_robotpy_native_wrapper_build_info(
name = "robotpy-native-wpimath-generator",
pyproject_toml = "src/main/python/native-pyproject.toml",
third_party_dirs = [
"gcem",
"sleipnir",
],
)
define_native_wrapper(
name = "robotpy-native-wpimath",
pyproject_toml = "src/main/python/native-pyproject.toml",
)
PYBIND_PKGCFG_DEPS = [
"//wpimath:native/wpimath/robotpy-native-wpimath.pc",
"//wpiutil:native/wpiutil/robotpy-native-wpiutil.pc",
"//wpiutil:robotpy-wpiutil.generated_pkgcfg_files",
]
generate_robotpy_pybind_build_info(
name = "robotpy-wpimath-generator",
additional_srcs = [":robotpy-native-wpimath.copy_headers"],
package_root_file = "src/main/python/wpimath/__init__.py",
pkgcfgs = PYBIND_PKGCFG_DEPS,
yaml_files = glob(["src/main/python/semiwrap/**/*.yml"]),
)
publish_library_casters()
wpimath_extension(
srcs = ["src/main/python/wpimath/src/wpimath.cpp"],
extra_hdrs = glob(["src/main/python/wpimath/_impl/src/**/*.h"]),
)
define_pybind_library(
name = "robotpy-wpimath",
pkgcfgs = PYBIND_PKGCFG_DEPS,
)
generate_robotpy_pybind_build_info(
name = "robotpy-wpimath_test-generator",
additional_srcs = ["src/test/python/cpp/wpimath_test/include/module.h"],
generated_file_name = "robotpy_pybind_test_info.bzl",
package_root_file = "src/test/python/cpp/wpimath_test/__init__.py",
pkgcfgs = [
"//wpimath:native/wpimath/robotpy-native-wpimath.pc",
"//wpimath:robotpy-wpimath.generated_pkgcfg_files",
"//wpiutil:native/wpiutil/robotpy-native-wpiutil.pc",
"//wpiutil:robotpy-wpiutil.generated_pkgcfg_files",
],
pyproject_toml = "src/test/python/cpp/pyproject.toml",
stripped_include_prefix = "src/test/python/cpp",
yaml_files = glob(["src/test/python/cpp/semiwrap/**/*.yml"]),
yml_prefix = "src/test/python/cpp/",
)
wpimath_test_extension(
srcs = glob(["src/test/python/cpp/wpimath_test/src/*.cpp"]),
extra_hdrs = ["src/test/python/cpp/wpimath_test/include/module.h"],
header_to_dat_deps = ["src/test/python/cpp/wpimath_test/include/module.h"],
includes = ["src/test/python/cpp/wpimath_test/include"],
)
define_test_pybind_library(
name = "robotpy-wpimath-test",
pkgcfgs = [
"//wpimath:native/wpimath/robotpy-native-wpimath.pc",
"//wpimath:robotpy-wpimath.generated_pkgcfg_files",
"//wpiutil:native/wpiutil/robotpy-native-wpiutil.pc",
"//wpiutil:robotpy-wpiutil.generated_pkgcfg_files",
],
)
robotpy_py_test(
"python_tests",
srcs = glob(
["src/test/python/**/*.py"],
exclude = ["src/test/python/cpp/**"],
),
deps = [
":robotpy-wpimath",
":robotpy-wpimath-test",
requirement("numpy"),
requirement("pytest"),
],
)