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

Skip to content

MAINT: Update numpy/f2py/_backends from main. #25489

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
2 changes: 1 addition & 1 deletion numpy/f2py/_backends/_distutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class DistutilsBackend(Backend):
def __init__(sef, *args, **kwargs):
warnings.warn(
"distutils has been deprecated since NumPy 1.26."
"distutils has been deprecated since NumPy 1.26.x"
"Use the Meson backend instead, or generate wrappers"
"without -c and use a custom build script",
VisibleDeprecationWarning,
Expand Down
50 changes: 26 additions & 24 deletions numpy/f2py/_backends/_meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(
deps: list[str],
libraries: list[str],
library_dirs: list[Path],
include_dirs: list[Path],
object_files: list[Path],
linker_args: list[str],
c_args: list[str],
Expand All @@ -38,12 +39,17 @@ def __init__(
self.deps = deps
self.libraries = libraries
self.library_dirs = library_dirs
if include_dirs is not None:
self.include_dirs = include_dirs
else:
self.include_dirs = []
self.substitutions = {}
self.objects = object_files
self.pipeline = [
self.initialize_template,
self.sources_substitution,
self.deps_substitution,
self.include_substitution,
self.libraries_substitution,
]
self.build_type = build_type
Expand All @@ -67,13 +73,13 @@ def initialize_template(self) -> None:
def sources_substitution(self) -> None:
indent = " " * 21
self.substitutions["source_list"] = f",\n{indent}".join(
[f"'{source}'" for source in self.sources]
[f"{indent}'{source}'" for source in self.sources]
)

def deps_substitution(self) -> None:
indent = " " * 21
self.substitutions["dep_list"] = f",\n{indent}".join(
[f"dependency('{dep}')" for dep in self.deps]
[f"{indent}dependency('{dep}')" for dep in self.deps]
)

def libraries_substitution(self) -> None:
Expand All @@ -93,10 +99,16 @@ def libraries_substitution(self) -> None:

indent = " " * 21
self.substitutions["lib_list"] = f"\n{indent}".join(
[f"{lib}," for lib in self.libraries]
[f"{indent}{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))]
[f"{indent}lib_dir_{i}," for i in range(len(self.library_dirs))]
)

def include_substitution(self) -> None:
indent = " " * 21
self.substitutions["inc_list"] = f",\n{indent}".join(
[f"{indent}'{inc}'" for inc in self.include_dirs]
)

def generate_meson_build(self):
Expand Down Expand Up @@ -130,13 +142,6 @@ def _move_exec_to_root(self, build_dir: Path):
shutil.copy2(path_object, dest_path)
os.remove(path_object)

def _get_build_command(self):
return [
"meson",
"setup",
self.meson_build_dir,
]

def write_meson_build(self, build_dir: Path) -> None:
"""Writes the meson build file at specified location"""
meson_template = MesonTemplate(
Expand All @@ -145,6 +150,7 @@ def write_meson_build(self, build_dir: Path) -> None:
self.dependencies,
self.libraries,
self.library_dirs,
self.include_dirs,
self.extra_objects,
self.flib_flags,
self.fc_flags,
Expand All @@ -157,19 +163,14 @@ def write_meson_build(self, build_dir: Path) -> None:
meson_build_file.write_text(src)
return meson_build_file

def _run_subprocess_command(self, command, cwd):
subprocess.run(command, cwd=cwd, check=True)

def run_meson(self, build_dir: Path):
completed_process = subprocess.run(self._get_build_command(), cwd=build_dir)
if completed_process.returncode != 0:
raise subprocess.CalledProcessError(
completed_process.returncode, completed_process.args
)
completed_process = subprocess.run(
["meson", "compile", "-C", self.meson_build_dir], cwd=build_dir
)
if completed_process.returncode != 0:
raise subprocess.CalledProcessError(
completed_process.returncode, completed_process.args
)
setup_command = ["meson", "setup", self.meson_build_dir]
self._run_subprocess_command(setup_command, build_dir)
compile_command = ["meson", "compile", "-C", self.meson_build_dir]
self._run_subprocess_command(compile_command, build_dir)

def compile(self) -> None:
self.sources = _prepare_sources(self.modulename, self.sources, self.build_dir)
Expand All @@ -183,7 +184,8 @@ def _prepare_sources(mname, sources, bdir):
Path(bdir).mkdir(parents=True, exist_ok=True)
# Copy sources
for source in sources:
shutil.copy(source, bdir)
if Path(source).exists() and Path(source).is_file():
shutil.copy(source, bdir)
generated_sources = [
Path(f"{mname}module.c"),
Path(f"{mname}-f2pywrappers2.f90"),
Expand Down
5 changes: 4 additions & 1 deletion numpy/f2py/_backends/meson.build.template
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ py.extension_module('${modulename}',
${source_list},
fortranobject_c
],
include_directories: [inc_np],
include_directories: [
inc_np,
${inc_list}
],
dependencies : [
py_dep,
quadmath_dep,
Expand Down