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_if, |
| 11 | copy_to, |
| 12 | ) |
| 13 | |
| 14 | |
| 15 | def run_source_replacements(memory_files): |
| 16 | for wpi_file in memory_files: |
| 17 | with open(wpi_file) as f: |
| 18 | content = f.read() |
| 19 | |
| 20 | # Fix #includes |
| 21 | content = content.replace('include "', 'include "wpi/memory/') |
| 22 | content = content.replace( |
| 23 | "wpi/memory/free_list_utils.hpp", "free_list_utils.hpp" |
| 24 | ) |
| 25 | |
| 26 | with open(wpi_file, "w") as f: |
| 27 | f.write(content) |
| 28 | |
| 29 | |
| 30 | def run_header_replacements(memory_files): |
| 31 | for wpi_file in memory_files: |
| 32 | if "detail" not in wpi_file: |
| 33 | continue |
| 34 | with open(wpi_file) as f: |
| 35 | content = f.read() |
| 36 | |
| 37 | # Fix #includes |
| 38 | content = content.replace('include "config.hpp', 'include "../config.hpp') |
| 39 | |
| 40 | with open(wpi_file, "w") as f: |
| 41 | f.write(content) |
| 42 | |
| 43 | |
| 44 | def run_global_replacements(memory_files): |
| 45 | for wpi_file in memory_files: |
| 46 | with open(wpi_file) as f: |
| 47 | content = f.read() |
| 48 | |
| 49 | # Rename namespace from foonathan to wpi |
| 50 | content = content.replace("namespace foonathan", "namespace wpi") |
| 51 | content = content.replace("foonathan::", "wpi::") |
| 52 | content = content.replace("FOONATHAN_", "WPI_") |
| 53 | |
| 54 | # Fix #includes |
| 55 | content = content.replace('include "foonathan', 'include "wpi') |
| 56 | |
| 57 | with open(wpi_file, "w") as f: |
| 58 | f.write(content) |
| 59 | |
| 60 | |
| 61 | def main(): |
| 62 | upstream_root = clone_repo("https://github.com/foonathan/memory", "v0.7-2") |
| 63 | wpilib_root = get_repo_root() |
| 64 | wpiutil = os.path.join(wpilib_root, "wpiutil") |
| 65 | |
| 66 | # Delete old install |
| 67 | for d in [ |
| 68 | "src/main/native/thirdparty/memory/src", |
| 69 | "src/main/native/thirdparty/memory/include", |
| 70 | ]: |
| 71 | shutil.rmtree(os.path.join(wpiutil, d), ignore_errors=True) |
| 72 | |
| 73 | # Copy sources |
| 74 | os.chdir(upstream_root) |
| 75 | src_files = walk_if("src", lambda dp, f: f.endswith(".cpp") or f.endswith(".hpp")) |
| 76 | src_files = copy_to( |
| 77 | src_files, os.path.join(wpiutil, "src/main/native/thirdparty/memory") |
| 78 | ) |
| 79 | run_global_replacements(src_files) |
| 80 | run_source_replacements(src_files) |
| 81 | |
| 82 | # Copy headers |
| 83 | os.chdir(os.path.join(upstream_root, "include", "foonathan")) |
| 84 | include_files = walk_if(".", lambda dp, f: f.endswith(".hpp")) |
| 85 | include_files = copy_to( |
| 86 | include_files, |
| 87 | os.path.join(wpiutil, "src/main/native/thirdparty/memory/include/wpi"), |
| 88 | ) |
| 89 | os.chdir(os.path.join("..", "..")) |
| 90 | run_global_replacements(include_files) |
| 91 | run_header_replacements(include_files) |
| 92 | |
| 93 | # Copy config_impl.hpp |
| 94 | shutil.copyfile( |
| 95 | os.path.join(wpilib_root, "upstream_utils/memory_files/config_impl.hpp"), |
| 96 | os.path.join( |
| 97 | wpiutil, |
| 98 | "src/main/native/thirdparty/memory/include/wpi/memory/config_impl.hpp", |
| 99 | ), |
| 100 | ) |
| 101 | |
| 102 | |
| 103 | if __name__ == "__main__": |
| 104 | main() |