From 98170b3422aefbb8f10b255bed8bba36c7fe9637 Mon Sep 17 00:00:00 2001 From: Ankith Date: Mon, 23 Sep 2024 20:09:29 +0530 Subject: [PATCH] Fix editable install on windows --- .gitignore | 1 + buildconfig/_meson_win_dll.py | 14 ++++++++++++++ meson.build | 9 +++++++++ src_py/__init__.py | 30 +++++++++++++++++++++++------- 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 buildconfig/_meson_win_dll.py diff --git a/.gitignore b/.gitignore index caf7b4a21a..67c03d4c6c 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,7 @@ dist *.so __pycache__ _headers/* +buildconfig/win_dll_dirs.json # cython generated files src_c/_sdl2/*.c diff --git a/buildconfig/_meson_win_dll.py b/buildconfig/_meson_win_dll.py new file mode 100644 index 0000000000..ab5e8c8de2 --- /dev/null +++ b/buildconfig/_meson_win_dll.py @@ -0,0 +1,14 @@ +""" +A helper file invoked by the meson buildconfig to write DLL paths to a json file +""" + +import json +import sys + +from pathlib import Path + +dll_parents = {str(Path(i).parent) for i in sys.argv[1:]} +win_dll_dirs_file = Path(__file__).parent / "win_dll_dirs.json" + +if __name__ == "__main__": + win_dll_dirs_file.write_text(json.dumps(list(dll_parents)), encoding="utf-8") diff --git a/meson.build b/meson.build index 63f2c252b9..d002aaf55b 100644 --- a/meson.build +++ b/meson.build @@ -167,6 +167,15 @@ if plat == 'win' and host_machine.cpu_family().startswith('x86') ) endif + run_command( + [ + find_program('python3', 'python'), + base_dir / 'buildconfig' / '_meson_win_dll.py', + dlls, + ], + check: true, + ) + # put dlls in root of install install_data(dlls, install_dir: pg_dir, install_tag: 'pg-tag') else diff --git a/src_py/__init__.py b/src_py/__init__.py index bce9fe3ebb..5f7e540547 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -30,16 +30,32 @@ # Choose Windows display driver if os.name == "nt": pygame_dir = os.path.split(__file__)[0] + dll_parents = {pygame_dir} + try: + # For editable support, add some more folders where DLLs are available. + # This block only executes under an editable install. In a "normal" + # install, the json file will not be installed at the supplied path. + with open( + os.path.join( + os.path.dirname(pygame_dir), "buildconfig", "win_dll_dirs.json" + ), + encoding="utf-8", + ) as f: + import json + + dll_parents.update(json.load(f)) + del json + except (FileNotFoundError, ValueError): + pass - # pypy does not find the dlls, so we add package folder to PATH. - os.environ["PATH"] = os.environ["PATH"] + ";" + pygame_dir - - # Windows store python does not find the dlls, so we run this - if sys.version_info > (3, 8): - os.add_dll_directory(pygame_dir) # only available in 3.8+ + for d in dll_parents: + # adding to PATH is the legacy way, os.add_dll_directory is the new + # and recommended method. For extra safety we do both + os.environ["PATH"] = os.environ["PATH"] + ";" + d + os.add_dll_directory(d) # cleanup namespace - del pygame_dir + del pygame_dir, dll_parents # when running under X11, always set the SDL window WM_CLASS to make the # window managers correctly match the pygame window.