Skip to content

Commit 7bccdb7

Browse files
committed
[nfc] Add ability to test code snippets
This demonstrates a flow that can be used to validate that all code snippets used in the FIRRTL specification can be compiled. This is implemented using a simple pandoc filter to extract all FIRRTL code blocks, write them to the build directory, and then run them all through firtool. This may be a little cumbersome as every code example has to be completely legal FIRRTL. I.e., this would require a version and circuit on every code example. This may not be such a bad thing. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent 35de1a6 commit 7bccdb7

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ IMG_SRCS=$(shell find include/img_src/ -type f -name '*.dot')
55
IMG_EPSS=$(IMG_SRCS:include/img_src/%.dot=build/%.eps)
66
IMG_PNG=$(IMG_SRCS:include/img_src/%.dot=build/%.png)
77

8-
.PHONY: all clean format images
8+
.PHONY: all clean format images test
99
.PRECIOUS: build/ build/img/
1010

1111
all: build/spec.pdf build/abi.pdf
@@ -18,6 +18,10 @@ format:
1818

1919
images: $(IMG_EPSS) $(IMG_PNGS)
2020

21+
test: abi.md spec.md | build/
22+
pandoc --filter scripts/extract-firrtl-code.py $^ -o /dev/null
23+
find -s build/ -type f -name '*.fir' | xargs -n1 firtool -parse-only -o /dev/null
24+
2125
PANDOC_FLAGS=\
2226
--pdf-engine=latexmk \
2327
--pdf-engine-opt=-logfilewarninglist \

scripts/extract-firrtl-code.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
3+
from pandocfilters import toJSONFilter, CodeBlock
4+
5+
index = 0
6+
7+
def extractFIRRTLCode(key, value, format, meta):
8+
global index
9+
if key == 'CodeBlock':
10+
[[ident, classes, keyvals], code] = value
11+
if 'firrtl' in classes:
12+
with open(f'build/code-example-{index:03d}.fir', 'w') as f:
13+
f.write(code)
14+
index += 1
15+
16+
if __name__ == "__main__":
17+
toJSONFilter(extractFIRRTLCode)

0 commit comments

Comments
 (0)