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

Skip to content

Commit c84d3d9

Browse files
authored
Merge pull request #25489 from charris/backport-f2py-backends
MAINT: Update ``numpy/f2py/_backends`` from main.
2 parents 8184462 + bf7265c commit c84d3d9

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

numpy/f2py/_backends/_distutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class DistutilsBackend(Backend):
1414
def __init__(sef, *args, **kwargs):
1515
warnings.warn(
16-
"distutils has been deprecated since NumPy 1.26."
16+
"distutils has been deprecated since NumPy 1.26.x"
1717
"Use the Meson backend instead, or generate wrappers"
1818
"without -c and use a custom build script",
1919
VisibleDeprecationWarning,

numpy/f2py/_backends/_meson.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(
2424
deps: list[str],
2525
libraries: list[str],
2626
library_dirs: list[Path],
27+
include_dirs: list[Path],
2728
object_files: list[Path],
2829
linker_args: list[str],
2930
c_args: list[str],
@@ -38,12 +39,17 @@ def __init__(
3839
self.deps = deps
3940
self.libraries = libraries
4041
self.library_dirs = library_dirs
42+
if include_dirs is not None:
43+
self.include_dirs = include_dirs
44+
else:
45+
self.include_dirs = []
4146
self.substitutions = {}
4247
self.objects = object_files
4348
self.pipeline = [
4449
self.initialize_template,
4550
self.sources_substitution,
4651
self.deps_substitution,
52+
self.include_substitution,
4753
self.libraries_substitution,
4854
]
4955
self.build_type = build_type
@@ -67,13 +73,13 @@ def initialize_template(self) -> None:
6773
def sources_substitution(self) -> None:
6874
indent = " " * 21
6975
self.substitutions["source_list"] = f",\n{indent}".join(
70-
[f"'{source}'" for source in self.sources]
76+
[f"{indent}'{source}'" for source in self.sources]
7177
)
7278

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

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

94100
indent = " " * 21
95101
self.substitutions["lib_list"] = f"\n{indent}".join(
96-
[f"{lib}," for lib in self.libraries]
102+
[f"{indent}{lib}," for lib in self.libraries]
97103
)
98104
self.substitutions["lib_dir_list"] = f"\n{indent}".join(
99-
[f"lib_dir_{i}," for i in range(len(self.library_dirs))]
105+
[f"{indent}lib_dir_{i}," for i in range(len(self.library_dirs))]
106+
)
107+
108+
def include_substitution(self) -> None:
109+
indent = " " * 21
110+
self.substitutions["inc_list"] = f",\n{indent}".join(
111+
[f"{indent}'{inc}'" for inc in self.include_dirs]
100112
)
101113

102114
def generate_meson_build(self):
@@ -130,13 +142,6 @@ def _move_exec_to_root(self, build_dir: Path):
130142
shutil.copy2(path_object, dest_path)
131143
os.remove(path_object)
132144

133-
def _get_build_command(self):
134-
return [
135-
"meson",
136-
"setup",
137-
self.meson_build_dir,
138-
]
139-
140145
def write_meson_build(self, build_dir: Path) -> None:
141146
"""Writes the meson build file at specified location"""
142147
meson_template = MesonTemplate(
@@ -145,6 +150,7 @@ def write_meson_build(self, build_dir: Path) -> None:
145150
self.dependencies,
146151
self.libraries,
147152
self.library_dirs,
153+
self.include_dirs,
148154
self.extra_objects,
149155
self.flib_flags,
150156
self.fc_flags,
@@ -157,19 +163,14 @@ def write_meson_build(self, build_dir: Path) -> None:
157163
meson_build_file.write_text(src)
158164
return meson_build_file
159165

166+
def _run_subprocess_command(self, command, cwd):
167+
subprocess.run(command, cwd=cwd, check=True)
168+
160169
def run_meson(self, build_dir: Path):
161-
completed_process = subprocess.run(self._get_build_command(), cwd=build_dir)
162-
if completed_process.returncode != 0:
163-
raise subprocess.CalledProcessError(
164-
completed_process.returncode, completed_process.args
165-
)
166-
completed_process = subprocess.run(
167-
["meson", "compile", "-C", self.meson_build_dir], cwd=build_dir
168-
)
169-
if completed_process.returncode != 0:
170-
raise subprocess.CalledProcessError(
171-
completed_process.returncode, completed_process.args
172-
)
170+
setup_command = ["meson", "setup", self.meson_build_dir]
171+
self._run_subprocess_command(setup_command, build_dir)
172+
compile_command = ["meson", "compile", "-C", self.meson_build_dir]
173+
self._run_subprocess_command(compile_command, build_dir)
173174

174175
def compile(self) -> None:
175176
self.sources = _prepare_sources(self.modulename, self.sources, self.build_dir)
@@ -183,7 +184,8 @@ def _prepare_sources(mname, sources, bdir):
183184
Path(bdir).mkdir(parents=True, exist_ok=True)
184185
# Copy sources
185186
for source in sources:
186-
shutil.copy(source, bdir)
187+
if Path(source).exists() and Path(source).is_file():
188+
shutil.copy(source, bdir)
187189
generated_sources = [
188190
Path(f"{mname}module.c"),
189191
Path(f"{mname}-f2pywrappers2.f90"),

numpy/f2py/_backends/meson.build.template

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ py.extension_module('${modulename}',
4040
${source_list},
4141
fortranobject_c
4242
],
43-
include_directories: [inc_np],
43+
include_directories: [
44+
inc_np,
45+
${inc_list}
46+
],
4447
dependencies : [
4548
py_dep,
4649
quadmath_dep,

0 commit comments

Comments
 (0)