-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathautogen.bzl
520 lines (455 loc) · 15.9 KB
/
autogen.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
load("//rules:stamp.bzl", "stamp_attr", "stamping_enabled")
load("//rules/opentitan:hw.bzl", "OpenTitanTopInfo")
"""Autogeneration rules for OpenTitan.
The rules in this file are for autogenerating various file resources
used by the OpenTitan build, such as register definition files generated
from hjson register descriptions.
"""
def _opentitan_ip_c_header_impl(ctx):
header = ctx.actions.declare_file("{}_regs.h".format(ctx.attr.ip))
top = ctx.attr.top[OpenTitanTopInfo]
if ctx.attr.ip not in top.ip_hjson:
fail("Cannot generate headers: top {} does not contain IP {}".format(top.name, ctx.attr.ip))
hjson = top.ip_hjson[ctx.attr.ip]
arguments = [
"-D",
"-q",
"-o",
header.path,
hjson.path,
]
inputs = [hjson]
# Add path to an alias path if it's needed.
if ctx.file.alias:
alias = ctx.file.alias
# add the alias argument.
arguments.extend([
"--alias",
alias.path,
])
# add the alias as an input file.
inputs.append(alias)
ctx.actions.run(
outputs = [header],
inputs = inputs,
arguments = arguments,
executable = ctx.executable._regtool,
)
return [
CcInfo(compilation_context = cc_common.create_compilation_context(
includes = depset([header.dirname]),
headers = depset([header]),
)),
DefaultInfo(files = depset([header])),
OutputGroupInfo(
header = depset([header]),
),
]
opentitan_ip_c_header = rule(
implementation = _opentitan_ip_c_header_impl,
doc = "Generate the C headers for an IP block as used in a top",
attrs = {
"top": attr.label(providers = [OpenTitanTopInfo], doc = "Opentitan top description"),
"ip": attr.string(doc = "Name of the IP block"),
"alias": attr.label(
mandatory = False,
allow_single_file = True,
doc = "A path to an alias file",
),
"_regtool": attr.label(
default = "//util:regtool",
executable = True,
cfg = "exec",
),
},
)
def _opentitan_ip_rust_header_impl(ctx):
tock = ctx.actions.declare_file("{}.rs".format(ctx.attr.ip))
top = ctx.attr.top[OpenTitanTopInfo]
if ctx.attr.ip not in top.ip_hjson:
fail("Cannot generate headers: top {} does not contain IP {}".format(top.name, ctx.attr.ip))
hjson = top.ip_hjson[ctx.attr.ip]
stamp_args = []
stamp_files = []
if stamping_enabled(ctx):
stamp_files = [ctx.version_file]
stamp_args.append("--version-stamp={}".format(ctx.version_file.path))
ctx.actions.run(
outputs = [tock],
inputs = [hjson] + stamp_files,
arguments = [
"--tock",
"-q",
"-o",
tock.path,
] + stamp_args + [hjson.path],
executable = ctx.executable._regtool,
)
return [
DefaultInfo(files = depset([tock])),
OutputGroupInfo(
tock = depset([tock]),
),
]
opentitan_ip_rust_header = rule(
implementation = _opentitan_ip_rust_header_impl,
doc = "Generate the Rust headers for an IP block as used in a top",
attrs = {
"top": attr.label(providers = [OpenTitanTopInfo], doc = "Opentitan top description"),
"ip": attr.string(doc = "Name of the IP block"),
"_regtool": attr.label(
default = "//util:regtool",
executable = True,
cfg = "exec",
),
} | stamp_attr(-1, "//rules:stamp_flag"),
)
def _opentitan_autogen_dif_gen(ctx):
outputs = []
outdir = "{}/{}".format(ctx.bin_dir.path, ctx.label.package)
top = ctx.attr.top[OpenTitanTopInfo]
# Fail if the requested IP is not in the top
if ctx.attr.ip not in top.ip_hjson:
fail("Cannot generate DIF: top {} does not contain IP {}".format(top.name, ctx.attr.ip))
ip_hjson = top.ip_hjson[ctx.attr.ip]
groups = {}
for group, files in ctx.attr.output_groups.items():
deps = []
for file in files:
deps.append(ctx.actions.declare_file(file))
outputs.extend(deps)
groups[group] = depset(deps)
inputs = [ip_hjson]
arguments = [
"--ipcfg",
ip_hjson.path,
"--outdir",
outdir,
]
ctx.actions.run(
outputs = outputs,
inputs = inputs,
arguments = arguments,
executable = ctx.executable._autogen_dif,
)
return [
DefaultInfo(files = depset(outputs)),
OutputGroupInfo(**groups),
]
opentitan_autogen_dif_gen = rule(
implementation = _opentitan_autogen_dif_gen,
doc = "Generate the DIFs file for an IP",
attrs = {
"top": attr.label(mandatory = True, providers = [OpenTitanTopInfo], doc = "Opentitan top description"),
"ip": attr.string(mandatory = True, doc = "Name of the IP for which to generate the DIF"),
"output_groups": attr.string_list_dict(
allow_empty = True,
doc = """
Mappings from output group names to lists of paths contained in
that group.
""",
),
"_autogen_dif": attr.label(
default = "//util:autogen_dif",
executable = True,
cfg = "exec",
),
},
)
# See opentitan_autogen_dif_gen for documentation of parameters.
def opentitan_autogen_dif(name, top, ip, target_compatible_with = []):
opentitan_autogen_dif_gen(
name = "{}_gen".format(name),
top = top,
ip = ip,
output_groups = {
"hdr": ["dif_{}_autogen.h".format(ip)],
"src": ["dif_{}_autogen.c".format(ip)],
"unittest": ["dif_{}_autogen_unittest.cc".format(ip)],
},
target_compatible_with = target_compatible_with,
)
for grp in ["hdr", "src", "unittest"]:
native.filegroup(
name = "{}_{}".format(name, grp),
srcs = [":{}_gen".format(name)],
output_group = grp,
)
def _opentitan_top_dt_gen(ctx):
outputs = []
outdir = "{}/{}".format(ctx.bin_dir.path, ctx.label.package)
groups = {}
for group, files in ctx.attr.output_groups.items():
deps = []
for file in files:
deps.append(ctx.actions.declare_file(file))
outputs.extend(deps)
groups[group] = depset(deps)
top = ctx.attr.top[OpenTitanTopInfo]
inputs = [top.hjson]
ips = []
for (ipname, hjson) in top.ip_hjson.items():
if ctx.attr.gen_top or ipname in ctx.attr.gen_ips:
inputs.append(hjson)
ips.extend(["-i", hjson.path])
arguments = [
"--topgencfg",
top.hjson.path,
"--outdir",
outdir,
]
arguments.append("--gen-top" if ctx.attr.gen_top else "--gen-ip")
for ipname in ctx.attr.gen_ips:
if ipname not in top.ip_hjson:
fail("Cannot generate IP headers: top {} does not contain IP {}".format(top.name, ipname))
arguments.extend(ips)
ctx.actions.run(
outputs = outputs,
inputs = inputs,
arguments = arguments,
executable = ctx.executable._dttool,
)
return [
DefaultInfo(files = depset(outputs)),
OutputGroupInfo(**groups),
]
opentitan_top_dt_gen = rule(
implementation = _opentitan_top_dt_gen,
doc = "Generate the C headers for an IP block as used in a top",
attrs = {
"top": attr.label(providers = [OpenTitanTopInfo], doc = "Opentitan top description"),
"gen_top": attr.bool(default = False, doc = "If true, generate the toplevel files"),
"gen_ips": attr.string_list(doc = "List of IPs for which to generate header files"),
"output_groups": attr.string_list_dict(
allow_empty = True,
doc = """
Mappings from output group names to lists of paths contained in
that group.
""",
),
"_dttool": attr.label(
default = "//util:dttool",
executable = True,
cfg = "exec",
),
},
)
def opentitan_ip_dt_header(name, top, ip, deps = None, target_compatible_with = []):
"""
Generate the C header for an IP block as used in a top. This library is created to the
provided top and can have additional dependencies. The top target must export an
OpenTitanTopInfo provider, e.g. by created by opentitan_top. If this IP is not included
in the top, an error will be thrown.
"""
if deps == None:
deps = []
if target_compatible_with == None:
target_compatible_with = []
opentitan_top_dt_gen(
name = "{}_gen".format(name),
top = top,
gen_ips = [ip],
output_groups = {
"hdr": ["dt/dt_{}.h".format(ip)],
"src": ["dt/dt_{}.c".format(ip)],
},
target_compatible_with = target_compatible_with,
)
for grp in ["hdr", "src"]:
native.filegroup(
name = "{}_{}".format(name, grp),
srcs = [":{}_gen".format(name)],
output_group = grp,
)
native.cc_library(
name = name,
srcs = [":{}_src".format(name)],
hdrs = [":{}_hdr".format(name)],
deps = deps,
# Make the header accessible as "dt_<ip>.h".
includes = ["."],
)
def opentitan_top_dt_api(name, top, deps = None):
"""
Create a library that exports the "dt_api.h" header. This library is created to the
provided top and can have additional dependencies. The top target must export an
OpenTitanTopInfo provider, e.g. by created by opentitan_top.
"""
if deps == None:
deps = []
opentitan_top_dt_gen(
name = "{}_gen".format(name),
top = top,
gen_top = True,
output_groups = {
"hdr": ["dt/dt_api.h"],
"src": ["dt/dt_api.c"],
},
)
for grp in ["src", "hdr"]:
native.filegroup(
name = "{}_{}".format(name, grp),
srcs = [":{}_gen".format(name)],
output_group = grp,
)
native.cc_library(
name = name,
srcs = [":{}_src".format(name)],
hdrs = [":{}_hdr".format(name)],
deps = deps,
# Make the dt_api.h header accessible as "dt/dt_api.h".
includes = ["."],
)
def _chip_info_src(ctx):
stamp_args = []
stamp_files = []
if stamping_enabled(ctx):
stamp_files = [ctx.version_file]
stamp_args.append("--ot_version_file")
stamp_args.append(ctx.version_file.path)
else:
print("NOTE: stamping is disabled, the chip_info section will use a fixed version string")
stamp_args.append("--default_version")
# The script expects a 20-character long hash: "OpenTitanOpenTitanOT"
stamp_args.append("4f70656e546974616e4f70656e546974616e4f54")
out_source = ctx.actions.declare_file("chip_info.c")
ctx.actions.run(
outputs = [
out_source,
],
inputs = [
ctx.executable._tool,
] + stamp_files,
arguments = [
"-o",
out_source.dirname,
] + stamp_args,
executable = ctx.executable._tool,
)
return [
DefaultInfo(files = depset([out_source])),
]
autogen_chip_info_src = rule(
implementation = _chip_info_src,
attrs = {
"_tool": attr.label(
default = "//util:rom_chip_info",
executable = True,
cfg = "exec",
),
} | stamp_attr(-1, "//rules:stamp_flag"),
)
def autogen_chip_info(name):
"""Generates a cc_library named `name` that defines chip info."""
# Generate a C source file that defines the chip info struct. This is an
# implementation detail and should not be depended on externally.
chip_info_src_target = name + "_gen_src"
autogen_chip_info_src(name = chip_info_src_target)
# Package up the generated source file with its corresponding header file
# and dependencies. Any target that wants access to the chip info should
# depend on this.
native.cc_library(
name = name,
srcs = [chip_info_src_target],
hdrs = ["//sw/device/silicon_creator/lib:chip_info.h"],
deps = [
"//sw/device/lib/base:macros",
],
)
def _cryptotest_hjson_external(ctx):
"""
Implementation of the Bazel rule for parsing externally-sourced test vectors.
Crypto test vectors are represented in a standard HJSON format; for
externally-sourced vectors, we need to parse the original data into the
standard format before it can be used.
This rule expects an executable script (the `parser` attribute) and a
single external data file to pass to this script (the `src` attribute). It
assumes that the parser accepts the following syntax:
<script> <input file> dst.hjson
...where <input file> is the external test data and dst.hjson is the HJSON
file to which the script writes the test vectors.
"""
hjson = ctx.actions.declare_file(ctx.attr.name + ".hjson")
parser_input = ctx.file.src
ctx.actions.run(
outputs = [hjson],
inputs = [ctx.executable.parser, parser_input],
arguments = [parser_input.path, hjson.path],
executable = ctx.executable.parser,
)
return [
DefaultInfo(files = depset([hjson])),
OutputGroupInfo(
hjson = depset([hjson]),
),
]
autogen_cryptotest_hjson_external = rule(
implementation = _cryptotest_hjson_external,
attrs = {
"deps": attr.label_list(allow_files = True),
"src": attr.label(allow_single_file = True),
"parser": attr.label(
mandatory = True,
executable = True,
cfg = "exec",
),
},
)
def _cryptotest_header(ctx):
"""
Implementation of the Bazel rule for generating crypto test vector headers.
Crypto tests are all represented in a standard HJSON format. This rule runs
an algorithm-specific script (the `test_setter` attribute) that reads an
HJSON file (the `hjson` attribute) and populates a header template (the
`template` attribute).
Assumes that `test_setter` scripts accept the following syntax:
<script> --template dst.h.tpl tests.hjson dst.h
...where dst.h.tpl is the header template, tests.hjson is the file
containing the HJSON test vectors and dst.h is the header file to which the
output will be written.
"""
template = ctx.file.template
if not template.basename.endswith(".h.tpl"):
fail("Expected template to have a `.h.tpl` extension, got: " + str(ctx.files.srcs))
header = ctx.actions.declare_file("{}/{}".format(ctx.label.name, template.basename[:-4]))
hjson = ctx.file.hjson
ctx.actions.run(
outputs = [header],
inputs = [template, hjson],
arguments = [
"-t",
template.path,
"-j",
hjson.path,
"-o",
header.path,
],
executable = ctx.executable.tool,
)
return [
CcInfo(compilation_context = cc_common.create_compilation_context(
includes = depset([header.dirname]),
headers = depset([header]),
defines = depset(["RULE_NAME=\"{}\"".format(ctx.label.name)]),
)),
DefaultInfo(files = depset([header]), default_runfiles = ctx.runfiles(files = [hjson])),
OutputGroupInfo(
header = depset([header]),
),
]
autogen_cryptotest_header = rule(
implementation = _cryptotest_header,
attrs = {
"deps": attr.label_list(allow_files = True),
"template": attr.label(mandatory = True, allow_single_file = [".tpl"]),
"hjson": attr.label(mandatory = True, allow_single_file = [".hjson"]),
"tool": attr.label(
default = "//sw/device/tests/crypto:ecdsa_p256_verify_set_testvectors",
executable = True,
cfg = "exec",
),
},
)