-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.bzl
354 lines (285 loc) · 13.2 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
""
load("@bazel_skylib//lib:sets.bzl", "sets")
load("@bazel_utilities//toolchains:extras_filegroups.bzl", "filegroup_translate_to_starlark")
load("@bazel_utilities//toolchains:hosts.bzl", "get_host_infos_from_rctx", "split_host_name", "HOST_EXTENSION")
load("@bazel_utilities//toolchains:registry.bzl", "get_archive_from_registry")
load("//:registry.bzl", "ARM_REGISTRY")
def _arm_compiler_archive_impl(rctx):
host_os, _, host_name = get_host_infos_from_rctx(rctx.os.name, rctx.os.arch)
if rctx.attr.override_host_name != "" and rctx.attr.override_host_name != "local":
host_os, _, host_name = split_host_name(rctx.attr.override_host_name)
registry = json.decode(rctx.attr.registry_json)
archive = get_archive_from_registry(registry, rctx.attr.toolchain_type, rctx.attr.toolchain_version)
substitutions = {
"%{rctx_name}": rctx.name,
"%{rctx_path}": "external/{}/".format(rctx.name),
"%{extension}": HOST_EXTENSION[host_os],
"%{host_name}": host_name,
"%{toolchain_type}": rctx.attr.toolchain_type,
"%{toolchain_version}": rctx.attr.toolchain_version,
"%{compiler_version}": archive["details"]["compiler_version"],
}
rctx.template(
"BUILD.bazel",
Label("//templates:BUILD.compiler.bazel.tpl"),
substitutions
)
host_archive = archive["archives"][host_name]
rctx.download_and_extract(
url = host_archive["url"],
sha256 = host_archive["sha256"],
stripPrefix = host_archive["strip_prefix"],
)
arm_compiler_archive = repository_rule(
implementation = _arm_compiler_archive_impl,
attrs = {
'override_host_name': attr.string(default = "local"),
'toolchain_type': attr.string(mandatory = True),
'toolchain_version': attr.string(default = "latest"),
'registry_json': attr.string(mandatory = True),
},
)
def _arm_toolchain_impl(rctx):
host_os, _, host_name = get_host_infos_from_rctx(rctx.os.name, rctx.os.arch)
if rctx.attr.override_host_name != "" and rctx.attr.override_host_name != "local":
host_os, _, host_name = split_host_name(rctx.attr.override_host_name)
registry = json.decode(rctx.attr.registry_json)
archive = get_archive_from_registry(registry, rctx.attr.toolchain_type, rctx.attr.toolchain_version)
toolchain_id = "{}_{}".format(rctx.attr.toolchain_type, archive["details"]["compiler_version"])
toolchain_path = "external/{}/".format(rctx.name)
compiler_package = ""
compiler_full_package = "@@{}//".format(rctx.name)
compiler_package_path = toolchain_path
if rctx.attr.compiler_archive_package != None and rctx.attr.compiler_archive_package != "":
compiler_package = "@@{}//".format(rctx.attr.compiler_archive_package.repo_name)
compiler_full_package = compiler_package
compiler_package_path = rctx.attr.compiler_archive_package.workspace_root + "/"
substitutions = {
"%{name}": rctx.name,
"%{rctx_name}": rctx.name,
"%{rctx_path}": toolchain_path,
"%{extension}": HOST_EXTENSION[host_os],
"%{host_name}": host_name,
"%{toolchain_id}": toolchain_id,
"%{toolchain_type}": rctx.attr.toolchain_type,
"%{toolchain_version}": rctx.attr.toolchain_version,
"%{compiler_version}": archive["details"]["compiler_version"],
"%{compiler_package}": compiler_package,
"%{compiler_full_package}": compiler_full_package,
"%{compiler_package_path}": compiler_package_path,
"%{add_toolchain_linkdirs}": json.encode(rctx.attr.add_toolchain_linkdirs),
"%{exec_compatible_with}": json.encode(rctx.attr.exec_compatible_with),
"%{target_compatible_with}": json.encode(rctx.attr.target_compatible_with),
"%{copts}": json.encode(rctx.attr.copts),
"%{conlyopts}": json.encode(rctx.attr.conlyopts),
"%{cxxopts}": json.encode(rctx.attr.cxxopts),
"%{linkopts}": json.encode(rctx.attr.linkopts),
"%{defines}": json.encode(rctx.attr.defines),
"%{includedirs}": json.encode(rctx.attr.includedirs),
"%{linkdirs}": json.encode(rctx.attr.linkdirs),
"%{linklibs}": json.encode(rctx.attr.linklibs),
# dbg / opt
"%{dbg_copts}": json.encode(rctx.attr.dbg_copts),
"%{dbg_linkopts}": json.encode(rctx.attr.dbg_linkopts),
"%{opt_copts}": json.encode(rctx.attr.opt_copts),
"%{opt_linkopts}": json.encode(rctx.attr.opt_linkopts),
"%{toolchain_extras_filegroups}": json.encode(filegroup_translate_to_starlark(rctx.attr.toolchain_extras_filegroups)),
}
rctx.template(
"BUILD.bazel",
Label("//templates:BUILD.bazel.tpl"),
substitutions
)
rctx.template(
"rules.bzl",
Label("//templates:rules.bzl.tpl"),
substitutions
)
rctx.template(
"vscode.bzl",
Label("//templates:vscode.bzl.tpl"),
substitutions
)
if rctx.attr.compiler_archive_package == None or rctx.attr.compiler_archive_package == "":
host_archive = archive["archives"][host_name]
rctx.download_and_extract(
url = host_archive["url"],
sha256 = host_archive["sha256"],
stripPrefix = host_archive["strip_prefix"],
)
_arm_toolchain = repository_rule(
implementation = _arm_toolchain_impl,
attrs = {
'override_host_name': attr.string(default = "local"),
'toolchain_type': attr.string(mandatory = True),
'toolchain_version': attr.string(default = "latest"),
'registry_json': attr.string(mandatory = True),
'add_toolchain_linkdirs': attr.bool(default = True),
'exec_compatible_with': attr.string_list(default = []),
'target_compatible_with': attr.string_list(default = []),
'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 = []),
'toolchain_extras_filegroups': attr.label_list(default = []),
'compiler_archive_package': attr.label(default = None),
},
)
def arm_toolchain(
name,
toolchain_type,
toolchain_version = "latest",
exec_compatible_with = [],
target_compatible_with = [],
copts = [],
conlyopts = [],
cxxopts = [],
linkopts = [],
defines = [],
includedirs = [],
linkdirs = [],
linklibs = [],
# dbg / opt
dbg_copts = [],
dbg_linkopts = [],
opt_copts = [],
opt_linkopts = [],
specs = [],
add_toolchain_linkdirs = True,
toolchain_extras_filegroups = [],
registry = ARM_REGISTRY,
compiler_archive_package = None,
override_host_name = "local",
):
"""arm Toolchain
This macro create a repository containing all files needded to get an hermetic toolchain
Args:
name: Name of the repo that will be created
toolchain_type: The arm type to use, avaible: [ arm-none-eabi ]
toolchain_version: The arm archive version
exec_compatible_with: The target_compatible_with list for the toolchain
target_compatible_with: The target_compatible_with list for the toolchain
copts: copts
conlyopts: conlyopts
cxxopts: cxxopts
linkopts: linkopts
defines: defines
includedirs: includedirs
linkdirs: linkdirs
# dbg / opt
linklibs: linklibs
dbg_copts: dbg_copts
dbg_linkopts: dbg_linkopts
opt_copts: opt_copts
opt_linkopts: opt_linkopts
specs: specs for the compiler (nano, nosys, ...)
add_toolchain_linkdirs: If the toolchain linkdirs are added to the compile command (aka: -L...). This shown some issue: when this is enable stm32 won't boot (TODO)
toolchain_extras_filegroups: filegroup added to the cc_toolchain rule to get access to thoses files when sandboxed
registry: The arm registry to use, to allow close environement to provide their own mirroir/url
compiler_archive_package: The arm archive to use. If none are provided, one will be defined automatically
override_host_name: override_host_name
"""
if registry == None:
registry = ARM_REGISTRY
linkopts = linkopts + [ "--specs={}.specs".format(spec) for spec in specs ]
_arm_toolchain(
name = name,
toolchain_type = toolchain_type,
toolchain_version = toolchain_version,
registry_json = json.encode(registry),
add_toolchain_linkdirs = add_toolchain_linkdirs,
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,
toolchain_extras_filegroups = toolchain_extras_filegroups,
compiler_archive_package = compiler_archive_package,
override_host_name = override_host_name,
)
def _arm_toolchain_extension_impl(module_ctx):
toolchain_versions_list = [
(toolchain.override_host_name, toolchain.toolchain_type, toolchain.toolchain_version)
for mod in module_ctx.modules
for toolchain in mod.tags.arm_toolchain
]
if len(toolchain_versions_list) == 0:
print("Should not end here ! You probably forgotten to put a mandatory argument to the arm_toolchain rule <maybe toolchain_type>")
toolchain_versions_list = sets.to_list(sets.make(toolchain_versions_list))
arm_registry = ARM_REGISTRY
for toolchain_version in toolchain_versions_list:
arm_compiler_archive(
name = "archive_arm-{}-{}-{}".format(toolchain_version[0], toolchain_version[1], toolchain_version[2]),
toolchain_type = toolchain_version[1],
toolchain_version = toolchain_version[2],
registry_json = json.encode(arm_registry),
override_host_name = toolchain_version[0],
)
for mod in module_ctx.modules:
for toolchain in mod.tags.arm_toolchain:
arm_toolchain(
name = toolchain.name,
toolchain_type = toolchain.toolchain_type,
toolchain_version = toolchain.toolchain_version,
exec_compatible_with = toolchain.exec_compatible_with,
target_compatible_with = toolchain.target_compatible_with,
copts = toolchain.copts,
conlyopts = toolchain.conlyopts,
cxxopts = toolchain.cxxopts,
linkopts = toolchain.linkopts,
defines = toolchain.defines,
includedirs = toolchain.includedirs,
linkdirs = toolchain.linkdirs,
linklibs = toolchain.linklibs,
toolchain_extras_filegroups = toolchain.toolchain_extras_filegroups,
compiler_archive_package = "@archive_arm-{}-{}-{}".format(toolchain.override_host_name, toolchain.toolchain_type, toolchain.toolchain_version),
override_host_name = toolchain.override_host_name,
)
arm_toolchain_extension = module_extension(
implementation = _arm_toolchain_extension_impl,
tag_classes = {
"arm_toolchain": tag_class(attrs = {
'override_host_name': attr.string(default = "local"),
'name': attr.string(mandatory = True),
'toolchain_type': attr.string(mandatory = True),
'toolchain_version': attr.string(default = "latest"),
'compiler_archive_package': attr.label(default = None),
'add_toolchain_linkdirs': attr.bool(default = True),
'exec_compatible_with': attr.string_list(default = []),
'target_compatible_with': attr.string_list(default = []),
'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 = []),
'toolchain_extras_filegroups': attr.label_list(default = []),
}),
},
)