From 078c5fb755360f48058ef6551775632dab9b42ef Mon Sep 17 00:00:00 2001 From: Kevin Turner <566360-keturn@users.noreply.gitlab.com> Date: Fri, 2 May 2025 20:11:22 -0700 Subject: [PATCH 1/3] build: use meson --- meson.build | 25 +++++++++++++++++++++++++ patchmatch/__init__.py | 2 +- pyproject.toml | 20 ++++++++++---------- 3 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 meson.build diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..afae130 --- /dev/null +++ b/meson.build @@ -0,0 +1,25 @@ +project('patchmatch', 'cpp', + version : '1.0.2', + default_options : ['warning_level=3', 'cpp_std=c++17']) + +opencv_dep = dependency('opencv4', required: true) + +py = import('python').find_installation(pure: false) + +csrc_files = files( + 'patchmatch/csrc/inpaint.cpp', + 'patchmatch/csrc/masked_image.cpp', + 'patchmatch/csrc/nnf.cpp', + 'patchmatch/csrc/pyinterface.cpp' +) + + +libpatchmatch = shared_library('patchmatch', + csrc_files, + install : true, + install_dir: py.get_install_dir() / 'patchmatch', + soversion : '0', + dependencies: [opencv_dep]) + +py.install_sources(['patchmatch/__init__.py', 'patchmatch/patch_match.py'], subdir: 'patchmatch') + diff --git a/patchmatch/__init__.py b/patchmatch/__init__.py index 70725a3..3d23c33 100644 --- a/patchmatch/__init__.py +++ b/patchmatch/__init__.py @@ -1,3 +1,3 @@ __app_id__ = "mauwii/PyPatchMatch" __app_name__ = "PyPatchMatch" -__version__ = "1.0.1" +__version__ = "1.0.2" diff --git a/pyproject.toml b/pyproject.toml index c866783..341850e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] -build-backend="setuptools.build_meta" -requires=["setuptools == 67.1.0", "wheel"] +build-backend = 'mesonpy' +requires = ['meson-python'] [project] authors=[ @@ -21,14 +21,14 @@ requires-python=">=3.9,<3.11" [project.urls] 'Source Code'='https://github.com/mauwii/PyPatchMatch' -[tool.setuptools.dynamic] -version={attr="patchmatch.__version__"} - -[tool.setuptools.packages.find] -include=["patchmatch", "patchmatch.csrc"] - -[tool.setuptools.package-data] -"patchmatch"=['Makefile', 'csrc/*', 'travis.sh'] +# [tool.setuptools.dynamic] +# version={attr="patchmatch.__version__"} +# +# [tool.setuptools.packages.find] +# include=["patchmatch", "patchmatch.csrc"] +# +# [tool.setuptools.package-data] +# "patchmatch"=['Makefile', 'csrc/*', 'travis.sh'] [project.optional-dependencies] "dev"=["black", "flake8", "flake8-black", "isort", "pre-commit", "pylance"] From 9dd8fa66fe158574df528a5e7716b235efe880f0 Mon Sep 17 00:00:00 2001 From: Kevin Turner <566360-keturn@users.noreply.gitlab.com> Date: Sat, 3 May 2025 11:38:43 -0700 Subject: [PATCH 2/3] build: remove runtime compilation fallback build: use importlib to find library so it works in editable mode as supported by meson-python --- meson.build | 1 - patchmatch/patch_match.py | 37 +++++++++---------------------------- pyproject.toml | 9 --------- 3 files changed, 9 insertions(+), 38 deletions(-) diff --git a/meson.build b/meson.build index afae130..57fb3ff 100644 --- a/meson.build +++ b/meson.build @@ -18,7 +18,6 @@ libpatchmatch = shared_library('patchmatch', csrc_files, install : true, install_dir: py.get_install_dir() / 'patchmatch', - soversion : '0', dependencies: [opencv_dep]) py.install_sources(['patchmatch/__init__.py', 'patchmatch/patch_match.py'], subdir: 'patchmatch') diff --git a/patchmatch/patch_match.py b/patchmatch/patch_match.py index 5d799ab..e968368 100644 --- a/patchmatch/patch_match.py +++ b/patchmatch/patch_match.py @@ -13,6 +13,7 @@ import ctypes +import importlib.resources import json import logging import os @@ -156,38 +157,18 @@ def download_url_to_file(url, dst, hash_prefix=None, progress=True): if lib_name.startswith("libpatchmatch_"): pypatchmatch_lib = lib_name - # Compile if we didn't find a platform-compatible version (and it's not compiled already) if pypatchmatch_lib is None: pypatchmatch_lib = "libpatchmatch.so" - if not os.path.exists(osp.join(osp.dirname(__file__), pypatchmatch_lib)): - import subprocess - # Streams make will write to - # TODO: use user-configured log-level to control this - make_stdout = subprocess.DEVNULL - make_stderr = subprocess.DEVNULL - - if os.environ.get("INVOKEAI_DEBUG_PATCHMATCH"): - make_stdout = None - make_stderr = None - - logger.info( - 'Compiling and loading c extensions from "{}".'.format( - osp.realpath(osp.dirname(__file__)) - ) - ) - # subprocess.check_call(['./travis.sh'], cwd=osp.dirname(__file__)) - # TODO: pipe output to logger instead of just swallowing it - subprocess.run( - "make clean && make", - cwd=osp.dirname(__file__), - shell=True, - check=True, - stdout=make_stdout, - stderr=make_stderr, - ) + # use importlib to find the resolve the library location so it works under + # both editable and built installations + patchmatch_filename = importlib.resources.files(__package__).joinpath(pypatchmatch_lib) + if not os.path.exists(patchmatch_filename): + raise RuntimeError(f"library not found at {patchmatch_filename}") + else: + logger.debug(f"library found at {patchmatch_filename}") - PMLIB = ctypes.CDLL(osp.join(osp.dirname(__file__), pypatchmatch_lib)) + PMLIB = ctypes.CDLL(patchmatch_filename) patchmatch_available = True PMLIB.PM_set_random_seed.argtypes = [ctypes.c_uint] diff --git a/pyproject.toml b/pyproject.toml index 341850e..c2898b4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,15 +21,6 @@ requires-python=">=3.9,<3.11" [project.urls] 'Source Code'='https://github.com/mauwii/PyPatchMatch' -# [tool.setuptools.dynamic] -# version={attr="patchmatch.__version__"} -# -# [tool.setuptools.packages.find] -# include=["patchmatch", "patchmatch.csrc"] -# -# [tool.setuptools.package-data] -# "patchmatch"=['Makefile', 'csrc/*', 'travis.sh'] - [project.optional-dependencies] "dev"=["black", "flake8", "flake8-black", "isort", "pre-commit", "pylance"] "dist"=[ From b1bedec7ead2cf84081ec2b97830124434465845 Mon Sep 17 00:00:00 2001 From: Kevin Turner <566360-keturn@users.noreply.gitlab.com> Date: Sat, 7 Jun 2025 10:55:51 -0700 Subject: [PATCH 3/3] fix: lib name is .dylib on macos --- patchmatch/patch_match.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/patchmatch/patch_match.py b/patchmatch/patch_match.py index e968368..7aece21 100644 --- a/patchmatch/patch_match.py +++ b/patchmatch/patch_match.py @@ -156,6 +156,8 @@ def download_url_to_file(url, dst, hash_prefix=None, progress=True): # Store patchmatch library name if lib_name.startswith("libpatchmatch_"): pypatchmatch_lib = lib_name + elif "darwin" in platform_slug: + pypatchmatch_lib = "libpatchmatch.dylib" if pypatchmatch_lib is None: pypatchmatch_lib = "libpatchmatch.so"