Skip to content

Commit

Permalink
dependencies: add pybind11 custom factory
Browse files Browse the repository at this point in the history
This works with pkg-config and cmake without any special support. The
custom factory adds further support for config-tool, via
`pybind11-config`. This is useful because the config-tool will work out
of the box when pybind11 is installed, but the pkg-config and cmake
files are shoved into python's site-packages, which is an unfortunate
distribution model and makes it impossible to use in an out of the box
manner.

It's possible to manually set up the PKG_CONFIG_PATH to detect it
anyway, but in case that does not happen, having the config-tool
fallback is extremely useful.
  • Loading branch information
eli-schwartz committed Feb 26, 2023
1 parent b80f845 commit b94d7fd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/markdown/Dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,12 @@ The `language` keyword may used.

`method` may be `auto`, `config-tool` or `pkg-config`.

## Pybind11

*(added 1.1.0)*

`method` may be `auto`, `pkg-config`, `config-tool`, or `cmake`.

## Python3

Python3 is handled specially by Meson:
Expand Down
9 changes: 9 additions & 0 deletions docs/markdown/snippets/pybind11_dep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## New pybind11 custom dependency

`dependency('pybind11')` works with pkg-config and cmake without any special
support, but did not handle the `pybind11-config` script.

This is useful because the config-tool will work out of the box when pybind11
is installed, but the pkg-config and cmake files are shoved into python's
site-packages, which makes it impossible to use in an out of the box manner.

3 changes: 2 additions & 1 deletion mesonbuild/dependencies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
dl_factory, openssl_factory, libcrypto_factory, libssl_factory,
)
from .platform import AppleFrameworks
from .python import python_factory as python3_factory
from .python import python_factory as python3_factory, pybind11_factory
from .qt import qt4_factory, qt5_factory, qt6_factory
from .ui import GnuStepDependency, WxDependency, gl_factory, sdl2_factory, vulkan_factory

Expand Down Expand Up @@ -270,6 +270,7 @@ def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.

# from python:
'python3': python3_factory,
'pybind11': pybind11_factory,

# From ui:
'gl': gl_factory,
Expand Down
23 changes: 23 additions & 0 deletions mesonbuild/dependencies/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from .. import mesonlib, mlog
from .base import process_method_kw, DependencyMethods, DependencyTypeName, ExternalDependency, SystemDependency
from .configtool import ConfigToolDependency
from .factory import DependencyFactory
from .framework import ExtraFrameworkDependency
from .pkgconfig import PkgConfigDependency
from ..environment import detect_cpu_family
Expand Down Expand Up @@ -49,6 +51,21 @@ class PythonIntrospectionDict(TypedDict):
_Base = object


class Pybind11ConfigToolDependency(ConfigToolDependency):

tools = ['pybind11-config']

# pybind11 in 2.10.4 added --version, sanity-check another flag unique to it
# in the meantime
skip_version = '--pkgconfigdir'

def __init__(self, name: str, environment: Environment, kwargs: T.Dict[str, T.Any]):
super().__init__(name, environment, kwargs)
if not self.is_found:
return
self.compile_args = self.get_config_value(['--includes'], 'compile_args')


class BasicPythonExternalProgram(ExternalProgram):
def __init__(self, name: str, command: T.Optional[T.List[str]] = None,
ext_prog: T.Optional[ExternalProgram] = None):
Expand Down Expand Up @@ -371,3 +388,9 @@ def set_env(name: str, value: str) -> None:
candidates.append(functools.partial(PythonFrameworkDependency, 'Python', env, nkwargs, installation))

return candidates

pybind11_factory = DependencyFactory(
'pybind11',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.CMAKE],
configtool_class=Pybind11ConfigToolDependency,
)

0 comments on commit b94d7fd

Please sign in to comment.