-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathrepo.lb
More file actions
229 lines (201 loc) · 9.39 KB
/
repo.lb
File metadata and controls
229 lines (201 loc) · 9.39 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
227
228
229
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, Fabian Greif
# Copyright (c) 2017-2018, Niklas Hauser
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------
import re
import sys
import glob
import json
import hashlib
import platform
from pathlib import Path
from git import Repo
from os.path import normpath
def StrictVersion(v):
return tuple(map(int, (v.split("."))))
# Check for miminum required lbuild version
import lbuild
min_lbuild_version = "1.21.6"
if StrictVersion(getattr(lbuild, "__version__", "0.1.0")) < StrictVersion(min_lbuild_version):
print("modm requires at least lbuild v{}, please upgrade!\n"
" pip3 install -U lbuild".format(min_lbuild_version))
exit(1)
# Check for submodule existance and their version
def check_submodules():
repo = Repo(localpath("."))
has_error = True
if any(not sm.module_exists() for sm in repo.submodules):
print("\n>> modm: One or more git submodules in `modm/ext/` is missing!\n"
">> modm: Please checkout the submodules:\n\n"
" cd modm\n"
" git submodule update --init\n")
exit(1)
elif any(sm.hexsha != sm.module().commit().hexsha for sm in repo.submodules):
print("\n>> modm: One or more git submodules in `modm/ext/` is not up-to-date!\n"
">> modm: Please update the submodules:\n\n"
" cd modm\n"
" git submodule sync\n"
" git submodule update --init\n")
# Import modm-device tools
sys.path.append(repopath("ext/modm-devices"))
try:
import modm_devices.parser
except ModuleNotFoundError:
check_submodules()
# =============================================================================
class DevicesCache(dict):
"""Loads and filters the available devices from modm-devices and tools/devices."""
def __init__(self):
dict.__init__(self)
modm_devices = Path(repopath("ext/modm-devices/devices"))
tools_devices = Path(repopath("tools/devices"))
ignored_devices = Path(repopath("test/all/ignored.txt"))
# Ignored partnames
ignored = [d for d in ignored_devices.read_text().strip().splitlines() if "#" not in d]
# Supported device families
supported = (
"at90", "attiny", "atmega",
"rp",
"samd21",
"samd51", "same51", "same53", "same54", "samg55",
"same70", "sams70", "samv70", "samv71",
"stm32c0",
"stm32f0", "stm32f1", "stm32f2", "stm32f3", "stm32f4", "stm32f7",
"stm32g0", "stm32g4",
"stm32h5", "stm32h7",
"stm32l0", "stm32l1", "stm32l4", "stm32l5",
"stm32u0", "stm32u3", "stm32u5",
)
# Load and filter the device prefixes
modm_db = json.loads((modm_devices / "db.json").read_text())
modm_db = {name: modm_devices / path
for prefix, values in modm_db.items()
for name, path in values.items()
if prefix in supported and not
any(name.startswith(i) for i in ignored)}
# Add all the tools devices
tools_db = json.loads((tools_devices / "db.json").read_text())
tools_db = {name: tools_devices / path for name, path in tools_db.items()}
self.mapping = modm_db
self.mapping.update(tools_db)
# Initialize the dictionary to None
for name in self.mapping.keys():
self[name] = None
def __getitem__(self, item):
value = dict.__getitem__(self, item)
if value is None:
# Parse the device file and build its devices
parser = modm_devices.parser.DeviceParser()
device_file = parser.parse(self.mapping[item])
for device in device_file.get_devices():
self[device.partname] = device
return self[item]
return value
# =============================================================================
class TargetOption(EnumerationOption):
"""
Allows the target string to be longer than required by selecting the best
fitting target string and adds the revision to the device identifier.
"""
def as_enumeration(self, value):
revision = "" # split revision from the input value
if "/rev" in value: value, revision = value.split("/rev");
target = self._obj_to_str(value)
targets = self._enumeration.keys()
# check if input string is part of keys OR longer
while(len(target) >= len("rp2040")):
if target in targets:
target = self._enumeration[target]
target._identifier.set("revision", revision.lower())
return target
else:
# cut off last character and try again
target = target[:-1]
# check if input string yields ambiguous results
target = self._obj_to_str(value)
targets = [t for t in targets if t.startswith(target)]
if len(targets):
from lbuild.exception import _bp, _hl
raise ValueError(_hl("Ambiguous target!\n") + "Multiple devices found:\n\n" + _bp(targets))
# Otherwise completely unknown target
raise TypeError("Target is unknown!")
# =============================================================================
from lbuild.format import ansi_escape as c
def modm_format_description(node, description):
# Remove the HTML comments we use for describing doc tests
description = re.sub(r"\n?<!--.*?-->", "", description, flags=re.S)
# Indent code content
for block in re.finditer(r"\n```.*?(\n.*?)\n```", description, flags=re.M|re.S):
description = description.replace(block.group(0), block.group(1).replace("\n", "\n "))
# format any markdown headers bold
description = re.sub(r"^(#+.*)", r"{}\g<1>{}".format(str(c("bold")), str(c("no_bold"))), description, flags=re.M)
return node.format_description(node, description)
def modm_format_short_description(node, description):
# All docs use Markdown, so they might start with a `# Header`
description = re.sub(r"\n?<!--.*?-->", "", description, flags=re.S)
return node.format_short_description(node, description.replace("#", ""))
# =============================================================================
def init(repo):
repo.name = "modm"
repo.description = FileReader("README.md")
repo.format_description = modm_format_description
repo.format_short_description = modm_format_short_description
repo.add_ignore_patterns("*/*.lb", "*/board.xml")
# Add the board configuration files as their module name aliases
for module in Path(repopath("src/modm/board")).glob("*/module.lb"):
module_text = module.read_text()
name = re.search(r"\.name += +\".*?:board:(.*?)\"", module_text).group(1)
if (module.parent / "module.md").exists():
description = FileReader(str(module.parent / "module.md"))
else:
description = re.search(r'description += +(""".*?"""|".*?")',
module_text, flags=re.S|re.M)
description = description.group(1).strip('"\\') if description else None
versions = re.search(r"#+ +[Rr]evisions? += +\[(.*)\]", module_text)
versions = versions.group(1).split(",") if versions is not None else [""]
config = {v.strip(): (module.parent / "board.xml") for v in versions}
repo.add_configuration(Configuration(name, description, config, versions[0].strip()))
# Add Jinja2 filters commonly used in this repository
if 'Windows' in platform.platform():
def windowsify(path, escape_level):
escaped = "\\" * (2 ** escape_level)
return path.replace("\\", escaped)
def posixify(path):
return path.replace("\\", "/")
else:
def windowsify(path, escape_level):
return path
def posixify(path):
return path
repo.add_filter("modm.windowsify", windowsify)
repo.add_filter("modm.posixify", posixify)
repo.add_filter("modm.ord", lambda letter: ord(letter[0].lower()) - ord("a"))
repo.add_filter("modm.chr", lambda num: chr(num + ord("A")))
repo.add_filter("modm.digsep", lambda num: f"{num:,}".replace(",", "'"))
# Add the target option with all available devices
repo.add_option(
TargetOption(name="target",
description=descr_target,
enumeration=DevicesCache()))
def prepare(repo, options):
repo.add_modules_recursive("ext", modulefile="*.lb")
repo.add_modules_recursive("src", modulefile="*.lb")
repo.add_modules_recursive("tools", modulefile="*.lb")
def build(env):
env.collect(":build:path.include", "modm/src")
# ============================ Option Descriptions ============================
descr_target = """# Device identifier
This is the long string written on your microcontroller that uniquely identifies
the target device for which to generate the modm HAL.
You can optionally add the hardware revision to the identifier string by
appending `/revX` where X is the revision. This can help modm apply workarounds
for specific hardware bugs as recommended by the errata sheet.
"""