blob: e04689a29215c82a66a4a170c14b8eda839449ef [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001#!/usr/bin/env python3
2
3import os
4import re
5import shutil
6
James Kuszmaulcf324122023-01-14 14:07:17 -08007from upstream_utils import (
8 get_repo_root,
9 clone_repo,
10 comment_out_invalid_includes,
11 walk_cwd_and_copy_if,
12 git_am,
13)
Austin Schuh812d0d12021-11-04 20:16:48 -070014
15
16def eigen_inclusions(dp, f):
17 """Returns true if the given file in the "Eigen" include directory of the
18 Eigen git repo should be copied into allwpilib
19
20 Keyword arguments:
21 dp -- directory path
22 f -- filename
23 """
24 if not dp.startswith("./Eigen"):
25 return False
26
27 abspath = os.path.join(dp, f)
28
29 # Exclude NonMPL2.h since all non-MPL2 code will be excluded anyway
30 if f == "NonMPL2.h":
31 return False
32
33 # Exclude BLAS support
34 if f.endswith("_BLAS.h") or "blas" in f:
35 return False
36
37 # Exclude LAPACK support
38 if f.endswith("_LAPACKE.h") or "lapack" in f:
39 return False
40
41 # Exclude MKL support
42 if "MKL" in f:
43 return False
44
45 # Include architectures we care about
46 if "Core/arch/" in abspath:
James Kuszmaulcf324122023-01-14 14:07:17 -080047 return (
48 "arch/AVX/" in abspath
49 or "arch/Default" in abspath
50 or "arch/NEON" in abspath
51 or "arch/SSE" in abspath
52 )
Austin Schuh812d0d12021-11-04 20:16:48 -070053
54 # Include the following modules
55 modules = [
56 "Cholesky",
57 "Core",
58 "Eigenvalues",
59 "Householder",
James Kuszmaulcf324122023-01-14 14:07:17 -080060 "IterativeLinearSolvers",
Austin Schuh812d0d12021-11-04 20:16:48 -070061 "Jacobi",
62 "LU",
James Kuszmaulcf324122023-01-14 14:07:17 -080063 "OrderingMethods",
Austin Schuh812d0d12021-11-04 20:16:48 -070064 "QR",
65 "SVD",
James Kuszmaulcf324122023-01-14 14:07:17 -080066 "SparseCholesky",
67 "SparseCore",
68 "SparseLU",
69 "SparseQR",
Austin Schuh812d0d12021-11-04 20:16:48 -070070 "StlSupport",
71 "misc",
72 "plugins",
73 ]
74 modules_rgx = r"|".join("/" + m for m in modules)
75
James Kuszmaulcf324122023-01-14 14:07:17 -080076 # "Std" matches StdDeque, StdList, and StdVector headers. Other modules are excluded.
77 return bool(re.search(modules_rgx, abspath) or "Std" in f)
Austin Schuh812d0d12021-11-04 20:16:48 -070078
79
80def unsupported_inclusions(dp, f):
81 """Returns true if the given file in the "unsupported" include directory of
82 the Eigen git repo should be copied into allwpilib
83
84 Keyword arguments:
85 dp -- directory path
86 f -- filename
87 """
88 if not dp.startswith("./unsupported"):
89 return False
90
91 abspath = os.path.join(dp, f)
92
93 # Exclude build system and READMEs
94 if f == "CMakeLists.txt" or "README" in f:
95 return False
96
James Kuszmaulcf324122023-01-14 14:07:17 -080097 # Include the MatrixFunctions module
98 return "MatrixFunctions" in abspath
Austin Schuh812d0d12021-11-04 20:16:48 -070099
100
101def main():
James Kuszmaulcf324122023-01-14 14:07:17 -0800102 upstream_root = clone_repo("https://gitlab.com/libeigen/eigen.git", "3.4.0")
103 wpilib_root = get_repo_root()
104 wpimath = os.path.join(wpilib_root, "wpimath")
105
106 # Apply patches to upstream Git repo
107 os.chdir(upstream_root)
108 for f in ["0001-Disable-warnings.patch"]:
109 git_am(os.path.join(wpilib_root, "upstream_utils/eigen_patches", f))
Austin Schuh812d0d12021-11-04 20:16:48 -0700110
111 # Delete old install
112 for d in [
James Kuszmaulcf324122023-01-14 14:07:17 -0800113 "src/main/native/thirdparty/eigen/include/Eigen",
114 "src/main/native/thirdparty/eigen/include/unsupported",
Austin Schuh812d0d12021-11-04 20:16:48 -0700115 ]:
116 shutil.rmtree(os.path.join(wpimath, d), ignore_errors=True)
117
118 # Copy Eigen headers into allwpilib
119 eigen_files = walk_cwd_and_copy_if(
James Kuszmaulcf324122023-01-14 14:07:17 -0800120 eigen_inclusions,
121 os.path.join(wpimath, "src/main/native/thirdparty/eigen/include"),
122 )
Austin Schuh812d0d12021-11-04 20:16:48 -0700123
124 # Copy unsupported headers into allwpilib
125 unsupported_files = walk_cwd_and_copy_if(
126 unsupported_inclusions,
James Kuszmaulcf324122023-01-14 14:07:17 -0800127 os.path.join(wpimath, "src/main/native/thirdparty/eigen/include"),
128 )
Austin Schuh812d0d12021-11-04 20:16:48 -0700129
130 for f in eigen_files:
131 comment_out_invalid_includes(
James Kuszmaulcf324122023-01-14 14:07:17 -0800132 f, [os.path.join(wpimath, "src/main/native/thirdparty/eigen/include")]
133 )
Austin Schuh812d0d12021-11-04 20:16:48 -0700134 for f in unsupported_files:
135 comment_out_invalid_includes(
James Kuszmaulcf324122023-01-14 14:07:17 -0800136 f, [os.path.join(wpimath, "src/main/native/thirdparty/eigen/include")]
137 )
Austin Schuh812d0d12021-11-04 20:16:48 -0700138
139
140if __name__ == "__main__":
141 main()