Skip to content

Commit

Permalink
Add command line option to inject define macros into RDL preprocessor…
Browse files Browse the repository at this point in the history
… namespace #24
  • Loading branch information
amykyta3 committed Jun 9, 2023
1 parent 8527296 commit 6ec238d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -30,7 +30,7 @@
"systemrdl-compiler >= 1.26.0",
"peakrdl-html >= 2.10.1",
"peakrdl-ipxact >= 3.4.1",
"peakrdl-regblock >= 0.14.0",
"peakrdl-regblock >= 0.15.0",
"peakrdl-systemrdl >= 0.3.0",
"peakrdl-uvm >= 2.3.0",
"tomli;python_version<'3.11'"
Expand Down
12 changes: 11 additions & 1 deletion src/peakrdl/cmd/preprocess.py
Expand Up @@ -3,6 +3,7 @@
from systemrdl import RDLCompiler

from ..subcommand import Subcommand
from ..process_input import parse_defines

if TYPE_CHECKING:
import argparse
Expand All @@ -25,6 +26,14 @@ def add_arguments(self, parser: 'argparse._ActionsContainer', importers: 'List[I
action="append",
help='Search directory for files included with `include "filename"',
)
parser.add_argument(
"-D",
dest="defines",
metavar="MACRO[=VALUE]",
action="append",
default=[],
help="Pre-define a Verilog-style preprocessor macro"
)
grp.add_argument(
"-o",
dest="output",
Expand All @@ -34,6 +43,7 @@ def add_arguments(self, parser: 'argparse._ActionsContainer', importers: 'List[I

def main(self, importers: 'List[ImporterPlugin]', options: 'argparse.Namespace') -> None:
rdlc = RDLCompiler()
f_info = rdlc.preprocess_file(options.file, options.incdirs)
defines = parse_defines(rdlc, options.defines)
f_info = rdlc.preprocess_file(options.file, options.incdirs, defines=defines)
with open(options.output, 'w', encoding='utf-8') as f:
f.write(f_info.preprocessed_text)
23 changes: 23 additions & 0 deletions src/peakrdl/process_input.py
Expand Up @@ -25,6 +25,14 @@ def add_rdl_compile_arguments(parser: 'argparse._ActionsContainer') -> None:
action="append",
help='Search directory for files included with `include "filename"',
)
parser.add_argument(
"-D",
dest="defines",
metavar="MACRO[=VALUE]",
action="append",
default=[],
help="Pre-define a Verilog-style preprocessor macro"
)


def add_importer_arguments(parser: 'argparse._ActionsContainer', importers: 'Sequence[Importer]') -> None:
Expand Down Expand Up @@ -75,8 +83,22 @@ def parse_parameters(rdlc: 'RDLCompiler', parameter_options: List[str]) -> Dict[

return parameters

def parse_defines(rdlc: 'RDLCompiler', define_options: List[str]) -> Dict[str, str]:
defines = {}
for raw_def in define_options:
m = re.fullmatch(r"(\w+)(?:=(.+))?", raw_def)
if not m:
rdlc.msg.fatal(f"Invalid define argument: {raw_def}")

k = m.group(1)
v = m.group(2) or ""
defines[k] = v
return defines


def process_input(rdlc: 'RDLCompiler', importers: 'Sequence[Importer]', input_files: List[str], options: 'argparse.Namespace') -> None:
defines = parse_defines(rdlc, options.defines)

for file in input_files:
if not os.path.exists(file):
rdlc.msg.fatal(f"Input file does not exist: {file}")
Expand All @@ -87,6 +109,7 @@ def process_input(rdlc: 'RDLCompiler', importers: 'Sequence[Importer]', input_fi
rdlc.compile_file(
file,
incl_search_paths=options.incdirs,
defines=defines,
)
else:
# Is foreign input file.
Expand Down

0 comments on commit 6ec238d

Please sign in to comment.