rename_api.py 7.2 KB

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