From 75d9e85652ada36137bd74a031ee2e92f918033c Mon Sep 17 00:00:00 2001 From: Andrea Date: Sat, 2 Dec 2023 16:31:37 +0100 Subject: [PATCH] BUG: Add external library handling for meson [f2py] --- .../f2py/buildtools/distutils-to-meson.rst | 21 +++++++++++++ numpy/f2py/_backends/_meson.py | 30 +++++++++++++++++++ numpy/f2py/_backends/meson.build.template | 5 ++++ 3 files changed, 56 insertions(+) diff --git a/doc/source/f2py/buildtools/distutils-to-meson.rst b/doc/source/f2py/buildtools/distutils-to-meson.rst index 338413928eda..236436c03d33 100644 --- a/doc/source/f2py/buildtools/distutils-to-meson.rst +++ b/doc/source/f2py/buildtools/distutils-to-meson.rst @@ -163,6 +163,27 @@ Here, ``meson`` can actually be used to set dependencies more robustly. of dependencies. They can be `customized further `_ to use CMake or other systems to resolve dependencies. +1.2.5 Libraries +^^^^^^^^^^^^^^^ + +Both ``meson`` and ``distutils`` are capable of linking against libraries. + +.. tab-set:: + + .. tab-item:: Distutils + :sync: distutils + + .. code-block:: bash + + python -m numpy.f2py -c fib.f90 -m fib --backend distutils -lmylib -L/path/to/mylib + + .. tab-item:: Meson + :sync: meson + + .. code-block:: bash + + python -m numpy.f2py -c fib.f90 -m fib --backend meson -lmylib -L/path/to/mylib + 1.3 Customizing builds ~~~~~~~~~~~~~~~~~~~~~~ diff --git a/numpy/f2py/_backends/_meson.py b/numpy/f2py/_backends/_meson.py index 4841bb3d5933..04a3ab0b2031 100644 --- a/numpy/f2py/_backends/_meson.py +++ b/numpy/f2py/_backends/_meson.py @@ -21,6 +21,8 @@ def __init__( modulename: str, sources: list[Path], deps: list[str], + libraries: list[str], + library_dirs: list[Path], object_files: list[Path], linker_args: list[str], c_args: list[str], @@ -32,12 +34,15 @@ def __init__( ) self.sources = sources self.deps = deps + self.libraries = libraries + self.library_dirs = library_dirs self.substitutions = {} self.objects = object_files self.pipeline = [ self.initialize_template, self.sources_substitution, self.deps_substitution, + self.libraries_substitution, ] self.build_type = build_type @@ -67,6 +72,29 @@ def deps_substitution(self) -> None: [f"dependency('{dep}')" for dep in self.deps] ) + def libraries_substitution(self) -> None: + self.substitutions["lib_dir_declarations"] = "\n".join( + [ + f"lib_dir_{i} = declare_dependency(link_args : ['-L{lib_dir}'])" + for i, lib_dir in enumerate(self.library_dirs) + ] + ) + + self.substitutions["lib_declarations"] = "\n".join( + [ + f"{lib} = declare_dependency(link_args : ['-l{lib}'])" + for lib in self.libraries + ] + ) + + indent = " " * 21 + self.substitutions["lib_list"] = f"\n{indent}".join( + [f"{lib}," for lib in self.libraries] + ) + self.substitutions["lib_dir_list"] = f"\n{indent}".join( + [f"lib_dir_{i}," for i in range(len(self.library_dirs))] + ) + def generate_meson_build(self): for node in self.pipeline: node() @@ -111,6 +139,8 @@ def write_meson_build(self, build_dir: Path) -> None: self.modulename, self.sources, self.dependencies, + self.libraries, + self.library_dirs, self.extra_objects, self.flib_flags, self.fc_flags, diff --git a/numpy/f2py/_backends/meson.build.template b/numpy/f2py/_backends/meson.build.template index 99bf42e3cbbc..65e3ecb19c30 100644 --- a/numpy/f2py/_backends/meson.build.template +++ b/numpy/f2py/_backends/meson.build.template @@ -32,6 +32,9 @@ inc_np = include_directories(incdir_numpy, incdir_f2py) # gh-25000 quadmath_dep = fc.find_library('quadmath', required: false) +${lib_declarations} +${lib_dir_declarations} + py.extension_module('${modulename}', [ ${source_list}, @@ -42,5 +45,7 @@ ${source_list}, py_dep, quadmath_dep, ${dep_list} +${lib_list} +${lib_dir_list} ], install : true)