librelane 2.4.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of librelane might be problematic. Click here for more details.

Files changed (166) hide show
  1. librelane/__init__.py +38 -0
  2. librelane/__main__.py +470 -0
  3. librelane/__version__.py +43 -0
  4. librelane/common/__init__.py +61 -0
  5. librelane/common/cli.py +75 -0
  6. librelane/common/drc.py +245 -0
  7. librelane/common/generic_dict.py +319 -0
  8. librelane/common/metrics/__init__.py +35 -0
  9. librelane/common/metrics/__main__.py +413 -0
  10. librelane/common/metrics/library.py +354 -0
  11. librelane/common/metrics/metric.py +186 -0
  12. librelane/common/metrics/util.py +279 -0
  13. librelane/common/misc.py +402 -0
  14. librelane/common/ring_buffer.py +63 -0
  15. librelane/common/tcl.py +80 -0
  16. librelane/common/toolbox.py +549 -0
  17. librelane/common/tpe.py +41 -0
  18. librelane/common/types.py +117 -0
  19. librelane/config/__init__.py +32 -0
  20. librelane/config/__main__.py +158 -0
  21. librelane/config/config.py +1025 -0
  22. librelane/config/flow.py +490 -0
  23. librelane/config/pdk_compat.py +255 -0
  24. librelane/config/preprocessor.py +464 -0
  25. librelane/config/removals.py +45 -0
  26. librelane/config/variable.py +722 -0
  27. librelane/container.py +264 -0
  28. librelane/env_info.py +306 -0
  29. librelane/examples/spm/config.yaml +33 -0
  30. librelane/examples/spm/pin_order.cfg +14 -0
  31. librelane/examples/spm/src/impl.sdc +73 -0
  32. librelane/examples/spm/src/signoff.sdc +68 -0
  33. librelane/examples/spm/src/spm.v +73 -0
  34. librelane/examples/spm/verify/spm_tb.v +106 -0
  35. librelane/examples/spm-user_project_wrapper/SPM_example.v +286 -0
  36. librelane/examples/spm-user_project_wrapper/base_sdc_file.sdc +145 -0
  37. librelane/examples/spm-user_project_wrapper/config-tut.json +12 -0
  38. librelane/examples/spm-user_project_wrapper/config.json +13 -0
  39. librelane/examples/spm-user_project_wrapper/defines.v +66 -0
  40. librelane/examples/spm-user_project_wrapper/template.def +7656 -0
  41. librelane/examples/spm-user_project_wrapper/user_project_wrapper.v +123 -0
  42. librelane/flows/__init__.py +24 -0
  43. librelane/flows/builtins.py +18 -0
  44. librelane/flows/classic.py +330 -0
  45. librelane/flows/cli.py +463 -0
  46. librelane/flows/flow.py +985 -0
  47. librelane/flows/misc.py +71 -0
  48. librelane/flows/optimizing.py +179 -0
  49. librelane/flows/sequential.py +367 -0
  50. librelane/flows/synth_explore.py +173 -0
  51. librelane/logging/__init__.py +40 -0
  52. librelane/logging/logger.py +323 -0
  53. librelane/open_pdks_rev +1 -0
  54. librelane/plugins.py +21 -0
  55. librelane/py.typed +0 -0
  56. librelane/scripts/base.sdc +80 -0
  57. librelane/scripts/klayout/Readme.md +2 -0
  58. librelane/scripts/klayout/open_design.py +63 -0
  59. librelane/scripts/klayout/render.py +121 -0
  60. librelane/scripts/klayout/stream_out.py +176 -0
  61. librelane/scripts/klayout/xml_drc_report_to_json.py +45 -0
  62. librelane/scripts/klayout/xor.drc +120 -0
  63. librelane/scripts/magic/Readme.md +1 -0
  64. librelane/scripts/magic/common/read.tcl +114 -0
  65. librelane/scripts/magic/def/antenna_check.tcl +35 -0
  66. librelane/scripts/magic/def/mag.tcl +19 -0
  67. librelane/scripts/magic/def/mag_gds.tcl +81 -0
  68. librelane/scripts/magic/drc.tcl +79 -0
  69. librelane/scripts/magic/extract_spice.tcl +98 -0
  70. librelane/scripts/magic/gds/drc_batch.tcl +74 -0
  71. librelane/scripts/magic/gds/erase_box.tcl +32 -0
  72. librelane/scripts/magic/gds/extras_mag.tcl +47 -0
  73. librelane/scripts/magic/gds/mag_with_pointers.tcl +32 -0
  74. librelane/scripts/magic/get_bbox.tcl +11 -0
  75. librelane/scripts/magic/lef/extras_maglef.tcl +63 -0
  76. librelane/scripts/magic/lef/maglef.tcl +27 -0
  77. librelane/scripts/magic/lef.tcl +57 -0
  78. librelane/scripts/magic/open.tcl +28 -0
  79. librelane/scripts/magic/wrapper.tcl +19 -0
  80. librelane/scripts/netgen/setup.tcl +28 -0
  81. librelane/scripts/odbpy/apply_def_template.py +49 -0
  82. librelane/scripts/odbpy/cell_frequency.py +107 -0
  83. librelane/scripts/odbpy/check_antenna_properties.py +116 -0
  84. librelane/scripts/odbpy/contextualize.py +109 -0
  85. librelane/scripts/odbpy/defutil.py +574 -0
  86. librelane/scripts/odbpy/diodes.py +373 -0
  87. librelane/scripts/odbpy/disconnected_pins.py +305 -0
  88. librelane/scripts/odbpy/exception_codes.py +17 -0
  89. librelane/scripts/odbpy/filter_unannotated.py +100 -0
  90. librelane/scripts/odbpy/io_place.py +482 -0
  91. librelane/scripts/odbpy/label_macro_pins.py +277 -0
  92. librelane/scripts/odbpy/lefutil.py +97 -0
  93. librelane/scripts/odbpy/placers.py +162 -0
  94. librelane/scripts/odbpy/power_utils.py +395 -0
  95. librelane/scripts/odbpy/random_place.py +57 -0
  96. librelane/scripts/odbpy/reader.py +246 -0
  97. librelane/scripts/odbpy/remove_buffers.py +173 -0
  98. librelane/scripts/odbpy/snap_to_grid.py +57 -0
  99. librelane/scripts/odbpy/wire_lengths.py +93 -0
  100. librelane/scripts/openroad/antenna_check.tcl +20 -0
  101. librelane/scripts/openroad/antenna_repair.tcl +31 -0
  102. librelane/scripts/openroad/basic_mp.tcl +24 -0
  103. librelane/scripts/openroad/buffer_list.tcl +10 -0
  104. librelane/scripts/openroad/common/dpl.tcl +24 -0
  105. librelane/scripts/openroad/common/dpl_cell_pad.tcl +26 -0
  106. librelane/scripts/openroad/common/grt.tcl +32 -0
  107. librelane/scripts/openroad/common/io.tcl +476 -0
  108. librelane/scripts/openroad/common/pdn_cfg.tcl +135 -0
  109. librelane/scripts/openroad/common/resizer.tcl +103 -0
  110. librelane/scripts/openroad/common/set_global_connections.tcl +78 -0
  111. librelane/scripts/openroad/common/set_layer_adjustments.tcl +31 -0
  112. librelane/scripts/openroad/common/set_power_nets.tcl +30 -0
  113. librelane/scripts/openroad/common/set_rc.tcl +75 -0
  114. librelane/scripts/openroad/common/set_routing_layers.tcl +30 -0
  115. librelane/scripts/openroad/cts.tcl +80 -0
  116. librelane/scripts/openroad/cut_rows.tcl +24 -0
  117. librelane/scripts/openroad/dpl.tcl +24 -0
  118. librelane/scripts/openroad/drt.tcl +37 -0
  119. librelane/scripts/openroad/fill.tcl +30 -0
  120. librelane/scripts/openroad/floorplan.tcl +145 -0
  121. librelane/scripts/openroad/gpl.tcl +88 -0
  122. librelane/scripts/openroad/grt.tcl +30 -0
  123. librelane/scripts/openroad/gui.tcl +15 -0
  124. librelane/scripts/openroad/insert_buffer.tcl +127 -0
  125. librelane/scripts/openroad/ioplacer.tcl +67 -0
  126. librelane/scripts/openroad/irdrop.tcl +51 -0
  127. librelane/scripts/openroad/pdn.tcl +52 -0
  128. librelane/scripts/openroad/rcx.tcl +32 -0
  129. librelane/scripts/openroad/repair_design.tcl +70 -0
  130. librelane/scripts/openroad/repair_design_postgrt.tcl +48 -0
  131. librelane/scripts/openroad/rsz_timing_postcts.tcl +68 -0
  132. librelane/scripts/openroad/rsz_timing_postgrt.tcl +70 -0
  133. librelane/scripts/openroad/sta/check_macro_instances.tcl +53 -0
  134. librelane/scripts/openroad/sta/corner.tcl +393 -0
  135. librelane/scripts/openroad/tapcell.tcl +25 -0
  136. librelane/scripts/openroad/write_views.tcl +27 -0
  137. librelane/scripts/pyosys/construct_abc_script.py +177 -0
  138. librelane/scripts/pyosys/json_header.py +84 -0
  139. librelane/scripts/pyosys/synthesize.py +493 -0
  140. librelane/scripts/pyosys/ys_common.py +153 -0
  141. librelane/scripts/tclsh/hello.tcl +1 -0
  142. librelane/state/__init__.py +24 -0
  143. librelane/state/__main__.py +61 -0
  144. librelane/state/design_format.py +180 -0
  145. librelane/state/state.py +351 -0
  146. librelane/steps/__init__.py +61 -0
  147. librelane/steps/__main__.py +511 -0
  148. librelane/steps/checker.py +637 -0
  149. librelane/steps/common_variables.py +340 -0
  150. librelane/steps/cvc_rv.py +169 -0
  151. librelane/steps/klayout.py +509 -0
  152. librelane/steps/magic.py +566 -0
  153. librelane/steps/misc.py +160 -0
  154. librelane/steps/netgen.py +253 -0
  155. librelane/steps/odb.py +955 -0
  156. librelane/steps/openroad.py +2433 -0
  157. librelane/steps/openroad_alerts.py +102 -0
  158. librelane/steps/pyosys.py +629 -0
  159. librelane/steps/step.py +1547 -0
  160. librelane/steps/tclstep.py +288 -0
  161. librelane/steps/verilator.py +222 -0
  162. librelane/steps/yosys.py +371 -0
  163. librelane-2.4.0.dev0.dist-info/METADATA +151 -0
  164. librelane-2.4.0.dev0.dist-info/RECORD +166 -0
  165. librelane-2.4.0.dev0.dist-info/WHEEL +4 -0
  166. librelane-2.4.0.dev0.dist-info/entry_points.txt +8 -0
@@ -0,0 +1,61 @@
1
+ # Copyright 2023 Efabless Corporation
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import sys
15
+ import json
16
+ from typing import Optional
17
+
18
+ import cloup
19
+
20
+ from ..common import get_latest_file
21
+ from ..common.cli import formatter_settings
22
+
23
+
24
+ @cloup.group(
25
+ no_args_is_help=True,
26
+ formatter_settings=formatter_settings,
27
+ )
28
+ def cli():
29
+ pass
30
+
31
+
32
+ @cloup.command()
33
+ @cloup.option(
34
+ "--extract-metrics-to",
35
+ default=None,
36
+ )
37
+ @cloup.argument("run_dir")
38
+ def latest(extract_metrics_to: Optional[str], run_dir: str):
39
+ exit_code = 0
40
+
41
+ if latest_state := get_latest_file(run_dir, "state_*.json"):
42
+ try:
43
+ state = json.load(open(latest_state, encoding="utf8"))
44
+ except json.JSONDecodeError as e:
45
+ print(f"Latest state at {latest_state} is invalid: {e}", file=sys.stderr)
46
+ exit(1)
47
+ metrics = state["metrics"]
48
+ print(latest_state, end="")
49
+ if output := extract_metrics_to:
50
+ json.dump(metrics, open(output, "w", encoding="utf8"))
51
+ else:
52
+ print("No state_*.json files found", file=sys.stderr)
53
+ exit_code = 1
54
+
55
+ exit(exit_code)
56
+
57
+
58
+ cli.add_command(latest)
59
+
60
+ if __name__ == "__main__":
61
+ cli()
@@ -0,0 +1,180 @@
1
+ # Copyright 2023 Efabless Corporation
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from enum import Enum
15
+ from dataclasses import dataclass
16
+ from typing import Dict, Optional
17
+
18
+
19
+ @dataclass
20
+ class DesignFormatObject:
21
+ """
22
+ Metadata about the various possible text or binary representations (views)
23
+ of any design.
24
+
25
+ For example, ``DesignFormat.NETLIST.value`` has the metadata for Netlist
26
+ views.
27
+
28
+ :param id: A lowercase alphanumeric identifier for the design format.
29
+ Some IDs in LibreLane 2.X use dashes. This is an inconsistency that will
30
+ be addressed in the next major version of LibreLane as it would be a
31
+ breaking change.
32
+ :param extension: The file extension for designs saved in this format.
33
+ :param name: A human-readable name for this design format.
34
+ :param folder_override: The subdirectory when
35
+ :meth:`librelane.state.State.save_snapshot` is called on a state. If
36
+ unset, the value for ``id`` will be used.
37
+ :param multiple: Whether this view may have multiple files (typically, files
38
+ that are different across multiple corners or similar.)
39
+ """
40
+
41
+ id: str
42
+ extension: str
43
+ name: str
44
+ folder_override: Optional[str] = None
45
+ multiple: bool = False
46
+
47
+ @property
48
+ def folder(self) -> str:
49
+ return self.folder_override or self.id
50
+
51
+
52
+ class DesignFormat(Enum):
53
+ """
54
+ An `enumeration <https://docs.python.org/3/library/enum.html>`_ of a number
55
+ of :class:`librelane.state.DesignFormatObject`\\s representing the various
56
+ possible text or binary representations (views) supported by LibreLane
57
+ states.
58
+
59
+ Members of this enumeration are used as the keys of
60
+ :class:`librelane.state.State` objects.
61
+ """
62
+
63
+ NETLIST: DesignFormatObject = DesignFormatObject(
64
+ "nl",
65
+ "nl.v",
66
+ "Verilog Netlist",
67
+ )
68
+ POWERED_NETLIST: DesignFormatObject = DesignFormatObject(
69
+ "pnl",
70
+ "pnl.v",
71
+ "Powered Verilog Netlist",
72
+ )
73
+ POWERED_NETLIST_SDF_FRIENDLY: DesignFormatObject = DesignFormatObject(
74
+ "pnl-sdf-friendly",
75
+ "pnl-sdf.v",
76
+ "Powered Verilog Netlist For SDF Simulation (Without Fill Cells)",
77
+ folder_override="pnl",
78
+ )
79
+ POWERED_NETLIST_NO_PHYSICAL_CELLS: DesignFormatObject = DesignFormatObject(
80
+ "pnl-npc",
81
+ "pnl-npc.v",
82
+ "Powered Verilog Netlist Without Physical Cells (Fill Cells and Diode Cells)",
83
+ folder_override="pnl",
84
+ )
85
+
86
+ DEF: DesignFormatObject = DesignFormatObject(
87
+ "def",
88
+ "def",
89
+ "Design Exchange Format",
90
+ )
91
+ LEF: DesignFormatObject = DesignFormatObject(
92
+ "lef",
93
+ "lef",
94
+ "Library Exchange Format",
95
+ )
96
+ OPENROAD_LEF: DesignFormatObject = DesignFormatObject(
97
+ "openroad-lef",
98
+ "openroad.lef",
99
+ "Library Exchange Format Generated by OpenROAD",
100
+ folder_override="lef",
101
+ )
102
+ ODB: DesignFormatObject = DesignFormatObject(
103
+ "odb",
104
+ "odb",
105
+ "OpenDB Database",
106
+ )
107
+
108
+ SDC: DesignFormatObject = DesignFormatObject(
109
+ "sdc",
110
+ "sdc",
111
+ "Design Constraints",
112
+ )
113
+ SDF: DesignFormatObject = DesignFormatObject(
114
+ "sdf",
115
+ "sdf",
116
+ "Standard Delay Format",
117
+ multiple=True,
118
+ )
119
+ SPEF: DesignFormatObject = DesignFormatObject(
120
+ "spef",
121
+ "spef",
122
+ "Standard Parasitics Extraction Format",
123
+ multiple=True, # nom, min, max, ...
124
+ )
125
+ LIB: DesignFormatObject = DesignFormatObject(
126
+ "lib",
127
+ "lib",
128
+ "LIB Timing Library Format",
129
+ multiple=True,
130
+ )
131
+ SPICE: DesignFormatObject = DesignFormatObject(
132
+ "spice",
133
+ "spice",
134
+ "Simulation Program with Integrated Circuit Emphasis",
135
+ )
136
+
137
+ MAG: DesignFormatObject = DesignFormatObject(
138
+ "mag",
139
+ "mag",
140
+ "Magic VLSI View",
141
+ )
142
+
143
+ GDS: DesignFormatObject = DesignFormatObject(
144
+ "gds",
145
+ "gds",
146
+ "GDSII Stream",
147
+ )
148
+ MAG_GDS: DesignFormatObject = DesignFormatObject(
149
+ "mag_gds",
150
+ "magic.gds",
151
+ "GDSII Stream (Magic)",
152
+ )
153
+ KLAYOUT_GDS: DesignFormatObject = DesignFormatObject(
154
+ "klayout_gds",
155
+ "klayout.gds",
156
+ "GDSII Stream (KLayout)",
157
+ )
158
+
159
+ JSON_HEADER: DesignFormatObject = DesignFormatObject(
160
+ "json_h",
161
+ "h.json",
162
+ "Design JSON Header File",
163
+ )
164
+ VERILOG_HEADER: DesignFormatObject = DesignFormatObject(
165
+ "vh",
166
+ "vh",
167
+ "Verilog Header",
168
+ )
169
+
170
+ def __str__(self) -> str:
171
+ return self.value.id
172
+
173
+ @staticmethod
174
+ def by_id(id: str) -> Optional["DesignFormat"]:
175
+ return _designformat_by_id.get(id)
176
+
177
+
178
+ _designformat_by_id: Dict[str, "DesignFormat"] = {
179
+ format.value.id: format for format in DesignFormat
180
+ }
@@ -0,0 +1,351 @@
1
+ # Copyright 2023 Efabless Corporation
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from __future__ import annotations
15
+
16
+ import os
17
+ import sys
18
+ import json
19
+ import shutil
20
+ from decimal import Decimal
21
+ from typing import Callable, List, Mapping, Tuple, Union, Optional, Dict, Any
22
+
23
+ from .design_format import (
24
+ DesignFormat,
25
+ DesignFormatObject,
26
+ )
27
+
28
+ from ..common import (
29
+ Path,
30
+ GenericImmutableDict,
31
+ mkdirp,
32
+ copy_recursive,
33
+ )
34
+ from ..logging import info
35
+
36
+
37
+ class InvalidState(RuntimeError):
38
+ pass
39
+
40
+
41
+ StateElement = Union[Path, List[Path], Dict[str, Union[Path, List[Path]]], None]
42
+
43
+
44
+ class State(GenericImmutableDict[str, StateElement]):
45
+ """
46
+ Basically, a dictionary from :class:`DesignFormat`\\s and values
47
+ of (nested dictionaries of) :class:`librelane.common.Path`\\.
48
+
49
+ The state is the only thing that can be altered by steps other than the
50
+ filesystem.
51
+
52
+ States are **immutable**. To construct a new state with some modifications,
53
+ you may do so as follows::
54
+
55
+ state_b = State(
56
+ copying=state_a,
57
+ overrides={
58
+
59
+ },
60
+ metrics=GenericImmutableDict(copying=state_a.metrics, overrides={
61
+
62
+ })
63
+ )
64
+
65
+ Though in the majority of cases, you do not have to construct States on your
66
+ own: after executing a Step, you only return your deltas and then the Flow
67
+ is responsible for the creation of a new Step object.
68
+
69
+ :param copying: A mutable or immutable mapping to use as the starting
70
+ value for this State.
71
+ :param overrides: A mutable or immutable mapping to override the starting
72
+ values with.
73
+ :param metrics: A dictionary that carries statistics about the design: area,
74
+ wire length, et cetera, but also miscellaneous data, for example, whether
75
+ it passed a certain check or not.
76
+ """
77
+
78
+ def __init__(
79
+ self,
80
+ copying: Optional[
81
+ Union[Mapping[str, StateElement], Mapping[DesignFormat, StateElement]]
82
+ ] = None,
83
+ *args,
84
+ overrides: Optional[
85
+ Union[Mapping[str, StateElement], Mapping[DesignFormat, StateElement]]
86
+ ] = None,
87
+ metrics: Optional[Mapping[str, Any]] = None,
88
+ **kwargs,
89
+ ) -> None:
90
+ copying_resolved: Dict[str, StateElement] = {}
91
+ if c_mapping := copying:
92
+ for key, value in c_mapping.items():
93
+ if isinstance(key, DesignFormat):
94
+ copying_resolved[key.value.id] = value
95
+ else:
96
+ copying_resolved[key] = value
97
+
98
+ for format in DesignFormat:
99
+ assert isinstance(format.value, DesignFormatObject) # type checker shut up
100
+ if format.value.id not in copying_resolved:
101
+ copying_resolved[format.value.id] = None
102
+
103
+ overrides_resolved = {}
104
+ if o_mapping := overrides:
105
+ for k, value in o_mapping.items():
106
+ if isinstance(k, DesignFormat):
107
+ assert isinstance(
108
+ k.value, DesignFormatObject
109
+ ) # type checker shut up
110
+ k = k.value.id
111
+ overrides_resolved[k] = value
112
+
113
+ self.metrics = GenericImmutableDict(metrics or {})
114
+
115
+ super().__init__(
116
+ copying_resolved,
117
+ *args,
118
+ overrides=overrides_resolved,
119
+ **kwargs,
120
+ )
121
+
122
+ def __getitem__(self, key: Union[DesignFormat, str]) -> StateElement:
123
+ if isinstance(key, DesignFormat):
124
+ id: str = key.value.id
125
+ key = id
126
+ return super().__getitem__(key)
127
+
128
+ def __setitem__(self, key: Union[DesignFormat, str], item: StateElement):
129
+ if isinstance(key, DesignFormat):
130
+ id: str = key.value.id
131
+ key = id
132
+ return super().__setitem__(key, item)
133
+
134
+ def __delitem__(self, key: Union[DesignFormat, str]):
135
+ if isinstance(key, DesignFormat):
136
+ id: str = key.value.id
137
+ key = id
138
+ return super().__delitem__(key)
139
+
140
+ def to_raw_dict(self, metrics: bool = True) -> Dict[str, Any]:
141
+ final = super().to_raw_dict()
142
+ if metrics:
143
+ final["metrics"] = self.metrics.to_raw_dict()
144
+ return final
145
+
146
+ def copy(self: "State") -> "State":
147
+ metrics: GenericImmutableDict[str, Any] = GenericImmutableDict(
148
+ copy_recursive(self.metrics)
149
+ )
150
+ new = State(self, metrics=metrics)
151
+ return new
152
+
153
+ def _walk(
154
+ self,
155
+ views: Union[Dict, "State"],
156
+ save_directory: Union[str, os.PathLike],
157
+ visit: Callable[[str, StateElement, str, str, int], StateElement],
158
+ key_path: str = "",
159
+ depth: int = 0,
160
+ top_key: Optional[str] = None,
161
+ ):
162
+ for key, value in views.items():
163
+ current_top_key = top_key
164
+ if current_top_key is None:
165
+ current_top_key = key
166
+ current_folder = key.strip("_*")
167
+ if df := DesignFormat.by_id(key):
168
+ # For type-checker: all guaranteed to be DesignFormatObjects
169
+ assert isinstance(df.value, DesignFormatObject)
170
+ current_folder = df.value.folder
171
+
172
+ target_dir = os.path.join(save_directory, current_folder)
173
+
174
+ current_key_path = f"{key_path}.{key}"
175
+ visit(current_key_path, value, current_top_key, target_dir, depth)
176
+ if isinstance(value, dict):
177
+ self._walk(
178
+ value,
179
+ target_dir,
180
+ visit,
181
+ current_key_path,
182
+ depth + 1,
183
+ current_top_key,
184
+ )
185
+ if isinstance(value, list):
186
+ for i, element in enumerate(value):
187
+ element_key_path = f"{current_key_path}[{i}]"
188
+ visit(
189
+ element_key_path,
190
+ element,
191
+ current_top_key,
192
+ target_dir,
193
+ depth + 1,
194
+ )
195
+
196
+ def save_snapshot(self, path: Union[str, os.PathLike]):
197
+ """
198
+ Validates the current state then saves all views to a folder by
199
+ design format, including the metrics.
200
+
201
+ :param path: The folder that would contain other folders.
202
+ """
203
+
204
+ def visitor(key, value, top_key, save_directory, depth):
205
+ if not isinstance(value, Path):
206
+ return
207
+ mkdirp(save_directory)
208
+ target_path = os.path.join(save_directory, os.path.basename(value))
209
+ shutil.copyfile(value, target_path, follow_symlinks=True)
210
+
211
+ self.validate()
212
+ info(f"Saving views to '{os.path.abspath(path)}'…")
213
+ mkdirp(path)
214
+ self._walk(self, path, visitor)
215
+ metrics_csv_path = os.path.join(path, "metrics.csv")
216
+ with open(metrics_csv_path, "w", encoding="utf8") as f:
217
+ f.write("Metric,Value\n")
218
+ for metric in self.metrics:
219
+ f.write(f"{metric},{self.metrics[metric]}\n")
220
+
221
+ metrics_json_path = os.path.join(path, "metrics.json")
222
+ with open(metrics_json_path, "w", encoding="utf8") as f:
223
+ f.write(self.metrics.dumps())
224
+
225
+ def validate(self):
226
+ """
227
+ Ensures that all paths exist in a State.
228
+ """
229
+
230
+ def visitor(key, value, top_key, _, depth):
231
+ if depth == 0 and DesignFormat.by_id(top_key) is None:
232
+ raise InvalidState(
233
+ f"Key '{top_key}' does not match a known design format."
234
+ )
235
+ if value is None:
236
+ return
237
+ if not (
238
+ isinstance(value, Path)
239
+ or isinstance(value, dict)
240
+ or isinstance(value, list)
241
+ ):
242
+ raise InvalidState(
243
+ f"Value at '{key}' is not a Path nor a dictionary/list of Paths: '{value}'."
244
+ )
245
+
246
+ self._walk(self.to_raw_dict(metrics=False), "", visit=visitor)
247
+
248
+ @classmethod
249
+ def __loads_recursive(
250
+ Self,
251
+ views: Dict,
252
+ validate_path: bool = True,
253
+ key_path: str = "",
254
+ ) -> dict:
255
+ target: dict = {}
256
+ for key, value in views.items():
257
+ current_key_path = f"{key_path}.{key}"
258
+ if value is None:
259
+ target[key] = value
260
+ continue
261
+
262
+ if isinstance(value, dict):
263
+ target[key] = Self.__loads_recursive(
264
+ value,
265
+ validate_path,
266
+ key_path=current_key_path,
267
+ )
268
+ else:
269
+ if validate_path and not os.path.exists(value):
270
+ raise ValueError(
271
+ f"Provided path '{value}' to design format '{current_key_path}' does not exist."
272
+ )
273
+ target[key] = Path(value)
274
+ return target
275
+
276
+ @classmethod
277
+ def loads(Self, json_in: str, validate_path: bool = True) -> "State":
278
+ try:
279
+ raw = json.loads(json_in, parse_float=Decimal)
280
+ except json.JSONDecodeError as e:
281
+ raise InvalidState(f"Invalid JSON string provided for state: {e}")
282
+
283
+ if not isinstance(raw, dict):
284
+ raise InvalidState("Failed to load state: JSON result is not a dictionary")
285
+
286
+ metrics = raw.get("metrics")
287
+ if metrics is not None:
288
+ del raw["metrics"]
289
+
290
+ views = Self.__loads_recursive(raw, validate_path)
291
+ state = Self(views, metrics=metrics)
292
+
293
+ return state
294
+
295
+ def __mapping_to_html_rec(
296
+ self,
297
+ mapping: Mapping[str, Any],
298
+ header_optional: Optional[Tuple[str, str]] = None,
299
+ ):
300
+ result = """
301
+ <table style="grid-column-start: 1; grid-column-end: 2; ">
302
+ """
303
+ if header := header_optional:
304
+ key_h, value_h = header
305
+ result += f"""
306
+ <tr>
307
+ <th style="text-align: left;">{key_h}</th>
308
+ <th style="text-align: left;">{value_h}</th>
309
+ </tr>
310
+ """
311
+
312
+ for id, value in mapping.items():
313
+ if value is None:
314
+ continue
315
+
316
+ key_content = id
317
+ if format := DesignFormat.by_id(id):
318
+ key_content = format.value.id
319
+
320
+ value_content = str(value)
321
+ if isinstance(value, Mapping):
322
+ value_content = self.__mapping_to_html_rec(value)
323
+ elif isinstance(value, Path):
324
+ value_rel = os.path.relpath(value, ".")
325
+
326
+ value_content = f'<a href="{value_rel}">{value_rel}</a>'
327
+ if "google.colab" in sys.modules:
328
+ # Can't link in colab
329
+ value_content = value_rel
330
+
331
+ result += f"""
332
+ <tr>
333
+ <td style="text-align: left;">{key_content}</td>
334
+ <td style="text-align: left;">{value_content}</td>
335
+ </tr>
336
+ """
337
+
338
+ result += """
339
+ </table>
340
+ """
341
+ return result
342
+
343
+ def _repr_html_(self) -> str:
344
+ return (
345
+ '<div style="display: grid; grid-auto-columns: minmax(0, 1fr); grid-auto-rows: minmax(0, 1fr); grid-auto-flow: column;">'
346
+ + self.__mapping_to_html_rec(
347
+ self.to_raw_dict(metrics=False),
348
+ ("Format", "Path"),
349
+ )
350
+ + "</div>"
351
+ )
@@ -0,0 +1,61 @@
1
+ # Copyright 2023 Efabless Corporation
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ The Step Module
17
+ ---------------
18
+
19
+ This modules includes various functions for importing and/or generating LibreLane
20
+ configuration objects. Configuration objects are the primary input to a flow.
21
+ """
22
+ from .step import (
23
+ StepError,
24
+ DeferredStepError,
25
+ StepException,
26
+ StepNotFound,
27
+ Step,
28
+ OutputProcessor,
29
+ DefaultOutputProcessor,
30
+ MetricsUpdate,
31
+ ViewsUpdate,
32
+ )
33
+ from .tclstep import TclStep
34
+ from . import checker as Checker
35
+
36
+ # You'll notice some TclStep subclasses are exposed separately-
37
+ # this is for documentation.
38
+ from . import yosys as Yosys
39
+ from .yosys import YosysStep
40
+
41
+ from .openroad_alerts import (
42
+ OpenROADAlert,
43
+ OpenROADOutputProcessor,
44
+ SupportsOpenROADAlerts,
45
+ )
46
+
47
+ from . import openroad as OpenROAD
48
+ from .openroad import OpenROADStep
49
+
50
+ from . import odb as Odb
51
+ from .odb import OdbpyStep
52
+
53
+ from . import magic as Magic
54
+ from .magic import MagicStep
55
+
56
+ from . import netgen as Netgen
57
+ from .netgen import NetgenStep
58
+
59
+ from . import klayout as KLayout
60
+ from . import misc as Misc
61
+ from . import verilator as Verilator