rename_api.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if symbol_type == "function":
  70. file.write("@@\n")
  71. file.write("@@\n")
  72. file.write("- %s\n" % oldname)
  73. file.write("+ %s\n" % newname)
  74. file.write(" (...)\n")
  75. if symbol_type == "symbol":
  76. file.write("@@\n")
  77. file.write("@@\n")
  78. file.write("- %s\n" % oldname)
  79. file.write("+ %s\n" % newname)
  80. # double check ?
  81. if symbol_type == "hint":
  82. file.write("@@\n")
  83. file.write("@@\n")
  84. file.write("- %s\n" % oldname)
  85. file.write("+ %s\n" % newname)
  86. if symbol_type == "enum" or symbol_type == "structure":
  87. file.write("@@\n")
  88. file.write("typedef %s, %s;\n" % (oldname, newname))
  89. file.write("@@\n")
  90. file.write("- %s\n" % oldname)
  91. file.write("+ %s\n" % newname)
  92. file.close()
  93. def add_symbol_to_oldnames(header, oldname, newname):
  94. file = (SDL_INCLUDE_DIR / "SDL_oldnames.h")
  95. lines = file.read_text().splitlines()
  96. mode = 0
  97. i = 0
  98. while i < len(lines):
  99. line = lines[i]
  100. if line == "#ifdef SDL_ENABLE_OLD_NAMES":
  101. if mode == 0:
  102. mode = 1
  103. section = ("/* ##%s */" % header)
  104. section_added = False
  105. content = ("#define %s %s" % (oldname, newname))
  106. content_added = False
  107. else:
  108. raise Exception("add_symbol_to_oldnames(): expected mode 0")
  109. elif line == "#elif !defined(SDL_DISABLE_OLD_NAMES)":
  110. if mode == 1:
  111. if not section_added:
  112. i = add_line(lines, i, section)
  113. if not content_added:
  114. i = add_content(lines, i, content, True)
  115. mode = 2
  116. section = ("/* ##%s */" % header)
  117. section_added = False
  118. content = ("#define %s %s_renamed_%s" % (oldname, oldname, newname))
  119. content_added = False
  120. else:
  121. raise Exception("add_symbol_to_oldnames(): expected mode 1")
  122. elif line == "#endif /* SDL_ENABLE_OLD_NAMES */":
  123. if mode == 2:
  124. if not section_added:
  125. i = add_line(lines, i, section)
  126. if not content_added:
  127. i = add_content(lines, i, content, True)
  128. mode = 3
  129. else:
  130. raise Exception("add_symbol_to_oldnames(): expected mode 2")
  131. elif line != "" and (mode == 1 or mode == 2):
  132. if line.startswith("/* ##"):
  133. if section_added:
  134. if not content_added:
  135. i = add_content(lines, i, content, True)
  136. content_added = True
  137. elif line == section:
  138. section_added = True
  139. elif section < line:
  140. i = add_line(lines, i, section)
  141. section_added = True
  142. i = add_content(lines, i, content, True)
  143. content_added = True
  144. elif line != "" and section_added and not content_added:
  145. if content == line:
  146. content_added = True
  147. elif content < line:
  148. i = add_content(lines, i, content, False)
  149. content_added = True
  150. i += 1
  151. file.write_text("\n".join(lines) + "\n")
  152. def add_symbol_to_migration(header, symbol_type, oldname, newname):
  153. file = (SDL_ROOT / "docs/README-migration.md")
  154. lines = file.read_text().splitlines()
  155. section = ("## %s" % header)
  156. section_added = False
  157. note = ("The following %ss have been renamed:" % symbol_type)
  158. note_added = False
  159. if symbol_type == "function":
  160. content = ("* %s() => %s()" % (oldname, newname))
  161. else:
  162. content = ("* %s => %s" % (oldname, newname))
  163. content_added = False
  164. mode = 0
  165. i = 0
  166. while i < len(lines):
  167. line = lines[i]
  168. if line.startswith("##") and line.endswith(".h"):
  169. if line == section:
  170. section_added = True
  171. elif section < line:
  172. break
  173. elif section_added and not note_added:
  174. if note == line:
  175. note_added = True
  176. elif note_added and not content_added:
  177. if content == line:
  178. content_added = True
  179. elif line == "" or content < line:
  180. i = add_line(lines, i, content)
  181. content_added = True
  182. i += 1
  183. if not section_added:
  184. i = add_line(lines, i, section)
  185. i = add_line(lines, i, "")
  186. if not note_added:
  187. i = add_line(lines, i, note)
  188. if not content_added:
  189. i = add_content(lines, i, content, True)
  190. file.write_text("\n".join(lines) + "\n")
  191. if __name__ == "__main__":
  192. parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
  193. parser.add_argument("--skip-header-check", action="store_true")
  194. parser.add_argument("header");
  195. parser.add_argument("type", choices=["enum", "function", "hint", "structure", "symbol"]);
  196. parser.add_argument("args", nargs="*")
  197. args = parser.parse_args()
  198. try:
  199. main()
  200. except Exception as e:
  201. print(e)
  202. exit(-1)
  203. exit(0)