rename_macros.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_WINRT",
  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. }
  49. def __init__(self):
  50. self.re_pp_command = re.compile(r"^[ \t]*#[ \t]*(\w+).*")
  51. self.re_platform_macros = re.compile(r"\W(" + "|".join(self.RENAMED_MACROS.keys()) + r")(?:\W|$)")
  52. self.re_deprecated_macros = re.compile(r"\W(" + "|".join(self.DEPRECATED_MACROS) + r")(?:\W|$)")
  53. def run(self, contents):
  54. def cb(m):
  55. macro = m.group(1)
  56. original = m.group(0)
  57. match_start, _ = m.span(0)
  58. platform_start, platform_end = m.span(1)
  59. new_text = "{0} /* FIXME: use '#ifdef {0}' or 'defined({0})' */".format(self.RENAMED_MACROS[macro])
  60. r = original[:(platform_start-match_start)] + new_text + original[platform_end-match_start:]
  61. return r
  62. contents, _ = self.re_platform_macros.subn(cb, contents)
  63. def cb(m):
  64. macro = m.group(1)
  65. original = m.group(0)
  66. match_start, _ = m.span(0)
  67. platform_start, platform_end = m.span(1)
  68. new_text = "{0} /* FIXME: {0} has been removed in SDL3 */".format(macro)
  69. r = original[:(platform_start-match_start)] + new_text + original[platform_end-match_start:]
  70. return r
  71. contents, _ = self.re_deprecated_macros.subn(cb, contents)
  72. return contents
  73. def apply_checks(paths):
  74. checks = (
  75. PlatformMacrosCheck(),
  76. )
  77. for entry in paths:
  78. path = pathlib.Path(entry)
  79. if not path.exists():
  80. print("{} does not exist, skipping".format(entry))
  81. continue
  82. apply_checks_in_path(path, checks)
  83. def apply_checks_in_file(file, checks):
  84. try:
  85. with file.open("r", encoding="UTF-8", newline="") as rfp:
  86. original = rfp.read()
  87. contents = original
  88. for check in checks:
  89. contents = check.run(contents)
  90. if contents != original:
  91. with file.open("w", encoding="UTF-8", newline="") as wfp:
  92. wfp.write(contents)
  93. except UnicodeDecodeError:
  94. print("%s is not text, skipping" % file)
  95. except Exception as err:
  96. print("%s" % err)
  97. def apply_checks_in_dir(path, checks):
  98. for entry in path.glob("*"):
  99. if entry.is_dir():
  100. apply_checks_in_dir(entry, checks)
  101. else:
  102. print("Processing %s" % entry)
  103. apply_checks_in_file(entry, checks)
  104. def apply_checks_in_path(path, checks):
  105. if path.is_dir():
  106. apply_checks_in_dir(path, checks)
  107. else:
  108. apply_checks_in_file(path, checks)
  109. def main():
  110. parser = argparse.ArgumentParser(fromfile_prefix_chars='@', description="Rename macros for SDL3")
  111. parser.add_argument("args", nargs="*", help="Input source files")
  112. args = parser.parse_args()
  113. try:
  114. apply_checks(args.args)
  115. except Exception as e:
  116. print(e)
  117. return 1
  118. if __name__ == "__main__":
  119. raise SystemExit(main())