Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Add external library handling for meson [f2py] #25485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2023
Merged
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
21 changes: 21 additions & 0 deletions doc/source/f2py/buildtools/distutils-to-meson.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ Here, ``meson`` can actually be used to set dependencies more robustly.
of dependencies. They can be `customized further <https://mesonbuild.com/Dependencies.html>`_
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
~~~~~~~~~~~~~~~~~~~~~~

Expand Down
30 changes: 30 additions & 0 deletions numpy/f2py/_backends/_meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions numpy/f2py/_backends/meson.build.template
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -42,5 +45,7 @@ ${source_list},
py_dep,
quadmath_dep,
${dep_list}
${lib_list}
${lib_dir_list}
],
install : true)