rename_macros.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python3
  2. #
  3. # This script renames SDL macros in the specified paths
  4. import argparse
  5. import pathlib
  6. import re
  7. class PlatformMacrosCheck:
  8. RENAMED_MACROS = {
  9. "__AIX__": "SDL_PLATFORM_AIX",
  10. "__HAIKU__": "SDL_PLATFORM_HAIKU",
  11. "__BSDI__": "SDL_PLATFORM_BSDI",
  12. "__FREEBSD__": "SDL_PLATFORM_FREEBSD",
  13. "__HPUX__": "SDL_PLATFORM_HPUX",
  14. "__IRIX__": "SDL_PLATFORM_IRIX",
  15. "__LINUX__": "SDL_PLATFORM_LINUX",
  16. "__OS2__": "SDL_PLATFORM_OS2",
  17. # "__ANDROID__": "SDL_PLATFORM_ANDROID,
  18. "__NGAGE__": "SDL_PLATFORM_NGAGE",
  19. "__APPLE__": "SDL_PLATFORM_APPLE",
  20. "__TVOS__": "SDL_PLATFORM_TVOS",
  21. "__IPHONEOS__": "SDL_PLATFORM_IOS",
  22. "__MACOSX__": "SDL_PLATFORM_MACOS",
  23. "__NETBSD__": "SDL_PLATFORM_NETBSD",
  24. "__OPENBSD__": "SDL_PLATFORM_OPENBSD",
  25. "__OSF__": "SDL_PLATFORM_OSF",
  26. "__QNXNTO__": "SDL_PLATFORM_QNXNTO",
  27. "__RISCOS__": "SDL_PLATFORM_RISCOS",
  28. "__SOLARIS__": "SDL_PLATFORM_SOLARIS",
  29. "__PSP__": "SDL_PLATFORM_PSP",
  30. "__PS2__": "SDL_PLATFORM_PS2",
  31. "__VITA__": "SDL_PLATFORM_VITA",
  32. "__3DS__": "SDL_PLATFORM_3DS",
  33. # "__unix__": "SDL_PLATFORM_UNIX,
  34. "__WINRT__": "SDL_PLATFORM_WINRT",
  35. "__XBOXSERIES__": "SDL_PLATFORM_XBOXSERIES",
  36. "__XBOXONE__": "SDL_PLATFORM_XBOXONE",
  37. "__WINDOWS__": "SDL_PLATFORM_WINDOWS",
  38. "__WIN32__": "SDL_PLATFORM_WIN32",
  39. # "__CYGWIN_": "SDL_PLATFORM_CYGWIN",
  40. "__WINGDK__": "SDL_PLATFORM_WINGDK",
  41. "__GDK__": "SDL_PLATFORM_GDK",
  42. # "__EMSCRIPTEN__": "SDL_PLATFORM_EMSCRIPTEN",
  43. }
  44. DEPRECATED_MACROS = {
  45. "__DREAMCAST__",
  46. "__NACL__",
  47. "__PNACL__",
  48. "__WINDOWS__",
  49. }
  50. def __init__(self):
  51. self.re_pp_command = re.compile(r"^[ \t]*#[ \t]*(\w+).*")
  52. self.re_platform_macros = re.compile(r"\W(" + "|".join(self.RENAMED_MACROS.keys()) + r")(?:\W|$)")
  53. self.re_deprecated_macros = re.compile(r"\W(" + "|".join(self.DEPRECATED_MACROS) + r")(?:\W|$)")
  54. def run(self, contents):
  55. def cb(m):
  56. macro = m.group(1)
  57. original = m.group(0)
  58. match_start, _ = m.span(0)
  59. platform_start, platform_end = m.span(1)
  60. new_text = self.RENAMED_MACROS[macro]
  61. r = original[:(platform_start-match_start)] + new_text + original[platform_end-match_start:]
  62. return r
  63. contents, _ = self.re_platform_macros.subn(cb, contents)
  64. def cb(m):
  65. macro = m.group(1)
  66. original = m.group(0)
  67. match_start, _ = m.span(0)
  68. platform_start, platform_end = m.span(1)
  69. new_text = "{0} /* FIXME: {0} has been removed in SDL3 */".format(macro)
  70. r = original[:(platform_start-match_start)] + new_text + original[platform_end-match_start:]
  71. return r
  72. contents, _ = self.re_deprecated_macros.subn(cb, contents)
  73. return contents
  74. def apply_checks(paths):
  75. checks = (
  76. PlatformMacrosCheck(),
  77. )
  78. for entry in paths:
  79. path = pathlib.Path(entry)
  80. if not path.exists():
  81. print("{} does not exist, skipping".format(entry))
  82. continue
  83. apply_checks_in_path(path, checks)
  84. def apply_checks_in_file(file, checks):
  85. try:
  86. with file.open("r", encoding="UTF-8", newline="") as rfp:
  87. original = rfp.read()
  88. contents = original
  89. for check in checks:
  90. contents = check.run(contents)
  91. if contents != original:
  92. with file.open("w", encoding="UTF-8", newline="") as wfp:
  93. wfp.write(contents)
  94. except UnicodeDecodeError:
  95. print("%s is not text, skipping" % file)
  96. except Exception as err:
  97. print("%s" % err)
  98. def apply_checks_in_dir(path, checks):
  99. for entry in path.glob("*"):
  100. if entry.is_dir():
  101. apply_checks_in_dir(entry, checks)
  102. else:
  103. print("Processing %s" % entry)
  104. apply_checks_in_file(entry, checks)
  105. def apply_checks_in_path(path, checks):
  106. if path.is_dir():
  107. apply_checks_in_dir(path, checks)
  108. else:
  109. apply_checks_in_file(path, checks)
  110. def main():
  111. parser = argparse.ArgumentParser(fromfile_prefix_chars='@', description="Rename macros for SDL3")
  112. parser.add_argument("args", nargs="*", help="Input source files")
  113. args = parser.parse_args()
  114. try:
  115. apply_checks(args.args)
  116. except Exception as e:
  117. print(e)
  118. return 1
  119. if __name__ == "__main__":
  120. raise SystemExit(main())