Skip to content

Commit 1fcd565

Browse files
oharboeclaude
andcommitted
Make KLayout an optional dependency
KLayout is a heavy end-user GUI tool that was previously required to complete the ORFS flow. The `finish` target depended on GDS_FINAL_FILE and `do-finish` called `do-gds`, meaning the flow could not complete without KLayout installed — even though most use cases never need GDS. This change decouples KLayout so it is only pulled in when explicitly requested via `make gds` / `make do-gds`, following the existing target naming pattern (place/do-place, finish/do-finish, gds/do-gds). Makefile changes: - Remove $(GDS_FINAL_FILE) from `finish` dependencies - Remove do-gds from `do-finish` recipe - Add explicit `gds` phony target - Add `check-klayout` guard on all KLayout-dependent targets (GDS merge, DRC, LVS, gallery, klayout viewer shortcuts) The interface is backwards compatible: `make finish gds` produces GDS as before, `do-gds` is unchanged, and bazel-orfs can invoke `do-gds` from a new `orfs_gds()` rule while `do-final` no longer requires KLayout in the toolchain. Extract .lyt tech file generation from sed to Python: - The do-klayout and do-klayout_wrap targets previously used fragile sed-based XML manipulation with shell variable expansion and ifeq/else branching. New util/generate_klayout_tech.py replaces this with stdlib xml/regex processing (no KLayout dependency). Refactor def2stream.py for testability: - Extract logic into merge_gds() function with pya as a parameter - Guard `import pya` so the module can be imported without KLayout - When run via `klayout -r`, the script behaves identically using klayout's -rd global variables Add mock-based unit tests (34 tests, no KLayout needed): - test_generate_klayout_tech.py: .lyt generation including validation against real platform templates (nangate45, asap7, sky130hd) - test_def2stream.py: cell clearing, VIA_ preservation, orphan detection, GDS_ALLOW_EMPTY regex, seal file merging, error counting - test_convertDrc.py: DRC report conversion (box/edge/text violations, waived items, comment assembly) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 294e641 commit 1fcd565

8 files changed

Lines changed: 1318 additions & 102 deletions

‎flow/Makefile‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,25 @@ $(OBJECTS_DIR)/klayout.lyt: $(KLAYOUT_TECH_FILE) $(OBJECTS_DIR)/klayout_tech.lef
195195

196196
.PHONY: do-klayout
197197
do-klayout:
198-
ifeq ($(KLAYOUT_ENV_VAR_IN_PATH),valid)
199-
SC_LEF_RELATIVE_PATH="$(shell realpath --relative-to=$(RESULTS_DIR) $(SC_LEF))"; \
200-
OTHER_LEFS_RELATIVE_PATHS=$$(echo "$(foreach file, $(OBJECTS_DIR)/klayout_tech.lef $(ADDITIONAL_LEFS),<lef-files>$$(realpath --relative-to=$(RESULTS_DIR) $(file))</lef-files>)"); \
201-
sed 's,<lef-files>.*</lef-files>,<lef-files>'"$$SC_LEF_RELATIVE_PATH"'</lef-files>'"$$OTHER_LEFS_RELATIVE_PATHS"',g' $(KLAYOUT_TECH_FILE) > $(OBJECTS_DIR)/klayout.lyt
202-
else
203-
sed 's,<lef-files>.*</lef-files>,$(foreach file, $(OBJECTS_DIR)/klayout_tech.lef $(SC_LEF) $(ADDITIONAL_LEFS),<lef-files>$(shell realpath --relative-to=$(RESULTS_DIR) $(file))</lef-files>),g' $(KLAYOUT_TECH_FILE) > $(OBJECTS_DIR)/klayout.lyt
204-
endif
205-
sed -i 's,<map-file>.*</map-file>,$(foreach file, $(FLOW_HOME)/platforms/$(PLATFORM)/*map,<map-file>$(shell realpath $(file))</map-file>),g' $(OBJECTS_DIR)/klayout.lyt
198+
@mkdir -p $(dir $(OBJECTS_DIR)/klayout.lyt)
199+
$(PYTHON_EXE) $(UTILS_DIR)/generate_klayout_tech.py \
200+
--template $(KLAYOUT_TECH_FILE) \
201+
--output $(OBJECTS_DIR)/klayout.lyt \
202+
--lef-files $(OBJECTS_DIR)/klayout_tech.lef $(SC_LEF) $(ADDITIONAL_LEFS) \
203+
--reference-dir $(RESULTS_DIR) \
204+
--map-files $(wildcard $(FLOW_HOME)/platforms/$(PLATFORM)/*map)
206205

207206
$(OBJECTS_DIR)/klayout_wrap.lyt: $(KLAYOUT_TECH_FILE) $(OBJECTS_DIR)/klayout_tech.lef
208207
$(UNSET_AND_MAKE) do-klayout_wrap
209208

210209
.PHONY: do-klayout_wrap
211210
do-klayout_wrap:
212-
sed 's,<lef-files>.*</lef-files>,$(foreach file, $(OBJECTS_DIR)/klayout_tech.lef $(WRAP_LEFS),<lef-files>$(shell realpath --relative-to=$(OBJECTS_DIR)/def $(file))</lef-files>),g' $(KLAYOUT_TECH_FILE) > $(OBJECTS_DIR)/klayout_wrap.lyt
211+
@mkdir -p $(dir $(OBJECTS_DIR)/klayout_wrap.lyt)
212+
$(PYTHON_EXE) $(UTILS_DIR)/generate_klayout_tech.py \
213+
--template $(KLAYOUT_TECH_FILE) \
214+
--output $(OBJECTS_DIR)/klayout_wrap.lyt \
215+
--lef-files $(OBJECTS_DIR)/klayout_tech.lef $(WRAP_LEFS) \
216+
--reference-dir $(OBJECTS_DIR)/def
213217

214218
$(WRAPPED_LEFS):
215219
mkdir -p $(OBJECTS_DIR)/lef $(OBJECTS_DIR)/def
@@ -594,8 +598,7 @@ klayout_guides: $(RESULTS_DIR)/5_route.def $(OBJECTS_DIR)/klayout.lyt
594598
.PHONY: finish
595599
finish: $(LOG_DIR)/6_report.log \
596600
$(RESULTS_DIR)/6_final.v \
597-
$(RESULTS_DIR)/6_final.sdc \
598-
$(GDS_FINAL_FILE)
601+
$(RESULTS_DIR)/6_final.sdc
599602
$(UNSET_AND_MAKE) elapsed
600603

601604
.PHONY: elapsed
@@ -627,7 +630,7 @@ final: finish
627630

628631
.PHONY: do-finish
629632
do-finish:
630-
$(UNSET_AND_MAKE) do-6_1_fill do-6_1_fill.sdc do-6_final.sdc do-6_report do-gds elapsed
633+
$(UNSET_AND_MAKE) do-6_1_fill do-6_1_fill.sdc do-6_final.sdc do-6_report elapsed
631634

632635
.PHONY: generate_abstract
633636
generate_abstract: $(RESULTS_DIR)/6_final.gds $(RESULTS_DIR)/6_final.def $(RESULTS_DIR)/6_final.v $(RESULTS_DIR)/6_final.sdc
@@ -643,6 +646,17 @@ do-generate_abstract:
643646
clean_abstract:
644647
rm -f $(RESULTS_DIR)/$(DESIGN_NAME).lib $(RESULTS_DIR)/$(DESIGN_NAME).lef
645648

649+
.PHONY: check-klayout
650+
check-klayout:
651+
@if [ -z "$(KLAYOUT_CMD)" ]; then \
652+
echo "Error: KLayout not found. Install KLayout or set KLAYOUT_CMD."; \
653+
echo "Hint: 'make finish' works without KLayout. Only GDS/DRC/LVS need it."; \
654+
exit 1; \
655+
fi
656+
657+
.PHONY: gds
658+
gds: $(GDS_FINAL_FILE)
659+
646660
# Merge wrapped macros using Klayout
647661
#-------------------------------------------------------------------------------
648662
$(WRAPPED_GDSOAS): $(OBJECTS_DIR)/klayout_wrap.lyt $(WRAPPED_LEFS)
@@ -658,7 +672,7 @@ $(WRAPPED_GDSOAS): $(OBJECTS_DIR)/klayout_wrap.lyt $(WRAPPED_LEFS)
658672

659673
# Merge GDS using Klayout
660674
#-------------------------------------------------------------------------------
661-
$(GDS_MERGED_FILE): $(RESULTS_DIR)/6_final.def $(OBJECTS_DIR)/klayout.lyt $(GDSOAS_FILES) $(WRAPPED_GDSOAS) $(SEAL_GDSOAS)
675+
$(GDS_MERGED_FILE): check-klayout $(RESULTS_DIR)/6_final.def $(OBJECTS_DIR)/klayout.lyt $(GDSOAS_FILES) $(WRAPPED_GDSOAS) $(SEAL_GDSOAS)
662676
$(UNSET_AND_MAKE) do-gds-merged
663677

664678
.PHONY: do-gds-merged
@@ -768,7 +782,7 @@ nuke: clean_test clean_issues
768782
# DEF/GDS/OAS viewer shortcuts
769783
#-------------------------------------------------------------------------------
770784
.PHONY: $(foreach file,$(RESULTS_DEF) $(RESULTS_GDS) $(RESULTS_OAS),klayout_$(file))
771-
$(foreach file,$(RESULTS_DEF) $(RESULTS_GDS) $(RESULTS_OAS),klayout_$(file)): klayout_%: $(OBJECTS_DIR)/klayout.lyt
785+
$(foreach file,$(RESULTS_DEF) $(RESULTS_GDS) $(RESULTS_OAS),klayout_$(file)): klayout_%: check-klayout $(OBJECTS_DIR)/klayout.lyt
772786
$(SCRIPTS_DIR)/klayout.sh -nn $(OBJECTS_DIR)/klayout.lyt $(RESULTS_DIR)/$*
773787

774788
$(eval $(call OPEN_GUI_SHORTCUT,synth,1_synth.odb))
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# KLayout as an Optional Dependency
2+
3+
KLayout is only required for GDS/OAS stream generation, DRC, and LVS
4+
verification. All other ORFS functionality — synthesis, floorplanning,
5+
placement, CTS, routing, timing reports, and abstract generation — works
6+
without KLayout installed.
7+
8+
## Makefile Targets
9+
10+
| Target | Requires KLayout | Description |
11+
|---|---|---|
12+
| `make finish` | No | Complete flow through timing reports |
13+
| `make gds` | Yes | Generate GDS/OAS from finished design |
14+
| `make do-finish` / `make do-final` | No | Same as `finish`, for bazel-orfs |
15+
| `make do-gds` | Yes | Same as `gds`, for bazel-orfs |
16+
| `make drc` | Yes | Run DRC checks (requires GDS) |
17+
| `make lvs` | Yes | Run LVS checks (requires GDS) |
18+
| `make gallery` | Yes | Generate layout screenshots |
19+
| `make klayout_<file>` | Yes | Open result in KLayout viewer |
20+
| `make generate_abstract` | No | Generate LEF/LIB abstracts |
21+
22+
A `check-klayout` guard produces a clear error message when KLayout is
23+
missing and a KLayout-dependent target is invoked:
24+
25+
```
26+
Error: KLayout not found. Install KLayout or set KLAYOUT_CMD.
27+
Hint: 'make finish' works without KLayout. Only GDS/DRC/LVS need it.
28+
```
29+
30+
## Use Cases
31+
32+
| Use Case | Needs KLayout? |
33+
|---|---|
34+
| Education / learning digital design | No |
35+
| Architectural exploration (PPA analysis) | No |
36+
| CI testing of ORFS / OpenROAD | No |
37+
| Local ORFS and OpenROAD development | No |
38+
| bazel-orfs default `orfs_flow()` | No |
39+
| GDS generation for tapeout | Yes |
40+
| DRC / LVS verification | Yes |
41+
| Layout visualization in KLayout GUI | Yes |
42+
43+
## bazel-orfs Integration
44+
45+
bazel-orfs uses the `do-` prefixed targets which bypass Make's dependency
46+
management. The relevant targets follow the standard pattern:
47+
48+
- `do-finish` / `do-final` — runs the finish stage without GDS
49+
- `do-gds` — runs GDS generation (requires KLayout)
50+
51+
An `orfs_gds()` Bazel rule can call `do-gds` independently from
52+
`orfs_flow()`, making KLayout an optional toolchain dependency configured
53+
in `MODULE.bazel`.
54+
55+
## KLayout Tech File Generation
56+
57+
The `do-klayout` and `do-klayout_wrap` targets generate `.lyt` technology
58+
files by substituting LEF and map file paths into platform templates.
59+
This is implemented in `util/generate_klayout_tech.py` using stdlib XML
60+
processing — no KLayout dependency required.
61+
62+
## Testing Without KLayout
63+
64+
Unit tests for all KLayout-related Python scripts use `unittest.mock` to
65+
mock the `pya` API:
66+
67+
```
68+
cd flow/test
69+
python -m unittest test_generate_klayout_tech test_def2stream test_convertDrc
70+
```
71+
72+
These tests cover:
73+
- `.lyt` tech file generation (`test_generate_klayout_tech.py`)
74+
- DEF-to-GDS merging logic (`test_def2stream.py`)
75+
- DRC report conversion (`test_convertDrc.py`)
76+
77+
## Future: OpenROAD `write_gds` and KLayout Coexistence
78+
79+
OpenROAD has an unfinished `write_gds` command. When it is eventually
80+
completed, it would eliminate the KLayout dependency for GDS generation
81+
in many cases. However, KLayout support will live alongside `write_gds`
82+
until there is confidence that `write_gds` is robust and fully covers
83+
all current use cases.
84+
85+
Even then, non-public or future PDKs may have requirements that benefit
86+
from KLayout's mature GDS handling. Whether KLayout support stays in
87+
ORFS indefinitely remains to be seen. Once `write_gds` works robustly
88+
for all of today's use cases, extending it to cover future ones may
89+
turn out to be straightforward.

0 commit comments

Comments
 (0)