| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import argparse |
| 7 |
import os |
| 8 |
import pathlib |
| 9 |
import pprint |
| 10 |
import re |
| 11 |
import sys |
| 12 |
from rename_symbols import create_regex_from_replacements, replace_symbols_in_path |
| 13 |
|
| 14 |
SDL_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 15 |
|
| 16 |
SDL_INCLUDE_DIR = SDL_ROOT / "include/SDL3_ttf" |
| 17 |
SDL_BUILD_SCRIPTS = SDL_ROOT / "build-scripts" |
| 18 |
|
| 19 |
|
| 20 |
def main(): |
| 21 |
if len(args.args) == 0 or (len(args.args) % 2) != 0: |
| 22 |
print("Usage: %s [-h] [--skip-header-check] {enum,function,hint,structure,symbol} [old new ...]" % sys.argv[0]) |
| 23 |
exit(1) |
| 24 |
|
| 25 |
|
| 26 |
header_text = pathlib.Path( SDL_INCLUDE_DIR / "SDL_ttf.h" ).read_text() |
| 27 |
if not re.search(r"SDL_TTF_MINOR_VERSION\s+[01]\s", header_text): |
| 28 |
raise Exception("ABI is frozen, symbols cannot be renamed") |
| 29 |
|
| 30 |
|
| 31 |
replacements = {} |
| 32 |
i = 0 |
| 33 |
while i < len(args.args): |
| 34 |
oldname = args.args[i + 0] |
| 35 |
newname = args.args[i + 1] |
| 36 |
|
| 37 |
if not args.skip_header_check and not re.search((r"\b%s\b" % oldname), header_text): |
| 38 |
raise Exception("Couldn't find %s in %s" % (oldname, header)) |
| 39 |
|
| 40 |
replacements[ oldname ] = newname |
| 41 |
i += 2 |
| 42 |
|
| 43 |
regex = create_regex_from_replacements(replacements) |
| 44 |
for dir in ["src", "examples", "include", "cmake/test"]: |
| 45 |
replace_symbols_in_path(SDL_ROOT / dir, regex, replacements) |
| 46 |
|
| 47 |
|
| 48 |
i = 0 |
| 49 |
while i < len(args.args): |
| 50 |
oldname = args.args[i + 0] |
| 51 |
newname = args.args[i + 1] |
| 52 |
|
| 53 |
add_symbol_to_migration(args.type, oldname, newname) |
| 54 |
add_symbol_to_coccinelle(args.type, oldname, newname) |
| 55 |
i += 2 |
| 56 |
|
| 57 |
|
| 58 |
def add_line(lines, i, section): |
| 59 |
lines.insert(i, section) |
| 60 |
i += 1 |
| 61 |
return i |
| 62 |
|
| 63 |
|
| 64 |
def add_content(lines, i, content, add_trailing_line): |
| 65 |
if lines[i - 1] == "": |
| 66 |
lines[i - 1] = content |
| 67 |
else: |
| 68 |
i = add_line(lines, i, content) |
| 69 |
|
| 70 |
if add_trailing_line: |
| 71 |
i = add_line(lines, i, "") |
| 72 |
return i |
| 73 |
|
| 74 |
|
| 75 |
def add_symbol_to_coccinelle(symbol_type, oldname, newname): |
| 76 |
file = open(SDL_BUILD_SCRIPTS / "SDL_migration.cocci", "a") |
| 77 |
|
| 78 |
|
| 79 |
if symbol_type == "function": |
| 80 |
file.write("@@\n") |
| 81 |
file.write("@@\n") |
| 82 |
file.write("- %s\n" % oldname) |
| 83 |
file.write("+ %s\n" % newname) |
| 84 |
file.write(" (...)\n") |
| 85 |
|
| 86 |
if symbol_type == "symbol": |
| 87 |
file.write("@@\n") |
| 88 |
file.write("@@\n") |
| 89 |
file.write("- %s\n" % oldname) |
| 90 |
file.write("+ %s\n" % newname) |
| 91 |
|
| 92 |
|
| 93 |
if symbol_type == "hint": |
| 94 |
file.write("@@\n") |
| 95 |
file.write("@@\n") |
| 96 |
file.write("- %s\n" % oldname) |
| 97 |
file.write("+ %s\n" % newname) |
| 98 |
|
| 99 |
if symbol_type == "enum" or symbol_type == "structure": |
| 100 |
file.write("@@\n") |
| 101 |
file.write("typedef %s, %s;\n" % (oldname, newname)) |
| 102 |
file.write("@@\n") |
| 103 |
file.write("- %s\n" % oldname) |
| 104 |
file.write("+ %s\n" % newname) |
| 105 |
|
| 106 |
file.close() |
| 107 |
|
| 108 |
|
| 109 |
def add_symbol_to_migration(symbol_type, oldname, newname): |
| 110 |
file = (SDL_ROOT / "docs/README-migration.md") |
| 111 |
lines = file.read_text().splitlines() |
| 112 |
note = ("The following %ss have been renamed:" % symbol_type) |
| 113 |
note_added = False |
| 114 |
if symbol_type == "function": |
| 115 |
content = ("* %s() => %s()" % (oldname, newname)) |
| 116 |
else: |
| 117 |
content = ("* %s => %s" % (oldname, newname)) |
| 118 |
content_added = False |
| 119 |
mode = 0 |
| 120 |
i = 0 |
| 121 |
while i < len(lines): |
| 122 |
line = lines[i] |
| 123 |
if not note_added: |
| 124 |
if note == line: |
| 125 |
note_added = True |
| 126 |
elif note_added and not content_added: |
| 127 |
if content == line: |
| 128 |
content_added = True |
| 129 |
elif line == "" or content < line: |
| 130 |
i = add_line(lines, i, content) |
| 131 |
content_added = True |
| 132 |
i += 1 |
| 133 |
|
| 134 |
if not note_added: |
| 135 |
i = add_line(lines, i, note) |
| 136 |
|
| 137 |
if not content_added: |
| 138 |
i = add_content(lines, i, content, True) |
| 139 |
|
| 140 |
file.write_text("\n".join(lines) + "\n") |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == "__main__": |
| 144 |
|
| 145 |
parser = argparse.ArgumentParser(fromfile_prefix_chars='@') |
| 146 |
parser.add_argument("--skip-header-check", action="store_true") |
| 147 |
parser.add_argument("type", choices=["enum", "function", "hint", "structure", "symbol"]) |
| 148 |
parser.add_argument("args", nargs="*") |
| 149 |
args = parser.parse_args() |
| 150 |
|
| 151 |
try: |
| 152 |
main() |
| 153 |
except Exception as e: |
| 154 |
print(e) |
| 155 |
exit(-1) |
| 156 |
|
| 157 |
exit(0) |
| 158 |
|