verilator/examples/xml_py/vl_file_copy

125 lines
4.3 KiB
Plaintext
Raw Normal View History

2019-11-02 20:35:50 +00:00
#!/usr/bin/env python3
# -*- Python -*- See copyright, etc below
######################################################################
import argparse
import os
import re
import subprocess
import sys
import tempfile
import xml.etree.ElementTree as ET
from shutil import copy2
2020-12-19 03:34:14 +00:00
from pprint import pprint, pformat
2019-11-02 20:35:50 +00:00
#######################################################################
2020-12-19 03:34:14 +00:00
2019-11-02 20:35:50 +00:00
class VlFileCopy:
2020-12-19 03:34:14 +00:00
def __init__(
2020-12-19 03:55:46 +00:00
self,
verilator_args, # presently all verilator options are passed-thru
# ideally this script would check against options mentioned in help
2020-12-19 03:34:14 +00:00
debug=0,
2020-12-19 03:55:46 +00:00
output_dir='copied'): # directory name we output file uses
2019-11-02 20:35:50 +00:00
self.debug = debug
xml_temp = tempfile.NamedTemporaryFile()
2020-12-19 03:34:14 +00:00
args = [
'--xml-output',
xml_temp.name,
'--bbox-sys', # Parse some stuff can't translate
'--bbox-unsup',
'--prefix vlxml'
] # So we know name of .xml output
2019-11-02 20:35:50 +00:00
args += verilator_args
self.run_verilator(args)
self.tree = ET.parse(xml_temp.name)
os.makedirs(output_dir, 0o777, True)
# Find and copy the files
root = self.tree.getroot()
xfiles = root.find('files')
for xfile in xfiles.findall('file'):
filename = xfile.get('filename')
if not re.match('^<', filename): # e.g. <built-in>
if self.debug:
print("\tcp %s %s" % (filename, output_dir))
copy2(filename, output_dir)
def run_verilator(self, args):
"""Run Verilator command, check errors"""
if os.getenv("VERILATOR_ROOT"):
command = os.getenv("VERILATOR_ROOT") + "/bin/verilator"
else:
command = "verilator"
command += ' ' + ' '.join(args)
if self.debug:
print("\t%s " % command)
status = subprocess.call(command, shell=True)
if status != 0:
2020-12-19 03:34:14 +00:00
raise Exception("Command failed running Verilator with '" +
command + "', stopped")
2019-11-02 20:35:50 +00:00
#######################################################################
2020-12-19 03:34:14 +00:00
if __name__ == '__main__':
2019-11-02 20:35:50 +00:00
parser = argparse.ArgumentParser(
allow_abbrev=False,
formatter_class=argparse.RawTextHelpFormatter,
description=
2020-12-19 03:34:14 +00:00
"""Example of using Verilator XML output to copy a list of files to an
2019-11-02 20:35:50 +00:00
output directory (-odir, defaults to 'copied'), e.g. to easily create a
tarball of the design to pass to others.
Example usage:
vl_file_copy -f input.vc top.v -odir mycopy
# This will make at least mycopy/top.v
""",
2020-12-19 03:34:14 +00:00
epilog="""All other arguments are pass-thru to Verilator: e.g.:
2019-11-02 20:35:50 +00:00
+define+<var>=<value> Set preprocessor define
-F <file> Parse options from a file, relatively
-f <file> Parse options from a file
-G<name>=<value> Overwrite toplevel parameter
+incdir+<dir> Directory to search for includes
+libext+<ext>+[ext]... Extensions for finding modules
-v <filename> Verilog library
-y <dir> Directory to search for modules
This file ONLY is placed under the Creative Commons Public Domain, for
any use, without warranty, 2019 by Wilson Snyder.
SPDX-License-Identifier: CC0-1.0
2020-12-19 03:34:14 +00:00
""")
parser.add_argument('-debug',
'--debug',
action='store_const',
const=9,
2019-11-02 20:35:50 +00:00
help='enable debug')
2020-12-19 03:34:14 +00:00
parser.add_argument('-odir',
'--odir',
action='store',
metavar='directory',
required=True,
2019-11-02 20:35:50 +00:00
help='target output directory')
(args, rem) = parser.parse_known_args()
2020-12-19 03:34:14 +00:00
print(
"NOTE: vl_file_copy is only an example starting point for writing your own tool."
)
2019-11-02 20:35:50 +00:00
# That is:
# 1. We will accept basic patches
# 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir)
# 3. "make install" will not install this.
# 4. This has not had production-worthy validation.
2020-12-19 03:34:14 +00:00
fc = VlFileCopy(output_dir=args.odir, debug=args.debug, verilator_args=rem)
2019-11-02 20:35:50 +00:00
######################################################################
### Local Variables:
### compile-command: "./vl_file_copy -h ; VERILATOR_ROOT=$V4 ./vl_file_copy +define+thru top.v"
### End: