-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject-srcs-into-meson-build.py
More file actions
105 lines (80 loc) · 2.4 KB
/
inject-srcs-into-meson-build.py
File metadata and controls
105 lines (80 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Inject source files into `meson.build`
Easier than doing this by hand and allows us to include this in pre-commit
"""
from __future__ import annotations
import re
import textwrap
from collections.abc import Iterable
from pathlib import Path
def get_src_pattern(meson_variable: str) -> str:
"""
Get the pattern to use to find the source files in `meson.build`
Parameters
----------
meson_variable
Meson variable to set
Returns
-------
:
Pattern to use to grep for the meson variable
"""
return rf"{meson_variable} = files\(\s*('[a-z\/_\.0-9]*',\s*)*\)"
def get_src_substitution(
meson_variable: str, srcs: Iterable[Path], rel_to: Path
) -> str:
"""
Get the value to substitute for the meson variable
Parameters
----------
meson_variable
Meson variable to set
srcs
Sources to use for this meson variable
rel_to
Path the sources should be relative to when setting `meson_variable`
Returns
-------
:
Value to use to set the meson variable
"""
inner = textwrap.indent(
",\n".join(f"'{v.relative_to(rel_to).as_posix()}'" for v in srcs),
prefix=" " * 4,
)
res = f"{meson_variable} = files(\n{inner},\n )"
return res
def main():
"""
Inject sources into `meson.build`
Nicer than typing by hand
"""
REPO_ROOT = Path(__file__).parents[1]
SRC_DIR = REPO_ROOT / "src"
srcs = []
srcs_ancillary_lib = []
for ffile in SRC_DIR.rglob("*.f90"):
if ffile.name.endswith("_wrapper.f90"):
srcs.append(ffile)
else:
srcs_ancillary_lib.append(ffile)
python_srcs = tuple(SRC_DIR.rglob("*.py"))
with open(REPO_ROOT / "meson.build") as fh:
meson_build_in = fh.read().strip()
meson_build_out = meson_build_in
for meson_variable, src_paths in (
("srcs", srcs),
("srcs_ancillary_lib", srcs_ancillary_lib),
("python_srcs", python_srcs),
):
pattern = get_src_pattern(meson_variable)
substitution = get_src_substitution(
meson_variable, sorted(src_paths), REPO_ROOT
)
# TODO: something wrong in here
meson_build_out = re.sub(pattern, substitution, meson_build_out)
with open(REPO_ROOT / "meson.build", "w") as fh:
fh.write(meson_build_out)
fh.write("\n")
if __name__ == "__main__":
main()