File size: 7,310 Bytes
784ee2c | 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 | #!/usr/bin/env python3
import unittest
import os
import sys
import tempfile
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "util"))
import generate_klayout_tech
MINIMAL_LYT = """\
<?xml version="1.0" encoding="utf-8"?>
<technology>
<name>Test</name>
<reader-options>
<lefdef>
<lef-files>./original.lef</lef-files>
</lefdef>
</reader-options>
</technology>
"""
LYT_WITH_MAP = """\
<?xml version="1.0" encoding="utf-8"?>
<technology>
<name>Test</name>
<reader-options>
<lefdef>
<lef-files>./original.lef</lef-files>
<map-file>original.map</map-file>
</lefdef>
</reader-options>
</technology>
"""
LYT_WITH_EMPTY_MAP = """\
<?xml version="1.0" encoding="utf-8"?>
<technology>
<name>Test</name>
<reader-options>
<lefdef>
<lef-files>./original.lef</lef-files>
<map-file/>
</lefdef>
</reader-options>
</technology>
"""
class TestReplaceLefFiles(unittest.TestCase):
def test_single_lef(self):
result = generate_klayout_tech.replace_lef_files(MINIMAL_LYT, ["tech.lef"])
self.assertIn("<lef-files>tech.lef</lef-files>", result)
self.assertNotIn("original.lef", result)
def test_multiple_lefs(self):
result = generate_klayout_tech.replace_lef_files(
MINIMAL_LYT, ["tech.lef", "sc.lef", "extra.lef"]
)
self.assertIn(
"<lef-files>tech.lef</lef-files>"
"<lef-files>sc.lef</lef-files>"
"<lef-files>extra.lef</lef-files>",
result,
)
self.assertNotIn("original.lef", result)
def test_empty_lefs(self):
result = generate_klayout_tech.replace_lef_files(MINIMAL_LYT, [])
self.assertNotIn("original.lef", result)
# Empty replacement removes the element content
self.assertNotIn("<lef-files>", result)
class TestReplaceMapFiles(unittest.TestCase):
def test_replace_existing_map(self):
result = generate_klayout_tech.replace_map_files(
LYT_WITH_MAP, ["/abs/path/layer.map"]
)
self.assertIn("<map-file>/abs/path/layer.map</map-file>", result)
self.assertNotIn("original.map", result)
def test_replace_empty_map(self):
result = generate_klayout_tech.replace_map_files(
LYT_WITH_EMPTY_MAP, ["/abs/path/layer.map"]
)
self.assertIn("<map-file>/abs/path/layer.map</map-file>", result)
def test_no_map_files_noop(self):
result = generate_klayout_tech.replace_map_files(LYT_WITH_MAP, [])
self.assertEqual(result, LYT_WITH_MAP)
def test_no_map_element_in_template(self):
result = generate_klayout_tech.replace_map_files(MINIMAL_LYT, ["/some/map"])
# No map element to replace, content unchanged
self.assertEqual(result, MINIMAL_LYT)
class TestGenerateKlayoutTech(unittest.TestCase):
def setUp(self):
self.tmp_dir = tempfile.TemporaryDirectory()
self.template = os.path.join(self.tmp_dir.name, "test.lyt")
self.output = os.path.join(self.tmp_dir.name, "output.lyt")
self.results_dir = os.path.join(self.tmp_dir.name, "results")
os.makedirs(self.results_dir, exist_ok=True)
def tearDown(self):
self.tmp_dir.cleanup()
def test_basic_generation(self):
with open(self.template, "w") as f:
f.write(MINIMAL_LYT)
lef_path = os.path.join(self.tmp_dir.name, "tech.lef")
with open(lef_path, "w") as f:
f.write("")
generate_klayout_tech.generate_klayout_tech(
template_lyt=self.template,
output_lyt=self.output,
lef_files=[lef_path],
map_files=[],
)
with open(self.output) as f:
content = f.read()
self.assertIn("<lef-files>", content)
self.assertNotIn("original.lef", content)
# LEF paths are written as plain abspath (not relpath, not realpath):
# klayout's Layout.read resolves relative <lef-files> entries
# against the realpath of the DEF being merged, which under a
# Bazel sandbox is the bare execroot -- the in-flight sibling
# files only exist in the per-action sandbox. Absolute paths
# bypass the relative-resolution dance.
self.assertIn(os.path.abspath(lef_path), content)
def test_with_map_files(self):
with open(self.template, "w") as f:
f.write(LYT_WITH_MAP)
lef_path = os.path.join(self.tmp_dir.name, "tech.lef")
map_path = os.path.join(self.tmp_dir.name, "layer.map")
for p in [lef_path, map_path]:
with open(p, "w") as f:
f.write("")
generate_klayout_tech.generate_klayout_tech(
template_lyt=self.template,
output_lyt=self.output,
lef_files=[lef_path],
map_files=[map_path],
)
with open(self.output) as f:
content = f.read()
# Same abspath semantics as LEFs: map files are also written as
# plain absolute paths so klayout doesn't resolve them relative
# to the bare-execroot realpath of the input DEF.
self.assertIn(os.path.abspath(map_path), content)
self.assertNotIn("original.map", content)
def test_multiple_lef_files(self):
with open(self.template, "w") as f:
f.write(MINIMAL_LYT)
lef_files = []
for name in ["tech.lef", "sc.lef", "extra.lef"]:
path = os.path.join(self.tmp_dir.name, name)
with open(path, "w") as f:
f.write("")
lef_files.append(path)
generate_klayout_tech.generate_klayout_tech(
template_lyt=self.template,
output_lyt=self.output,
lef_files=lef_files,
map_files=[],
)
with open(self.output) as f:
content = f.read()
# Should have three lef-files elements
self.assertEqual(content.count("<lef-files>"), 3)
class TestRealPlatformLyt(unittest.TestCase):
"""Test against actual platform .lyt files to catch regressions."""
PLATFORMS_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "..", "platforms"
)
def _test_platform(self, lyt_path):
if not os.path.exists(lyt_path):
self.skipTest(f"{lyt_path} not found")
with open(lyt_path) as f:
content = f.read()
result = generate_klayout_tech.replace_lef_files(
content, ["replaced_tech.lef", "replaced_sc.lef"]
)
self.assertIn("<lef-files>replaced_tech.lef</lef-files>", result)
self.assertIn("<lef-files>replaced_sc.lef</lef-files>", result)
# Original lef-files content should be gone
self.assertNotIn("NangateOpenCellLibrary", result)
self.assertNotIn("asap7_tech", result)
def test_nangate45(self):
self._test_platform(
os.path.join(self.PLATFORMS_DIR, "nangate45", "FreePDK45.lyt")
)
def test_asap7(self):
self._test_platform(
os.path.join(self.PLATFORMS_DIR, "asap7", "KLayout", "asap7.lyt")
)
def test_sky130hd(self):
self._test_platform(
os.path.join(self.PLATFORMS_DIR, "sky130hd", "sky130hd.lyt")
)
if __name__ == "__main__":
unittest.main()
|