Skip to content

Commit 027f374

Browse files
committed
Add global variable for yosys type selection
Signed-off-by: Robert Winkler <rwinkler@antmicro.com>
1 parent 0150f15 commit 027f374

File tree

3 files changed

+85
-37
lines changed

3 files changed

+85
-37
lines changed

README.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,19 @@ Required
8282
.. |yosys| replace:: ``yosys``
8383
.. _yosys: https://github.com/YosysHQ/yosys
8484

85-
By default, `verilog-diagram` uses the `yowasp-yosys` package provided in PyPI. It can be installed by running `pip install -r requirements.txt`.
86-
However, you could also use the `yosys` that is installed on your system, by adding the following line in `setup(app)` inside conf.py.
85+
By default, ``verilog-diagram`` uses the ``yowasp-yosys`` package provided in PyPI.
86+
It can be installed by running ``pip install -r requirements.txt``.
87+
However, you could also use Yosys that is installed on your system,
88+
or point to the specific Yosys binary using ``verilog_diagram_yosys`` variable
89+
in the Sphinx ``conf.py`` file:
8790

88-
.. code-block:: py
91+
To use Yosys that is available in your system, use the following setting::
8992

90-
def setup(app):
91-
...
92-
VerilogDiagram.use_yowasp = False
93-
...
93+
verilog_diagram_yosys = "system"
9494

95+
If you want to point to the specific Yosys binary, provide the path to the program::
96+
97+
verilog_diagram_yosys = "<path-to-Yosys>"
9598

9699
Optional
97100
~~~~~~~~

docs/conf.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#
3838
import os
3939
import sys
40-
from sphinxcontrib_verilog_diagrams import VerilogDiagram
4140
sys.path.insert(0, os.path.abspath('..'))
4241

4342
# -- General configuration ------------------------------------------------
@@ -252,7 +251,3 @@
252251

253252
# Example configuration for intersphinx: refer to the Python standard library.
254253
intersphinx_mapping = {'https://docs.python.org/': None}
255-
256-
def setup(app):
257-
# VerilogDiagram.use_yowasp = False
258-
pass

sphinxcontrib_verilog_diagrams/__init__.py

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ class VerilogDiagram(Directive):
148148

149149
final_argument_whitespace = False
150150

151-
use_yowasp = True
152-
153151
option_spec = {
154152
'type': str,
155153
'module': str,
@@ -162,6 +160,13 @@ class VerilogDiagram(Directive):
162160
'caption': directives.unchanged,
163161
}
164162

163+
global_variable_options = {
164+
"verilog_diagram_output_format": ["svg", "png"],
165+
"verilog_diagram_skin": ["default"], # or path
166+
"verilog_diagram_yosys_script": ["default"], # or path
167+
"verilog_diagram_yosys": ["yowasp", "system"] # or path
168+
}
169+
165170
def run(self):
166171
# type: () -> List[nodes.Node]
167172

@@ -231,24 +236,38 @@ def run(self):
231236
return [node]
232237

233238

234-
def run_yosys(src, cmd):
235-
print("Running yosys: yosys -q -p", "'{}'".format(cmd), src)
236-
if VerilogDiagram.use_yowasp:
239+
def run_yosys(src, cmd, yosys='yowasp'):
240+
if yosys == 'yowasp':
237241
import yowasp_yosys
238242
ycmd = ["-q", "-p", "{}".format(cmd), src]
243+
print("Running YoWASP yosys: {}".format(ycmd))
239244
yowasp_yosys.run_yosys(ycmd)
240-
else:
245+
elif yosys == 'system':
241246
ycmd = "yosys -p '{cmd}' {src}".format(src=src, cmd=cmd)
247+
print("Running yosys: {}".format(ycmd))
242248
subprocess.check_output(ycmd, shell=True)
249+
else:
250+
ycmd = "{yosys} -p '{cmd}' {src}".format(yosys=yosys, src=src, cmd=cmd)
251+
print("Running yosys: {}".format(ycmd))
252+
subprocess.check_output(ycmd, shell=True)
253+
243254

255+
def diagram_yosys(ipath, opath, module='top', flatten=False,
256+
yosys_script='default', yosys='yowasp'):
257+
258+
# Assertions
244259

245-
def diagram_yosys(ipath, opath, module='top', flatten=False, yosys_script='default'):
246260
assert path.exists(ipath), 'Input file missing: {}'.format(ipath)
247261
assert not path.exists(opath), 'Output file exists: {}'.format(opath)
262+
yosys_options = VerilogDiagram.global_variable_options["verilog_diagram_yosys"]
263+
assert yosys in yosys_options or os.path.exists(yosys), "Invalid verilog_diagram_yosys value!"
248264
if yosys_script != 'default':
249265
assert path.exists(yosys_script), 'Yosys script file missing: {}'.format(yosys_script)
250266
oprefix, oext = path.splitext(opath)
251267
assert oext.startswith('.'), oext
268+
269+
# Diagram generation
270+
252271
oext = oext[1:]
253272

254273
if flatten:
@@ -261,18 +280,21 @@ def diagram_yosys(ipath, opath, module='top', flatten=False, yosys_script='defau
261280
else:
262281
yosys_script_cmd = "script {}".format(yosys_script)
263282

264-
run_yosys(
265-
src=ipath,
266-
cmd = """\
267-
prep -top {top} {flatten}; cd {top}; {script}; show -format {fmt} -prefix {oprefix}
268-
""".format(top=module, flatten=flatten, fmt=oext, oprefix=oprefix, script=yosys_script_cmd).strip(),
269-
)
283+
yosys_cmd = "prep -top {top} {flatten}; cd {top}; {script}; show -format {fmt} -prefix {oprefix}".format(
284+
top=module,
285+
flatten=flatten,
286+
fmt=oext,
287+
oprefix=oprefix,
288+
script=yosys_script_cmd
289+
).strip()
290+
run_yosys(ipath, yosys_cmd, yosys)
270291

271-
if VerilogDiagram.use_yowasp:
292+
if yosys == 'yowasp':
272293
# somehow yowasp_yosys fails to execute `dot` to convert the dot file to svg,
273294
# which works on native yosys, perhaps a limitation with wasm
274295
svgdata = subprocess.check_output(["dot", "-Tsvg", "{}.dot".format(oprefix)])
275-
open("{}.svg".format(oprefix), "wb").write(svgdata)
296+
with open("{}.svg".format(oprefix), "wb") as img:
297+
img.write(svgdata)
276298

277299
assert path.exists(opath), 'Output file {} was not created!'.format(oopath)
278300
print('Output file created: {}'.format(opath))
@@ -281,7 +303,7 @@ def run_netlistsvg(ipath, opath, skin='default'):
281303
assert path.exists(ipath), 'Input file missing: {}'.format(ipath)
282304
assert not path.exists(opath), 'Output file exists: {}'.format(opath)
283305
if skin != 'default':
284-
assert path.exists(skin), 'Skin file missing: {}'.format(skin)
306+
assert path.exists(skin), 'Skin file missing: {}'.format(skin)
285307

286308
netlistsvg_cmd = "netlistsvg {ipath} -o {opath}".format(ipath=ipath, opath=opath)
287309
if skin != 'default':
@@ -294,15 +316,23 @@ def run_netlistsvg(ipath, opath, skin='default'):
294316
print('netlistsvg - Output file created: {}'.format(opath))
295317

296318

297-
def diagram_netlistsvg(ipath, opath, module='top', flatten=False, yosys_script='default', skin='default'):
319+
def diagram_netlistsvg(ipath, opath, module='top', flatten=False,
320+
yosys_script='default', skin='default', yosys='yowasp'):
321+
# Assertions
322+
298323
assert path.exists(ipath), 'Input file missing: {}'.format(ipath)
299324
assert not path.exists(opath), 'Output file exists: {}'.format(opath)
325+
yosys_options = VerilogDiagram.global_variable_options["verilog_diagram_yosys"]
326+
assert yosys in yosys_options or os.path.exists(yosys), "Invalid verilog_diagram_yosys value!"
300327
if yosys_script != 'default':
301328
assert path.exists(yosys_script), 'Yosys script file missing: {}'.format(yosys_script)
302329
if skin != 'default':
303330
assert path.exists(skin), 'Skin file missing: {}'.format(skin)
304331
oprefix, oext = path.splitext(opath)
305332
assert oext.startswith('.'), oext
333+
334+
# Diagram generation
335+
306336
oext = oext[1:]
307337

308338
if flatten:
@@ -319,11 +349,14 @@ def diagram_netlistsvg(ipath, opath, module='top', flatten=False, yosys_script='
319349
if path.exists(ojson):
320350
os.remove(ojson)
321351

322-
run_yosys(
323-
src=ipath,
324-
cmd = """\
325-
prep -top {top} {flatten}; cd {top}; {script}; write_json -compat-int {ojson}
326-
""".format(top=module, flatten=flatten, ojson=ojson, script=yosys_script_cmd).strip())
352+
yosys_cmd = """prep -top {top} {flatten}; cd {top}; {script}; write_json {compat} {ojson}""".format(
353+
top=module,
354+
flatten=flatten,
355+
ojson=ojson,
356+
script=yosys_script_cmd,
357+
compat="-compat-int" if yosys == 'yowasp' else ""
358+
).strip()
359+
run_yosys(ipath, yosys_cmd, yosys)
327360
assert path.exists(ojson), 'Output file {} was not created!'.format(ojson)
328361

329362
run_netlistsvg(ojson, opath, skin)
@@ -349,14 +382,31 @@ def render_diagram(self, code, options, format, skin, yosys_script):
349382
yosys_script = options['yosys_script'] if options['yosys_script'] is not None else yosys_script
350383
skin = options['skin'] if options['skin'] is not None else skin
351384

385+
yosys = self.builder.config.verilog_diagram_yosys
386+
yosys_options = VerilogDiagram.global_variable_options["verilog_diagram_yosys"]
387+
if yosys not in yosys_options and not os.path.exists(yosys):
388+
raise VerilogDiagramError("Yosys not found!")
389+
else:
390+
yosys = yosys if yosys in yosys_options else os.path.realpath(yosys)
391+
352392
diagram_type = options['type']
353393
if diagram_type.startswith('yosys'):
354394
assert diagram_type.startswith('yosys-'), diagram_type
355395
diagram_yosys(
356-
verilog_path, outfn, module=options['module'], flatten=options['flatten'], yosys_script=yosys_script)
396+
verilog_path,
397+
outfn,
398+
module=options['module'],
399+
flatten=options['flatten'],
400+
yosys_script=yosys_script,
401+
yosys=yosys)
357402
elif diagram_type == 'netlistsvg':
358403
diagram_netlistsvg(
359-
verilog_path, outfn, module=options['module'], flatten=options['flatten'], skin=skin)
404+
verilog_path,
405+
outfn,
406+
module=options['module'],
407+
flatten=options['flatten'],
408+
skin=skin,
409+
yosys=yosys)
360410
else:
361411
raise Exception('Invalid diagram type "%s"' % diagram_type)
362412
#raise self.severe(\n' %
@@ -499,5 +549,5 @@ def setup(app):
499549
app.add_config_value('verilog_diagram_output_format', 'svg', 'html')
500550
app.add_config_value('verilog_diagram_skin', 'default', 'html')
501551
app.add_config_value('verilog_diagram_yosys_script', 'default', 'html')
552+
app.add_config_value('verilog_diagram_yosys', 'yowasp', 'html')
502553
return {'version': '1.0', 'parallel_read_safe': True}
503-

0 commit comments

Comments
 (0)