-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.bzl
293 lines (230 loc) · 9.66 KB
/
rules.bzl
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
""
load("@bazel_arm//:registry.bzl", "ARM_REGISTRY")
load("@bazel_arm//:rules.bzl", "arm_toolchain", "arm_compiler_archive")
load("@bazel_skylib//lib:sets.bzl", "sets")
load("//mcu:stm32_famillies.bzl", "STM32_FAMILLIES_LUT")
def _stm32_rules_impl(rctx):
substitutions = {
"%{rctx_name}": rctx.name,
"%{toolchain_path}": "external/{}/".format(rctx.name),
"%{arm_none_eabi_repo_name}": rctx.attr.arm_none_eabi_repo_name,
"%{MCU_ID}": rctx.attr.mcu,
"%{MCU_FAMILLY}": rctx.attr.stm32_familly,
"%{exec_compatible_with}": json.encode(rctx.attr.exec_compatible_with),
"%{target_compatible_with}": json.encode(rctx.attr.target_compatible_with),
"%{toolchain_mcu_constraint}": json.encode(rctx.attr.toolchain_mcu_constraint),
}
rctx.template(
"BUILD.bazel",
Label("//templates:BUILD.bazel.tpl"),
substitutions
)
rctx.template(
"rules.bzl",
Label("//templates:rules.bzl.tpl"),
substitutions
)
_stm32_rules = repository_rule(
implementation = _stm32_rules_impl,
attrs = {
'arm_none_eabi_repo_name': attr.string(mandatory = True),
'mcu': attr.string(mandatory = True),
'stm32_familly': attr.string(mandatory = True),
'exec_compatible_with': attr.string_list(default = []),
'toolchain_mcu_constraint': attr.string_list(default = []),
'target_compatible_with': attr.string_list(default = []),
},
)
def stm32_toolchain(
name,
mcu,
device_group,
copts = [],
conlyopts = [],
cxxopts = [],
linkopts = [],
defines = [],
includedirs = [],
linkdirs = [],
linklibs = [],
# dbg / opt
dbg_copts = [],
dbg_linkopts = [],
opt_copts = [],
opt_linkopts = [],
specs = [],
gc_sections = True,
arm_toolchain_extras_filegroups = [],
exec_compatible_with = [],
target_compatible_with = [],
use_mcu_constraint = True,
arm_none_eabi_version = "latest",
arm_registry = None,
arm_compiler_archive_package = None,
custom_stm32_info = None,
):
"""STM32 toolchain
This macro create a repository containing all files needded to get an STM32 toolchain using an arm-none-eabi Toolchain
Args:
name: Name of the repo that will be created
mcu: STM32 mcu name
device_group: device_group
copts: copts
conlyopts: conlyopts
cxxopts: cxxopts
linkopts: linkopts
defines: defines
includedirs: includedirs
linkdirs: linkdirs
linklibs: linklibs
# dbg / opt
dbg_copts: dbg_copts
dbg_linkopts: dbg_linkopts
opt_copts: opt_copts
opt_linkopts: opt_linkopts
specs: specs for the compiler (nano, nosys, ...)
gc_sections: Enable the garbage collection of unused sections
arm_toolchain_extras_filegroups: arm_toolchain_extras_filegroups
exec_compatible_with: The exec_compatible_with list for the toolchain
target_compatible_with: The target_compatible_with list for the toolchain
use_mcu_constraint: Add the mcu_constraint list (cpu / stm32 familly) to the target_compatible_with
arm_none_eabi_version: The arm-none-eabi archive version
arm_registry: The arm registry to use. Default to @bazel_arm//:ARM_REGISTRY
arm_compiler_archive_package: The arm archive to use. If none are provided, one will be define automatically with this name: ":arm-none-eabi-" + mcu
custom_stm32_info: information about the mcu you are using if you don't want to use the provided STM32_FAMILLIES_LUT
"""
mcu = mcu.upper()
stm32_familly = mcu[:7]
stm32_familly_info = custom_stm32_info
if stm32_familly_info == None:
stm32_familly_info = STM32_FAMILLIES_LUT[stm32_familly]
mcu_flags = [ stm32_familly_info.cpu, "-mthumb" ]
if hasattr(stm32_familly_info, "fpu") and stm32_familly_info.fpu != None:
mcu_flags += [ stm32_familly_info.fpu_abi, stm32_familly_info.fpu ]
copts = mcu_flags + copts
linkopts = mcu_flags + linkopts
defines = defines + [ "USE_HAL_DRIVER", device_group ]
includedirs = includedirs + [
"Core/Inc",
"Drivers/{stm32_familly}xx_HAL_Driver/Inc".format(stm32_familly = stm32_familly),
"Drivers/{stm32_familly}xx_HAL_Driver/Inc/Legacy".format(stm32_familly = stm32_familly),
"Drivers/CMSIS/Device/ST/{stm32_familly}xx/Include".format(stm32_familly = stm32_familly),
"Drivers/CMSIS/Include"
]
linkopts = linkopts
if gc_sections:
copts += [ "-fdata-sections", "-ffunction-sections" ]
linkopts.append("-Wl,--gc-sections")
toolchain_mcu_constraint = [
"@platforms//cpu:{}".format(stm32_familly_info.arm_cpu_version),
# "@bazel_stm32//mcu:{}".format(stm32_familly.lower()),
]
if use_mcu_constraint:
target_compatible_with = target_compatible_with + toolchain_mcu_constraint
arm_toolchain(
name = "arm-none-eabi-" + name,
toolchain_type = "arm-none-eabi",
toolchain_version = arm_none_eabi_version,
exec_compatible_with = exec_compatible_with,
target_compatible_with = target_compatible_with,
copts = copts,
conlyopts = conlyopts,
cxxopts = cxxopts,
linkopts = linkopts,
defines = defines,
includedirs = includedirs,
linkdirs = linkdirs,
linklibs = linklibs,
# dbg / opt
dbg_copts = dbg_copts,
dbg_linkopts = dbg_linkopts,
opt_copts = opt_copts,
opt_linkopts = opt_linkopts,
specs = specs,
add_toolchain_linkdirs = False,
toolchain_extras_filegroups = arm_toolchain_extras_filegroups,
registry = arm_registry,
compiler_archive_package = arm_compiler_archive_package,
)
_stm32_rules(
name = name,
arm_none_eabi_repo_name = "arm-none-eabi-" + name,
mcu = mcu,
stm32_familly = stm32_familly,
toolchain_mcu_constraint = toolchain_mcu_constraint,
target_compatible_with = target_compatible_with,
)
def _stm32_toolchain_extension_impl(module_ctx):
toolchain_versions_list = [
platform.toolchain_version
for mod in module_ctx.modules
for platform in mod.tags.stm32_platform
]
if len(toolchain_versions_list) == 0:
toolchain_versions_list.append("latest")
toolchain_versions_list = sets.to_list(sets.make(toolchain_versions_list))
arm_registry = ARM_REGISTRY
for version in toolchain_versions_list:
arm_compiler_archive(
name = "archive_arm-none-eabi-" + version,
toolchain_type = "arm-none-eabi",
toolchain_version = version,
registry_json = json.encode(arm_registry),
)
for mod in module_ctx.modules:
for platform in mod.tags.stm32_platform:
stm32_toolchain(
name = platform.name,
mcu = platform.mcu,
device_group = platform.device_group,
copts = platform.copts,
conlyopts = platform.conlyopts,
cxxopts = platform.cxxopts,
linkopts = platform.linkopts,
defines = platform.defines,
includedirs = platform.includedirs,
linkdirs = platform.linkdirs,
linklibs = platform.linklibs,
# dbg / opt
dbg_copts = platform.dbg_copts,
dbg_linkopts = platform.dbg_linkopts,
opt_copts = platform.opt_copts,
opt_linkopts = platform.opt_linkopts,
specs = platform.specs,
gc_sections = platform.gc_sections,
arm_toolchain_extras_filegroups = platform.arm_toolchain_extras_filegroups,
exec_compatible_with = platform.exec_compatible_with,
target_compatible_with = platform.target_compatible_with,
use_mcu_constraint = platform.use_mcu_constraint,
arm_compiler_archive_package = "@archive_arm-none-eabi-" + platform.toolchain_version,
)
stm32_toolchain_extension = module_extension(
implementation = _stm32_toolchain_extension_impl,
tag_classes = {
"stm32_platform": tag_class(attrs = {
'name': attr.string(mandatory = True),
'toolchain_version': attr.string(default = "latest"),
'mcu': attr.string(mandatory = True),
'device_group': attr.string(mandatory = True),
'exec_compatible_with': attr.string_list(default = []),
'target_compatible_with': attr.string_list(default = []),
'use_mcu_constraint': attr.bool(default = True),
'copts': attr.string_list(default = []),
'conlyopts': attr.string_list(default = []),
'cxxopts': attr.string_list(default = []),
'linkopts': attr.string_list(default = []),
'defines': attr.string_list(default = []),
'includedirs': attr.string_list(default = []),
'linkdirs': attr.string_list(default = []),
'linklibs': attr.string_list(default = []),
# dbg / opt
'dbg_copts': attr.string_list(default = []),
'dbg_linkopts': attr.string_list(default = []),
'opt_copts': attr.string_list(default = []),
'opt_linkopts': attr.string_list(default = []),
'specs': attr.string_list(default = []),
'gc_sections': attr.bool(default = True),
'arm_toolchain_extras_filegroups': attr.label_list(default = []),
}),
},
)