|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""The make rules that run a KLayout application: a Ruby DRC or LVS deck |
| 3 | +and the klayout_<file> viewers. With KLayout, each rule hands it the |
| 4 | +rule's arguments as they are; without it, each stops with an error and |
| 5 | +runs nothing. |
| 6 | +
|
| 7 | +KLayout is stubbed: `klayout` logs its argv and writes the report file it |
| 8 | +is given, so make needs nothing installed. The inputs the rules depend on |
| 9 | +are written empty and kept as they are with make's -o. |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +import subprocess |
| 14 | +import tempfile |
| 15 | +import unittest |
| 16 | + |
| 17 | +FLOW_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") |
| 18 | +DESIGN_CONFIG = "designs/nangate45/gcd/config.mk" |
| 19 | + |
| 20 | +FAKE_KLAYOUT = """\ |
| 21 | +#!/usr/bin/env bash |
| 22 | +printf "%s|" "$@" >> "$ARGV_LOG" |
| 23 | +echo >> "$ARGV_LOG" |
| 24 | +for arg in "$@"; do |
| 25 | + case "$arg" in |
| 26 | + report_file=*) echo "<value>1</value>" > "${arg#report_file=}" ;; |
| 27 | + esac |
| 28 | +done |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +class TestKlayoutMake(unittest.TestCase): |
| 33 | + def setUp(self): |
| 34 | + self.tmp_dir = tempfile.TemporaryDirectory() |
| 35 | + tmp = self.tmp_dir.name |
| 36 | + self.log = os.path.join(tmp, "argv.log") |
| 37 | + self.klayout = os.path.join(tmp, "klayout") |
| 38 | + with open(self.klayout, "w") as f: |
| 39 | + f.write(FAKE_KLAYOUT) |
| 40 | + os.chmod(self.klayout, 0o755) |
| 41 | + self.deck = {} |
| 42 | + for name in ("drc.lydrc", "lvs.lylvs"): |
| 43 | + self.deck[name] = os.path.join(tmp, name) |
| 44 | + open(self.deck[name], "w").close() |
| 45 | + self.args = [ |
| 46 | + f"DESIGN_CONFIG={DESIGN_CONFIG}", |
| 47 | + f"WORK_HOME={os.path.join(tmp, 'work')}", |
| 48 | + "FLOW_VARIANT=klayout_make", |
| 49 | + ] |
| 50 | + self.env = dict(os.environ, ARGV_LOG=self.log) |
| 51 | + for name in ("KLAYOUT_CMD", "MAKEFLAGS"): |
| 52 | + self.env.pop(name, None) |
| 53 | + self.dirs = { |
| 54 | + name: self.print_var(name) |
| 55 | + for name in ("RESULTS_DIR", "OBJECTS_DIR", "REPORTS_DIR", "LOG_DIR") |
| 56 | + } |
| 57 | + for path in self.dirs.values(): |
| 58 | + os.makedirs(path, exist_ok=True) |
| 59 | + self.gds = self.input("RESULTS_DIR", "6_final.gds") |
| 60 | + self.lyt = self.input("OBJECTS_DIR", "klayout.lyt") |
| 61 | + self.cdl = self.input("OBJECTS_DIR", "6_final_concat.cdl") |
| 62 | + |
| 63 | + def tearDown(self): |
| 64 | + self.tmp_dir.cleanup() |
| 65 | + |
| 66 | + def input(self, directory, name): |
| 67 | + path = os.path.join(self.dirs[directory], name) |
| 68 | + open(path, "w").close() |
| 69 | + return path |
| 70 | + |
| 71 | + def print_var(self, name): |
| 72 | + result = subprocess.run( |
| 73 | + ["make", "--no-print-directory", *self.args, f"print-{name}"], |
| 74 | + cwd=FLOW_DIR, |
| 75 | + env=self.env, |
| 76 | + capture_output=True, |
| 77 | + text=True, |
| 78 | + check=True, |
| 79 | + ) |
| 80 | + return result.stdout.split(f"{name}: ")[1].strip() |
| 81 | + |
| 82 | + def make(self, target, klayout_cmd, *args): |
| 83 | + if os.path.exists(self.log): |
| 84 | + os.remove(self.log) |
| 85 | + result = subprocess.run( |
| 86 | + [ |
| 87 | + "make", |
| 88 | + "--no-print-directory", |
| 89 | + *self.args, |
| 90 | + f"KLAYOUT_CMD={klayout_cmd}", |
| 91 | + *(f"-o{path}" for path in (self.gds, self.lyt, self.cdl)), |
| 92 | + *args, |
| 93 | + target, |
| 94 | + ], |
| 95 | + cwd=FLOW_DIR, |
| 96 | + env=self.env, |
| 97 | + stdin=subprocess.DEVNULL, |
| 98 | + capture_output=True, |
| 99 | + text=True, |
| 100 | + ) |
| 101 | + calls = [] |
| 102 | + if os.path.exists(self.log): |
| 103 | + with open(self.log) as f: |
| 104 | + calls = [line.split("|")[:-1] for line in f.read().splitlines()] |
| 105 | + return result, calls |
| 106 | + |
| 107 | + def routes(self): |
| 108 | + """Each route: the make target, its variables and the arguments |
| 109 | + KLayout gets.""" |
| 110 | + drc_report = os.path.join(self.dirs["REPORTS_DIR"], "6_drc.lyrdb") |
| 111 | + lvs_report = os.path.join(self.dirs["RESULTS_DIR"], "6_lvs.lvsdb") |
| 112 | + return { |
| 113 | + "viewer": ( |
| 114 | + "klayout_6_final.gds", |
| 115 | + [], |
| 116 | + ["-nn", self.lyt, self.gds], |
| 117 | + ), |
| 118 | + "ruby drc": ( |
| 119 | + drc_report, |
| 120 | + [f"KLAYOUT_DRC_FILE={self.deck['drc.lydrc']}"], |
| 121 | + [ |
| 122 | + "-zz", |
| 123 | + "-rd", |
| 124 | + f"in_gds={self.gds}", |
| 125 | + "-rd", |
| 126 | + f"report_file={drc_report}", |
| 127 | + "-r", |
| 128 | + self.deck["drc.lydrc"], |
| 129 | + ], |
| 130 | + ), |
| 131 | + "ruby lvs": ( |
| 132 | + lvs_report, |
| 133 | + [f"KLAYOUT_LVS_FILE={self.deck['lvs.lylvs']}"], |
| 134 | + [ |
| 135 | + "-b", |
| 136 | + "-rd", |
| 137 | + f"in_gds={self.gds}", |
| 138 | + "-rd", |
| 139 | + f"cdl_file={self.cdl}", |
| 140 | + "-rd", |
| 141 | + "top_cell=gcd", |
| 142 | + "-rd", |
| 143 | + f"report_file={lvs_report}", |
| 144 | + "-r", |
| 145 | + self.deck["lvs.lylvs"], |
| 146 | + ], |
| 147 | + ), |
| 148 | + } |
| 149 | + |
| 150 | + def test_klayout_gets_the_arguments(self): |
| 151 | + for route, (target, args, expected) in self.routes().items(): |
| 152 | + with self.subTest(route): |
| 153 | + result, calls = self.make(target, self.klayout, *args) |
| 154 | + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 155 | + self.assertEqual(calls, [expected]) |
| 156 | + |
| 157 | + def test_missing_klayout_stops_the_rule(self): |
| 158 | + for route, (target, args, _) in self.routes().items(): |
| 159 | + with self.subTest(route): |
| 160 | + result, calls = self.make(target, "", *args) |
| 161 | + self.assertNotEqual(result.returncode, 0) |
| 162 | + self.assertIn("KLayout not found", result.stdout + result.stderr) |
| 163 | + self.assertEqual(calls, []) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + unittest.main() |
0 commit comments