Skip to content

Commit 5c719ce

Browse files
authored
[bazel] Put eigen in an external repo like bzlmod (#8169)
This sets us up to use AOS, which wants @eigen to resolve, without introducing a second version or copy of eigen.
1 parent 7d34f43 commit 5c719ce

File tree

13 files changed

+869
-9
lines changed

13 files changed

+869
-9
lines changed

WORKSPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ http_archive(
116116
url = "https://github.com/TendTo/rules_doxygen/releases/download/2.4.2/rules_doxygen-2.4.2.tar.gz",
117117
)
118118

119+
# This gives us a repository layout which matches what normal BCR modules expect.
120+
# The goal here is to make it easier to depend on external projects which already
121+
# include @eigen without introducing multiple eigen versions.
122+
local_repository(
123+
name = "eigen",
124+
path = "wpimath/src/main/native/thirdparty/eigen/include/",
125+
)
126+
119127
load("@bazel_features//:deps.bzl", "bazel_features_deps")
120128

121129
bazel_features_deps()

shared/bazel/rules/packaging.bzl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,40 @@ load("@rules_pkg//:mappings.bzl", "pkg_filegroup", "pkg_files")
22
load("@rules_pkg//pkg:zip.bzl", "pkg_zip")
33
load("//shared/bazel/rules:publishing.bzl", "architectures_pkg_zip", "platform_prefix", "wpilib_maven_export")
44

5+
def _headers_impl(ctx):
6+
# Get the CcInfo provider from the dependency.
7+
# We only expect one dependency, so we take the first from the list.
8+
target_cc_info = ctx.attr.dep[CcInfo]
9+
10+
# Extract the set of public headers from the compilation context.
11+
headers = target_cc_info.compilation_context.headers
12+
13+
# Return the generated archive as a default output.
14+
return [DefaultInfo(files = headers)]
15+
16+
_headers = rule(
17+
implementation = _headers_impl,
18+
attrs = {
19+
"dep": attr.label(
20+
providers = [CcInfo],
21+
mandatory = True,
22+
doc = "The cc_library target whose headers should be extracted.",
23+
),
24+
},
25+
doc = "Extracts the public headers of a C/C++ library.",
26+
)
27+
28+
def pkg_files_headers(name, dep):
29+
"""Makes a pkg_files out of the headers of a cc_library target."""
30+
_headers(name = name + "-headers", dep = dep)
31+
pkg_files(
32+
name = name,
33+
srcs = [
34+
":" + name + "-headers",
35+
],
36+
strip_prefix = "",
37+
)
38+
539
def pkg_java_src_files(name):
640
pkg_files(
741
name = name + "-java-srcs",

upstream_utils/eigen.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,19 @@ def copy_upstream_src(wpilib_root: Path):
136136
f, [wpimath / "src/main/native/thirdparty/eigen/include"]
137137
)
138138

139-
shutil.copyfile(
139+
for f in [
140140
".clang-format",
141-
wpimath / "src/main/native/thirdparty/eigen/include/.clang-format",
142-
)
141+
"BUILD.bazel",
142+
"COPYING.APACHE",
143+
"COPYING.BSD",
144+
"COPYING.MINPACK",
145+
"COPYING.MPL2",
146+
"WORKSPACE",
147+
]:
148+
shutil.copyfile(
149+
f,
150+
wpimath / "src/main/native/thirdparty/eigen/include" / f,
151+
)
143152

144153

145154
def main():

upstream_utils/eigen_patches/0001-Disable-warnings.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Tyler Veness <[email protected]>
33
Date: Wed, 18 May 2022 09:14:24 -0700
4-
Subject: [PATCH 1/2] Disable warnings
4+
Subject: [PATCH 1/3] Disable warnings
55

66
---
77
Eigen/src/Core/util/DisableStupidWarnings.h | 9 +++++++++

upstream_utils/eigen_patches/0002-Intellisense-fix.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Peter Johnson <[email protected]>
33
Date: Fri, 20 Jan 2023 23:41:56 -0800
4-
Subject: [PATCH 2/2] Intellisense fix
4+
Subject: [PATCH 2/3] Intellisense fix
55

66
---
77
Eigen/src/Core/util/ConfigureVectorization.h | 7 +++++++
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Austin Schuh <[email protected]>
3+
Date: Sat, 9 Aug 2025 12:45:23 -0700
4+
Subject: [PATCH 3/3] Add build files from bzlmod for eigen
5+
6+
This makes it so our vendored version of eigen matches the same API as
7+
the upstream eigen in bcr.
8+
---
9+
BUILD.bazel | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
10+
WORKSPACE | 0
11+
2 files changed, 64 insertions(+)
12+
create mode 100644 BUILD.bazel
13+
create mode 100644 WORKSPACE
14+
15+
diff --git a/BUILD.bazel b/BUILD.bazel
16+
new file mode 100644
17+
index 0000000000000000000000000000000000000000..38ce6cb417485fb37cafaf1e0f536e7ac56706b0
18+
--- /dev/null
19+
+++ b/BUILD.bazel
20+
@@ -0,0 +1,64 @@
21+
+load("@rules_cc//cc:cc_library.bzl", "cc_library")
22+
+load("@rules_license//rules:license.bzl", "license")
23+
+
24+
+package(
25+
+ default_applicable_licenses = [
26+
+ ":license.APACHE",
27+
+ ":license.BSD",
28+
+ ":license.MINPACK", # Only used by unsupported/** not by Eigen/**.
29+
+ ":license.MPL2",
30+
+ ],
31+
+)
32+
+
33+
+exports_files(glob(["COPYING.*"]))
34+
+
35+
+# Note: Eigen is primarily an MPL2 library with small bits of code under other
36+
+# licenses. Previous versions of Eigen contained LGPL code which needed to be
37+
+# carefully excluded, but as of approximately 2023-02-07 all LGPL code has been
38+
+# removed upstream so does not need any special handling here.
39+
+
40+
+license(
41+
+ name = "license.APACHE",
42+
+ license_kinds = ["@rules_license//licenses/spdx:Apache-2.0"],
43+
+ license_text = "COPYING.APACHE",
44+
+)
45+
+
46+
+license(
47+
+ name = "license.BSD",
48+
+ license_kinds = ["@rules_license//licenses/spdx:BSD-3-Clause"],
49+
+ license_text = "COPYING.BSD",
50+
+)
51+
+
52+
+license(
53+
+ name = "license.MINPACK",
54+
+ license_kinds = ["@rules_license//licenses/spdx:BSD-3-Clause-Attribution"],
55+
+ license_text = "COPYING.MINPACK",
56+
+)
57+
+
58+
+license(
59+
+ name = "license.MPL2",
60+
+ license_kinds = ["@rules_license//licenses/spdx:MPL-2.0"],
61+
+ license_text = "COPYING.MPL2",
62+
+)
63+
+
64+
+HDRS = glob(
65+
+ [
66+
+ "Eigen/**",
67+
+ "unsupported/Eigen/**",
68+
+ ],
69+
+ exclude = [
70+
+ # We don't want any documentation files.
71+
+ "**/*.md",
72+
+ "**/*.txt",
73+
+ "unsupported/Eigen/NonLinearOptimization",
74+
+ #"unsupported/Eigen/MatrixFunctions",
75+
+ "unsupported/Eigen/Polynomials",
76+
+ ],
77+
+)
78+
+
79+
+cc_library(
80+
+ name = "eigen",
81+
+ hdrs = HDRS,
82+
+ includes = ["."],
83+
+ visibility = ["//visibility:public"],
84+
+)
85+
diff --git a/WORKSPACE b/WORKSPACE
86+
new file mode 100644
87+
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391

wpimath/BUILD.bazel

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ load("@rules_python//python:defs.bzl", "py_binary")
77
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")
88
load("//shared/bazel/rules:java_rules.bzl", "wpilib_java_junit5_test")
99
load("//shared/bazel/rules:jni_rules.bzl", "wpilib_jni_cc_library", "wpilib_jni_java_library")
10-
load("//shared/bazel/rules:packaging.bzl", "package_default_jni_project")
10+
load("//shared/bazel/rules:packaging.bzl", "package_default_jni_project", "pkg_files_headers")
1111
load("//wpimath:generate.bzl", "generate_wpimath")
1212

1313
filegroup(
@@ -99,12 +99,17 @@ filegroup(
9999
srcs = glob(["src/generated/main/java/**/*.java"]),
100100
)
101101

102-
third_party_cc_lib_helper(
103-
name = "eigen",
104-
include_root = "src/main/native/thirdparty/eigen/include",
102+
alias(
103+
name = "eigen-headers",
104+
actual = "@eigen",
105105
visibility = ["//visibility:public"],
106106
)
107107

108+
pkg_files_headers(
109+
name = "eigen-hdrs-pkg",
110+
dep = "@eigen",
111+
)
112+
108113
third_party_cc_lib_helper(
109114
name = "gcem",
110115
include_root = "src/main/native/thirdparty/gcem/include",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
2+
load("@rules_license//rules:license.bzl", "license")
3+
4+
package(
5+
default_applicable_licenses = [
6+
":license.APACHE",
7+
":license.BSD",
8+
":license.MINPACK", # Only used by unsupported/** not by Eigen/**.
9+
":license.MPL2",
10+
],
11+
)
12+
13+
exports_files(glob(["COPYING.*"]))
14+
15+
# Note: Eigen is primarily an MPL2 library with small bits of code under other
16+
# licenses. Previous versions of Eigen contained LGPL code which needed to be
17+
# carefully excluded, but as of approximately 2023-02-07 all LGPL code has been
18+
# removed upstream so does not need any special handling here.
19+
20+
license(
21+
name = "license.APACHE",
22+
license_kinds = ["@rules_license//licenses/spdx:Apache-2.0"],
23+
license_text = "COPYING.APACHE",
24+
)
25+
26+
license(
27+
name = "license.BSD",
28+
license_kinds = ["@rules_license//licenses/spdx:BSD-3-Clause"],
29+
license_text = "COPYING.BSD",
30+
)
31+
32+
license(
33+
name = "license.MINPACK",
34+
license_kinds = ["@rules_license//licenses/spdx:BSD-3-Clause-Attribution"],
35+
license_text = "COPYING.MINPACK",
36+
)
37+
38+
license(
39+
name = "license.MPL2",
40+
license_kinds = ["@rules_license//licenses/spdx:MPL-2.0"],
41+
license_text = "COPYING.MPL2",
42+
)
43+
44+
HDRS = glob(
45+
[
46+
"Eigen/**",
47+
"unsupported/Eigen/**",
48+
],
49+
exclude = [
50+
# We don't want any documentation files.
51+
"**/*.md",
52+
"**/*.txt",
53+
"unsupported/Eigen/NonLinearOptimization",
54+
#"unsupported/Eigen/MatrixFunctions",
55+
"unsupported/Eigen/Polynomials",
56+
],
57+
)
58+
59+
cc_library(
60+
name = "eigen",
61+
hdrs = HDRS,
62+
includes = ["."],
63+
visibility = ["//visibility:public"],
64+
)

0 commit comments

Comments
 (0)