Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import os |
| 4 | import re |
| 5 | import shutil |
| 6 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 7 | from 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def 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 Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 47 | 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 53 | |
| 54 | # Include the following modules |
| 55 | modules = [ |
| 56 | "Cholesky", |
| 57 | "Core", |
| 58 | "Eigenvalues", |
| 59 | "Householder", |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 60 | "IterativeLinearSolvers", |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 61 | "Jacobi", |
| 62 | "LU", |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 63 | "OrderingMethods", |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 64 | "QR", |
| 65 | "SVD", |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 66 | "SparseCholesky", |
| 67 | "SparseCore", |
| 68 | "SparseLU", |
| 69 | "SparseQR", |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 70 | "misc", |
| 71 | "plugins", |
| 72 | ] |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 73 | return bool(re.search(r"|".join("/" + m for m in modules), abspath)) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def 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 Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 93 | # Include the MatrixFunctions module |
| 94 | return "MatrixFunctions" in abspath |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 95 | |
| 96 | |
| 97 | def main(): |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 98 | 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 Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 104 | 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 Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 109 | git_am(os.path.join(wpilib_root, "upstream_utils/eigen_patches", f)) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 110 | |
| 111 | # Delete old install |
| 112 | for d in [ |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 113 | "src/main/native/thirdparty/eigen/include/Eigen", |
| 114 | "src/main/native/thirdparty/eigen/include/unsupported", |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 115 | ]: |
| 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 Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 120 | eigen_inclusions, |
| 121 | os.path.join(wpimath, "src/main/native/thirdparty/eigen/include"), |
| 122 | ) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 123 | |
| 124 | # Copy unsupported headers into allwpilib |
| 125 | unsupported_files = walk_cwd_and_copy_if( |
| 126 | unsupported_inclusions, |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 127 | os.path.join(wpimath, "src/main/native/thirdparty/eigen/include"), |
| 128 | ) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 129 | |
| 130 | for f in eigen_files: |
| 131 | comment_out_invalid_includes( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 132 | f, [os.path.join(wpimath, "src/main/native/thirdparty/eigen/include")] |
| 133 | ) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 134 | for f in unsupported_files: |
| 135 | comment_out_invalid_includes( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 136 | f, [os.path.join(wpimath, "src/main/native/thirdparty/eigen/include")] |
| 137 | ) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 138 | |
| 139 | |
| 140 | if __name__ == "__main__": |
| 141 | main() |