Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bazel] Add macros to build jni code and java tests #7693

Merged
merged 7 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wpiformat was fighting buildifier when it comes to empty BUILD.bazel files

}

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",
],
)
25 changes: 23 additions & 2 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 @@ -52,9 +54,20 @@ cc_library(
] + HAL_DEPS,
)

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

wpilib_jni_java_library(
name = "hal-java",
srcs = [":generated_java"] + glob(["src/main/java/**/*.java"]),
native_libs = [":wpiHaljni"],
visibility = ["//visibility:public"],
deps = [
"//wpiutil:wpiutil-java",
Expand All @@ -74,6 +87,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.
35 changes: 35 additions & 0 deletions shared/bazel/rules/java_rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@rules_java//java:defs.bzl", "java_binary")

def wpilib_java_junit5_test(
name,
deps = [],
runtime_deps = [],
args = [],
tags = [],
package = "edu",
**kwargs):
"""
Convenience helper to make a junit5 test
"""
junit_deps = [
"@maven//:org_junit_jupiter_junit_jupiter_api",
"@maven//:org_junit_jupiter_junit_jupiter_params",
"@maven//:org_junit_jupiter_junit_jupiter_engine",
]

junit_runtime_deps = [
"@maven//:org_junit_platform_junit_platform_console",
]

# TODO - replace with java_test once shared libraries are hooked up.
java_binary(
name = name,
deps = deps + junit_deps,
runtime_deps = runtime_deps + junit_runtime_deps,
args = args + ["--select-package", package],
main_class = "org.junit.platform.console.ConsoleLauncher",
use_testrunner = False,
testonly = True,
tags = tags + ["no-roborio", "no-bionic", "no-raspbian", "allwpilib-build-java", "no-asan", "no-tsan", "no-ubsan"],
**kwargs
)
Loading
Loading