diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..57fb3ff --- /dev/null +++ b/meson.build @@ -0,0 +1,24 @@ +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', + 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..7aece21 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 @@ -155,39 +156,21 @@ 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" - # 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 ba9330d..43e8ff1 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,15 +21,6 @@ requires-python=">=3.9,<3.13" [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"=[