|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import errno |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from ._backend import Backend |
| 9 | +from string import Template |
| 10 | + |
| 11 | +import warnings |
| 12 | + |
| 13 | + |
| 14 | +class MesonTemplate: |
| 15 | + """Template meson build file generation class.""" |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + modulename: str, |
| 20 | + sources: list[Path], |
| 21 | + deps: list[str], |
| 22 | + object_files: list[Path], |
| 23 | + linker_args: list[str], |
| 24 | + c_args: list[str], |
| 25 | + build_type: str, |
| 26 | + ): |
| 27 | + self.modulename = modulename |
| 28 | + self.build_template_path = ( |
| 29 | + Path(__file__).parent.absolute() / "meson.build.template" |
| 30 | + ) |
| 31 | + self.sources = sources |
| 32 | + self.deps = deps |
| 33 | + self.substitutions = {} |
| 34 | + self.objects = object_files |
| 35 | + self.pipeline = [ |
| 36 | + self.initialize_template, |
| 37 | + self.sources_substitution, |
| 38 | + self.deps_substitution, |
| 39 | + ] |
| 40 | + self.build_type = build_type |
| 41 | + |
| 42 | + def meson_build_template(self) -> str: |
| 43 | + if not self.build_template_path.is_file(): |
| 44 | + raise FileNotFoundError( |
| 45 | + errno.ENOENT, |
| 46 | + "Meson build template" |
| 47 | + f" {self.build_template_path.absolute()}" |
| 48 | + " does not exist.", |
| 49 | + ) |
| 50 | + return self.build_template_path.read_text() |
| 51 | + |
| 52 | + def initialize_template(self) -> None: |
| 53 | + self.substitutions["modulename"] = self.modulename |
| 54 | + self.substitutions["buildtype"] = self.build_type |
| 55 | + |
| 56 | + def sources_substitution(self) -> None: |
| 57 | + indent = " " * 21 |
| 58 | + self.substitutions["source_list"] = f",\n{indent}".join( |
| 59 | + [f"'{source}'" for source in self.sources] |
| 60 | + ) |
| 61 | + |
| 62 | + def deps_substitution(self) -> None: |
| 63 | + indent = " " * 21 |
| 64 | + self.substitutions["dep_list"] = f",\n{indent}".join( |
| 65 | + [f"dependency('{dep}')" for dep in self.deps] |
| 66 | + ) |
| 67 | + |
| 68 | + def generate_meson_build(self): |
| 69 | + for node in self.pipeline: |
| 70 | + node() |
| 71 | + template = Template(self.meson_build_template()) |
| 72 | + return template.substitute(self.substitutions) |
| 73 | + |
| 74 | + |
| 75 | +class MesonBackend(Backend): |
| 76 | + def __init__(self, *args, **kwargs): |
| 77 | + super().__init__(*args, **kwargs) |
| 78 | + self.dependencies = self.extra_dat.get("dependencies", []) |
| 79 | + self.meson_build_dir = "bbdir" |
| 80 | + self.build_type = ( |
| 81 | + "debug" if any("debug" in flag for flag in self.fc_flags) else "release" |
| 82 | + ) |
| 83 | + |
| 84 | + def _move_exec_to_root(self, build_dir: Path): |
| 85 | + walk_dir = Path(build_dir) / self.meson_build_dir |
| 86 | + path_objects = walk_dir.glob(f"{self.modulename}*.so") |
| 87 | + for path_object in path_objects: |
| 88 | + shutil.move(path_object, Path.cwd()) |
| 89 | + |
| 90 | + def _get_build_command(self): |
| 91 | + return [ |
| 92 | + "meson", |
| 93 | + "setup", |
| 94 | + self.meson_build_dir, |
| 95 | + ] |
| 96 | + |
| 97 | + def write_meson_build(self, build_dir: Path) -> None: |
| 98 | + """Writes the meson build file at specified location""" |
| 99 | + meson_template = MesonTemplate( |
| 100 | + self.modulename, |
| 101 | + self.sources, |
| 102 | + self.dependencies, |
| 103 | + self.extra_objects, |
| 104 | + self.flib_flags, |
| 105 | + self.fc_flags, |
| 106 | + self.build_type, |
| 107 | + ) |
| 108 | + src = meson_template.generate_meson_build() |
| 109 | + Path(build_dir).mkdir(parents=True, exist_ok=True) |
| 110 | + meson_build_file = Path(build_dir) / "meson.build" |
| 111 | + meson_build_file.write_text(src) |
| 112 | + return meson_build_file |
| 113 | + |
| 114 | + def run_meson(self, build_dir: Path): |
| 115 | + completed_process = subprocess.run(self._get_build_command(), cwd=build_dir) |
| 116 | + if completed_process.returncode != 0: |
| 117 | + raise subprocess.CalledProcessError( |
| 118 | + completed_process.returncode, completed_process.args |
| 119 | + ) |
| 120 | + completed_process = subprocess.run( |
| 121 | + ["meson", "compile", "-C", self.meson_build_dir], cwd=build_dir |
| 122 | + ) |
| 123 | + if completed_process.returncode != 0: |
| 124 | + raise subprocess.CalledProcessError( |
| 125 | + completed_process.returncode, completed_process.args |
| 126 | + ) |
| 127 | + |
| 128 | + def compile(self) -> None: |
| 129 | + self.sources = _prepare_sources(self.modulename, self.sources, self.build_dir) |
| 130 | + self.write_meson_build(self.build_dir) |
| 131 | + self.run_meson(self.build_dir) |
| 132 | + self._move_exec_to_root(self.build_dir) |
| 133 | + |
| 134 | + |
| 135 | +def _prepare_sources(mname, sources, bdir): |
| 136 | + extended_sources = sources.copy() |
| 137 | + Path(bdir).mkdir(parents=True, exist_ok=True) |
| 138 | + # Copy sources |
| 139 | + for source in sources: |
| 140 | + shutil.copy(source, bdir) |
| 141 | + generated_sources = [ |
| 142 | + Path(f"{mname}module.c"), |
| 143 | + Path(f"{mname}-f2pywrappers2.f90"), |
| 144 | + Path(f"{mname}-f2pywrappers.f"), |
| 145 | + ] |
| 146 | + bdir = Path(bdir) |
| 147 | + for generated_source in generated_sources: |
| 148 | + if generated_source.exists(): |
| 149 | + shutil.copy(generated_source, bdir / generated_source.name) |
| 150 | + extended_sources.append(generated_source.name) |
| 151 | + generated_source.unlink() |
| 152 | + extended_sources = [ |
| 153 | + Path(source).name |
| 154 | + for source in extended_sources |
| 155 | + if not Path(source).suffix == ".pyf" |
| 156 | + ] |
| 157 | + return extended_sources |
0 commit comments