8000 BUG: Handle `--f77flags` and `--f90flags` for `meson` by HaoZeke · Pull Request #26703 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Handle --f77flags and --f90flags for meson #26703

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 4 commits into from
Jun 16, 2024
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
31 changes: 30 additions & 1 deletion numpy/f2py/_backends/_meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(
include_dirs: list[Path],
object_files: list[Path],
linker_args: list[str],
c_args: list[str],
fortran_args: list[str],
build_type: str,
python_exe: str,
):
Expand All @@ -46,12 +46,18 @@ def __init__(
self.include_dirs = []
self.substitutions = {}
self.objects = object_files
# Convert args to '' wrapped variant for meson
self.fortran_args = [
f"'{x}'" if not (x.startswith("'") and x.endswith("'")) else x
for x in fortran_args
]
self.pipeline = [
self.initialize_template,
self.sources_substitution,
self.deps_substitution,
self.include_substitution,
self.libraries_substitution,
self.fortran_args_substitution,
]
self.build_type = build_type
self.python_exe = python_exe
Expand Down Expand Up @@ -109,6 +115,14 @@ def include_substitution(self) -> None:
[f"{self.indent}'''{inc}'''," for inc in self.include_dirs]
)

def fortran_args_substitution(self) -> None:
if self.fortran_args:
self.substitutions["fortran_args"] = (
f"{self.indent}fortran_args: [{', '.join([arg for arg in self.fortran_args])}],"
)
else:
self.substitutions["fortran_args"] = ""

def generate_meson_build(self):
for node in self.pipeline:
node()
Expand All @@ -126,6 +140,7 @@ def __init__(self, *args, **kwargs):
self.build_type = (
"debug" if any("debug" in flag for flag in self.fc_flags) else "release"
)
self.fc_flags = _get_flags(self.fc_flags)

def _move_exec_to_root(self, build_dir: Path):
walk_dir = Path(build_dir) / self.meson_build_dir
Expand Down Expand Up @@ -203,3 +218,17 @@ def _prepare_sources(mname, sources, bdir):
if not Path(source).suffix == ".pyf"
]
return extended_sources


def _get_flags(fc_flags):
flag_values = []
flag_pattern = re.compile(r"--f(77|90)flags=(.*)")
for flag in fc_flags:
match_result = flag_pattern.match(flag)
if match_result:
values = match_result.group(2).strip().split()
values = [val.strip("'\"") for val in values]
flag_values.extend(values)
# Hacky way to preserve order of flags
unique_flags = list(dict.fromkeys(flag_values))
return unique_flags
1 change: 1 addition & 0 deletions numpy/f2py/_backends/meson.build.template
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ ${dep_list}
${lib_list}
${lib_dir_list}
],
${fortran_args}
install : true)
12 changes: 8 additions & 4 deletions numpy/f2py/f2py2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,14 @@ def run_compile():
r'--((f(90)?compiler(-exec|)|compiler)=|help-compiler)')
flib_flags = [_m for _m in sys.argv[1:] if _reg3.match(_m)]
sys.argv = [_m for _m in sys.argv if _m not in flib_flags]
_reg4 = re.compile(
r'--((f(77|90)(flags|exec)|opt|arch)=|(debug|noopt|noarch|help-fcompiler))')
fc_flags = [_m for _m in sys.argv[1:] if _reg4.match(_m)]
sys.argv = [_m for _m in sys.argv if _m not in fc_flags]
# TODO: Once distutils is dropped completely, i.e. min_ver >= 3.12, unify into --fflags
reg_f77_f90_flags = re.compile(r'--f(77|90)flags=')
reg_distutils_flags = re.compile(r'--((f(77|90)exec|opt|arch)=|(debug|noopt|noarch|help-fcompiler))')
fc_flags = [_m for _m in sys.argv[1:] if reg_f77_f90_flags.match(_m)]
distutils_flags = [_m for _m in sys.argv[1:] if reg_distutils_flags.match(_m)]
if not (MESON_ONLY_VER or backend_key == 'meson'):
fc_flags.extend(distutils_flags)
sys.argv = [_m for _m in sys.argv if _m not in (fc_flags + distutils_flags)]

del_list = []
for s in flib_flags:
Expand Down
5 changes: 5 additions & 0 deletions numpy/f2py/tests/src/regression/f77fixedform.f95
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
C This is an invalid file, but it does compile with -ffixed-form
subroutine mwe(
& x)
real x
end subroutine mwe
18 changes: 18 additions & 0 deletions numpy/f2py/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def test_gh26148b(self):
assert(res[0] == 8)
assert(res[1] == 15)

@pytest.mark.slow
def test_gh26623():
# Including libraries with . should not generate an incorrect meson.build
try:
Expand All @@ -119,3 +120,20 @@ def test_gh26623():
)
except RuntimeError as rerr:
assert "lparen got assign" not in str(rerr)


@pytest.mark.slow
def test_gh25784():
# Compile dubious file using passed flags
try:
aa = util.build_module(
[util.getpath("tests", "src", "regression", "f77fixedform.f95")],
options=[
# Meson will collect and dedup these to pass to fortran_args:
"--f77flags='-ffixed-form -O2'",
"--f90flags=\"-ffixed-form -Og\"",
],
module_name="Blah",
)
except ImportError as rerr:
assert "unknown_subroutine_" in str(rerr)
2 changes: 1 addition & 1 deletion numpy/f2py/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def build_module(source_files, options=[], skip=[], only=[], module_name=None):
dst_sources.append(dst)

base, ext = os.path.splitext(dst)
if ext in (".f90", ".f", ".c", ".pyf"):
if ext in (".f90", ".f95", ".f", ".c", ".pyf"):
f2py_sources.append(dst)

assert f2py_sources
Expand Down
Loading
31C0
0