Skip to content

Commit

Permalink
[bazel] Add macros to build jni code and java tests (#7693)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjreiniger authored Jan 27, 2025
1 parent eee30c4 commit 3b345fe
Show file tree
Hide file tree
Showing 19 changed files with 468 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
matrix:
include:
- { name: "Linux (native)", os: ubuntu-24.04, action: "test", config: "--config=linux", }
- { name: "Linux (systemcore)", os: ubuntu-24.04, action: "build", config: "--config=systemcore", }
name: "${{ matrix.name }}"
runs-on: ${{ matrix.os }}
steps:
Expand Down
1 change: 1 addition & 0 deletions .styleguide
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ modifiableFileExclude {
thirdparty/
\.patch$
gradlew
BUILD.bazel
}

generatedFileExclude {
Expand Down
67 changes: 67 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
rules_jvm_external_deps()

load("@rules_jvm_external//:defs.bzl", "maven_install")
load("@rules_jvm_external//:specs.bzl", "maven")

maven_artifacts = [
"org.ejml:ejml-simple:0.43.1",
Expand All @@ -21,6 +22,72 @@ maven_artifacts = [
"com.fasterxml.jackson.core:jackson-databind:2.15.2",
"us.hebi.quickbuf:quickbuf-runtime:1.3.3",
"com.google.code.gson:gson:2.10.1",
maven.artifact(
"org.junit.jupiter",
"junit-jupiter",
"5.10.1",
testonly = True,
),
maven.artifact(
"org.junit.platform",
"junit-platform-console",
"1.10.1",
testonly = True,
),
maven.artifact(
"org.junit.platform",
"junit-platform-launcher",
"1.10.1",
testonly = True,
),
maven.artifact(
"org.junit.platform",
"junit-platform-reporting",
"1.10.1",
testonly = True,
),
maven.artifact(
"com.google.code.gson",
"gson",
"2.10.1",
testonly = False,
),
maven.artifact(
"org.hamcrest",
"hamcrest-all",
"1.3",
testonly = True,
),
maven.artifact(
"com.googlecode.junit-toolbox",
"junit-toolbox",
"2.4",
testonly = True,
),
maven.artifact(
"org.apache.ant",
"ant",
"1.10.12",
testonly = True,
),
maven.artifact(
"org.apache.ant",
"ant-junit",
"1.10.12",
testonly = True,
),
maven.artifact(
"org.mockito",
"mockito-core",
"4.1.0",
testonly = True,
),
maven.artifact(
"com.google.testing.compile",
"compile-testing",
"0.21.0",
testonly = True,
),
]

maven_install(
Expand Down
31 changes: 29 additions & 2 deletions apriltag/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
load("@rules_java//java:defs.bzl", "java_binary")
load("@rules_python//python:defs.bzl", "py_binary")
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/gen:gen-resources.bzl", "generate_resources")

cc_library(
Expand Down Expand Up @@ -59,9 +61,20 @@ cc_library(
],
)

java_library(
wpilib_jni_cc_library(
name = "apriltagjni",
srcs = glob(["src/main/native/cpp/jni/**"]),
java_dep = ":apriltag-java",
visibility = ["//visibility:public"],
deps = [
":apriltag.static",
],
)

wpilib_jni_java_library(
name = "apriltag-java",
srcs = glob(["src/main/java/**/*.java"]),
native_libs = [":apriltagjni"],
resource_strip_prefix = "apriltag/src/main/native/resources",
resources = glob(["src/main/native/resources/**"]),
visibility = ["//visibility:public"],
Expand All @@ -88,6 +101,20 @@ cc_test(
],
)

wpilib_java_junit5_test(
name = "apriltag-java-test",
srcs = glob(["src/test/java/**/*.java"]),
resource_strip_prefix = "apriltag/src/test/resources",
resources = glob(["src/test/resources/**"]),
deps = [
":apriltag-java",
"//wpimath:wpimath-java",
"//wpiutil:wpiutil-java",
"@bzlmodrio-opencv//libraries/java/opencv",
"@maven//:com_fasterxml_jackson_core_jackson_databind",
],
)

cc_binary(
name = "DevMain-Cpp",
srcs = ["src/dev/native/cpp/main.cpp"],
Expand Down
26 changes: 24 additions & 2 deletions cscore/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test", "objc_library")
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
load("@rules_java//java:defs.bzl", "java_binary")
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")

WIN_SRCS = glob([
"src/main/native/windows/**/*.cpp",
Expand Down Expand Up @@ -71,9 +73,20 @@ cc_library(
}),
)

java_library(
wpilib_jni_cc_library(
name = "cscorejni",
srcs = glob(["src/main/native/cpp/jni/**"]),
java_dep = ":cscore-java",
visibility = ["//visibility:public"],
deps = [
":cscore.static",
],
)

wpilib_jni_java_library(
name = "cscore-java",
srcs = glob(["src/main/java/**/*.java"]),
native_libs = [":cscorejni"],
visibility = ["//visibility:public"],
deps = [
"//wpiutil:wpiutil-java",
Expand All @@ -91,6 +104,15 @@ cc_test(
],
)

wpilib_java_junit5_test(
name = "cscore-java-test",
srcs = glob(["src/test/java/**/*.java"]),
deps = [
":cscore-java",
"//wpiutil:wpiutil-java",
],
)

cc_binary(
name = "DevMain-Cpp",
srcs = ["src/dev/native/cpp/main.cpp"],
Expand Down
13 changes: 13 additions & 0 deletions epilogue-processor/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
load("//shared/bazel/rules:java_rules.bzl", "wpilib_java_junit5_test")

java_library(
name = "processor",
Expand All @@ -20,3 +21,15 @@ java_plugin(
":processor",
],
)

wpilib_java_junit5_test(
name = "processor-test",
srcs = glob(["src/test/java/**/*.java"]),
plugins = [
":plugin",
],
deps = [
":processor",
"@maven//:com_google_testing_compile_compile_testing",
],
)
10 changes: 10 additions & 0 deletions epilogue-runtime/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_java//java:defs.bzl", "java_library")
load("//shared/bazel/rules:java_rules.bzl", "wpilib_java_junit5_test")

java_library(
name = "epilogue",
Expand All @@ -10,3 +11,12 @@ java_library(
"//wpiutil:wpiutil-java",
],
)

wpilib_java_junit5_test(
name = "epilogue-test",
srcs = glob(["src/test/java/**/*.java"]),
deps = [
":epilogue",
"//wpiutil:wpiutil-java",
],
)
9 changes: 9 additions & 0 deletions fieldImages/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_java//java:defs.bzl", "java_library")
load("//shared/bazel/rules:java_rules.bzl", "wpilib_java_junit5_test")
load("//shared/bazel/rules/gen:gen-resources.bzl", "generate_resources")

generate_resources(
Expand Down Expand Up @@ -29,3 +30,11 @@ java_library(
"@maven//:com_fasterxml_jackson_core_jackson_databind",
],
)

wpilib_java_junit5_test(
name = "fieldImages-test",
srcs = glob(["src/test/java/**/*.java"]),
deps = [
":fieldImages-java",
],
)
42 changes: 27 additions & 15 deletions hal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
load("@rules_java//java:defs.bzl", "java_binary")
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")

cc_library(
name = "generated_cc_headers",
Expand Down Expand Up @@ -31,23 +33,14 @@ filegroup(
visibility = ["//hal:__subpackages__"],
)

ATHENA_SRCS = glob(["src/main/native/athena/**"])

ATHENA_DEPS = ["@bzlmodrio-ni//libraries/cpp/ni:shared"]
SYSTEMCORE_SRCS = glob(["src/main/native/systemcore/**"])

SIM_SRCS = glob(["src/main/native/sim/**"])

SIM_DEPS = []

HAL_DEPS = select({
"@rules_bzlmodrio_toolchains//constraints/is_roborio:roborio": ATHENA_DEPS,
"//conditions:default": SIM_DEPS,
})

filegroup(
name = "platform-srcs",
srcs = select({
"@rules_bzlmodrio_toolchains//constraints/is_roborio:roborio": ATHENA_SRCS,
"@rules_bzlmodrio_toolchains//constraints/is_systemcore:systemcore": SYSTEMCORE_SRCS,
"//conditions:default": SIM_SRCS,
}),
)
Expand All @@ -67,17 +60,28 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
":generated_cc_headers",
":mrc_cc_headers",
":generated_mrc_cc_headers",
":mrc_cc_headers",
"//ntcore:ntcore.static",
"//wpinet:wpinet.static",
"//wpiutil:wpiutil.static",
] + HAL_DEPS,
],
)

wpilib_jni_cc_library(
name = "wpiHaljni",
srcs = glob(["src/main/native/cpp/jni/**"]),
java_dep = ":hal-java",
visibility = ["//visibility:public"],
deps = [
":wpiHal.static",
],
)

java_library(
wpilib_jni_java_library(
name = "hal-java",
srcs = [":generated_java"] + glob(["src/main/java/**/*.java"]),
native_libs = [":wpiHaljni"],
visibility = ["//visibility:public"],
deps = [
"//ntcore:networktables-java",
Expand All @@ -99,6 +103,14 @@ cc_test(
],
)

wpilib_java_junit5_test(
name = "hal-java-test",
srcs = glob(["src/test/java/**/*.java"]),
deps = [
":hal-java",
],
)

cc_binary(
name = "DevMain-Cpp",
srcs = ["src/dev/native/cpp/main.cpp"],
Expand Down
33 changes: 31 additions & 2 deletions ntcore/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
load("@rules_java//java:defs.bzl", "java_binary")
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")

cc_library(
name = "generated_cc_headers",
Expand All @@ -18,6 +20,12 @@ filegroup(
visibility = ["//ntcore:__subpackages__"],
)

filegroup(
name = "generated_jni",
srcs = glob(["src/generated/main/native/cpp/jni/**"]),
visibility = ["//ntcore:__subpackages__"],
)

filegroup(
name = "generated_java",
srcs = glob(["src/generated/main/java/**/*.java"]),
Expand All @@ -44,9 +52,20 @@ cc_library(
],
)

java_library(
wpilib_jni_cc_library(
name = "ntcorejni",
srcs = glob(["src/main/native/cpp/jni/**"]) + [":generated_jni"],
java_dep = ":networktables-java",
visibility = ["//visibility:public"],
deps = [
":ntcore.static",
],
)

wpilib_jni_java_library(
name = "networktables-java",
srcs = glob(["src/main/java/**/*.java"]) + [":generated_java"],
native_libs = [":ntcorejni"],
visibility = ["//visibility:public"],
deps = [
"//wpiutil:wpiutil-java",
Expand All @@ -73,6 +92,16 @@ cc_test(
],
)

wpilib_java_junit5_test(
name = "ntcore-java-test",
srcs = glob(["src/test/java/**/*.java"]),
tags = ["exclusive"],
deps = [
":networktables-java",
"//wpiutil:wpiutil-java",
],
)

cc_binary(
name = "DevMain-Cpp",
srcs = ["src/dev/native/cpp/main.cpp"],
Expand Down
Empty file added shared/bazel/rules/BUILD.bazel
Empty file.
Loading

0 comments on commit 3b345fe

Please sign in to comment.