-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prototype a new CLI #904
Draft
whitequark
wants to merge
4
commits into
amaranth-lang:main
Choose a base branch
from
whitequark:amaranth-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Prototype a new CLI #904
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0e6378b
amaranth._cli: prototype. (WIP)
whitequark 797cffd
lib.fifo: annotate for use with CLI. (WIP)
whitequark 7ff2e44
back.rtlil: put hierarchy in module name instead of an attribute.
whitequark 1fd9388
Improve Amaranth CLI message when given component name fails to match…
cr1901 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
""" | ||
This file is not a part of the Amaranth module tree because the CLI needs to emit Make-style | ||
dependency files as a part of the generation process. In order for `from amaranth import *` | ||
to work as a prelude, it has to load several of the files under `amaranth/`, which means | ||
these will not be loaded later in the process, and not recorded as dependencies. | ||
""" | ||
|
||
import importlib | ||
import argparse | ||
import stat | ||
import sys | ||
import os | ||
import re | ||
|
||
|
||
def _build_parser(): | ||
def component(reference): | ||
from amaranth import Elaboratable | ||
|
||
if m := re.match(r"(\w+(?:\.\w+)*):(\w+(?:\.\w+)*)", reference, re.IGNORECASE|re.ASCII): | ||
mod_name, qual_name = m[1], m[2] | ||
try: | ||
obj = importlib.import_module(mod_name) | ||
except ImportError as e: | ||
raise argparse.ArgumentTypeError(f"{mod_name!r} does not refer to " | ||
"an importable Python module") from e | ||
try: | ||
for attr in qual_name.split("."): | ||
obj = getattr(obj, attr) | ||
except AttributeError as e: | ||
raise argparse.ArgumentTypeError(f"{qual_name!r} does not refer to an object " | ||
f"within the {mod_name!r} module") from e | ||
if not issubclass(obj, Elaboratable): | ||
raise argparse.ArgumentTypeError(f"'{qual_name}:{mod_name}' refers to an object that is not elaboratable") | ||
return obj | ||
else: | ||
raise argparse.ArgumentTypeError(f"{reference!r} is not a Python object reference") | ||
|
||
parser = argparse.ArgumentParser( | ||
"amaranth", description=""" | ||
Amaranth HDL command line interface. | ||
""") | ||
operation = parser.add_subparsers( | ||
metavar="OPERATION", help="operation to perform", | ||
dest="operation", required=True) | ||
|
||
op_generate = operation.add_parser( | ||
"generate", help="generate code in a different language from Amaranth code", | ||
aliases=("gen", "g")) | ||
op_generate.add_argument( | ||
metavar="COMPONENT", help="Amaranth component to convert, e.g. `pkg.mod:Cls`", | ||
dest="component", type=component) | ||
op_generate.add_argument( | ||
"-n", "--name", metavar="NAME", help="name of the toplevel module, also prefixed to others", | ||
dest="name", type=str, default=None) | ||
op_generate.add_argument( | ||
"-p", "--param", metavar=("NAME", "VALUE"), help="parameter(s) for the component", | ||
dest="params", nargs=2, type=str, action="append", default=[]) | ||
gen_language = op_generate.add_subparsers( | ||
metavar="LANGUAGE", help="language to generate code in", | ||
dest="language", required=True) | ||
|
||
lang_verilog = gen_language.add_parser( | ||
"verilog", help="generate Verilog code") | ||
lang_verilog.add_argument( | ||
"-v", metavar="VERILOG-FILE", help="Verilog file to write", | ||
dest="verilog_file", type=argparse.FileType("w")) | ||
lang_verilog.add_argument( | ||
"-d", metavar="DEP-FILE", help="Make-style dependency file to write", | ||
dest="dep_file", type=argparse.FileType("w")) | ||
|
||
return parser | ||
|
||
|
||
def main(args=None): | ||
# Hook the `open()` function to find out which files are being opened by Amaranth code. | ||
files_being_opened = set() | ||
special_file_opened = False | ||
def dep_audit_hook(event, args): | ||
nonlocal special_file_opened | ||
if files_being_opened is not None and event == "open": | ||
filename, mode, flags = args | ||
if mode is None or "r" in mode or "+" in mode: | ||
if isinstance(filename, bytes): | ||
filename = filename.decode("utf-8") | ||
if isinstance(filename, str) and stat.S_ISREG(os.stat(filename).st_mode): | ||
files_being_opened.add(filename) | ||
else: | ||
special_file_opened = True | ||
sys.addaudithook(dep_audit_hook) | ||
|
||
# Parse arguments and instantiate components | ||
args = _build_parser().parse_args(args) | ||
if args.operation in ("generate", "gen", "g"): | ||
params = dict(args.params) | ||
whitequark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
params = {name: cls(params[name]) | ||
for name, cls in args.component.__init__.__annotations__.items()} | ||
component = args.component(**params) | ||
|
||
# Capture the set of opened files, as well as the loaded Python modules. | ||
files_opened, files_being_opened = files_being_opened, None | ||
modules_after = list(sys.modules.values()) | ||
|
||
# Remove *.pyc files from the set of open files and replace them with their *.py equivalents. | ||
dep_files = set() | ||
dep_files.update(files_opened) | ||
for module in modules_after: | ||
if getattr(module, "__spec__", None) is None: | ||
continue | ||
if module.__spec__.cached in dep_files: | ||
dep_files.discard(module.__spec__.cached) | ||
dep_files.add(module.__spec__.origin) | ||
|
||
if args.operation in ("generate", "gen", "g"): | ||
if args.language == "verilog": | ||
# Generate Verilog file with `-v` or without arguments. | ||
if args.verilog_file or not (args.verilog_file or args.dep_file): | ||
from amaranth.back.verilog import convert | ||
code = convert(component, name=(args.name or args.component.__name__),) | ||
(args.verilog_file or sys.stdout).write(code) | ||
|
||
# Generate dependency file with `-d`. | ||
if args.verilog_file and args.dep_file: | ||
args.dep_file.write(f"{args.verilog_file.name}:") | ||
if not special_file_opened: | ||
for file in sorted(dep_files): | ||
args.dep_file.write(f" \\\n {file}") | ||
args.dep_file.write("\n") | ||
else: | ||
args.dep_file.write(f"\n.PHONY: {args.verilog_file.name}\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It took me a while to realize that by "object reference", the error means "your input argument fails to match a regex", rather than "Python couldn't find an object with that name" (which is what the
ImportError
exception handler is for).Perhaps this
else
branch could have "expected format ispath.to.mod:Obj
" or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep this could be improved!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Care to submit a PR against a PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do in the coming days (my response time will be sporadic until possibly this Monday afternoon).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR is done: whitequark#1