4
0

bridge32_patch_juce.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """LYNX-bridge32.exe (round 176a): JUCE 8 passes lambdas where Win32 wants a __stdcall callback. MSVC converts a lambda to
  2. any calling convention; clang, on 32-bit x86, only to the default (cdecl) one, so those calls don't compile. This makes
  3. copies of the affected modules under <out> with each such lambda turned into a static CALLBACK function. 64-bit builds
  4. are unaffected (one calling convention). Copies are only rewritten when their content changes (object cache).
  5. usage: bridge32_patch_juce.py <juce modules> <patched modules (may not exist)> <out modules>"""
  6. import os, shutil, sys, filecmp
  7. src, patched, out = sys.argv[1:4]
  8. # (module, file inside it, text to find, replacement)
  9. PATCHES = [
  10. ("juce_core", "native/juce_PlatformTimer_windows.cpp",
  11. """ const auto callback = [] (UINT, UINT, DWORD_PTR context, DWORD_PTR, DWORD_PTR)
  12. {
  13. reinterpret_cast<PlatformTimerListener*> (context)->onTimerExpired();
  14. };
  15. """,
  16. """ struct LynxCallback { static void CALLBACK call (UINT, UINT, DWORD_PTR context, DWORD_PTR, DWORD_PTR)
  17. {
  18. reinterpret_cast<PlatformTimerListener*> (context)->onTimerExpired();
  19. } };
  20. const auto callback = &LynxCallback::call;
  21. """),
  22. ("juce_gui_basics", "native/juce_NativeMessageBox_windows.cpp",
  23. "config.pfCallback = [] (HWND hwnd, UINT msg, WPARAM, LPARAM, LONG_PTR lpRefData)\n",
  24. "config.pfCallback = [] (HWND hwnd, UINT msg, WPARAM, LPARAM, LONG_PTR lpRefData) __stdcall\n"),
  25. ]
  26. def base_of(module):
  27. p = os.path.join(patched, module)
  28. return p if os.path.isfile(os.path.join(p, module + ".cpp")) else os.path.join(src, module)
  29. for module in sorted({p[0] for p in PATCHES}):
  30. tmp = os.path.join(out, module + ".new")
  31. shutil.rmtree(tmp, ignore_errors=True)
  32. shutil.copytree(base_of(module), tmp)
  33. for m, rel, old, new in PATCHES:
  34. if m != module:
  35. continue
  36. path = os.path.join(tmp, rel)
  37. text = open(path, encoding="utf-8").read()
  38. if old not in text:
  39. sys.exit("bridge32_patch_juce: pattern not found in %s/%s" % (module, rel))
  40. open(path, "w", encoding="utf-8").write(text.replace(old, new))
  41. dest = os.path.join(out, module)
  42. if os.path.isdir(dest):
  43. cmp = filecmp.dircmp(tmp, dest)
  44. def changed(c):
  45. return c.diff_files or c.left_only or c.right_only or any(changed(s) for s in c.subdirs.values())
  46. if not changed(cmp):
  47. shutil.rmtree(tmp)
  48. continue
  49. shutil.rmtree(dest)
  50. os.rename(tmp, dest)