summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/build-scripts/rename_symbols.py
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/build-scripts/rename_symbols.py
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/build-scripts/rename_symbols.py')
-rwxr-xr-xsrc/contrib/SDL-3.2.20/build-scripts/rename_symbols.py130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/build-scripts/rename_symbols.py b/src/contrib/SDL-3.2.20/build-scripts/rename_symbols.py
new file mode 100755
index 0000000..33a92dd
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/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
5import argparse
6import os
7import pathlib
8import re
9import sys
10
11
12SDL_ROOT = pathlib.Path(__file__).resolve().parents[1]
13
14SDL_INCLUDE_DIR = SDL_ROOT / "include/SDL3"
15
16
17def 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
48def 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
77def create_regex_from_replacements(replacements):
78 return re.compile(r"\b(%s)\b" % "|".join(map(re.escape, replacements.keys())))
79
80
81def create_substring_regex_from_replacements(replacements):
82 return re.compile(r"(%s)" % "|".join(map(re.escape, replacements.keys())))
83
84
85def 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
99def 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
108def 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
115if __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