-
Notifications
You must be signed in to change notification settings - Fork 0
/
forge.py
executable file
·303 lines (256 loc) · 8.11 KB
/
forge.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/python3
import argparse
import os
import stat
import shutil
import re
import ninja
import colors
import tables
from peripherals import parse_cube_file, Clk
parser = argparse.ArgumentParser(
prog="forge",
description="Generates build files and stuff for SDCC based STM8 projects",
)
parser.add_argument(
"--cube-file",
metavar="cf",
dest="cube_file",
type=str,
default=None,
action="store",
help="Path to STM8CubeMX .txt report file",
)
parser.add_argument(
"--debug",
metavar="d",
const="--debug",
action="store_const",
default="",
help="Build with dbg stuff",
)
parser.add_argument(
"--stdp",
dest="stdp_path",
metavar="stpd path",
action="store",
default=".",
help="Path to the STM8S_StdPeriph_Lib. defaults to `.`",
)
parser.add_argument(
"--src",
dest="src",
metavar="src",
action="store",
default=".",
help="Location of project source files, defaults to `.`",
)
parser.add_argument(
"--no-clk",
dest="no_clk",
action="store_const",
const=True,
default=False,
help="disables inclusion of the CLK peripheral by default",
)
args = parser.parse_args()
ninja_file = "build.ninja"
output_dir = "./build"
lib_to_driver = "STM8S_StdPeriph_Lib/Libraries/STM8S_StdPeriph_Driver/"
class ForgeError(Exception):
pass
def create_openocd_file(model):
if shutil.which("openocd") is None:
colors.warning("No executable for openocd was found. Skipping")
return
conf = None
for c in tables.openocd_configs:
if model.startswith(c.upper()):
conf = c
if conf is None:
colors.waning(
"There is no suitable openocd config for {model}. Skipping"
)
return
command = (
"#!/usr/bin/env bash\n"
+ "openocd -f /usr/share/openocd/scripts/interface/stlink-dap.cfg "
+ f"-f /usr/share/openocd/scripts/target/{conf}.cfg "
+ '-c "init" -c "reset halt"'
)
open("serve_openocd", "w").write(command)
st = os.stat("serve_openocd")
os.chmod("serve_openocd", st.st_mode | stat.S_IEXEC)
colors.success("Wrote openocd command to ./serve_openocd")
def gen_rel_target(dep):
return os.path.join(
output_dir, "rel", dep.split("/")[-1].replace(".c", ".rel")
)
def get_sources():
has_main = False
has_it_c = False
has_conf_h = False
sources = []
for file in os.listdir(args.src):
if file == "stm8s_conf.h":
has_conf_h = True
if file.endswith(".c"):
if file == "main.c":
has_main = True
continue
if file == "stm8s_it.c":
has_it_c = True
sources.append(os.path.join(args.src, file))
if not has_main:
raise ForgeError(f"No main.c file was found in {args.src}")
if not has_it_c:
colors.warning(f"No stm8s_it.c file was found in {args.src}")
if not has_conf_h:
colors.warning(f"No stm8s_conf.h file was found in {args.src}")
return sources
def get_flash_model(mcu):
mcu = mcu.lower()
match = None
for flash_model in tables.stm8flash_suported_models:
model_re = re.compile(flash_model.replace("?", "\\w?"))
if model_re.match(mcu):
if match is None:
match = flash_model
else:
colors.warning(
f"{match} and {flash_model} are matching flash configs"
)
if match is None:
raise ForgeError(f"Can't find a neat flasher config for {mcu}")
return match
def create_buildfile(model, peripheral_deps=["./stm8s_it.c"]):
with open(ninja_file, "w") as f:
sources = get_sources() + peripheral_deps
w = ninja.Writer(f)
w.variable("device", find_compatible_mcu(model))
w.variable("outdir", output_dir)
w.variable("flash_model", get_flash_model(model))
w.variable(
"includes",
"-I./ "
+ "-I"
+ os.path.join(args.stdp_path, lib_to_driver, "inc"),
)
w.variable(
"compile_directives",
"--stack-auto --fverbose-asm --float-reent "
+ "--no-peep --all-callee-saves --opt-code-size",
)
main_output = os.path.join(output_dir, "main.ihx")
if args.debug:
w.variable("debug", "--debug --out-fmt-elf")
main_output = os.path.join(output_dir, "main.elf")
else:
w.variable("debug", "")
w.variable(
"cflags",
"-mstm8 --std-sdcc99 -D $device $compile_directives $debug",
)
w.newline()
w.rule("rel", "sdcc $cflags $includes -o $outdir/rel/ -c $in")
w.rule("main", "sdcc $cflags $includes -o $outdir/ $in")
flash_cmd = "stm8flash -c stlinkv2 -p $flash_model -w $in"
if args.debug:
flash_cmd = 'echo "Can\'t flash when built with --debug" && exit 1'
w.rule("write_to_flash", flash_cmd)
w.newline()
w.build(
main_output,
"main",
["main.c", *[gen_rel_target(dep) for dep in sources]],
)
w.build(
"flash",
"write_to_flash",
[main_output],
)
w.build(
"build",
"phony",
[main_output],
)
w.newline()
w.comment("deps")
for dep in sources:
w.build(gen_rel_target(dep), "rel", [dep])
def swallow(exceptions, fn):
def wrap(*args, **kwargs):
try:
fn(*args, **kwargs)
except Exception as e:
for excemption in exceptions:
if isinstance(e, excemption):
return
raise e
return wrap
def find_compatible_mcu(mcu):
matching_mcu = None
for model in tables.stdp_supported_models:
if mcu.startswith(model):
matching_mcu = model
break
if mcu is not None and matching_mcu is None:
raise ForgeError(
f"Cube report specified {mcu} as the MCU model "
+ "and we can't dealwith that"
)
return matching_mcu
if __name__ == "__main__":
swallow([FileNotFoundError], os.replace)(ninja_file, "_" + ninja_file)
if shutil.which("ninja") is None:
colors.error("ninja was not found on this system")
quit(1)
try:
os.stat(os.path.join(args.stdp_path, lib_to_driver, "inc", "stm8s.h"))
except FileNotFoundError:
colors.error(
"Could not find stm8s.h in "
+ f'{os.path.join(args.stdp_path, lib_to_driver, "inc")} '
+ "forge is kinda certain that --stdp_path is wrong."
)
quit(1)
try:
if args.cube_file is None:
colors.error("No cube file specified")
exit(1)
with open(args.cube_file, "r") as cube_file:
[mcu, deps] = parse_cube_file(cube_file)
if not args.no_clk:
deps.add(Clk())
if mcu is None:
raise ForgeError("No MCU model found in cube file")
dep_paths = []
for d in deps:
dep_paths = dep_paths + list(
map(
lambda x: os.path.join(
args.stdp_path, lib_to_driver, "src", x
),
d.sources,
)
)
colors.success(
f"Resolving peripherals and mcu model from {cube_file.name}"
)
create_buildfile(mcu, peripheral_deps=dep_paths)
colors.success(f"Build config written to ./{ninja_file}")
if args.debug:
create_openocd_file(mcu)
swallow([FileNotFoundError], shutil.rmtree)("build/")
quit(0)
except ForgeError as e:
colors.error(e)
quit(1)
except Exception as e:
swallow([FileNotFoundError], os.replace)("_" + ninja_file, ninja_file)
colors.error("Unkown error when creating build config:")
raise e
finally:
swallow([FileNotFoundError], os.remove)("_" + ninja_file)
parser.print_help()
quit(1)