From 35ef16b81ce75f930ffc722669fd49610942d828 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Tue, 30 Jan 2024 22:01:46 +0100 Subject: [PATCH] Add a numpy dependency with pkg-config and configtool methods These are being added for NumPy 2.0 The implementation closely follows the one for the pybind11 dependency. --- docs/markdown/Dependencies.md | 8 ++++++++ .../snippets/numpy-custom-dependency.md | 9 +++++++++ mesonbuild/dependencies/__init__.py | 1 + mesonbuild/dependencies/python.py | 17 +++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 docs/markdown/snippets/numpy-custom-dependency.md diff --git a/docs/markdown/Dependencies.md b/docs/markdown/Dependencies.md index d412a2526bcb..965a3dde91a4 100644 --- a/docs/markdown/Dependencies.md +++ b/docs/markdown/Dependencies.md @@ -653,6 +653,14 @@ The `language` keyword may used. `method` may be `auto`, `pkg-config`, `system` or `cmake`. +## NumPy + +*(added 1.4.0)* + +`method` may be `auto`, `pkg-config`, or `config-tool`. +`dependency('numpy')` supports regular use of the NumPy C API. +Use of `numpy.f2py` for binding Fortran code isn't yet supported. + ## pcap *(added 0.42.0)* diff --git a/docs/markdown/snippets/numpy-custom-dependency.md b/docs/markdown/snippets/numpy-custom-dependency.md new file mode 100644 index 000000000000..5934180c001b --- /dev/null +++ b/docs/markdown/snippets/numpy-custom-dependency.md @@ -0,0 +1,9 @@ +## New numpy custom dependency + +Support for `dependency('numpy')` was added, via supporting the `numpy-config` tool and +pkg-config support, both of which are available since NumPy 2.0.0. + +Config-tool support is useful because it will work out of the box when +``numpy`` is installed, while the pkg-config file is located inside python's +site-packages, which makes it impossible to use in an out of the box manner +without setting `PKG_CONFIG_PATH`. diff --git a/mesonbuild/dependencies/__init__.py b/mesonbuild/dependencies/__init__.py index 0bfca97842d2..abc2e22b2d86 100644 --- a/mesonbuild/dependencies/__init__.py +++ b/mesonbuild/dependencies/__init__.py @@ -228,6 +228,7 @@ def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T. 'appleframeworks': 'platform', # from python: + 'numpy': 'python', 'python3': 'python', 'pybind11': 'python', diff --git a/mesonbuild/dependencies/python.py b/mesonbuild/dependencies/python.py index 342d94e72f4d..6620eff20f16 100644 --- a/mesonbuild/dependencies/python.py +++ b/mesonbuild/dependencies/python.py @@ -61,6 +61,17 @@ def __init__(self, name: str, environment: Environment, kwargs: T.Dict[str, T.An self.compile_args = self.get_config_value(['--includes'], 'compile_args') +class NumPyConfigToolDependency(ConfigToolDependency): + + tools = ['numpy-config'] + + 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(['--cflags'], 'compile_args') + + class BasicPythonExternalProgram(ExternalProgram): def __init__(self, name: str, command: T.Optional[T.List[str]] = None, ext_prog: T.Optional[ExternalProgram] = None): @@ -412,3 +423,9 @@ def set_env(name: str, value: str) -> None: [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.CMAKE], configtool_class=Pybind11ConfigToolDependency, ) + +packages['numpy'] = numpy_factory = DependencyFactory( + 'numpy', + [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL], + configtool_class=NumPyConfigToolDependency, +)