rename_api.py 7.1 KB

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