_meson.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. from __future__ import annotations
  2. import os
  3. import errno
  4. import shutil
  5. import subprocess
  6. import sys
  7. from pathlib import Path
  8. from ._backend import Backend
  9. from string import Template
  10. from itertools import chain
  11. import warnings
  12. class MesonTemplate:
  13. """Template meson build file generation class."""
  14. def __init__(
  15. self,
  16. modulename: str,
  17. sources: list[Path],
  18. deps: list[str],
  19. libraries: list[str],
  20. library_dirs: list[Path],
  21. include_dirs: list[Path],
  22. object_files: list[Path],
  23. linker_args: list[str],
  24. c_args: list[str],
  25. build_type: str,
  26. python_exe: str,
  27. ):
  28. self.modulename = modulename
  29. self.build_template_path = (
  30. Path(__file__).parent.absolute() / "meson.build.template"
  31. )
  32. self.sources = sources
  33. self.deps = deps
  34. self.libraries = libraries
  35. self.library_dirs = library_dirs
  36. if include_dirs is not None:
  37. self.include_dirs = include_dirs
  38. else:
  39. self.include_dirs = []
  40. self.substitutions = {}
  41. self.objects = object_files
  42. self.pipeline = [
  43. self.initialize_template,
  44. self.sources_substitution,
  45. self.deps_substitution,
  46. self.include_substitution,
  47. self.libraries_substitution,
  48. ]
  49. self.build_type = build_type
  50. self.python_exe = python_exe
  51. def meson_build_template(self) -> str:
  52. if not self.build_template_path.is_file():
  53. raise FileNotFoundError(
  54. errno.ENOENT,
  55. "Meson build template"
  56. f" {self.build_template_path.absolute()}"
  57. " does not exist.",
  58. )
  59. return self.build_template_path.read_text()
  60. def initialize_template(self) -> None:
  61. self.substitutions["modulename"] = self.modulename
  62. self.substitutions["buildtype"] = self.build_type
  63. self.substitutions["python"] = self.python_exe
  64. def sources_substitution(self) -> None:
  65. indent = " " * 21
  66. self.substitutions["source_list"] = f",\n{indent}".join(
  67. [f"{indent}'{source}'" for source in self.sources]
  68. )
  69. def deps_substitution(self) -> None:
  70. indent = " " * 21
  71. self.substitutions["dep_list"] = f",\n{indent}".join(
  72. [f"{indent}dependency('{dep}')" for dep in self.deps]
  73. )
  74. def libraries_substitution(self) -> None:
  75. self.substitutions["lib_dir_declarations"] = "\n".join(
  76. [
  77. f"lib_dir_{i} = declare_dependency(link_args : ['-L{lib_dir}'])"
  78. for i, lib_dir in enumerate(self.library_dirs)
  79. ]
  80. )
  81. self.substitutions["lib_declarations"] = "\n".join(
  82. [
  83. f"{lib} = declare_dependency(link_args : ['-l{lib}'])"
  84. for lib in self.libraries
  85. ]
  86. )
  87. indent = " " * 21
  88. self.substitutions["lib_list"] = f"\n{indent}".join(
  89. [f"{indent}{lib}," for lib in self.libraries]
  90. )
  91. self.substitutions["lib_dir_list"] = f"\n{indent}".join(
  92. [f"{indent}lib_dir_{i}," for i in range(len(self.library_dirs))]
  93. )
  94. def include_substitution(self) -> None:
  95. indent = " " * 21
  96. self.substitutions["inc_list"] = f",\n{indent}".join(
  97. [f"{indent}'{inc}'" for inc in self.include_dirs]
  98. )
  99. def generate_meson_build(self):
  100. for node in self.pipeline:
  101. node()
  102. template = Template(self.meson_build_template())
  103. return template.substitute(self.substitutions)
  104. class MesonBackend(Backend):
  105. def __init__(self, *args, **kwargs):
  106. super().__init__(*args, **kwargs)
  107. self.dependencies = self.extra_dat.get("dependencies", [])
  108. self.meson_build_dir = "bbdir"
  109. self.build_type = (
  110. "debug" if any("debug" in flag for flag in self.fc_flags) else "release"
  111. )
  112. def _move_exec_to_root(self, build_dir: Path):
  113. walk_dir = Path(build_dir) / self.meson_build_dir
  114. path_objects = chain(
  115. walk_dir.glob(f"{self.modulename}*.so"),
  116. walk_dir.glob(f"{self.modulename}*.pyd"),
  117. )
  118. # Same behavior as distutils
  119. # https://github.com/numpy/numpy/issues/24874#issuecomment-1835632293
  120. for path_object in path_objects:
  121. dest_path = Path.cwd() / path_object.name
  122. if dest_path.exists():
  123. dest_path.unlink()
  124. shutil.copy2(path_object, dest_path)
  125. os.remove(path_object)
  126. def write_meson_build(self, build_dir: Path) -> None:
  127. """Writes the meson build file at specified location"""
  128. meson_template = MesonTemplate(
  129. self.modulename,
  130. self.sources,
  131. self.dependencies,
  132. self.libraries,
  133. self.library_dirs,
  134. self.include_dirs,
  135. self.extra_objects,
  136. self.flib_flags,
  137. self.fc_flags,
  138. self.build_type,
  139. sys.executable,
  140. )
  141. src = meson_template.generate_meson_build()
  142. Path(build_dir).mkdir(parents=True, exist_ok=True)
  143. meson_build_file = Path(build_dir) / "meson.build"
  144. meson_build_file.write_text(src)
  145. return meson_build_file
  146. def _run_subprocess_command(self, command, cwd):
  147. subprocess.run(command, cwd=cwd, check=True)
  148. def run_meson(self, build_dir: Path):
  149. setup_command = ["meson", "setup", self.meson_build_dir]
  150. self._run_subprocess_command(setup_command, build_dir)
  151. compile_command = ["meson", "compile", "-C", self.meson_build_dir]
  152. self._run_subprocess_command(compile_command, build_dir)
  153. def compile(self) -> None:
  154. self.sources = _prepare_sources(self.modulename, self.sources, self.build_dir)
  155. self.write_meson_build(self.build_dir)
  156. self.run_meson(self.build_dir)
  157. self._move_exec_to_root(self.build_dir)
  158. def _prepare_sources(mname, sources, bdir):
  159. extended_sources = sources.copy()
  160. Path(bdir).mkdir(parents=True, exist_ok=True)
  161. # Copy sources
  162. for source in sources:
  163. if Path(source).exists() and Path(source).is_file():
  164. shutil.copy(source, bdir)
  165. generated_sources = [
  166. Path(f"{mname}module.c"),
  167. Path(f"{mname}-f2pywrappers2.f90"),
  168. Path(f"{mname}-f2pywrappers.f"),
  169. ]
  170. bdir = Path(bdir)
  171. for generated_source in generated_sources:
  172. if generated_source.exists():
  173. shutil.copy(generated_source, bdir / generated_source.name)
  174. extended_sources.append(generated_source.name)
  175. generated_source.unlink()
  176. extended_sources = [
  177. Path(source).name
  178. for source in extended_sources
  179. if not Path(source).suffix == ".pyf"
  180. ]
  181. return extended_sources