Skip to content

Commit be30d43

Browse files
authored
Merge pull request #52 from antmicro/skin_yosys
Add skin and yosys_script options to verilog-diagram directive
2 parents 6f4ec0e + 32819ae commit be30d43

File tree

15 files changed

+2896
-19
lines changed

15 files changed

+2896
-19
lines changed

.travis.yml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@ language: minimal
33
before_install:
44
- make env
55

6-
script:
7-
- make test
8-
- make build
6+
stages:
7+
- name:
8+
- Test
9+
- Build
10+
11+
jobs:
12+
- stage: Tests
13+
name: "Test :skin: option"
14+
script:
15+
- source env/conda/bin/activate sphinxcontrib-verilog-diagrams
16+
- cd tests && python3 -m unittest test.TestSkins
17+
18+
- stage: Tests
19+
name: "Test :yosys_script: option"
20+
script:
21+
- source env/conda/bin/activate sphinxcontrib-verilog-diagrams
22+
- cd tests && python3 -m unittest test.TestYosysScript
23+
24+
- stage: Build
25+
name: "Build"
26+
script:
27+
- make build
928

1029
before_deploy:
1130
- make clean

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ upload: build | $(CONDA_ENV_PYTHON)
6262

6363
test: $(VERSION_PY) | $(CONDA_ENV_PYTHON)
6464
$(IN_CONDA_ENV) cd docs; make html
65+
$(IN_CONDA_ENV) cd tests; make test
6566

6667
.PHONY: test

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ Check out the `examples <https://sphinxcontrib-verilog-diagrams.readthedocs.io/e
101101

102102
.. code-block:: rst
103103
104-
105104
.. verilog-diagram:: file.v
106105
:type: XXXXX
107106
:module: XXXX
107+
:skin: XXXX
108+
:yosys_script: XXXX
108109
:flatten:
109110
110111
Options

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ sphinx
44

55
# Needed to upload to PyPi
66
twine
7+
8+
# Needed for tests
9+
jinja2

sphinxcontrib_verilog_diagrams/__init__.py

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class VerilogDiagram(Directive):
152152
'type': str,
153153
'module': str,
154154
'flatten': bool,
155+
'skin': str,
156+
'yosys_script': str,
155157

156158
'alt': directives.unchanged,
157159
'align': align_spec,
@@ -199,6 +201,26 @@ def run(self):
199201
if 'align' in self.options:
200202
node['align'] = self.options['align']
201203

204+
yosys_script = self.options.get('yosys_script', None)
205+
if yosys_script not in [None, 'default']:
206+
_, yosys_script_filename = env.relfn2path(yosys_script)
207+
if not path.exists(yosys_script_filename):
208+
raise VerilogDiagramError("Yosys script {} does not exist!".format(yosys_script_filename))
209+
else:
210+
node['options']['yosys_script'] = yosys_script_filename
211+
else:
212+
node['options']['yosys_script'] = yosys_script
213+
214+
skin = self.options.get('skin', None)
215+
if skin not in [None, 'default']:
216+
_, skin_filename = env.relfn2path(skin)
217+
if not os.path.exists(skin_filename):
218+
raise VerilogDiagramError("Skin file {} does not exist!".format(skin_filename))
219+
else:
220+
node['options']['skin'] = skin_filename
221+
else:
222+
node['options']['skin'] = skin
223+
202224
caption = self.options.get('caption')
203225
if caption:
204226
node = figure_wrapper(self, node, caption)
@@ -213,9 +235,11 @@ def run_yosys(src, cmd):
213235
subprocess.check_output(ycmd, shell=True)
214236

215237

216-
def diagram_yosys(ipath, opath, module='top', flatten=False):
238+
def diagram_yosys(ipath, opath, module='top', flatten=False, yosys_script='default'):
217239
assert path.exists(ipath), 'Input file missing: {}'.format(ipath)
218240
assert not path.exists(opath), 'Output file exists: {}'.format(opath)
241+
if yosys_script != 'default':
242+
assert path.exists(yosys_script), 'Yosys script file missing: {}'.format(yosys_script)
219243
oprefix, oext = path.splitext(opath)
220244
assert oext.startswith('.'), oext
221245
oext = oext[1:]
@@ -225,33 +249,45 @@ def diagram_yosys(ipath, opath, module='top', flatten=False):
225249
else:
226250
flatten = ''
227251

252+
if yosys_script == 'default':
253+
yosys_script_cmd = ""
254+
else:
255+
yosys_script_cmd = "script {}".format(yosys_script)
256+
228257
run_yosys(
229258
src=ipath,
230259
cmd = """\
231-
prep -top {top} {flatten}; cd {top}; show -format {fmt} -prefix {oprefix}
232-
""".format(top=module, flatten=flatten, fmt=oext, oprefix=oprefix).strip(),
260+
prep -top {top} {flatten}; cd {top}; {script}; show -format {fmt} -prefix {oprefix}
261+
""".format(top=module, flatten=flatten, fmt=oext, oprefix=oprefix, script=yosys_script_cmd).strip(),
233262
)
234263

235264
assert path.exists(opath), 'Output file {} was not created!'.format(oopath)
236265
print('Output file created: {}'.format(opath))
237266

238-
239-
def run_netlistsvg(ipath, opath, skin=None):
267+
def run_netlistsvg(ipath, opath, skin='default'):
240268
assert path.exists(ipath), 'Input file missing: {}'.format(ipath)
241269
assert not path.exists(opath), 'Output file exists: {}'.format(opath)
270+
if skin != 'default':
271+
assert path.exists(skin), 'Skin file missing: {}'.format(skin)
242272

243273
netlistsvg_cmd = "netlistsvg {ipath} -o {opath}".format(ipath=ipath, opath=opath)
244-
if skin:
274+
if skin != 'default':
245275
netlistsvg_cmd += " --skin {skin}".format(skin=skin)
276+
277+
print("Running netlistsvg:", netlistsvg_cmd)
246278
subprocess.check_output(netlistsvg_cmd, shell=True)
247279

248280
assert path.exists(opath), 'Output file {} was not created!'.format(opath)
249281
print('netlistsvg - Output file created: {}'.format(opath))
250282

251283

252-
def diagram_netlistsvg(ipath, opath, module='top', flatten=False):
284+
def diagram_netlistsvg(ipath, opath, module='top', flatten=False, yosys_script='default', skin='default'):
253285
assert path.exists(ipath), 'Input file missing: {}'.format(ipath)
254286
assert not path.exists(opath), 'Output file exists: {}'.format(opath)
287+
if yosys_script != 'default':
288+
assert path.exists(yosys_script), 'Yosys script file missing: {}'.format(yosys_script)
289+
if skin != 'default':
290+
assert path.exists(skin), 'Skin file missing: {}'.format(skin)
255291
oprefix, oext = path.splitext(opath)
256292
assert oext.startswith('.'), oext
257293
oext = oext[1:]
@@ -261,22 +297,27 @@ def diagram_netlistsvg(ipath, opath, module='top', flatten=False):
261297
else:
262298
flatten = ''
263299

300+
if yosys_script == 'default':
301+
yosys_script_cmd = ""
302+
else:
303+
yosys_script_cmd = "script {}".format(yosys_script)
304+
264305
ojson = oprefix + '.json'
265306
if path.exists(ojson):
266307
os.remove(ojson)
267308

268309
run_yosys(
269310
src=ipath,
270311
cmd = """\
271-
prep -top {top} {flatten}; cd {top}; write_json {ojson}
272-
""".format(top=module, flatten=flatten, ojson=ojson).strip())
312+
prep -top {top} {flatten}; cd {top}; {script}; write_json {ojson}
313+
""".format(top=module, flatten=flatten, ojson=ojson, script=yosys_script_cmd).strip())
273314
assert path.exists(ojson), 'Output file {} was not created!'.format(ojson)
274315

275-
run_netlistsvg(ojson, opath)
316+
run_netlistsvg(ojson, opath, skin)
276317
print('netlistsvg - Output file created: {}'.format(ojson))
277318

278319

279-
def render_diagram(self, code, options, format):
320+
def render_diagram(self, code, options, format, skin, yosys_script):
280321
# type: (nodes.NodeVisitor, unicode, Dict, unicode, unicode) -> Tuple[unicode, unicode]
281322
"""Render verilog_diagram code into a PNG or SVG output file."""
282323

@@ -292,14 +333,17 @@ def render_diagram(self, code, options, format):
292333

293334
ensuredir(path.dirname(outfn))
294335

336+
yosys_script = options['yosys_script'] if options['yosys_script'] is not None else yosys_script
337+
skin = options['skin'] if options['skin'] is not None else skin
338+
295339
diagram_type = options['type']
296340
if diagram_type.startswith('yosys'):
297341
assert diagram_type.startswith('yosys-'), diagram_type
298342
diagram_yosys(
299-
verilog_path, outfn, module=options['module'], flatten=options['flatten'])
343+
verilog_path, outfn, module=options['module'], flatten=options['flatten'], yosys_script=yosys_script)
300344
elif diagram_type == 'netlistsvg':
301345
diagram_netlistsvg(
302-
verilog_path, outfn, module=options['module'], flatten=options['flatten'])
346+
verilog_path, outfn, module=options['module'], flatten=options['flatten'], skin=skin)
303347
else:
304348
raise Exception('Invalid diagram type "%s"' % diagram_type)
305349
#raise self.severe(\n' %
@@ -311,13 +355,21 @@ def render_diagram(self, code, options, format):
311355
def render_diagram_html(
312356
self, node, code, options, imgcls=None, alt=None):
313357
# type: (nodes.NodeVisitor, verilog_diagram, unicode, Dict, unicode, unicode, unicode) -> Tuple[unicode, unicode] # NOQA
314-
format = self.builder.config.verilog_diagram_output_format
315358

359+
yosys_script = self.builder.config.verilog_diagram_yosys_script
360+
if yosys_script != 'default' and not path.exists(yosys_script):
361+
raise VerilogDiagramError("Yosys script file {} does not exist! Change verilog_diagram_yosys_script variable".format(yosys_script))
362+
363+
skin = self.builder.config.verilog_diagram_skin
364+
if skin != 'default' and not path.exists(skin):
365+
raise VerilogDiagramError("Skin file {} does not exist! Change verilog_diagram_skin variable".format(skin))
366+
367+
format = self.builder.config.verilog_diagram_output_format
316368
try:
317369
if format not in ('png', 'svg'):
318370
raise VerilogDiagramError("verilog_diagram_output_format must be one of 'png', "
319371
"'svg', but is %r" % format)
320-
fname, outfn = render_diagram(self, code, options, format)
372+
fname, outfn = render_diagram(self, code, options, format, skin, yosys_script)
321373
except VerilogDiagramError as exc:
322374
logger.warning('verilog_diagram code %r: ' % code + str(exc))
323375
raise nodes.SkipNode
@@ -432,5 +484,7 @@ def setup(app):
432484
app.add_directive('verilog-diagram', VerilogDiagram)
433485
app.add_directive('no-license', NoLicenseInclude)
434486
app.add_config_value('verilog_diagram_output_format', 'svg', 'html')
487+
app.add_config_value('verilog_diagram_skin', 'default', 'html')
488+
app.add_config_value('verilog_diagram_yosys_script', 'default', 'html')
435489
return {'version': '1.0', 'parallel_read_safe': True}
436490

tests/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test:
2+
make clean
3+
python3 test.py
4+
5+
clean:
6+
-rm -rf build
7+
8+
.PHONY: test clean

tests/conf.py.template

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# This file only contains a selection of the most common options. For a full
4+
# list see the documentation:
5+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
6+
7+
# -- Path setup --------------------------------------------------------------
8+
9+
# If extensions (or modules to document with autodoc) are in another directory,
10+
# add these directories to sys.path here. If the directory is relative to the
11+
# documentation root, use os.path.abspath to make it absolute, like shown here.
12+
#
13+
import os
14+
import sys
15+
16+
sys.path.insert(0, {{ verilog_diagrams_path }})
17+
18+
# -- Project information -----------------------------------------------------
19+
20+
project = 'Sphinx Verilog Diagrams Tests'
21+
copyright = '2020, SymbiFlow Team'
22+
author = 'SymbiFlow Team'
23+
24+
# The full version, including alpha/beta/rc tags
25+
release = '0.1'
26+
27+
28+
# -- General configuration ---------------------------------------------------
29+
30+
# Add any Sphinx extension module names here, as strings. They can be
31+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
32+
# ones.
33+
extensions = [
34+
'sphinxcontrib_verilog_diagrams',
35+
]
36+
37+
# Add any paths that contain templates here, relative to this directory.
38+
templates_path = ['_templates']
39+
40+
# List of patterns, relative to source directory, that match files and
41+
# directories to ignore when looking for source files.
42+
# This pattern also affects html_static_path and html_extra_path.
43+
exclude_patterns = []
44+
45+
46+
# -- Options for HTML output -------------------------------------------------
47+
48+
# The theme to use for HTML and HTML Help pages. See the documentation for
49+
# a list of builtin themes.
50+
#
51+
html_theme = 'alabaster'
52+
53+
# Add any paths that contain custom static files (such as style sheets) here,
54+
# relative to this directory. They are copied after the builtin static files,
55+
# so a file named "default.css" will overwrite the builtin "default.css".
56+
57+
master_doc = {{ master_doc }}
58+
{{ custom_variables }}

0 commit comments

Comments
 (0)