From ea998b7db04b4db51215f07b2557714a5842ae80 Mon Sep 17 00:00:00 2001 From: Dennis Date: Sat, 8 May 2021 11:17:56 +0200 Subject: [PATCH] Add argparse.ArgumentParser based CliArguments class This wrapper lives in a seperate module and class so that possible processing of arguments can be done before the config dictionary gets passed to the compiler. --- srdl2sv/cli/__init__.py | 0 srdl2sv/cli/cli.py | 63 +++++++++++++++++++++++++++++++++++++++++ srdl2sv/main.py | 20 +++++++------ 3 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 srdl2sv/cli/__init__.py create mode 100644 srdl2sv/cli/cli.py diff --git a/srdl2sv/cli/__init__.py b/srdl2sv/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/srdl2sv/cli/cli.py b/srdl2sv/cli/cli.py new file mode 100644 index 0000000..464b68c --- /dev/null +++ b/srdl2sv/cli/cli.py @@ -0,0 +1,63 @@ +import argparse +import os +from itertools import chain + +class CliArguments(): + def __init__(self): + self.parser = argparse.ArgumentParser( + description="SystemRDL 2 SystemVerilog compiler") + + self.parser.add_argument( + "-v", + "--verbosity", + action="count", + help="Increase output verbosity.") + + self.parser.add_argument( + "-q", + "--quiet", + action="store_true") + + self.parser.add_argument( + "-o", + "--out_dir", + type=str, + help="Define output directory to dump files.\ + If directory is non-existent, it will be created.") + + self.parser.add_argument( + "-d", + "--search_paths", + type=str, + nargs="+", + help="Point to one (or more) directories that will\ + be searched for RDL files.") + + self.parser.add_argument( + "-r", + "--recursive_search", + action="store_true", + help="If set, the dependency directories will be\ + searched recursively."); + + self.parser.add_argument( + "IN_RDL", + type=str, + nargs="+", + help="Location of RDL file(s) with root addrmap.") + + def get_config(self) -> dict(): + args = self.parser.parse_args() + + config = dict() + + config['input_file'] = args.IN_RDL + config['verbosity'] = args.verbosity + config['quiet'] = args.quiet + + if args.recursive_search: + config['search_paths'] = [x[0] for y in args.search_paths for x in os.walk(y)] + else: + config['search_paths'] = args.search_paths + + return config diff --git a/srdl2sv/main.py b/srdl2sv/main.py index d476e37..815cec6 100755 --- a/srdl2sv/main.py +++ b/srdl2sv/main.py @@ -1,27 +1,31 @@ #!/usr/bin/env python3 +# Standard modules import sys -import os -import re import time -from systemrdl import RDLCompiler, RDLCompileError, RDLWalker, RDLListener, node -from systemrdl.node import FieldNode +# Imported modules +from systemrdl import RDLCompiler, RDLCompileError +# Local modules from components.addrmap import AddrMap - +from cli.cli import CliArguments if __name__ == "__main__": # Take start timestamp start = time.time() + # Construct command line arguments + cli_arguments = CliArguments() + config = cli_arguments.get_config() + # Compile and elaborate files provided from the command line - input_files = sys.argv[1:] rdlc = RDLCompiler() try: - for input_file in input_files: - rdlc.compile_file(input_file) + for input_file in config['input_file']: + rdlc.compile_file( + input_file, incl_search_paths=config['search_paths']) root = rdlc.elaborate() except RDLCompileError: