From bf7265ccb7cfac2a8c1ea681d1cccb4f13b3ee34 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sun, 24 Dec 2023 11:51:19 -0700 Subject: [PATCH] MAINT: Update ``numpy/f2py/_backends`` from main. --- numpy/f2py/_backends/_distutils.py | 2 +- numpy/f2py/_backends/_meson.py | 50 ++++++++++++----------- numpy/f2py/_backends/meson.build.template | 5 ++- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/numpy/f2py/_backends/_distutils.py b/numpy/f2py/_backends/_distutils.py index e548fc543010..e9b22a3921a5 100644 --- a/numpy/f2py/_backends/_distutils.py +++ b/numpy/f2py/_backends/_distutils.py @@ -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, diff --git a/numpy/f2py/_backends/_meson.py b/numpy/f2py/_backends/_meson.py index 71760e18607d..f324e0f595fb 100644 --- a/numpy/f2py/_backends/_meson.py +++ b/numpy/f2py/_backends/_meson.py @@ -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], @@ -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 @@ -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: @@ -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): @@ -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( @@ -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, @@ -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) @@ -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"), diff --git a/numpy/f2py/_backends/meson.build.template b/numpy/f2py/_backends/meson.build.template index 52e34634493a..8e34fdc8d4d6 100644 --- a/numpy/f2py/_backends/meson.build.template +++ b/numpy/f2py/_backends/meson.build.template @@ -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,