rename_api.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env python3
  2. #
  3. # This script renames symbols in the API, updating SDL_oldnames.h and
  4. # adding documentation for the change.
  5. import argparse
  6. import os
  7. import pathlib
  8. import pprint
  9. import re
  10. import sys
  11. from rename_symbols import create_regex_from_replacements, replace_symbols_in_path
  12. SDL_ROOT = pathlib.Path(__file__).resolve().parents[1]
  13. SDL_INCLUDE_DIR = SDL_ROOT / "include/SDL3"
  14. def main():
  15. if len(args.args) == 0 or (len(args.args) % 2) != 0:
  16. print("Usage: %s [-h] [--skip-header-check] header {enum,function,hint,macro,structure,symbol} [old new ...]" % sys.argv[0])
  17. exit(1)
  18. # Check whether we can still modify the ABI
  19. version_header = pathlib.Path( SDL_INCLUDE_DIR / "SDL_version.h" ).read_text()
  20. if not re.search("SDL_MINOR_VERSION\s+[01]\s", version_header):
  21. raise Exception("ABI is frozen, symbols cannot be renamed")
  22. # Find the symbol in the headers
  23. if pathlib.Path(args.header).is_file():
  24. header = pathlib.Path(args.header)
  25. else:
  26. header = pathlib.Path(SDL_INCLUDE_DIR / args.header)
  27. if not header.exists():
  28. raise Exception("Couldn't find header %s" % header)
  29. header_text = header.read_text()
  30. # Replace the symbols in source code
  31. replacements = {}
  32. i = 0
  33. while i < len(args.args):
  34. oldname = args.args[i + 0]
  35. newname = args.args[i + 1]
  36. if not args.skip_header_check and not re.search((r"\b%s\b" % oldname), header_text):
  37. raise Exception("Couldn't find %s in %s" % (oldname, header))
  38. replacements[ oldname ] = newname
  39. replacements[ oldname + "_REAL" ] = newname + "_REAL"
  40. i += 2
  41. regex = create_regex_from_replacements(replacements)
  42. for dir in ["src", "test", "include", "docs", "cmake/test", "Xcode-iOS/Demos"]:
  43. replace_symbols_in_path(SDL_ROOT / dir, regex, replacements)
  44. # Replace the symbols in documentation
  45. i = 0
  46. while i < len(args.args):
  47. oldname = args.args[i + 0]
  48. newname = args.args[i + 1]
  49. add_symbol_to_oldnames(header.name, oldname, newname)
  50. add_symbol_to_migration(header.name, args.type, oldname, newname)
  51. i += 2
  52. def add_line(lines, i, section):
  53. lines.insert(i, section)
  54. i += 1
  55. return i
  56. def add_content(lines, i, content, add_trailing_line):
  57. if lines[i - 1] == "":
  58. lines[i - 1] = content
  59. else:
  60. i = add_line(lines, i, content)
  61. if add_trailing_line:
  62. i = add_line(lines, i, "")
  63. return i
  64. def add_symbol_to_oldnames(header, oldname, newname):
  65. file = (SDL_INCLUDE_DIR / "SDL_oldnames.h")
  66. lines = file.read_text().splitlines()
  67. mode = 0
  68. i = 0
  69. while i < len(lines):
  70. line = lines[i]
  71. if line == "#ifdef SDL_ENABLE_OLD_NAMES":
  72. if mode == 0:
  73. mode = 1
  74. section = ("/* ##%s */" % header)
  75. section_added = False
  76. content = ("#define %s %s" % (oldname, newname))
  77. content_added = False
  78. else:
  79. raise Exception("add_symbol_to_oldnames(): expected mode 0")
  80. elif line == "#else /* !SDL_ENABLE_OLD_NAMES */":
  81. if mode == 1:
  82. if not section_added:
  83. i = add_line(lines, i, section)
  84. if not content_added:
  85. i = add_content(lines, i, content, True)
  86. mode = 2
  87. section = ("/* ##%s */" % header)
  88. section_added = False
  89. content = ("#define %s %s_renamed_%s" % (oldname, oldname, newname))
  90. content_added = False
  91. else:
  92. raise Exception("add_symbol_to_oldnames(): expected mode 1")
  93. elif line == "#endif /* SDL_ENABLE_OLD_NAMES */":
  94. if mode == 2:
  95. if not section_added:
  96. i = add_line(lines, i, section)
  97. if not content_added:
  98. i = add_content(lines, i, content, True)
  99. mode = 3
  100. else:
  101. raise Exception("add_symbol_to_oldnames(): expected mode 2")
  102. elif line != "" and (mode == 1 or mode == 2):
  103. if line.startswith("/* ##"):
  104. if section_added:
  105. if not content_added:
  106. i = add_content(lines, i, content, True)
  107. content_added = True
  108. elif line == section:
  109. section_added = True
  110. elif section < line:
  111. i = add_line(lines, i, section)
  112. section_added = True
  113. i = add_content(lines, i, content, True)
  114. content_added = True
  115. elif line != "" and section_added and not content_added:
  116. if content == line:
  117. content_added = True
  118. elif content < line:
  119. i = add_content(lines, i, content, False)
  120. content_added = True
  121. i += 1
  122. file.write_text("\n".join(lines) + "\n")
  123. def add_symbol_to_migration(header, symbol_type, oldname, newname):
  124. file = (SDL_ROOT / "docs/README-migration.md")
  125. lines = file.read_text().splitlines()
  126. section = ("## %s" % header)
  127. section_added = False
  128. note = ("The following %ss have been renamed:" % symbol_type)
  129. note_added = False
  130. content = ("* %s => %s" % (oldname, newname))
  131. content_added = False
  132. mode = 0
  133. i = 0
  134. while i < len(lines):
  135. line = lines[i]
  136. if line.startswith("##") and line.endswith(".h"):
  137. if line == section:
  138. section_added = True
  139. elif section < line:
  140. break
  141. elif section_added and not note_added:
  142. if note == line:
  143. note_added = True
  144. elif note_added and not content_added:
  145. if content == line:
  146. content_added = True
  147. elif line == "" or content < line:
  148. i = add_line(lines, i, content)
  149. content_added = True
  150. i += 1
  151. if not section_added:
  152. i = add_line(lines, i, section)
  153. i = add_line(lines, i, "")
  154. if not note_added:
  155. i = add_line(lines, i, note)
  156. if not content_added:
  157. i = add_content(lines, i, content, True)
  158. file.write_text("\n".join(lines) + "\n")
  159. if __name__ == "__main__":
  160. parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
  161. parser.add_argument("--skip-header-check", action="store_true")
  162. parser.add_argument("header");
  163. parser.add_argument("type", choices=["enum", "function", "hint", "macro", "structure", "symbol"]);
  164. parser.add_argument("args", nargs="*")
  165. args = parser.parse_args()
  166. try:
  167. main()
  168. except Exception as e:
  169. print(e)
  170. exit(-1)
  171. exit(0)