From 59467ff063755efa71abc31180729385b0be29b0 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 27 Dec 2022 07:31:27 -0800
Subject: [PATCH] Make it easier to mass rename symbols in the SDL API
---
build-scripts/rename_api.py | 62 +++++++++++++++++++++------------
build-scripts/rename_symbols.py | 26 +++++++++-----
2 files changed, 56 insertions(+), 32 deletions(-)
diff --git a/build-scripts/rename_api.py b/build-scripts/rename_api.py
index 2b6fb895805c..f054384f4963 100755
--- a/build-scripts/rename_api.py
+++ b/build-scripts/rename_api.py
@@ -1,14 +1,14 @@
#!/usr/bin/env python3
-
-# WHAT IS THIS?
-# This script renames symbols in the API, updating SDL_oldnames.h and
-# adding documentation for the change.
+#
+# This script renames symbols in the API, updating SDL_oldnames.h and
+# adding documentation for the change.
import argparse
import os
import pathlib
import pprint
import re
+import sys
from rename_symbols import create_regex_from_replacements, replace_symbols_in_path
SDL_ROOT = pathlib.Path(__file__).resolve().parents[1]
@@ -17,13 +17,15 @@
def main():
+ if len(args.args) == 0 or (len(args.args) % 2) != 0:
+ print("Usage: %s [-h] [--skip-header-check] header {enum,function,macro,structure,symbol} [old new ...]" % sys.argv[0])
+ exit(1)
+
# Check whether we can still modify the ABI
version_header = pathlib.Path( SDL_INCLUDE_DIR / "SDL_version.h" ).read_text()
if not re.search("SDL_MINOR_VERSION\s+[01]\s", version_header):
raise Exception("ABI is frozen, symbols cannot be renamed")
- pattern = re.compile(r"\b%s\b" % args.oldname)
-
# Find the symbol in the headers
if pathlib.Path(args.header).is_file():
header = pathlib.Path(args.header)
@@ -33,21 +35,36 @@ def main():
if not header.exists():
raise Exception("Couldn't find header %s" % header)
- if not args.skip_header_check and not pattern.search(header.read_text()):
- raise Exception("Couldn't find %s in %s" % (args.oldname, header))
+ header_text = header.read_text()
+
+ # Replace the symbols in source code
+ replacements = {}
+ i = 0
+ while i < len(args.args):
+ oldname = args.args[i + 0]
+ newname = args.args[i + 1]
+
+ if not args.skip_header_check and not re.search((r"\b%s\b" % oldname), header_text):
+ raise Exception("Couldn't find %s in %s" % (oldname, header))
+
+ replacements[ oldname ] = newname
+ replacements[ oldname + "_REAL" ] = newname + "_REAL"
+ i += 2
- # Replace the symbol in source code and documentation
- replacements = {
- args.oldname: args.newname,
- args.oldname + "_REAL": args.newname + "_REAL"
- }
regex = create_regex_from_replacements(replacements)
for dir in ["src", "test", "include", "docs", "Xcode-iOS/Demos"]:
replace_symbols_in_path(SDL_ROOT / dir, regex, replacements)
- add_symbol_to_oldnames(header.name, args.oldname, args.newname)
- add_symbol_to_migration(header.name, args.type, args.oldname, args.newname)
- add_symbol_to_whatsnew(args.type, args.oldname, args.newname)
+ # Replace the symbols in documentation
+ i = 0
+ while i < len(args.args):
+ oldname = args.args[i + 0]
+ newname = args.args[i + 1]
+
+ add_symbol_to_oldnames(header.name, oldname, newname)
+ add_symbol_to_migration(header.name, args.type, oldname, newname)
+ add_symbol_to_whatsnew(args.type, oldname, newname)
+ i += 2
def add_line(lines, i, section):
@@ -207,14 +224,13 @@ def add_symbol_to_whatsnew(symbol_type, oldname, newname):
file.write_text("\r\n".join(lines) + "\r\n")
-if __name__ == '__main__':
+if __name__ == "__main__":
- parser = argparse.ArgumentParser()
- parser.add_argument('--skip-header-check', action='store_true')
- parser.add_argument('header');
- parser.add_argument('type', choices=['enum', 'function', 'macro', 'structure']);
- parser.add_argument('oldname');
- parser.add_argument('newname');
+ parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
+ parser.add_argument("--skip-header-check", action="store_true")
+ parser.add_argument("header");
+ parser.add_argument("type", choices=["enum", "function", "macro", "structure", "symbol"]);
+ parser.add_argument("args", nargs="*")
args = parser.parse_args()
try:
diff --git a/build-scripts/rename_symbols.py b/build-scripts/rename_symbols.py
index 90e584407914..2c53d224bc75 100755
--- a/build-scripts/rename_symbols.py
+++ b/build-scripts/rename_symbols.py
@@ -17,7 +17,7 @@
def main():
if args.all_symbols:
if len(args.args) < 1:
- print("Usage: %s --all-symbols files_or_directories ...")
+ print("Usage: %s --all-symbols files_or_directories ..." % sys.argv[0])
exit(1)
replacements = get_all_replacements()
@@ -25,13 +25,16 @@ def main():
else:
if len(args.args) < 3:
- print("Usage: %s oldname newname files_or_directories ...")
+ print("Usage: %s oldname newname files_or_directories ..." % sys.argv[0])
exit(1)
replacements = { args.args[0]: args.args[1] }
entries = args.args[2:]
- regex = create_regex_from_replacements(replacements)
+ if args.substring:
+ regex = create_substring_regex_from_replacements(replacements)
+ else:
+ regex = create_regex_from_replacements(replacements)
for entry in entries:
path = pathlib.Path(entry)
@@ -68,11 +71,15 @@ def create_regex_from_replacements(replacements):
return re.compile(r"\b(%s)\b" % "|".join(map(re.escape, replacements.keys())))
+def create_substring_regex_from_replacements(replacements):
+ return re.compile(r"(%s)" % "|".join(map(re.escape, replacements.keys())))
+
+
def replace_symbols_in_file(file, regex, replacements):
try:
- with file.open('r', encoding='UTF-8', newline='') as rfp:
+ with file.open("r", encoding="UTF-8", newline="") as rfp:
contents = regex.sub(lambda mo: replacements[mo.string[mo.start():mo.end()]], rfp.read())
- with file.open('w', encoding='UTF-8', newline='') as wfp:
+ with file.open("w", encoding="UTF-8", newline="") as wfp:
wfp.write(contents)
except UnicodeDecodeError:
print("%s is not text, skipping" % file)
@@ -94,11 +101,12 @@ def replace_symbols_in_path(path, regex, replacements):
replace_symbols_in_file(path, regex, replacements)
-if __name__ == '__main__':
+if __name__ == "__main__":
- parser = argparse.ArgumentParser()
- parser.add_argument('--all-symbols', action='store_true')
- parser.add_argument('args', nargs='*')
+ parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
+ parser.add_argument("--all-symbols", action="store_true")
+ parser.add_argument("--substring", action="store_true")
+ parser.add_argument("args", nargs="*")
args = parser.parse_args()
try: