-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch-runtime.py
More file actions
226 lines (186 loc) · 6.62 KB
/
Copy pathpatch-runtime.py
File metadata and controls
226 lines (186 loc) · 6.62 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# This script takes the RP2040 runtimes generated by build-rts.py and
# patches them to add/change some files in ways that are not supported by
# the bb-runtimes infrastructure. In particular, this script adds or overwrites
# files in the runtime based on template files, such as adding an alire.toml.
import argparse
import pathlib
import re
import shutil
import sys
import tomllib
from typing import Dict
# Project files that need to be "with"ed for light runtimes
project_file_withs_light = """\
with "runtime_build.gpr";\
"""
# Project files that need to be "with"ed for light-tasking/embedded runtimes
project_file_withs_ravenscar = """\
with "runtime_build.gpr";
with "ravenscar_build.gpr";\
"""
# Source directory names for each target
target_src_dirs = {
"rp2040": "rp2040_src",
"rp2350": "rp2350_src",
"nrf52832": "nrf52_src",
"nrf52833": "nrf52_src",
"nrf52840": "nrf52_src",
"nrf54l_app": "nrf54l_src",
"stm32f0xx": "stm32f0_src",
"stm32g0xx": "stm32g0_src",
"stm32g4xx": "stm32g4_src",
}
def patch_target_options(gpr_file: pathlib.Path, profile: str, target: str):
"""Patch the target_options.gpr file to add the crate name as a prefix
to GPR variables.
E.g. for light-tasking-rp2040:
BUILD becomes LIGHT_TASKING_RP2040_BUILD
LIBRARY_TYPE becomes LIGHT_TASKING_RP2040_LIBRARY_TYPE
This follows the Alire best practices and allows the build mode to be
set for just the runtime crate if need be.
"""
# Change "light-tasking" to "light_tasking"
profile = profile.replace("-", "_")
with open(gpr_file, "r") as f:
content = f.read()
content = content.replace(
f'"BUILD"',
f'"{profile.upper()}_{target.upper()}_BUILD"',
)
content = content.replace(
f'"LIBRARY_TYPE"',
f'"{profile.upper()}_{target.upper()}_LIBRARY_TYPE"',
)
with open(gpr_file, "w") as f:
f.write(content)
def gen_from_template(
template_file: pathlib.Path,
out_file: pathlib.Path,
template_values: Dict[str, str],
):
with open(template_file, "r") as f:
content = f.read()
for key, value in template_values.items():
content = content.replace(f"$({key})", value)
# Check for any unrecognised template variables
m = re.search(r"\$\([^\)]+\)", content)
if m:
print(
f"Error: unrecognised template variable '{m.group(0)}'"
f" in file {str(template_file)}",
file=sys.stderr,
)
sys.exit(1)
m = re.search(r"\$\(\w*", content)
if m:
print(
f"Error: malformed template variable '{m.group(0)}'"
f" in file {str(template_file)}",
file=sys.stderr,
)
sys.exit(1)
with open(out_file, "w", newline="\n") as f:
f.write(content)
def gen_templates_from_manifest(
templates_dir: pathlib.Path,
runtime_dir: pathlib.Path,
profile: str,
template_values: Dict[str, str],
):
manifest_file = templates_dir / "manifest.toml"
if manifest_file.exists():
with open(manifest_file, "rb") as f:
manifest = tomllib.load(f)
templates = manifest["template"]
for template_info in templates:
if (
"excluded_profiles" not in template_info
or profile not in template_info["excluded_profiles"]
):
src = template_info["src"]
dst = template_info["dst"]
for k, v in template_values.items():
src = src.replace(f"$({k})", v)
for k, v in template_values.items():
dst = dst.replace(f"$({k})", v)
gen_from_template(
template_file=templates_dir / pathlib.Path(src),
out_file=runtime_dir / pathlib.Path(dst),
template_values=template_values,
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--runtime-dir",
type=str,
required=True,
help="Path to the runtime directory to patch",
)
parser.add_argument(
"--profile",
type=str,
choices=["light", "light-tasking", "embedded"],
required=True,
help="The runtime profile",
)
parser.add_argument(
"--target",
type=str,
choices=target_src_dirs.keys(),
required=True,
help="The name of the target (e.g. rp2040). Must not contain spaces",
)
parser.add_argument(
"--version",
type=str,
default="15.3.0-dev",
help="Version string to put in the alire.toml file",
)
args = parser.parse_args()
runtime_dir = pathlib.Path(args.runtime_dir)
profile = args.profile
pretty_target = args.target.upper()
target = args.target.replace("-", "_")
has_libgnarl = (runtime_dir / "ravenscar_build.gpr").exists()
project_files = ["runtime_build.gpr"]
if has_libgnarl:
project_files.append("ravenscar_build.gpr")
patch_target_options(
gpr_file=runtime_dir / "target_options.gpr", profile=profile, target=target
)
# light and light-tasking require "Ada" and "Asm_Cpp".
# embedded also requires "C".
languages_list = ["Ada", "Asm_Cpp"]
if profile == "embedded":
languages_list.append("C")
template_values = {
"profile": profile,
"profile_underscored": profile.replace("-", "_"),
"target": target,
"pretty_target": pretty_target,
"project_files_list": str(project_files),
"version": args.version,
"languages_list": ", ".join(f'"{lang}"' for lang in languages_list),
"project_file_withs": (
project_file_withs_ravenscar if has_libgnarl else project_file_withs_light
),
"max_cpus": "2" if has_libgnarl else "1",
}
target_templates_dir = (
pathlib.Path(__file__).parent / target_src_dirs[target] / "templates"
)
gen_templates_from_manifest(
templates_dir=target_templates_dir,
runtime_dir=runtime_dir,
profile=profile,
template_values=template_values,
)
ld_dir = pathlib.Path(__file__).parent / target_src_dirs[target] / "ld"
if ld_dir.exists():
shutil.copytree(
src=ld_dir,
dst=runtime_dir / "ld",
dirs_exist_ok=True,
)
if __name__ == "__main__":
main()