-
Notifications
You must be signed in to change notification settings - Fork 5
/
openroad.bzl
1516 lines (1378 loc) · 47.4 KB
/
openroad.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Rules for the building the OpenROAD-flow-scripts stages"""
load("@orfs_variable_metadata//:json.bzl", "orfs_variable_metadata")
def _map(function, iterable):
return [function(x) for x in iterable]
def _union(*lists):
merged_dict = {}
for list1 in lists:
dict1 = {key: True for key in list1}
merged_dict.update(dict1)
return list(merged_dict.keys())
OrfsInfo = provider(
"The outputs of a OpenROAD-flow-scripts stage.",
fields = [
"stage",
"config",
"variant",
"odb",
"gds",
"lef",
"lib",
"additional_gds",
"additional_lefs",
"additional_libs",
],
)
PdkInfo = provider(
"A process design kit.",
fields = [
"name",
"files",
],
)
TopInfo = provider(
"The name of the netlist top module.",
fields = ["module_top"],
)
OrfsDepInfo = provider(
"The name of the netlist top module.",
fields = [
"make",
"config",
"renames",
"files",
"runfiles",
],
)
LoggingInfo = provider(
"Logs and reports for current and previous stages",
fields = [
"logs",
"reports",
],
)
def _pdk_impl(ctx):
return [
DefaultInfo(
files = depset(ctx.files.srcs),
),
PdkInfo(
name = ctx.attr.name,
files = depset(ctx.files.srcs),
),
]
orfs_pdk = rule(
implementation = _pdk_impl,
attrs = {
"srcs": attr.label_list(
allow_files = True,
providers = [DefaultInfo],
),
},
)
def _macro_impl(ctx):
info = {}
for field in ["odb", "gds", "lef", "lib"]:
if not getattr(ctx.attr, field):
continue
for file in getattr(ctx.attr, field).files.to_list():
if file.extension != field:
continue
info[file.extension] = file
return [
DefaultInfo(
files = depset(ctx.files.odb + ctx.files.gds + ctx.files.lef + ctx.files.lib),
),
OutputGroupInfo(
**{f.basename: depset([f]) for f in ctx.files.odb + ctx.files.gds + ctx.files.lef + ctx.files.lib}
),
OrfsInfo(
odb = info.get("odb"),
gds = info.get("gds"),
lef = info.get("lef"),
lib = info.get("lib"),
additional_gds = depset([]),
additional_lefs = depset([]),
additional_libs = depset([]),
),
TopInfo(
module_top = ctx.attr.module_top,
),
]
orfs_macro = rule(
implementation = _macro_impl,
provides = [DefaultInfo, OutputGroupInfo, OrfsInfo, TopInfo],
attrs = {
"module_top": attr.string(mandatory = True),
} | {
field: attr.label(
allow_files = [field],
providers = [DefaultInfo],
)
for field in [
"odb",
"gds",
"lef",
"lib",
]
},
)
def odb_environment(ctx):
if ctx.attr.src[OrfsInfo].odb:
return {"ODB_FILE": ctx.attr.src[OrfsInfo].odb.path}
return {}
def orfs_environment(ctx):
return {
"HOME": _work_home(ctx),
"STDBUF_CMD": "",
"TCL_LIBRARY": commonpath(ctx.files._tcl),
"WORK_HOME": _work_home(ctx),
}
def flow_environment(ctx):
return {
"DLN_LIBRARY_PATH": commonpath(ctx.files._ruby_dynamic),
"FLOW_HOME": ctx.file._makefile.dirname,
"KLAYOUT_CMD": ctx.executable._klayout.path,
"OPENROAD_EXE": ctx.executable._openroad.path,
"OPENSTA_EXE": ctx.executable._opensta.path,
"QT_PLUGIN_PATH": commonpath(ctx.files._qt_plugins),
"QT_QPA_PLATFORM_PLUGIN_PATH": commonpath(ctx.files._qt_plugins),
"RUBYLIB": ":".join([commonpath(ctx.files._ruby), commonpath(ctx.files._ruby_dynamic)]),
} | orfs_environment(ctx)
def yosys_environment(ctx):
return {
"ABC": ctx.executable._abc.path,
"YOSYS_EXE": ctx.executable._yosys.path,
"FLOW_HOME": ctx.file._makefile_yosys.dirname,
} | orfs_environment(ctx)
def config_environment(config):
return {"DESIGN_CONFIG": config.path}
def flow_inputs(ctx):
return depset(
[
ctx.executable._klayout,
ctx.executable._make,
ctx.executable._openroad,
ctx.executable._opensta,
ctx.file._makefile,
] +
ctx.files._ruby +
ctx.files._ruby_dynamic +
ctx.files._tcl +
ctx.files._opengl +
ctx.files._qt_plugins +
ctx.files._gio_modules,
transitive = [
ctx.attr._openroad[DefaultInfo].default_runfiles.files,
ctx.attr._openroad[DefaultInfo].default_runfiles.symlinks,
ctx.attr._opensta[DefaultInfo].default_runfiles.files,
ctx.attr._opensta[DefaultInfo].default_runfiles.symlinks,
ctx.attr._klayout[DefaultInfo].default_runfiles.files,
ctx.attr._klayout[DefaultInfo].default_runfiles.symlinks,
ctx.attr._makefile[DefaultInfo].default_runfiles.files,
ctx.attr._makefile[DefaultInfo].default_runfiles.symlinks,
ctx.attr._make[DefaultInfo].default_runfiles.files,
ctx.attr._make[DefaultInfo].default_runfiles.symlinks,
],
)
def yosys_inputs(ctx):
return depset(
[
ctx.executable._abc,
ctx.executable._yosys,
ctx.executable._make,
ctx.file._makefile_yosys,
] +
ctx.files._tcl,
transitive = [
ctx.attr._abc[DefaultInfo].default_runfiles.files,
ctx.attr._abc[DefaultInfo].default_runfiles.symlinks,
ctx.attr._yosys[DefaultInfo].default_runfiles.files,
ctx.attr._yosys[DefaultInfo].default_runfiles.symlinks,
ctx.attr._makefile_yosys[DefaultInfo].default_runfiles.files,
ctx.attr._makefile_yosys[DefaultInfo].default_runfiles.symlinks,
ctx.attr._make[DefaultInfo].default_runfiles.files,
ctx.attr._make[DefaultInfo].default_runfiles.symlinks,
],
)
def data_inputs(ctx):
return depset(
ctx.files.data,
transitive = [datum.default_runfiles.files for datum in ctx.attr.data] +
[datum.default_runfiles.symlinks for datum in ctx.attr.data],
)
def source_inputs(ctx):
return depset(
ctx.files.src,
transitive = [
ctx.attr.src[OrfsInfo].additional_gds,
ctx.attr.src[OrfsInfo].additional_lefs,
ctx.attr.src[OrfsInfo].additional_libs,
ctx.attr.src[PdkInfo].files,
],
)
def rename_inputs(ctx):
return depset(transitive = [
target.files
for target in ctx.attr.renamed_inputs.values()
])
def pdk_inputs(ctx):
return depset(transitive = [ctx.attr.pdk[PdkInfo].files])
def deps_inputs(ctx):
return depset(
[dep[OrfsInfo].gds for dep in ctx.attr.deps if dep[OrfsInfo].gds] +
[dep[OrfsInfo].lef for dep in ctx.attr.deps if dep[OrfsInfo].lef] +
[dep[OrfsInfo].lib for dep in ctx.attr.deps if dep[OrfsInfo].lib],
)
def commonprefix(*args):
"""
Return the longest path prefix.
Return the longest path prefix (taken character-by-character)
that is a prefix of all paths in `*args`. If `*args` is empty,
return the empty string ('').
Args:
*args: Sequence of strings.
Returns:
Longest common prefix of each string in `*args`.
"""
prefix = ""
for t in zip(*args):
for x in t:
if x != t[0]:
return prefix
prefix += t[0]
return prefix
def commonpath(files):
"""
Return the longest common sub-path of each file in the sequence `files`.
Args:
files: Sequence of files.
Returns:
Longest common sub-path of each file in the sequence `files`.
"""
prefix = commonprefix(*[f.path.elems() for f in files])
path, _, _ = prefix.rpartition("/")
return path
def flow_substitutions(ctx):
return {
"${DLN_LIBRARY_PATH}": commonpath(ctx.files._ruby_dynamic),
"${FLOW_HOME}": ctx.file._makefile.dirname,
"${GIO_MODULE_DIR}": commonpath(ctx.files._gio_modules),
"${KLAYOUT_PATH}": ctx.executable._klayout.path,
"${LIBGL_DRIVERS_PATH}": commonpath(ctx.files._opengl),
"${MAKEFILE_PATH}": ctx.file._makefile.path,
"${MAKE_PATH}": ctx.executable._make.path,
"${OPENROAD_PATH}": ctx.executable._openroad.path,
"${OPENSTA_PATH}": ctx.executable._opensta.path,
"${QT_PLUGIN_PATH}": commonpath(ctx.files._qt_plugins),
"${RUBY_PATH}": commonpath(ctx.files._ruby),
"${STDBUF_PATH}": "",
"${TCL_LIBRARY}": commonpath(ctx.files._tcl),
}
def yosys_substitutions(ctx):
return {
"${ABC}": ctx.executable._abc.path,
"${YOSYS_PATH}": ctx.executable._yosys.path,
}
def _deps_impl(ctx):
exe = _declare_artifact(ctx, "results", ctx.attr.name + ".sh")
ctx.actions.expand_template(
template = ctx.file._deploy_template,
output = exe,
substitutions = {
"${GENFILES}": " ".join(sorted([f.short_path for f in ctx.attr.src[OrfsDepInfo].files.to_list()])),
"${RENAMES}": " ".join(["{}:{}".format(rename.src, rename.dst) for rename in ctx.attr.src[OrfsDepInfo].renames]),
"${CONFIG}": ctx.attr.src[OrfsDepInfo].config.short_path,
"${MAKE}": ctx.attr.src[OrfsDepInfo].make.short_path,
},
)
return [
ctx.attr.src[OrfsInfo],
ctx.attr.src[PdkInfo],
ctx.attr.src[TopInfo],
DefaultInfo(
executable = exe,
files = ctx.attr.src[OrfsDepInfo].files,
runfiles = ctx.attr.src[OrfsDepInfo].runfiles,
),
]
def flow_provides():
return [DefaultInfo, OutputGroupInfo, OrfsDepInfo, OrfsInfo, LoggingInfo, PdkInfo, TopInfo]
def orfs_attrs():
return {
"arguments": attr.string_dict(
doc = "Dictionary of additional flow arguments.",
default = {},
),
"extra_configs": attr.label_list(
doc = "List of additional flow configuration files.",
allow_files = True,
default = [],
),
"data": attr.label_list(
doc = "List of additional flow data.",
allow_files = True,
default = [],
),
"variant": attr.string(
doc = "Variant of the used flow.",
default = "base",
),
"_make": attr.label(
doc = "make binary.",
executable = True,
allow_files = True,
cfg = "exec",
default = Label("@docker_orfs//:make"),
),
"_tcl": attr.label(
doc = "Tcl library.",
allow_files = True,
default = Label("@docker_orfs//:tcl8.6"),
),
"_makefile": attr.label(
doc = "Top level makefile.",
allow_single_file = ["Makefile"],
default = Label("@docker_orfs//:makefile"),
),
}
def flow_attrs():
return {
"_deploy_template": attr.label(
default = ":deploy.tpl",
allow_single_file = True,
),
"_make_template": attr.label(
default = ":make.tpl",
allow_single_file = True,
),
"_openroad": attr.label(
doc = "OpenROAD binary.",
executable = True,
allow_files = True,
cfg = "exec",
default = Label("@docker_orfs//:openroad"),
),
"_opensta": attr.label(
doc = "OpenSTA binary.",
executable = True,
allow_files = True,
cfg = "exec",
default = Label("@docker_orfs//:sta"),
),
"_klayout": attr.label(
doc = "Klayout binary.",
executable = True,
allow_files = True,
cfg = "exec",
default = Label("@docker_orfs//:klayout"),
),
"_ruby": attr.label(
doc = "Ruby library.",
allow_files = True,
default = Label("@docker_orfs//:ruby3.0.0"),
),
"_ruby_dynamic": attr.label(
doc = "Ruby dynamic library.",
allow_files = True,
default = Label("@docker_orfs//:ruby_dynamic3.0.0"),
),
"_opengl": attr.label(
doc = "OpenGL drivers.",
allow_files = True,
default = Label("@docker_orfs//:opengl"),
),
"_qt_plugins": attr.label(
doc = "Qt plugins.",
allow_files = True,
default = Label("@docker_orfs//:qt_plugins"),
),
"_gio_modules": attr.label(
doc = "GIO modules.",
allow_files = True,
default = Label("@docker_orfs//:gio_modules"),
),
} | orfs_attrs()
def yosys_only_attrs():
return {
"_abc": attr.label(
doc = "Abc binary.",
executable = True,
allow_files = True,
cfg = "exec",
default = Label("@docker_orfs//:yosys-abc"),
),
"_yosys": attr.label(
doc = "Yosys binary.",
executable = True,
allow_files = True,
cfg = "exec",
default = Label("@docker_orfs//:yosys"),
),
"_makefile_yosys": attr.label(
doc = "Top level makefile yosys.",
allow_single_file = ["Makefile"],
default = Label("@docker_orfs//:makefile_yosys"),
),
}
def renamed_inputs_attr():
return {
"renamed_inputs": attr.string_keyed_label_dict(
default = {},
),
}
def synth_attrs():
return {
"verilog_files": attr.label_list(
allow_files = [
".v",
".sv",
],
allow_rules = [
],
providers = [DefaultInfo],
),
"deps": attr.label_list(
default = [],
providers = [OrfsInfo, TopInfo],
),
"module_top": attr.string(mandatory = True),
"pdk": attr.label(
doc = "Process design kit.",
default = Label("@docker_orfs//:asap7"),
providers = [PdkInfo],
),
}
def openroad_only_attrs():
return {
"src": attr.label(
providers = [DefaultInfo],
),
}
def yosys_attrs():
# flow_attrs() is not used by synthesis, but by bazel run foo_synth to
# open synthesis results in OpenROAD
return flow_attrs() | yosys_only_attrs()
def openroad_attrs():
return flow_attrs() | openroad_only_attrs()
def _module_top(ctx):
return ctx.attr.module_top if hasattr(ctx.attr, "module_top") else ctx.attr.src[TopInfo].module_top
def _platform(ctx):
return ctx.attr.pdk[PdkInfo].name if hasattr(ctx.attr, "pdk") else ctx.attr.src[PdkInfo].name
def _required_arguments(ctx):
return {
"PLATFORM": _platform(ctx),
"DESIGN_NAME": _module_top(ctx),
"FLOW_VARIANT": ctx.attr.variant,
"GENERATE_ARTIFACTS_ON_FAILURE": "1",
}
def _orfs_arguments(*args, short = False):
gds = depset([info.gds for info in args if info.gds], transitive = [info.additional_gds for info in args])
lefs = depset([info.lef for info in args if info.lef], transitive = [info.additional_lefs for info in args])
libs = depset([info.lib for info in args if info.lib], transitive = [info.additional_libs for info in args])
arguments = {}
if gds.to_list():
arguments["ADDITIONAL_GDS"] = " ".join(sorted([file.short_path if short else file.path for file in gds.to_list()]))
if lefs.to_list():
arguments["ADDITIONAL_LEFS"] = " ".join(sorted([file.short_path if short else file.path for file in lefs.to_list()]))
if libs.to_list():
arguments["ADDITIONAL_LIBS"] = " ".join(sorted([file.short_path if short else file.path for file in libs.to_list()]))
return arguments
def _verilog_arguments(files, short = False):
return {"VERILOG_FILES": " ".join(sorted([file.short_path if short else file.path for file in files]))}
def _config_content(arguments, paths):
return ("".join(sorted(["export {}?={}\n".format(*pair) for pair in arguments.items()]) +
["include {}\n".format(path) for path in paths]))
def _hack_away_prefix(arguments, prefix):
return {k: v.removeprefix(prefix + "/") for k, v in arguments.items()}
def _data_arguments(ctx):
return {k: ctx.expand_location(v, ctx.attr.data) for k, v in ctx.attr.arguments.items()}
def _generation_commands(optional_files):
if optional_files:
return [
"mkdir -p " + " ".join(sorted([result.dirname for result in optional_files])),
"touch " + " ".join(sorted([result.path for result in optional_files])),
]
return []
def _input_commands(renames):
cmds = []
for rename in renames:
cmds.extend(_mv_cmds(rename.src, rename.dst))
return cmds
def _mv_cmds(src, dst):
dir, _, _ = dst.rpartition("/")
return [
"mkdir -p {}".format(dir),
"mv {} {}".format(src, dst),
]
def _remap(s, a, b):
if s.endswith(a):
return s.replace("/" + a, "/" + b)
return s.replace("/" + a + "/", "/" + b + "/")
def _renames(ctx, inputs, short = False):
"""Move inputs to the expected input locations"""
renames = []
for file in inputs:
if ctx.attr.src[OrfsInfo].variant != ctx.attr.variant:
renames.append(struct(
src = file.short_path if short else file.path,
dst = _remap(file.short_path if short else file.path, ctx.attr.src[OrfsInfo].variant, ctx.attr.variant),
))
# renamed_inputs win over variant renaming
for file in inputs:
if file.basename in ctx.attr.renamed_inputs:
for src in ctx.attr.renamed_inputs[file.basename].files.to_list():
renames.append(struct(
src = src.short_path if short else src.path,
dst = _remap(file.short_path if short else file.path, ctx.attr.src[OrfsInfo].variant, ctx.attr.variant),
))
return renames
def _work_home(ctx):
if ctx.label.package:
return "/".join([ctx.genfiles_dir.path, ctx.label.package])
return ctx.genfiles_dir.path
def _artifact_name(ctx, category, name = None):
return "/".join([
category,
_platform(ctx),
_module_top(ctx),
ctx.attr.variant,
name,
])
def _declare_artifact(ctx, category, name):
return ctx.actions.declare_file(_artifact_name(ctx, category, name))
def _run_impl(ctx):
config = ctx.attr.src[OrfsInfo].config
outs = []
for k in dir(ctx.outputs):
outs.extend(getattr(ctx.outputs, k))
ctx.actions.run_shell(
arguments = [
"--file",
ctx.file._makefile.path,
"run",
],
command = " ".join([
ctx.executable._make.path,
"$@",
ctx.expand_location(ctx.attr.extra_args, ctx.attr.data),
]),
env = odb_environment(ctx) |
flow_environment(ctx) |
config_environment(config) |
_data_arguments(ctx) |
{"RUN_SCRIPT": ctx.file.script.path},
inputs = depset(
[config, ctx.file.script],
transitive = [
data_inputs(ctx),
flow_inputs(ctx),
source_inputs(ctx),
],
),
outputs = outs,
)
return [
DefaultInfo(
files = depset(outs),
),
OutputGroupInfo(
**{f.basename: depset([f]) for f in outs}
),
]
orfs_run = rule(
implementation = _run_impl,
attrs = openroad_attrs() | {
"script": attr.label(
mandatory = True,
allow_single_file = ["tcl"],
),
"outs": attr.output_list(
mandatory = True,
allow_empty = False,
),
"extra_args": attr.string(
mandatory = False,
default = "",
),
},
)
CANON_OUTPUT = "1_synth.rtlil"
SYNTH_OUTPUTS = ["1_synth.v", "1_synth.sdc", "mem.json"]
def _yosys_impl(ctx):
all_arguments = _data_arguments(ctx) | _required_arguments(ctx) | _orfs_arguments(*[dep[OrfsInfo] for dep in ctx.attr.deps])
config = _declare_artifact(ctx, "results", "1_synth.mk")
ctx.actions.write(
output = config,
content = _config_content(all_arguments, [file.path for file in ctx.files.extra_configs]),
)
canon_logs = []
for log in ["1_1_yosys_canonicalize.log"]:
canon_logs.append(_declare_artifact(ctx, "logs", log))
canon_output = _declare_artifact(ctx, "results", CANON_OUTPUT)
commands = _generation_commands(canon_logs) + [ctx.executable._make.path + " $@"]
ctx.actions.run_shell(
arguments = ["--file", ctx.file._makefile_yosys.path, canon_output.path],
command = " && ".join(commands),
env = _verilog_arguments(ctx.files.verilog_files) |
yosys_environment(ctx) |
config_environment(config),
inputs = depset(
[config] +
ctx.files.verilog_files +
ctx.files.extra_configs,
transitive = [
yosys_inputs(ctx),
data_inputs(ctx),
pdk_inputs(ctx),
deps_inputs(ctx),
],
),
outputs = [canon_output] + canon_logs,
)
synth_logs = []
for log in ["1_1_yosys.log", "1_1_yosys_metrics.log", "1_1_yosys_hier_report.log"]:
synth_logs.append(_declare_artifact(ctx, "logs", log))
synth_outputs = []
for output in SYNTH_OUTPUTS:
synth_outputs.append(_declare_artifact(ctx, "results", output))
synth_outputs.append(_declare_artifact(ctx, "objects", "lib/merged.lib"))
commands = _generation_commands(synth_logs) + [ctx.executable._make.path + " $@"]
ctx.actions.run_shell(
arguments = [
"--file",
ctx.file._makefile_yosys.path,
"--old-file",
canon_output.path,
"yosys-dependencies",
"do-yosys-stats",
"do-yosys",
"do-synth",
],
command = " && ".join(commands),
env = _verilog_arguments([]) |
yosys_environment(ctx) |
config_environment(config),
inputs = depset(
[canon_output, config] +
ctx.files.extra_configs,
transitive = [
yosys_inputs(ctx),
data_inputs(ctx),
pdk_inputs(ctx),
deps_inputs(ctx),
],
),
outputs = synth_outputs + synth_logs,
)
# Dummy action to make sure that the flow environment is available to
# bazel run foo_synth without causing resynthesis when upgrading
# ORFS where yosys did not change, saves many hours of synthesis
dummy_output = _declare_artifact(ctx, "results", "dummy.txt")
ctx.actions.run_shell(
command = "touch " + dummy_output.path,
env = flow_environment(ctx),
inputs = depset(
transitive = [
flow_inputs(ctx),
],
),
outputs = [dummy_output],
)
outputs = [canon_output] + synth_outputs + [dummy_output]
config_short = _declare_artifact(ctx, "results", "1_synth.short.mk")
ctx.actions.write(
output = config_short,
content = _config_content(
arguments = _hack_away_prefix(
arguments = _data_arguments(ctx) | _required_arguments(ctx) | _orfs_arguments(short = True, *[dep[OrfsInfo] for dep in ctx.attr.deps]) | _verilog_arguments(ctx.files.verilog_files, short = True),
prefix = config_short.root.path,
),
paths = [file.short_path for file in ctx.files.extra_configs],
),
)
make = ctx.actions.declare_file("make_1_synth")
ctx.actions.expand_template(
template = ctx.file._make_template,
output = make,
substitutions = flow_substitutions(ctx) | yosys_substitutions(ctx) | {'"$@"': 'WORK_HOME="./{}" DESIGN_CONFIG="config.mk" "$@"'.format(ctx.label.package)},
)
exe = ctx.actions.declare_file(ctx.attr.name + ".sh")
ctx.actions.expand_template(
template = ctx.file._deploy_template,
output = exe,
substitutions = {
"${GENFILES}": " ".join(sorted([f.short_path for f in [config_short] + outputs + canon_logs + synth_logs])),
"${CONFIG}": config_short.short_path,
"${MAKE}": make.short_path,
},
)
return [
DefaultInfo(
executable = exe,
files = depset(outputs),
runfiles = ctx.runfiles(
[config_short, make] + outputs + canon_logs + synth_logs +
ctx.files.extra_configs,
transitive_files = depset(transitive = [
flow_inputs(ctx),
deps_inputs(ctx),
pdk_inputs(ctx),
]),
),
),
OutputGroupInfo(
logs = depset(canon_logs + synth_logs),
reports = depset([]),
**{f.basename: depset([f]) for f in [config] + outputs}
),
OrfsDepInfo(
make = make,
config = config_short,
renames = [],
files = depset([config_short] + ctx.files.verilog_files + ctx.files.extra_configs),
runfiles = ctx.runfiles(transitive_files = depset(
[config_short, make] +
ctx.files.verilog_files + ctx.files.extra_configs,
transitive = [
flow_inputs(ctx),
yosys_inputs(ctx),
data_inputs(ctx),
pdk_inputs(ctx),
deps_inputs(ctx),
],
)),
),
OrfsInfo(
stage = "1_synth",
config = config,
variant = ctx.attr.variant,
odb = None,
gds = None,
lef = None,
lib = None,
additional_gds = depset([dep[OrfsInfo].gds for dep in ctx.attr.deps if dep[OrfsInfo].gds]),
additional_lefs = depset([dep[OrfsInfo].lef for dep in ctx.attr.deps if dep[OrfsInfo].lef]),
additional_libs = depset([dep[OrfsInfo].lib for dep in ctx.attr.deps if dep[OrfsInfo].lib]),
),
ctx.attr.pdk[PdkInfo],
TopInfo(
module_top = ctx.attr.module_top,
),
LoggingInfo(
logs = depset(canon_logs + synth_logs),
reports = depset([]),
),
]
orfs_synth = rule(
implementation = _yosys_impl,
attrs = yosys_attrs() | synth_attrs(),
provides = [DefaultInfo, OutputGroupInfo, OrfsDepInfo, OrfsInfo, PdkInfo, TopInfo, LoggingInfo],
executable = True,
)
def _make_impl(ctx, stage, steps, forwarded_names = [], result_names = [], object_names = [], log_names = [], report_names = [], extra_arguments = {}):
"""
Implementation function for the OpenROAD-flow-scripts stages.
Args:
ctx: The context object.
stage: The stage name.
steps: Makefile targets to run.
forwarded_names: The names of files to be forwarded from `src`.
result_names: The names of the result files.
object_names: The names of the object files.
log_names: The names of the log files.
report_names: The names of the report files.
extra_arguments: Extra arguments to add to the configuration.
Returns:
A list of providers. The returned PdkInfo and TopInfo providers are taken from the first
target of a ctx.attr.srcs list.
"""
all_arguments = extra_arguments | _data_arguments(ctx) | _required_arguments(ctx) | _orfs_arguments(ctx.attr.src[OrfsInfo])
config = _declare_artifact(ctx, "results", stage + ".mk")
ctx.actions.write(
output = config,
content = _config_content(
arguments = all_arguments,
paths = [file.path for file in ctx.files.extra_configs],
),
)
results = []
for result in result_names:
results.append(_declare_artifact(ctx, "results", result))
objects = []
for object in object_names:
objects.append(_declare_artifact(ctx, "objects", object))
logs = []
for log in log_names:
logs.append(_declare_artifact(ctx, "logs", log))
reports = []
for report in report_names:
reports.append(_declare_artifact(ctx, "reports", report))
forwards = [f for f in ctx.files.src if f.basename in forwarded_names]
info = {}
for file in forwards + results:
info[file.extension] = file
commands = _generation_commands(reports + logs) + _input_commands(_renames(ctx, ctx.files.src)) + [ctx.executable._make.path + " $@"]
ctx.actions.run_shell(
arguments = ["--file", ctx.file._makefile.path] + steps,
command = " && ".join(commands),
env = flow_environment(ctx) | config_environment(config),
inputs = depset(
[config] +
ctx.files.extra_configs,
transitive = [
flow_inputs(ctx),
data_inputs(ctx),
source_inputs(ctx),
rename_inputs(ctx),
],
),
outputs = results + objects + logs + reports,
)
config_short = _declare_artifact(ctx, "results", stage + ".short.mk")
ctx.actions.write(
output = config_short,
content = _config_content(
arguments = _hack_away_prefix(
arguments = extra_arguments | _data_arguments(ctx) | _required_arguments(ctx) | _orfs_arguments(ctx.attr.src[OrfsInfo], short = True),
prefix = config_short.root.path,
),
paths = [file.short_path for file in ctx.files.extra_configs],
),
)
make = ctx.actions.declare_file("make_{}_{}_{}".format(ctx.attr.name, ctx.attr.variant, stage))
ctx.actions.expand_template(
template = ctx.file._make_template,
output = make,
substitutions = flow_substitutions(ctx) | {'"$@"': 'WORK_HOME="./{}" DESIGN_CONFIG="config.mk" "$@"'.format(ctx.label.package)},
)
exe = ctx.actions.declare_file(ctx.attr.name + ".sh")
ctx.actions.expand_template(
template = ctx.file._deploy_template,
output = exe,
substitutions = {
"${GENFILES}": " ".join(sorted([f.short_path for f in [config_short] + results + logs + reports])),
"${CONFIG}": config_short.short_path,
"${MAKE}": make.short_path,
},
)
return [
DefaultInfo(
executable = exe,
files = depset(forwards + results + reports),
runfiles = ctx.runfiles(
[config_short, make] +
forwards + results + logs + reports + ctx.files.extra_configs,
transitive_files = depset(transitive = [
flow_inputs(ctx),
ctx.attr.src[PdkInfo].files,
ctx.attr.src[OrfsInfo].additional_gds,
ctx.attr.src[OrfsInfo].additional_lefs,
ctx.attr.src[OrfsInfo].additional_libs,
]),
),
),
OutputGroupInfo(
logs = depset(logs),
reports = depset(reports),
**{f.basename: depset([f]) for f in [config] + results + objects + logs + reports}
),
OrfsDepInfo(
make = make,
config = config_short,
renames = _renames(ctx, ctx.files.src, short = True),
files = depset([config_short] + ctx.files.src + ctx.files.data + ctx.files.extra_configs),
runfiles = ctx.runfiles(transitive_files = depset(
[config_short, make] +
ctx.files.src + ctx.files.extra_configs,
transitive = [
flow_inputs(ctx),
data_inputs(ctx),
source_inputs(ctx),
rename_inputs(ctx),
],
)),
),
OrfsInfo(
stage = stage,
config = config,
variant = ctx.attr.variant,
odb = info.get("odb"),
gds = info.get("gds"),
lef = info.get("lef"),
lib = info.get("lib"),
additional_gds = ctx.attr.src[OrfsInfo].additional_gds,