James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import os |
| 4 | import shutil |
| 5 | |
| 6 | from upstream_utils import ( |
| 7 | get_repo_root, |
| 8 | clone_repo, |
| 9 | comment_out_invalid_includes, |
| 10 | walk_cwd_and_copy_if, |
| 11 | git_am, |
| 12 | ) |
| 13 | |
| 14 | |
| 15 | def run_global_replacements(wpiutil_llvm_files): |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 16 | for wpi_file in wpiutil_llvm_files: |
| 17 | with open(wpi_file) as f: |
| 18 | content = f.read() |
| 19 | |
| 20 | # Rename namespace from llvm to wpi |
| 21 | content = content.replace("namespace llvm", "namespace wpi") |
| 22 | content = content.replace("llvm::", "wpi::") |
| 23 | |
| 24 | # Fix #includes |
| 25 | content = content.replace('include "llvm/ADT', 'include "wpi') |
| 26 | content = content.replace('include "llvm/Config', 'include "wpi') |
| 27 | content = content.replace('include "llvm/Support', 'include "wpi') |
| 28 | |
| 29 | # Fix uses of span |
| 30 | content = content.replace("span", "std::span") |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 31 | content = content.replace("include <std::span>", "include <span>") |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 32 | if wpi_file.endswith("ConvertUTFWrapper.cpp"): |
| 33 | content = content.replace( |
| 34 | "const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin());", |
| 35 | "const UTF16 *Src = reinterpret_cast<const UTF16 *>(&*SrcBytes.begin());", |
| 36 | ) |
| 37 | content = content.replace( |
| 38 | "const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end());", |
| 39 | "const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(&*SrcBytes.begin() + SrcBytes.size());", |
| 40 | ) |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 41 | content = content.replace( |
| 42 | "const UTF32 *Src = reinterpret_cast<const UTF32 *>(SrcBytes.begin());", |
| 43 | "const UTF32 *Src = reinterpret_cast<const UTF32 *>(&*SrcBytes.begin());", |
| 44 | ) |
| 45 | content = content.replace( |
| 46 | "const UTF32 *SrcEnd = reinterpret_cast<const UTF32 *>(SrcBytes.end());", |
| 47 | "const UTF32 *SrcEnd = reinterpret_cast<const UTF32 *>(&*SrcBytes.begin() + SrcBytes.size());", |
| 48 | ) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 49 | |
| 50 | # Remove unused headers |
| 51 | content = content.replace('#include "llvm-c/ErrorHandling.h"\n', "") |
| 52 | content = content.replace('#include "wpi/Debug.h"\n', "") |
| 53 | content = content.replace('#include "wpi/Error.h"\n', "") |
| 54 | content = content.replace('#include "wpi/Format.h"\n', "") |
| 55 | content = content.replace('#include "wpi/FormatVariadic.h"\n', "") |
| 56 | content = content.replace('#include "wpi/NativeFormatting.h"\n', "") |
| 57 | content = content.replace('#include "wpi/Threading.h"\n', "") |
| 58 | content = content.replace('#include "wpi/DataTypes.h"\n', "") |
| 59 | content = content.replace('#include "wpi/llvm-config.h"\n', "") |
| 60 | content = content.replace('#include "wpi/abi-breaking.h"\n', "") |
| 61 | content = content.replace('#include "wpi/config.h"\n', "") |
| 62 | content = content.replace('#include "wpi/Signals.h"\n', "") |
| 63 | content = content.replace('#include "wpi/Process.h"\n', "") |
| 64 | content = content.replace('#include "wpi/Path.h"\n', "") |
| 65 | content = content.replace('#include "wpi/Program.h"\n', "") |
| 66 | |
| 67 | # Fix include guards |
| 68 | content = content.replace("LLVM_ADT_", "WPIUTIL_WPI_") |
| 69 | content = content.replace("LLVM_SUPPORT_", "WPIUTIL_WPI_") |
| 70 | content = content.replace("LLVM_DEFINED_HAS_FEATURE", "WPI_DEFINED_HAS_FEATURE") |
| 71 | |
| 72 | content = content.replace("const std::string_view &", "std::string_view ") |
| 73 | content = content.replace("sys::fs::openFileForRead", "fs::OpenFileForRead") |
| 74 | content = content.replace("sys::fs::closeFile", "fs::CloseFile") |
| 75 | content = content.replace("sys::fs::", "fs::") |
| 76 | |
| 77 | # Replace wpi/FileSystem.h with wpi/fs.h |
| 78 | content = content.replace('include "wpi/FileSystem.h"', 'include "wpi/fs.h"') |
| 79 | |
| 80 | # Replace llvm_unreachable() with wpi_unreachable() |
| 81 | content = content.replace("llvm_unreachable", "wpi_unreachable") |
| 82 | |
| 83 | content = content.replace("llvm_is_multithreaded()", "1") |
| 84 | |
| 85 | # Revert message in copyright header |
| 86 | content = content.replace("/// Defines the wpi::", "/// Defines the llvm::") |
| 87 | content = content.replace("// end llvm namespace", "// end wpi namespace") |
| 88 | content = content.replace("// end namespace llvm", "// end namespace wpi") |
| 89 | content = content.replace("// End llvm namespace", "// End wpi namespace") |
| 90 | |
| 91 | content = content.replace("fs::openFileForRead", "fs::OpenFileForRead") |
| 92 | |
| 93 | with open(wpi_file, "w") as f: |
| 94 | f.write(content) |
| 95 | |
| 96 | |
| 97 | def flattened_llvm_files(llvm, dirs_to_keep): |
| 98 | file_lookup = {} |
| 99 | |
| 100 | for dir_to_keep in dirs_to_keep: |
| 101 | dir_to_crawl = os.path.join(llvm, dir_to_keep) |
| 102 | for root, _, files in os.walk(dir_to_crawl): |
| 103 | for f in files: |
| 104 | file_lookup[f] = os.path.join(root, f) |
| 105 | |
| 106 | return file_lookup |
| 107 | |
| 108 | |
| 109 | def find_wpiutil_llvm_files(wpiutil_root, subfolder): |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 110 | # These files have substantial changes, not worth managing with the patching process |
| 111 | ignore_list = [ |
| 112 | "StringExtras.h", |
| 113 | "StringExtras.cpp", |
| 114 | "MemoryBuffer.cpp", |
| 115 | "MemoryBuffer.h", |
| 116 | "SmallVectorMemoryBuffer.h", |
| 117 | ] |
| 118 | |
| 119 | wpiutil_files = [] |
| 120 | for root, _, files in os.walk(os.path.join(wpiutil_root, subfolder)): |
| 121 | for f in files: |
| 122 | if f not in ignore_list: |
| 123 | full_file = os.path.join(root, f) |
| 124 | wpiutil_files.append(full_file) |
| 125 | |
| 126 | return wpiutil_files |
| 127 | |
| 128 | |
| 129 | def overwrite_files(wpiutil_files, llvm_files): |
| 130 | # Very sparse rips from LLVM sources. Not worth tyring to make match upstream |
| 131 | unmatched_files_whitelist = ["fs.h", "fs.cpp", "function_ref.h"] |
| 132 | |
| 133 | for wpi_file in wpiutil_files: |
| 134 | wpi_base_name = os.path.basename(wpi_file) |
| 135 | if wpi_base_name in llvm_files: |
| 136 | shutil.copyfile(llvm_files[wpi_base_name], wpi_file) |
| 137 | |
| 138 | elif wpi_base_name not in unmatched_files_whitelist: |
| 139 | print(f"No file match for {wpi_file}, check if LLVM deleted it") |
| 140 | |
| 141 | |
| 142 | def overwrite_source(wpiutil_root, llvm_root): |
| 143 | llvm_files = flattened_llvm_files( |
| 144 | llvm_root, |
| 145 | [ |
| 146 | "llvm/include/llvm/ADT/", |
| 147 | "llvm/include/llvm/Config", |
| 148 | "llvm/include/llvm/Support/", |
| 149 | "llvm/lib/Support/", |
| 150 | ], |
| 151 | ) |
| 152 | wpi_files = find_wpiutil_llvm_files( |
| 153 | wpiutil_root, "src/main/native/thirdparty/llvm/include/wpi" |
| 154 | ) + find_wpiutil_llvm_files( |
| 155 | wpiutil_root, "src/main/native/thirdparty/llvm/cpp/llvm" |
| 156 | ) |
| 157 | |
| 158 | overwrite_files(wpi_files, llvm_files) |
| 159 | run_global_replacements(wpi_files) |
| 160 | |
| 161 | |
| 162 | def overwrite_tests(wpiutil_root, llvm_root): |
| 163 | llvm_files = flattened_llvm_files( |
| 164 | llvm_root, |
| 165 | ["llvm/unittests/ADT/", "llvm/unittests/Config", "llvm/unittests/Support/"], |
| 166 | ) |
| 167 | wpi_files = find_wpiutil_llvm_files(wpiutil_root, "src/test/native/cpp/llvm") |
| 168 | |
| 169 | overwrite_files(wpi_files, llvm_files) |
| 170 | run_global_replacements(wpi_files) |
| 171 | |
| 172 | |
| 173 | def main(): |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 174 | upstream_root = clone_repo("https://github.com/llvm/llvm-project", "llvmorg-17.0.4") |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 175 | wpilib_root = get_repo_root() |
| 176 | wpiutil = os.path.join(wpilib_root, "wpiutil") |
| 177 | |
| 178 | # Apply patches to upstream Git repo |
| 179 | os.chdir(upstream_root) |
| 180 | for f in [ |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 181 | "0001-Remove-StringRef-ArrayRef-and-Optional.patch", |
| 182 | "0002-Wrap-std-min-max-calls-in-parens-for-Windows-warning.patch", |
| 183 | "0003-Change-unique_function-storage-size.patch", |
| 184 | "0004-Threading-updates.patch", |
| 185 | "0005-ifdef-guard-safety.patch", |
| 186 | "0006-Explicitly-use-std.patch", |
| 187 | "0007-Remove-format_provider.patch", |
| 188 | "0008-Add-compiler-warning-pragmas.patch", |
| 189 | "0009-Remove-unused-functions.patch", |
| 190 | "0010-Detemplatize-SmallVectorBase.patch", |
| 191 | "0011-Add-vectors-to-raw_ostream.patch", |
| 192 | "0012-Extra-collections-features.patch", |
| 193 | "0013-EpochTracker-ABI-macro.patch", |
| 194 | "0014-Delete-numbers-from-MathExtras.patch", |
| 195 | "0015-Add-lerp-and-sgn.patch", |
| 196 | "0016-Fixup-includes.patch", |
| 197 | "0017-Use-std-is_trivially_copy_constructible.patch", |
| 198 | "0018-Windows-support.patch", |
| 199 | "0019-Prefer-fmtlib.patch", |
| 200 | "0020-Prefer-wpi-s-fs.h.patch", |
| 201 | "0021-Remove-unused-functions.patch", |
| 202 | "0022-OS-specific-changes.patch", |
| 203 | "0023-Use-SmallVector-for-UTF-conversion.patch", |
| 204 | "0024-Prefer-to-use-static-pointers-in-raw_ostream.patch", |
| 205 | "0025-constexpr-endian-byte-swap.patch", |
| 206 | "0026-Copy-type-traits-from-STLExtras.h-into-PointerUnion..patch", |
| 207 | "0027-Remove-StringMap-test-for-llvm-sort.patch", |
| 208 | "0028-Unused-variable-in-release-mode.patch", |
| 209 | "0029-Use-C-20-bit-header.patch", |
| 210 | "0030-Remove-DenseMap-GTest-printer-test.patch", |
| 211 | "0031-Replace-deprecated-std-aligned_storage_t.patch", |
| 212 | "0032-Fix-compilation-of-MathExtras.h-on-Windows-with-sdl.patch", |
| 213 | "0033-raw_ostream-Add-SetNumBytesInBuffer.patch", |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 214 | ]: |
| 215 | git_am( |
| 216 | os.path.join(wpilib_root, "upstream_utils/llvm_patches", f), |
| 217 | use_threeway=True, |
| 218 | ) |
| 219 | |
| 220 | overwrite_source(wpiutil, upstream_root) |
| 221 | overwrite_tests(wpiutil, upstream_root) |
| 222 | |
| 223 | |
| 224 | if __name__ == "__main__": |
| 225 | main() |