Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -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')

39 changes: 11 additions & 28 deletions patchmatch/patch_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


import ctypes
import importlib.resources
import json
import logging
import os
Expand Down Expand Up @@ -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"
Copy link
Owner

@mauwii mauwii Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I change line 161 to pypatchmatch_lib = "libpatchmatch.dylib", it seems to be working properly on my Mac

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a check to use than name when platform is darwin.

It's suspicious to me to have to hardcode these extensions here rather than using a more standardized interface that already knows these platform-specific conventions, but I didn't find one of those.

Copy link
Owner

@mauwii mauwii Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was playing around with Windows to see if I can get it working there as well, but I only have access to a VM which runs on ARM, which is why I think I am failing ...

What's nagging me about the dylib: When I build the main branch on my Mac, it is generating a libpatchmatch.so instead. So maybe there is a attribute in meson.build to change that behaviour 🤔

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]
Expand Down
13 changes: 2 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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=[
Expand All @@ -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"=[
Expand Down
Loading