| #!/usr/bin/env python3 | |
| # | |
| # Copyright 2020, Data61 | |
| # Commonwealth Scientific and Industrial Research Organisation (CSIRO) | |
| # ABN 41 687 119 230. | |
| # | |
| # This software may be distributed and modified according to the terms of | |
| # the BSD 2-Clause license. Note that NO WARRANTY is provided. | |
| # See "LICENSE_BSD2.txt" for details. | |
| # | |
| # @TAG(DATA61_BSD) | |
| # | |
| import builtins | |
| import os | |
| import argparse | |
| import yaml | |
| import logging | |
| from jinja2 import Environment, BaseLoader | |
| HEADER_TEMPLATE = '''/* | |
| * Copyright 2020, Data61 | |
| * Commonwealth Scientific and Industrial Research Organisation (CSIRO) | |
| * ABN 41 687 119 230. | |
| * | |
| * This software may be distributed and modified according to the terms of | |
| * the BSD 2-Clause license. Note that NO WARRANTY is provided. | |
| * See "LICENSE_BSD2.txt" for details. | |
| * | |
| * @TAG(DATA61_BSD) | |
| */ | |
| /* This file is auto-generated by device_header_gen.py in libplatsupport. */ | |
| #pragma once | |
| {% for dev in devices %} | |
| #define {{ dev }} {{ loop.index0 }} | |
| {% if loop.last %} | |
| #define MAX_{{ device_type.upper() }}_ID {{ loop.index0 }} | |
| {% endif %} | |
| {% endfor %} | |
| ''' | |
| def main(args: argparse.Namespace): | |
| try: | |
| with open(args.device_list) as device_list: | |
| parsed_dev_dict = yaml.safe_load(device_list) | |
| list_key = '{0}_list'.format(args.device_type) | |
| devices = parsed_dev_dict[list_key] | |
| template = Environment(loader=BaseLoader, trim_blocks=True, | |
| lstrip_blocks=True).from_string(HEADER_TEMPLATE) | |
| template_args = dict(builtins.__dict__, **{ | |
| 'device_type': args.device_type, | |
| 'devices': devices, | |
| }) | |
| header_contents = template.render(template_args) | |
| args.header_out.write(header_contents) | |
| args.header_out.close() | |
| except: | |
| logging.fatal("Failed to parse the device list file {0}".format(args.device_list.name)) | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser( | |
| description='transform sub-device (GPIO etc.) list to libplatsupport build artefacts' | |
| ) | |
| # Inputs | |
| parser.add_argument('--device-list', help='sub-device list to parse for generation', | |
| required=True, type=str) | |
| parser.add_argument('--device-type', help='device type of the input type', required=True) | |
| # Outputs | |
| parser.add_argument('--header-out', help='output location of the resultant C header', | |
| required=True, type=argparse.FileType('w')) | |
| args = parser.parse_args() | |
| main(args) |