From 35d9732653ae388c9a83e6ccf76e41a1e2277da1 Mon Sep 17 00:00:00 2001 From: Almar Klein Date: Fri, 10 Nov 2023 23:19:05 +0100 Subject: [PATCH] Make pyinstaller work also without explicit backend imports --- wgpu/__pyinstaller/hook-wgpu.py | 12 ++++++++++++ wgpu/__pyinstaller/test_wgpu.py | 34 ++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/wgpu/__pyinstaller/hook-wgpu.py b/wgpu/__pyinstaller/hook-wgpu.py index b05e7608..1bd99348 100644 --- a/wgpu/__pyinstaller/hook-wgpu.py +++ b/wgpu/__pyinstaller/hook-wgpu.py @@ -1,5 +1,17 @@ from PyInstaller.utils.hooks import collect_data_files, collect_dynamic_libs +# Include our data and binaires datas = collect_data_files("wgpu") binaries = collect_dynamic_libs("wgpu") hiddenimports = ["wgpu.resources"] + +# Include backends that we want to Just Work +hiddenimports += ["wgpu.backends.auto", "wgpu.backends.rs"] + +# Include gui backends that we want to Just Work. +# Note that if someone wants to use Qt, both the qt lib and the +# wgpu.gui.qt must be explicitly imported. +hiddenimports += ["wgpu.gui.offscreen", "wgpu.gui.glfw"] + +# We also need to help glfw, since it does not have a hook like this one. +binaries += collect_dynamic_libs("glfw") diff --git a/wgpu/__pyinstaller/test_wgpu.py b/wgpu/__pyinstaller/test_wgpu.py index b8e3035b..45a2038a 100644 --- a/wgpu/__pyinstaller/test_wgpu.py +++ b/wgpu/__pyinstaller/test_wgpu.py @@ -1,6 +1,30 @@ +script = """ +# The script part +import sys +import wgpu +import importlib + +# The test part +if "is_test" in sys.argv: + included_modules = [ + "wgpu.backends.rs", + "wgpu.gui.offscreen", + "wgpu.gui.glfw", + ] + excluded_modules = [ + "PySide6", + "PyQt6", + ] + for module_name in included_modules: + importlib.import_module(module_name) + for module_name in excluded_modules: + try: + importlib.import_module(module_name) + except ModuleNotFoundError: + continue + raise RuntimeError(module_name + " is not supposed to be importable.") +""" + + def test_pyi_wgpu(pyi_builder): - pyi_builder.test_source( - """ - import wgpu.backends.rs - """ - ) + pyi_builder.test_source(script, app_args=["is_test"])