From dcd2f898af9d4efcb22417c19fa1b3abc8c548c5 Mon Sep 17 00:00:00 2001 From: aqua Date: Wed, 7 Aug 2024 17:36:57 +0300 Subject: Added bazel rule for interface_generator --- tools/interface_generator/interface_generator.py | 95 +++++++++++++++--------- 1 file changed, 59 insertions(+), 36 deletions(-) (limited to 'tools/interface_generator/interface_generator.py') diff --git a/tools/interface_generator/interface_generator.py b/tools/interface_generator/interface_generator.py index 4eb8d69..4d3afa4 100755 --- a/tools/interface_generator/interface_generator.py +++ b/tools/interface_generator/interface_generator.py @@ -1,8 +1,15 @@ #!/usr/bin/env python3 +""" +interface_generator.py +""" + from argparse import ArgumentParser -from mako.template import Template +from pathlib import Path +import sys from mako.lookup import TemplateLookup +from interface_definition import parse as parse_interface +from templates import get_templates PROG = { "name": "interface_generator", @@ -10,50 +17,66 @@ PROG = { } -def generate_file(template, kwargs): - """generate file using a tempalte""" +def generate_file(template: Path, templates: Path, output, kwargs): + """generate file using a tempalte and write it to the output location""" lookup = TemplateLookup(directories=[".", "templates"]) - template = lookup.get_template(template) - result = template.render(**kwargs, PROG=PROG) + mako_template = lookup.get_template(str(template.relative_to(templates))) + result = mako_template.render(**kwargs, PROG=PROG) + + output_name = template.stem.replace("interface", kwargs["name"]) + print(f'{kwargs["name"]} via {template.name} => {output_name}') - # print(kwargs) - print(result) + if isinstance(output, Path): + # print(f"writing to {(output / output_name).absolute()}") + with open(output / output_name, "w") as output_file: + print(result, file=output_file) + else: + print(result, file=output) def main(): + """main function""" parser = ArgumentParser( prog="interface_generator", description="Generate a C header file from an interface definition", ) - parser.add_argument("-l", "--license", required=True) - - parser_args = parser.parse_args() - print(parser_args) - - args = { - "name": "stdio", - "license": parser_args.license, - "system_includes": ["stdarg.h"], - "types": [ - { - "name": "FILE", - "members": [ - "int fd", - "int (*putc)(const struct FILE*, const char)", - "int (*puts)(const struct FILE*, const char*)", - ], - }, - ], - "functions": [ - { - "name": "printf", - "return": "int", - "arguments": ["const char*__restrict__ format", "..."], - }, - ], - } - - generate_file("c_header.mako", args) + parser.add_argument( + "-i", + "--interface", + type=Path, + # required=True, + help="path to interface file", + ) + parser.add_argument( + "-t", + "--templates", + type=Path, + default=Path("templates"), + help="templates location", + ) + parser.add_argument( + "-l", + "--license", + type=Path, + required=True, + help="path to license file", + ) + parser.add_argument( + "-o", + "--output", + type=Path, + default=sys.stdout, + help="path to output, stdout by default", + ) + + args = parser.parse_args() + # print(args) + + interface = parse_interface(args) + # print(interface) + + for template in get_templates(args.templates): + generate_file(template, args.templates, args.output, interface) if __name__ == "__main__": -- cgit v1.2.1