1#!/usr/bin/env python3 2 3# 4# Copyright 2020, Data61 5# Commonwealth Scientific and Industrial Research Organisation (CSIRO) 6# ABN 41 687 119 230. 7# 8# This software may be distributed and modified according to the terms of 9# the BSD 2-Clause license. Note that NO WARRANTY is provided. 10# See "LICENSE_BSD2.txt" for details. 11# 12# @TAG(DATA61_BSD) 13# 14 15import builtins 16import os 17import argparse 18import yaml 19import logging 20 21from jinja2 import Environment, BaseLoader 22 23HEADER_TEMPLATE = '''/* 24 * Copyright 2020, Data61 25 * Commonwealth Scientific and Industrial Research Organisation (CSIRO) 26 * ABN 41 687 119 230. 27 * 28 * This software may be distributed and modified according to the terms of 29 * the BSD 2-Clause license. Note that NO WARRANTY is provided. 30 * See "LICENSE_BSD2.txt" for details. 31 * 32 * @TAG(DATA61_BSD) 33 */ 34 35/* This file is auto-generated by device_header_gen.py in libplatsupport. */ 36 37#pragma once 38 39{% for dev in devices %} 40#define {{ dev }} {{ loop.index0 }} 41 {% if loop.last %} 42#define MAX_{{ device_type.upper() }}_ID {{ loop.index0 }} 43{% endif %} 44{% endfor %} 45''' 46 47 48def main(args: argparse.Namespace): 49 try: 50 with open(args.device_list) as device_list: 51 parsed_dev_dict = yaml.safe_load(device_list) 52 list_key = '{0}_list'.format(args.device_type) 53 devices = parsed_dev_dict[list_key] 54 template = Environment(loader=BaseLoader, trim_blocks=True, 55 lstrip_blocks=True).from_string(HEADER_TEMPLATE) 56 template_args = dict(builtins.__dict__, **{ 57 'device_type': args.device_type, 58 'devices': devices, 59 }) 60 header_contents = template.render(template_args) 61 args.header_out.write(header_contents) 62 args.header_out.close() 63 except: 64 logging.fatal("Failed to parse the device list file {0}".format(args.device_list.name)) 65 66 67if __name__ == '__main__': 68 parser = argparse.ArgumentParser( 69 description='transform sub-device (GPIO etc.) list to libplatsupport build artefacts' 70 ) 71 72 # Inputs 73 parser.add_argument('--device-list', help='sub-device list to parse for generation', 74 required=True, type=str) 75 parser.add_argument('--device-type', help='device type of the input type', required=True) 76 77 # Outputs 78 parser.add_argument('--header-out', help='output location of the resultant C header', 79 required=True, type=argparse.FileType('w')) 80 81 args = parser.parse_args() 82 83 main(args) 84