Skip to content

Commit 08a80a5

Browse files
committed
BUG: Use fortran args from f2py in meson
1 parent 03c86db commit 08a80a5

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

numpy/f2py/_backends/_meson.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(
2828
include_dirs: list[Path],
2929
object_files: list[Path],
3030
linker_args: list[str],
31-
c_args: list[str],
31+
fortran_args: list[str],
3232
build_type: str,
3333
python_exe: str,
3434
):
@@ -46,12 +46,18 @@ def __init__(
4646
self.include_dirs = []
4747
self.substitutions = {}
4848
self.objects = object_files
49+
# Convert args to '' wrapped variant for meson
50+
self.fortran_args = [
51+
f"'{x}'" if not (x.startswith("'") and x.endswith("'")) else x
52+
for x in fortran_args
53+
]
4954
self.pipeline = [
5055
self.initialize_template,
5156
self.sources_substitution,
5257
self.deps_substitution,
5358
self.include_substitution,
5459
self.libraries_substitution,
60+
self.fortran_args_substitution,
5561
]
5662
self.build_type = build_type
5763
self.python_exe = python_exe
@@ -109,6 +115,14 @@ def include_substitution(self) -> None:
109115
[f"{self.indent}'''{inc}'''," for inc in self.include_dirs]
110116
)
111117

118+
def fortran_args_substitution(self) -> None:
119+
if self.fortran_args:
120+
self.substitutions["fortran_args"] = (
121+
f"{self.indent}fortran_args: [{', '.join([arg for arg in self.fortran_args])}],"
122+
)
123+
else:
124+
self.substitutions["fortran_args"] = ""
125+
112126
def generate_meson_build(self):
113127
for node in self.pipeline:
114128
node()
@@ -126,6 +140,7 @@ def __init__(self, *args, **kwargs):
126140
self.build_type = (
127141
"debug" if any("debug" in flag for flag in self.fc_flags) else "release"
128142
)
143+
self.fc_flags = _get_flags(self.fc_flags)
129144

130145
def _move_exec_to_root(self, build_dir: Path):
131146
walk_dir = Path(build_dir) / self.meson_build_dir
@@ -203,3 +218,17 @@ def _prepare_sources(mname, sources, bdir):
203218
if not Path(source).suffix == ".pyf"
204219
]
205220
return extended_sources
221+
222+
223+
def _get_flags(fc_flags):
224+
flag_values = []
225+
flag_pattern = re.compile(r"--f(77|90)flags=(.*)")
226+
227+
for flag in fc_flags:
228+
match_result = flag_pattern.match(flag)
229+
if match_result:
230+
values = match_result.group(2).strip().split()
231+
flag_values.extend(values)
232+
# Hacky way to preserve order of flags
233+
unique_flags = list(dict.fromkeys(flag_values))
234+
return unique_flags

numpy/f2py/_backends/meson.build.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ ${dep_list}
5151
${lib_list}
5252
${lib_dir_list}
5353
],
54+
${fortran_args}
5455
install : true)

0 commit comments

Comments
 (0)