59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Print a help menu of this project's make targets.
|
||
|
|
|
||
|
|
Parses the repo Makefile and lists every target with the first line of its
|
||
|
|
preceding comment block as the description. Self-maintaining: add a target plus
|
||
|
|
a comment and it appears here automatically -- no list to keep in sync.
|
||
|
|
|
||
|
|
Run via `make help` (GNU make reserves `make --help` for its own options, so a
|
||
|
|
project cannot override that flag).
|
||
|
|
"""
|
||
|
|
import os
|
||
|
|
import re
|
||
|
|
|
||
|
|
# The Makefile lives at the repo root; this script is in scripts/.
|
||
|
|
MAKEFILE = os.path.join(
|
||
|
|
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "Makefile"
|
||
|
|
)
|
||
|
|
|
||
|
|
# A target line: name at column 0, optional whitespace, ':' NOT immediately
|
||
|
|
# followed by '=' (which would make it a variable assignment like `HUGO := hugo`).
|
||
|
|
# Names starting with '.' (e.g. the .PHONY line) are excluded.
|
||
|
|
TARGET_RE = re.compile(r"^([A-Za-z0-9_-]+)[ \t]*:(?!=)")
|
||
|
|
|
||
|
|
|
||
|
|
def parse_targets(path):
|
||
|
|
"""Return an ordered list of (name, first_comment_line) for each target."""
|
||
|
|
targets = []
|
||
|
|
comment_buf = []
|
||
|
|
with open(path) as f:
|
||
|
|
for line in f:
|
||
|
|
stripped = line.lstrip()
|
||
|
|
if stripped.startswith("#"):
|
||
|
|
comment_buf.append(stripped.lstrip("#").strip())
|
||
|
|
continue
|
||
|
|
if not line[0].isspace():
|
||
|
|
m = TARGET_RE.match(line)
|
||
|
|
if m:
|
||
|
|
targets.append((m.group(1), comment_buf[0] if comment_buf else ""))
|
||
|
|
comment_buf = []
|
||
|
|
continue
|
||
|
|
# Any other non-comment line resets the pending comment block.
|
||
|
|
comment_buf = []
|
||
|
|
return targets
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
targets = parse_targets(MAKEFILE)
|
||
|
|
width = max((len(name) for name, _ in targets), default=0)
|
||
|
|
print("Liberty Tools -- available make targets:")
|
||
|
|
print()
|
||
|
|
for name, desc in targets:
|
||
|
|
print(f" make {name:<{width}} {desc}")
|
||
|
|
print()
|
||
|
|
print("Note: `make --help` is reserved by GNU make; use `make help` for this menu.")
|
||
|
|
print("Toolchain is pinned in .tools/ (run `make bootstrap` once). See README.md for details.")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|