blob: ce4c6203dd24aa318efeb2fe507ff2fd792d8d00 [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 "misc",
71 "plugins",
72 ]
James Kuszmaulb13e13f2023-11-22 20:44:04 -080073 return bool(re.search(r"|".join("/" + m for m in modules), abspath))
Austin Schuh812d0d12021-11-04 20:16:48 -070074
75
76def unsupported_inclusions(dp, f):
77 """Returns true if the given file in the "unsupported" include directory of
78 the Eigen git repo should be copied into allwpilib
79
80 Keyword arguments:
81 dp -- directory path
82 f -- filename
83 """
84 if not dp.startswith("./unsupported"):
85 return False
86
87 abspath = os.path.join(dp, f)
88
89 # Exclude build system and READMEs
90 if f == "CMakeLists.txt" or "README" in f:
91 return False
92
James Kuszmaulcf324122023-01-14 14:07:17 -080093 # Include the MatrixFunctions module
94 return "MatrixFunctions" in abspath
Austin Schuh812d0d12021-11-04 20:16:48 -070095
96
97def main():
James Kuszmaulcf324122023-01-14 14:07:17 -080098 upstream_root = clone_repo("https://gitlab.com/libeigen/eigen.git", "3.4.0")
99 wpilib_root = get_repo_root()
100 wpimath = os.path.join(wpilib_root, "wpimath")
101
102 # Apply patches to upstream Git repo
103 os.chdir(upstream_root)
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800104 for f in [
105 "0001-Disable-warnings.patch",
106 "0002-Intellisense-fix.patch",
107 "0003-Eigen-Sparse-fix-warnings-Wunused-but-set-variable.patch",
108 ]:
James Kuszmaulcf324122023-01-14 14:07:17 -0800109 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()