File size: 6,442 Bytes
34292e0 | 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 | try:
import pya
except ImportError:
pya = None
import argparse
import os
import re
import sys
def merge_gds(
pya_mod,
tech_file,
layer_map,
in_def,
design_name,
in_files,
seal_file,
out_file,
allow_empty="",
allow_empty_cells=frozenset(),
):
"""Merge DEF and GDS/OAS files into a single stream file.
Args:
pya_mod: The pya module (klayout Python API).
tech_file: Path to klayout technology file.
layer_map: Path to layer map file (empty string if none).
in_def: Path to input DEF file.
design_name: Top-level design name.
in_files: Space-separated string of GDS/OAS files to merge.
seal_file: Path to seal ring GDS/OAS file (empty string if none).
out_file: Path to output GDS/OAS file.
allow_empty: Regex pattern for cells allowed to be empty.
allow_empty_cells: Names of cells allowed to be empty.
Returns:
Number of errors encountered.
"""
errors = 0
# Load technology file
tech = pya_mod.Technology()
tech.load(tech_file)
layout_options = tech.load_layout_options
if len(layer_map) > 0:
layout_options.lefdef_config.map_file = layer_map
# Load def file
main_layout = pya_mod.Layout()
print("[INFO] Reporting cells prior to loading DEF ...")
for i in main_layout.each_cell():
print("[INFO] '{0}'".format(i.name))
main_layout.read(in_def, layout_options)
# Clear cells
top_cell_index = main_layout.cell(design_name).cell_index()
# remove orphan cell BUT preserve cell with VIA_
# - KLayout is prepending VIA_ when reading DEF that instantiates LEF's via
for i in main_layout.each_cell():
if i.cell_index() != top_cell_index:
if not i.name.startswith("VIA_") and not i.name.endswith("_DEF_FILL"):
i.clear()
# Load in the gds to merge
for fil in in_files.split():
print("\t{0}".format(fil))
main_layout.read(fil)
# Copy the top level only to a new layout
top_only_layout = pya_mod.Layout()
top_only_layout.dbu = main_layout.dbu
top = top_only_layout.create_cell(design_name)
top.copy_tree(main_layout.cell(design_name))
missing_cell = False
regex = re.compile(allow_empty) if allow_empty else None
if allow_empty:
print(f"[INFO] GDS_ALLOW_EMPTY={allow_empty}")
for i in top_only_layout.each_cell():
if i.is_empty():
missing_cell = True
if regex is not None and regex.match(i.name):
print(
"[WARNING] LEF Cell '{0}' ignored. Matches GDS_ALLOW_EMPTY.".format(
i.name
)
)
elif i.name in allow_empty_cells:
print(
"[WARNING] LEF Cell '{0}' ignored. AUTO_MEMORIES generated"
" its views; it has no GDS.".format(i.name)
)
else:
print(
"[ERROR] LEF Cell '{0}' has no matching GDS/OAS cell."
" Cell will be empty.".format(i.name)
)
errors += 1
if not missing_cell:
print("[INFO] All LEF cells have matching GDS/OAS cells")
orphan_cell = False
for i in top_only_layout.each_cell():
if i.name != design_name and i.parent_cells() == 0:
orphan_cell = True
print("[ERROR] Found orphan cell '{0}'".format(i.name))
errors += 1
if not orphan_cell:
print("[INFO] No orphan cells in the final layout")
if seal_file:
top_cell = top_only_layout.top_cell()
top_only_layout.read(seal_file)
for cell in top_only_layout.top_cells():
if cell != top_cell:
print(
"[INFO] Merging '{0}' as child of '{1}'".format(
cell.name, top_cell.name
)
)
top.insert(pya_mod.CellInstArray(cell.cell_index(), pya_mod.Trans()))
# Write out the GDS
top_only_layout.write(out_file)
return errors
def auto_memories_cells(environ):
"""The modules AUTO_MEMORIES turned into macros.
Their LEF and liberty views are generated and they have no GDS, so
they are empty in the merged layout by design.
"""
if environ.get("AUTO_MEMORIES") != "1":
return frozenset()
# do-auto-memories always writes the list, empty when nothing was
# converted; without it the merge cannot tell a generated macro from
# a cell whose GDS is missing.
memories_dir = environ.get("AUTO_MEMORIES_DIR")
if not memories_dir:
raise RuntimeError("AUTO_MEMORIES=1 but AUTO_MEMORIES_DIR is not set")
path = os.path.join(memories_dir, "blackboxes.txt")
if not os.path.isfile(path):
raise RuntimeError(
f"AUTO_MEMORIES=1 but {path} is missing; do-auto-memories writes it"
)
with open(path, encoding="utf-8") as f:
return frozenset(line.split()[0] for line in f if line.strip())
def parse_args(argv):
parser = argparse.ArgumentParser(
description="Merge a DEF file and GDS/OAS files into one stream file."
)
parser.add_argument("--design-name", required=True)
parser.add_argument("--in-def", required=True)
parser.add_argument("--in-files", default="")
parser.add_argument("--seal-file", default="")
parser.add_argument("--out-file", required=True)
parser.add_argument("--tech-file", required=True)
parser.add_argument("--layer-map", default="")
return parser.parse_args(argv)
def main(argv, environ=os.environ):
"""Write the stream file with the klayout Python module."""
args = parse_args(argv)
if pya is None:
print(
"ERROR: the klayout Python module is not installed, pip install"
" klayout (etc/requirements-common_lock.txt)"
)
return 1
return merge_gds(
pya_mod=pya,
tech_file=args.tech_file,
layer_map=args.layer_map,
in_def=args.in_def,
design_name=args.design_name,
in_files=args.in_files,
seal_file=args.seal_file,
out_file=args.out_file,
allow_empty=environ.get("GDS_ALLOW_EMPTY", ""),
allow_empty_cells=auto_memories_cells(environ),
)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
|