diff --git a/contrib/gds-corpus.toml b/contrib/gds-corpus.toml new file mode 100644 index 0000000..25eeefb --- /dev/null +++ b/contrib/gds-corpus.toml @@ -0,0 +1,71 @@ +# Pinned GDSII test inputs. Each digest covers the original upstream bytes. + +[gdstk] +repo = "heitzmann/gdstk" +revision = "1099be134da34d4afd9e5fb1f4f0dbae9ef4c17a" +license = "BSL-1.0" +license_sha256 = "beb8e42e9d6b4284e03304d05a81a0755200a965fc8d0a5e0aea1e84cf805d6e" + +[gdstk.layouts] +"tests/proof_lib.gds" = "af7b95ed191ebe32438c0f0cb5ed824ba2a98673cfa9ce6f1a9ff5a827c3dbf7" + +[gdsfactory] +repo = "gdsfactory/gdsfactory" +revision = "7407836dd5d872c704fc06f38562a2ffc88049dc" +license = "MIT" +license_sha256 = "cdace89991b496bd052909790d00ece97feab566afd964d0883d2c0605462b5b" + +[gdsfactory.layouts] +"tests/gds/alphabet_3top_cells.gds" = "09b75d5bfeb0051e4972a4d9fc26df5d342604f7ed2a0994376cd7c77e929019" +"tests/gds/big_rect.gds" = "de71342365dc4bd84dbb03307537b7ca2b4dffab98efdf88713cc451e44e6ac2" +"tests/gds/mmi1x2.gds" = "e12e81a9d5b9dc8463cce05915f567bc7174f848a52c7e092dc8a602819dafa0" +"tests/gds/mzi2x2.gds" = "49499f13c888de137eb26342c45ea379d9ea2ecd82fc990780d38fa014002d38" +"tests/gds/small_rect.gds" = "345480096d17bc724ae32cc0f42bd915a39c6e64806b43054d687755afd5faba" +"tests/gds/straight.gds" = "dc5e971b5be91864895870ef70a5ae959bd52dab5971caa0dcfc9aa6e13fa40d" + +[klayout] +repo = "KLayout/klayout" +revision = "53911acd6dfaa113c188d11d20b2ae290aa60a6b" +license = "GPL-3.0 (upstream LICENSE)" +license_sha256 = "589ed823e9a84c56feb95ac58e7cf384626b9cbf4fda2a907bc36e103de1bad2" + +[klayout.layouts] +"samples/lvs/ringo.gds" = "f24d4d11ac7941021d0b0c15d8b32ced1c6735cbd4da68de1482830ca45be3bd" +"testdata/drc/texts.gds" = "ee8ddf7de65ebccf9772fd98cb5d45313e353f8a3455d162611320885061f907" +"testdata/algo/deep_texts_l1.gds" = "253d24491a097102a729ef7f101c9b7acf7684c4a90c16d212ac91d9087e3b4d" +"testdata/gds/arefs.gds" = "fadde330ade22d0e8b92e4040377dcccb6e7a572d9d69df6b3fa722a2bfbcdcd" +"testdata/gds/arefs_skew.gds" = "a681a47cd1d2d74b881d0ab10c3d6fd6c9674c819cf39c077936a4328511b1ae" +"testdata/gds/arefs_skew2.gds" = "363dd2650b4a3011cbe33a6d8febccc77b22dd9b7563f087029a1c5afbe2ae94" +"testdata/gds/basic_instances.gds" = "6ccfffd590c159a5ab450c8eed826afb8e1d68c9ae2f85ec089ab21c0b40ec1b" +"testdata/gds/issue-1353.gds" = "f0e9cbe2b0582d766b3e6e1737617865ca5e74dc525f7bc2872e09ffe6a82482" +"testdata/gds/issue_1835.gds" = "b2d78e8ed4f43e63738857c7aa81606c8ccc5ca0910e3d4e4569c058415d9171" +"testdata/gds/issue_893.gds" = "30a2bf58ea8ae5b36f6df67c5d5221bc257e916b985bc1e7a74cf19ba244eff8" +"testdata/gds/issue_893_au.gds" = "e33cbc5ae2a3f3e9779f6c1cbaf89762c33644c76836c96445e887820dd4eab6" +"testdata/gds/t166_au.gds.gz" = "bd5f0eda73cf13daae77d61eb2a2d9376f2752e66cd458307cfee0e4d5b5ea57" + +[sky130] +repo = "google/skywater-pdk-libs-sky130_fd_sc_hd" +revision = "ac7fb61f06e6470b94e8afdf7c25268f62fbd7b1" +license = "Apache-2.0" +license_sha256 = "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30" + +[sky130.layouts] +"cells/inv/sky130_fd_sc_hd__inv_1.gds" = "18a9cfa399957c7c7b5fc75175e03fc921d56ecc6b8de8fae34ba301bb7914c4" +"cells/inv/sky130_fd_sc_hd__inv_4.gds" = "1e0fb091a3e4583297e7124d68542e4f29ee6d1f711e80dcb41fc1ed8465c0ce" +"cells/nand2/sky130_fd_sc_hd__nand2_1.gds" = "4e7fa6255df901f59151791d6e8ff4c421b63c1ea3822d6d5d25b48221782a3a" +"cells/dfxtp/sky130_fd_sc_hd__dfxtp_1.gds" = "2b0eaaeedd5b7b2c359942098aee0cef69387108e02e10bc09940680e4cc7ec3" +"cells/tapvpwrvgnd/sky130_fd_sc_hd__tapvpwrvgnd_1.gds" = "642f8841345c7c09ec910fe7893ae803ab760606adfab65229e2aedb055005a5" +"cells/fill/sky130_fd_sc_hd__fill_1.gds" = "9f718b6252a027fc769c2e4298c182e1c4ee3b160fefcd6acf7d947d5c6c3453" + +[ihp] +repo = "IHP-GmbH/IHP-Open-PDK" +revision = "e73adb4045731ddec21634146d536380057f6a26" +license = "Apache-2.0" +license_sha256 = "c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4" + +[ihp.layouts] +"ihp-sg13g2/libs.ref/sg13g2_pr/gds/sg13g2_pr.gds" = "3c640413b455354da11ec4db01c25f165489bcfd4cdec0f79559a9f78331ab42" +"ihp-sg13g2/libs.ref/sg13g2_stdcell/gds/sg13g2_stdcell.gds" = "ab9f2802c78935d4463093c24860f9d3f244057a839179fb19fc0243a18cf3f4" +"ihp-sg13g2/libs.ref/sg13g2_sram/gds/RM_IHPSG13_1P_1024x16_c2_bm_bist.gds" = "1ed0af3f9cc3a3455f4c0beb08394502a875dda3c54bc03123357e397b26e929" +"ihp-sg13g2/libs.qa/drc/devices/sg13g2_qacells.gds" = "21d8b2663ba15fa4c99752ad7a030f486043ba17950236bee8dc483e64c948b2" +"ihp-sg13g2/libs.qa/drc/special/sg13g2_stdcell_abutment.gds" = "e125bd8e6bf8976f41bf29fb761c28559064b50571289da98c148684676e1728" diff --git a/docs/development.md b/docs/development.md index da07331..493f35b 100644 --- a/docs/development.md +++ b/docs/development.md @@ -24,6 +24,20 @@ built library without reinstalling, set `PHEON_LIBRARY` to its absolute path and run `python -m unittest discover -s tests/python` from the checkout. Unset this override when checking installed artifacts with `check_installed.py`. +## Check upstream layouts + +The [GDSII corpus](../contrib/gds-corpus.toml) pins open-source fixtures and cell +libraries. After installing `.[arrow]` and building the release CLI, run: + +```sh +python -I tools/check_gds_corpus.py --fetch +``` + +The checker uses `curl` to cache layouts and license files in `.cache/gds-corpus/` +and verifies their SHA-256 digests. Omit `--fetch` to rerun offline. It checks +full reads, scans, selected reads, and CLI self-comparisons. It writes diagnostics +to `.cache/gds-corpus/report.json` and exits with status 1 if a check fails. + ## Interfaces and reports The CFFI declarations live in [pheon/_ffi.h](https://mpxd.net/code/jan/pheon/src/branch/master/pheon/_ffi.h). Keep them aligned diff --git a/tools/check_gds_corpus.py b/tools/check_gds_corpus.py new file mode 100644 index 0000000..8331145 --- /dev/null +++ b/tools/check_gds_corpus.py @@ -0,0 +1,167 @@ +"""Check a pinned open-source layout corpus against Pheon's installed readers.""" + +import argparse +import gzip +import hashlib +import json +import os +from pathlib import Path +import subprocess +import tomllib +from urllib.parse import quote + +from pheon import arrow + + +ROOT = Path(__file__).resolve().parents[1] + + +def read_input(path, url, digest, fetch): + if path.is_file(): + data = path.read_bytes() + elif fetch: + data = subprocess.check_output( + ["curl", "-fLsS", "--retry", "2", "--max-time", "60", url] + ) + else: + raise FileNotFoundError(f"{path} is missing. Rerun with --fetch.") + if hashlib.sha256(data).hexdigest() != digest: + raise ValueError(f"SHA-256 mismatch for {path}") + if not path.is_file(): + path.write_bytes(data) + return data + + +def inspect_layout(cli, path, data): + results = {} + reads = { + "full": lambda: arrow.read_path(path), + "scan": lambda: arrow.scan_bytes(data), + } + layouts = {} + for name, read in reads.items(): + try: + layout = read() + layouts[name] = layout + results[name] = { + "status": "ok", + "cells": len(layout.field("cells")[0].values), + } + except (ValueError, OSError, TypeError) as error: + results[name] = {"status": "failed", "message": str(error)} + + if "scan" in layouts: + cells = layouts["scan"].field("cells")[0].values + ranges = list( + zip( + cells.field("struct_start_offset").to_pylist(), + cells.field("struct_end_offset").to_pylist(), + strict=True, + ) + ) + try: + selected = arrow.read_cells_bytes(data, ranges) + if "full" in layouts and not selected.equals(layouts["full"]): + raise ValueError( + "Reading all scanned cell ranges differs from the full read" + ) + results["selected"] = { + "status": "ok", + "cells": len(selected.field("cells")[0].values), + } + except (ValueError, OSError, TypeError) as error: + results["selected"] = {"status": "failed", "message": str(error)} + else: + results["selected"] = {"status": "skipped"} + + try: + run = subprocess.run( + [cli, "diff", path, path, "--format", "json"], + capture_output=True, + timeout=60, + ) + except (subprocess.TimeoutExpired, OSError) as error: + results["cli"] = {"status": "failed", "message": str(error)} + return results + try: + report = json.loads(run.stdout) + results["cli"] = { + "status": report["status"], + "result": report["result"], + "exit_code": run.returncode, + "diagnostics": report["diagnostics"], + } + if (report["status"], report["result"], run.returncode) not in { + ("complete", "equal", 0), + ("incomplete", "unknown", 2), + ("failed", "unknown", 2), + }: + results["cli"]["status"] = "failed" + except (ValueError, KeyError, TypeError) as error: + results["cli"] = { + "status": "failed", + "message": str(error), + "exit_code": run.returncode, + "stderr": run.stderr[-4096:].decode("utf-8", errors="replace"), + } + return results + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--fetch", action="store_true", help="download missing inputs using curl" + ) + binary = "pheon.exe" if os.name == "nt" else "pheon" + parser.add_argument("--cli", type=Path, default=ROOT / "target/release" / binary) + parser.add_argument("--cache", type=Path, default=ROOT / ".cache/gds-corpus") + args = parser.parse_args() + cli = args.cli.resolve() + if not cli.is_file(): + parser.error(f"{cli} is missing. Build the CLI or pass --cli.") + + for name in ("files", "licenses"): + (args.cache / name).mkdir(parents=True, exist_ok=True) + sources = tomllib.loads( + (ROOT / "contrib/gds-corpus.toml").read_text(encoding="utf-8") + ) + all_results = {} + for key, source in sources.items(): + base = ( + f"https://raw.githubusercontent.com/{source['repo']}/{source['revision']}" + ) + read_input( + args.cache / "licenses" / f"{key}-LICENSE", + base + "/LICENSE", + source["license_sha256"], + args.fetch, + ) + for upstream, digest in source["layouts"].items(): + name = f"{key}-{Path(upstream).name}" + path = args.cache / "files" / name + raw = read_input(path, f"{base}/{quote(upstream)}", digest, args.fetch) + data = gzip.decompress(raw) if path.suffix == ".gz" else raw + results = inspect_layout(cli, path, data) + all_results[name] = results + print( + name, + *( + f"{operation}={result['status']}" + for operation, result in results.items() + ), + ) + + report = args.cache / "report.json" + report.write_text(json.dumps(all_results, indent=2) + "\n", encoding="utf-8") + failures = sum( + any(result["status"] == "failed" for result in case.values()) + for case in all_results.values() + ) + print( + f"{len(all_results)} layouts checked, {failures} with failures. See {report}." + ) + return int(failures != 0) + + +if __name__ == "__main__": + raise SystemExit(main())