summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/build-scripts/rename_headers.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/build-scripts/rename_headers.py')
-rwxr-xr-xsrc/contrib/SDL-3.2.20/build-scripts/rename_headers.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/build-scripts/rename_headers.py b/src/contrib/SDL-3.2.20/build-scripts/rename_headers.py
new file mode 100755
index 0000000..b61e477
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/build-scripts/rename_headers.py
@@ -0,0 +1,75 @@
1#!/usr/bin/env python3
2#
3# This script renames SDL headers in the specified paths
4
5import argparse
6import pathlib
7import re
8
9
10def do_include_replacements(paths):
11 replacements = [
12 ( re.compile(r"(?:[\"<])(?:SDL2/)?SDL_image.h(?:[\">])"), r"<SDL3_image/SDL_image.h>" ),
13 ( re.compile(r"(?:[\"<])(?:SDL2/)?SDL_mixer.h(?:[\">])"), r"<SDL3_mixer/SDL_mixer.h>" ),
14 ( re.compile(r"(?:[\"<])(?:SDL2/)?SDL_net.h(?:[\">])"), r"<SDL3_net/SDL_net.h>" ),
15 ( re.compile(r"(?:[\"<])(?:SDL2/)?SDL_rtf.h(?:[\">])"), r"<SDL3_rtf/SDL_rtf.h>" ),
16 ( re.compile(r"(?:[\"<])(?:SDL2/)?SDL_ttf.h(?:[\">])"), r"<SDL3_ttf/SDL_ttf.h>" ),
17 ( re.compile(r"(?:[\"<])(?:SDL2/)?SDL_gamecontroller.h(?:[\">])"), r"<SDL3/SDL_gamepad.h>" ),
18 ( re.compile(r"(?:[\"<])(?:SDL2/)?begin_code.h(?:[\">])"), r"<SDL3/SDL_begin_code.h>" ),
19 ( re.compile(r"(?:[\"<])(?:SDL2/)?close_code.h(?:[\">])"), r"<SDL3/SDL_close_code.h>" ),
20 ( re.compile(r"(?:[\"<])(?:SDL2/)?(SDL[_a-z0-9]*\.h)(?:[\">])"), r"<SDL3/\1>" )
21 ]
22 for entry in paths:
23 path = pathlib.Path(entry)
24 if not path.exists():
25 print("{} does not exist, skipping".format(entry))
26 continue
27
28 replace_headers_in_path(path, replacements)
29
30
31def replace_headers_in_file(file, replacements):
32 try:
33 with file.open("r", encoding="UTF-8", newline="") as rfp:
34 original = rfp.read()
35 contents = original
36 for regex, replacement in replacements:
37 contents = regex.sub(replacement, contents)
38 if contents != original:
39 with file.open("w", encoding="UTF-8", newline="") as wfp:
40 wfp.write(contents)
41 except UnicodeDecodeError:
42 print("%s is not text, skipping" % file)
43 except Exception as err:
44 print("%s" % err)
45
46
47def replace_headers_in_dir(path, replacements):
48 for entry in path.glob("*"):
49 if entry.is_dir():
50 replace_headers_in_dir(entry, replacements)
51 else:
52 print("Processing %s" % entry)
53 replace_headers_in_file(entry, replacements)
54
55
56def replace_headers_in_path(path, replacements):
57 if path.is_dir():
58 replace_headers_in_dir(path, replacements)
59 else:
60 replace_headers_in_file(path, replacements)
61
62
63def main():
64 parser = argparse.ArgumentParser(fromfile_prefix_chars='@', description="Rename #include's for SDL3.")
65 parser.add_argument("args", metavar="PATH", nargs="*", help="Input source file")
66 args = parser.parse_args()
67
68 try:
69 do_include_replacements(args.args)
70 except Exception as e:
71 print(e)
72 return 1
73
74if __name__ == "__main__":
75 raise SystemExit(main())