blob: 7f9f48fbca5b7c6f54f871bed46affd1b64a1806 [file] [log] [blame]
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -04001#!/usr/bin/env python3
2# Copyright lowRISC contributors.
3# Licensed under the Apache License, Version 2.0, see LICENSE for details.
4# SPDX-License-Identifier: Apache-2.0
5
Sam Elliottf18219f2020-09-23 17:46:10 +01006# make_new_dif.py is a script for instantiating templates needed to begin
7# development on a new DIF.
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -04008#
Sam Elliottf18219f2020-09-23 17:46:10 +01009# To instantiate the files for a new IP named my_ip, run the command
10# $ util/make_new_dif.py --ip my_ip --peripheral "my peripheral"
11# where "my peripheral" is a documentation-friendly name for your peripheral.
12# Compare "pwrmgr" and "power manager".
13#
14# It will instantiate:
15# - `sw/device/lib/dif/dif_template.h.tpl` as the DIF Header (into dif_<ip>.h).
16# - `doc/project/sw_checklist.md.tpl` as the DIF Checklist (into dif_<ip>.md).
17#
18# See both templates for more information.
19#
20# You can also use the `--only=header` or `--only=checklist` to instantiate a
21# subset of the templates. This can be passed multiple times, and including
22# `--only=all` will instantiate every template.
23#
24# The produced files will still need some cleaning up before they can be used.
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040025
26import argparse
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040027from pathlib import Path
28
29from mako.template import Template
30
31# This file is $REPO_TOP/util/make_new_dif.py, so it takes two parent()
32# calls to get back to the top.
33REPO_TOP = Path(__file__).resolve().parent.parent
34
Sam Elliottf18219f2020-09-23 17:46:10 +010035ALL_PARTS = ["header", "checklist"]
36
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040037
38def main():
39 dif_dir = REPO_TOP / 'sw/device/lib/dif'
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040040
41 parser = argparse.ArgumentParser()
42 parser.add_argument('--ip',
43 '-i',
44 required=True,
45 help='the short name of the IP, in snake_case')
46 parser.add_argument('--peripheral',
47 '-p',
48 required=True,
49 help='the documentation-friendly name of the IP')
50 parser.add_argument(
51 '--handle-param',
52 '-a',
53 default='handle',
Sam Elliottf18219f2020-09-23 17:46:10 +010054 help='an optional name to replace the `handle` parameter name')
55 parser.add_argument('--only',
56 choices=['all'] + ALL_PARTS,
57 default=[],
58 action='append',
59 help='only create some files; defaults to all.')
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040060 parser.add_argument('--output',
61 '-o',
62 type=Path,
Sam Elliottf18219f2020-09-23 17:46:10 +010063 default=dif_dir,
64 help='directory to place the output files into.')
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040065 args = parser.parse_args()
66
Sam Elliottf18219f2020-09-23 17:46:10 +010067
68 if len(args.only) == 0:
69 args.only += ['all']
70 if 'all' in args.only:
71 args.only += ALL_PARTS
72
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040073 ip_snake = args.ip
74 ip_camel = ''.join([word.capitalize() for word in args.ip.split('_')])
75 ip_upper = ip_snake.upper()
76 periph_lower = args.peripheral
77 # We just want to set the first character to title case. In particular,
78 # .capitalize() does not do the right thing, since it would convert
79 # UART to Uart.
80 periph_upper = periph_lower[0].upper() + periph_lower[1:]
81 handle = args.handle_param
82
Sam Elliottf18219f2020-09-23 17:46:10 +010083 if len(args.only) > 0:
84 args.output.mkdir(exist_ok=True)
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040085
Sam Elliottf18219f2020-09-23 17:46:10 +010086 if "header" in args.only:
87 header_template_file = args.output / 'dif_template.h.tpl'
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -040088
Sam Elliottf18219f2020-09-23 17:46:10 +010089 with header_template_file.open('r') as f:
90 header_template = Template(f.read())
91
92 header_out_file = dif_dir / 'dif_{}.h'.format(ip_snake)
93 with header_out_file.open('w') as f:
94 f.write(
95 header_template.render(
96 ip_snake=ip_snake,
97 ip_camel=ip_camel,
98 ip_upper=ip_upper,
99 periph_lower=periph_lower,
100 periph_upper=periph_upper,
101 handle=handle,
102 ))
103
104 print('DIF header successfully written to {}.'.format(
105 str(header_out_file)))
106
107 if "checklist" in args.only:
108 checklist_template_file = REPO_TOP / 'doc/project/sw_checklist.md.tpl'
109
110 with checklist_template_file.open('r') as f:
111 markdown_template = Template(f.read())
112
113 checklist_out_file = args.output / 'dif_{}.md'.format(ip_snake)
114 with checklist_out_file.open('w') as f:
115 f.write(
116 markdown_template.render(
117 ip_name=ip_snake,
118 dif_name=ip_snake,
119 display_name=periph_upper,
120 ))
121
122 print('DIF Checklist successfully written to {}.'.format(
123 str(checklist_out_file)))
Miguel Young de la Sotaff15b052020-09-09 11:26:17 -0400124
125
126if __name__ == '__main__':
127 main()