From 38e12fa78b98cff362cb88d88f9be6375f75f71f Mon Sep 17 00:00:00 2001 From: Xiretza Date: Fri, 28 Feb 2020 11:21:08 +0100 Subject: [PATCH] Add Makefile and VHDL headers generation --- Makefile | 35 ++++++++++++++++++++++++ generate_entity_headers.py | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 Makefile create mode 100755 generate_entity_headers.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..23658aa --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +ifndef YARM_DIRECTORY +$(error YARM_DIRECTORY must be set to the root directory of the YARM SoC) +endif + +VHDL_DIR = $(YARM_DIRECTORY)/vhdl + +CODEDIR = code + +.PHONY: all +all: Diplomschrift.pdf + +$(CODEDIR): + mkdir -p $@ + +define headers_template = +$1: $2 + mkdir -p $1 + ./generate_entity_headers.py --skip-missing --dest-dir $1 $2 + +HEADER_DIRS += $1 +endef + +$(eval $(call headers_template,core/entities/,$(wildcard $(VHDL_DIR)/core/*.vhd))) + +.PHONY: entity_headers +entity_headers: $(HEADER_DIRS) + +.PHONY: Diplomschrift.pdf +Diplomschrift.pdf: $(HEADER_DIRS) Diplomschrift.tex + latexmk --pdf --pdflatex="pdflatex -interaction=nonstopmode" --use-make Diplomschrift.tex + +.PHONY: clean +clean: + rm -rf $(HEADER_DIRS) + latexmk -C diff --git a/generate_entity_headers.py b/generate_entity_headers.py new file mode 100755 index 0000000..d354a81 --- /dev/null +++ b/generate_entity_headers.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +import argparse +import logging +import os +import re +import sys + +#declaration_match = re.compile(r'^entity (?P.*?) is$.*?^end (?:(?P=entity_name)|entity);$', re.MULTILINE | re.DOTALL) +declaration_match = re.compile(r'^entity (?P.*?) is$.*?^end (?P=entity_name);$', re.MULTILINE | re.DOTALL) + +def directory_path(string): + if os.path.isdir(string): + return string + else: + raise NotADirectoryError(string) + +def get_parser(): + parser = argparse.ArgumentParser() + parser.add_argument('-d', '--dest-dir', help='The destination directory to store the entity headers in', type=directory_path) + parser.add_argument('source_files', nargs='*', type=argparse.FileType('r')) + parser.add_argument('--pattern', default='{entity_name}_entity.vhd', help='The pattern to use for the destination filename. Available substitutions: {entity_name}.') + parser.add_argument('-s', '--skip-missing', action='store_true', help='Skip over files missing an entity declaration') + parser.add_argument('-v', '--verbose', action='count') + + return parser + +def parse_header(src_file): + if m := re.search(declaration_match, src_file.read()): + return (m.group('entity_name'), m.group(0)) + else: + return None + +if __name__ == '__main__': + parser = get_parser() + args = parser.parse_args() + + logging.basicConfig(level=args.verbose) + + for src_file in args.source_files: + if res := parse_header(src_file): + (entity_name, declaration) = res + dest_file_path = os.path.join(args.dest_dir, args.pattern.format_map({'entity_name': entity_name})) + logging.info(f'Writing header for {entity_name} to {dest_file_path}') + + with open(dest_file_path, 'w') as destfile: + destfile.write(declaration) + else: + errmsg = f'No entity declaration found in {src_file.name}' + if args.skip_missing: + logging.warning(errmsg) + else: + logging.error(errmsg) + sys.exit(1) +