diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/build-scripts/rename_symbols.py | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/build-scripts/rename_symbols.py')
| -rwxr-xr-x | contrib/SDL-3.2.8/build-scripts/rename_symbols.py | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/build-scripts/rename_symbols.py b/contrib/SDL-3.2.8/build-scripts/rename_symbols.py new file mode 100755 index 0000000..33a92dd --- /dev/null +++ b/contrib/SDL-3.2.8/build-scripts/rename_symbols.py | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | # | ||
| 3 | # This script renames symbols in the specified paths | ||
| 4 | |||
| 5 | import argparse | ||
| 6 | import os | ||
| 7 | import pathlib | ||
| 8 | import re | ||
| 9 | import sys | ||
| 10 | |||
| 11 | |||
| 12 | SDL_ROOT = pathlib.Path(__file__).resolve().parents[1] | ||
| 13 | |||
| 14 | SDL_INCLUDE_DIR = SDL_ROOT / "include/SDL3" | ||
| 15 | |||
| 16 | |||
| 17 | def main(): | ||
| 18 | if args.all_symbols: | ||
| 19 | if len(args.args) < 1: | ||
| 20 | print("Usage: %s --all-symbols files_or_directories ..." % sys.argv[0]) | ||
| 21 | exit(1) | ||
| 22 | |||
| 23 | replacements = get_all_replacements() | ||
| 24 | entries = args.args | ||
| 25 | |||
| 26 | else: | ||
| 27 | if len(args.args) < 3: | ||
| 28 | print("Usage: %s [--substring] oldname newname files_or_directories ..." % sys.argv[0]) | ||
| 29 | exit(1) | ||
| 30 | |||
| 31 | replacements = { args.args[0]: args.args[1] } | ||
| 32 | entries = args.args[2:] | ||
| 33 | |||
| 34 | if args.substring: | ||
| 35 | regex = create_substring_regex_from_replacements(replacements) | ||
| 36 | else: | ||
| 37 | regex = create_regex_from_replacements(replacements) | ||
| 38 | |||
| 39 | for entry in entries: | ||
| 40 | path = pathlib.Path(entry) | ||
| 41 | if not path.exists(): | ||
| 42 | print("%s doesn't exist, skipping" % entry) | ||
| 43 | continue | ||
| 44 | |||
| 45 | replace_symbols_in_path(path, regex, replacements) | ||
| 46 | |||
| 47 | |||
| 48 | def get_all_replacements(): | ||
| 49 | replacements = {} | ||
| 50 | file = (SDL_INCLUDE_DIR / "SDL_oldnames.h") | ||
| 51 | mode = 0 | ||
| 52 | for line in file.read_text().splitlines(): | ||
| 53 | if line == "#ifdef SDL_ENABLE_OLD_NAMES": | ||
| 54 | if mode == 0: | ||
| 55 | mode = 1 | ||
| 56 | else: | ||
| 57 | raise Exception("get_all_replacements(): expected mode 0") | ||
| 58 | elif line == "#elif !defined(SDL_DISABLE_OLD_NAMES)": | ||
| 59 | if mode == 1: | ||
| 60 | mode = 2 | ||
| 61 | else: | ||
| 62 | raise Exception("get_all_replacements(): expected mode 1") | ||
| 63 | elif line == "#endif /* SDL_ENABLE_OLD_NAMES */": | ||
| 64 | if mode == 2: | ||
| 65 | mode = 3 | ||
| 66 | else: | ||
| 67 | raise Exception("add_symbol_to_oldnames(): expected mode 2") | ||
| 68 | elif mode == 1 and line.startswith("#define "): | ||
| 69 | words = line.split() | ||
| 70 | replacements[words[1]] = words[2] | ||
| 71 | # In case things are accidentally renamed to the "X_renamed_Y" symbol | ||
| 72 | #replacements[words[1] + "_renamed_" + words[2]] = words[2] | ||
| 73 | |||
| 74 | return replacements | ||
| 75 | |||
| 76 | |||
| 77 | def create_regex_from_replacements(replacements): | ||
| 78 | return re.compile(r"\b(%s)\b" % "|".join(map(re.escape, replacements.keys()))) | ||
| 79 | |||
| 80 | |||
| 81 | def create_substring_regex_from_replacements(replacements): | ||
| 82 | return re.compile(r"(%s)" % "|".join(map(re.escape, replacements.keys()))) | ||
| 83 | |||
| 84 | |||
| 85 | def replace_symbols_in_file(file, regex, replacements): | ||
| 86 | try: | ||
| 87 | with file.open("r", encoding="UTF-8", newline="") as rfp: | ||
| 88 | original = rfp.read() | ||
| 89 | contents = regex.sub(lambda mo: replacements[mo.string[mo.start():mo.end()]], original) | ||
| 90 | if contents != original: | ||
| 91 | with file.open("w", encoding="UTF-8", newline="") as wfp: | ||
| 92 | wfp.write(contents) | ||
| 93 | except UnicodeDecodeError: | ||
| 94 | print("%s is not text, skipping" % file) | ||
| 95 | except Exception as err: | ||
| 96 | print("%s" % err) | ||
| 97 | |||
| 98 | |||
| 99 | def replace_symbols_in_dir(path, regex, replacements): | ||
| 100 | for entry in path.glob("*"): | ||
| 101 | if entry.is_dir(): | ||
| 102 | replace_symbols_in_dir(entry, regex, replacements) | ||
| 103 | else: | ||
| 104 | print("Processing %s" % entry) | ||
| 105 | replace_symbols_in_file(entry, regex, replacements) | ||
| 106 | |||
| 107 | |||
| 108 | def replace_symbols_in_path(path, regex, replacements): | ||
| 109 | if path.is_dir(): | ||
| 110 | replace_symbols_in_dir(path, regex, replacements) | ||
| 111 | else: | ||
| 112 | replace_symbols_in_file(path, regex, replacements) | ||
| 113 | |||
| 114 | |||
| 115 | if __name__ == "__main__": | ||
| 116 | |||
| 117 | parser = argparse.ArgumentParser(fromfile_prefix_chars='@') | ||
| 118 | parser.add_argument("--all-symbols", action="store_true") | ||
| 119 | parser.add_argument("--substring", action="store_true") | ||
| 120 | parser.add_argument("args", nargs="*") | ||
| 121 | args = parser.parse_args() | ||
| 122 | |||
| 123 | try: | ||
| 124 | main() | ||
| 125 | except Exception as e: | ||
| 126 | print(e) | ||
| 127 | exit(-1) | ||
| 128 | |||
| 129 | exit(0) | ||
| 130 | |||
