Paul Fagerburg | 5f794bf | 2020-02-12 13:01:36 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """Add a new variant to the Kconfig and Kconfig.name for the baseboard |
| 7 | |
| 8 | To start a new variant of an existing baseboard, we need to add |
| 9 | the variant into the Kconfig and Kconfig.name files for the |
| 10 | baseboard. In Kconfig, we have two sections that need additional |
| 11 | entries, MAINBOARD_PART_NUMBER and VARIANT_DIR. |
| 12 | |
| 13 | The MAINBOARD_PART_NUMBER and VARIANT_DIR just use various |
| 14 | capitalizations of the variant name to create the strings. |
| 15 | |
| 16 | Kconfig.name adds an entire section for the new variant, and all |
| 17 | of these use various capitalizations of the variant name. The strings |
| 18 | in this section are SOC-specific, so we'll need versions for each |
| 19 | SOC that we support. |
| 20 | """ |
| 21 | |
| 22 | from __future__ import print_function |
| 23 | import argparse |
| 24 | import sys |
| 25 | |
| 26 | |
| 27 | def main(argv): |
| 28 | """Add strings to coreboot Kconfig and Kconfig.name for a new variant |
| 29 | |
| 30 | Args: |
| 31 | argv: Command-line arguments |
| 32 | """ |
| 33 | parser = argparse.ArgumentParser(description=__doc__) |
| 34 | parser.add_argument('--board', type=str, required=True, |
Paul Fagerburg | f3fe9e8 | 2020-05-21 17:13:09 -0600 | [diff] [blame] | 35 | choices=('hatch', 'volteer', 'trembyle', 'dalboz', |
Paul Fagerburg | 3ef1cbb | 2020-06-02 16:38:36 -0600 | [diff] [blame] | 36 | 'waddledee', 'waddledoo', 'puff'), |
Paul Fagerburg | 5f794bf | 2020-02-12 13:01:36 -0700 | [diff] [blame] | 37 | help='Name of the baseboard') |
| 38 | parser.add_argument('--variant', type=str, required=True, |
| 39 | help='Name of the board variant') |
| 40 | opts = parser.parse_args(argv) |
| 41 | |
| 42 | add_to_kconfig(opts.variant) |
| 43 | add_to_kconfig_name(opts.board, opts.variant) |
| 44 | return True |
| 45 | |
| 46 | |
| 47 | def add_to_kconfig(variant_name): |
| 48 | """Add options for the variant to the Kconfig |
| 49 | |
| 50 | Open the Kconfig file and read it line-by-line. When we detect that we're |
| 51 | in one of the sections of interest, wait until we get a blank line |
| 52 | (signalling the end of that section), and then add our new line before |
| 53 | the blank line. The updated lines are written out to Kconfig.new in the |
| 54 | same directory as Kconfig. |
| 55 | |
| 56 | Args: |
| 57 | variant_name: The name of the board variant, e.g. 'kohaku' |
| 58 | """ |
| 59 | # These are the part of the strings that we'll add to the sections. |
| 60 | BOARD = 'BOARD_GOOGLE_' + variant_name.upper() |
| 61 | lowercase = variant_name.lower() |
| 62 | capitalized = lowercase.capitalize() |
| 63 | |
| 64 | # These flags track if we're in a section where we need to add an option. |
| 65 | in_mainboard_part_number = False |
| 66 | in_variant_dir = False |
| 67 | |
| 68 | inputname = 'Kconfig' |
| 69 | outputname = 'Kconfig.new' |
| 70 | with open(outputname, 'w', encoding='utf-8') as outfile, \ |
| 71 | open(inputname, 'r', encoding='utf-8') as infile: |
| 72 | for rawline in infile: |
| 73 | line = rawline.rstrip() |
| 74 | |
| 75 | # Are we in one of the sections of interest? |
| 76 | if line == 'config MAINBOARD_PART_NUMBER': |
| 77 | in_mainboard_part_number = True |
| 78 | if line == 'config VARIANT_DIR': |
| 79 | in_variant_dir = True |
| 80 | |
| 81 | # Are we at the end of a section, and if so, is it one of the |
| 82 | # sections of interest? |
| 83 | if not line: |
| 84 | if in_mainboard_part_number: |
| 85 | print(f'\tdefault "{capitalized}" if {BOARD}', |
| 86 | file=outfile) |
| 87 | in_mainboard_part_number = False |
| 88 | if in_variant_dir: |
| 89 | print(f'\tdefault "{lowercase}" if {BOARD}', |
| 90 | file=outfile) |
| 91 | in_variant_dir = False |
| 92 | |
| 93 | print(line, file=outfile) |
| 94 | |
| 95 | |
| 96 | def add_to_kconfig_name(baseboard_name, variant_name): |
| 97 | """Add a config section for the variant to the Kconfig.name |
| 98 | |
| 99 | Kconfig.name is easier to modify than Kconfig; it only has a block at |
| 100 | the end with the new variant's details. |
| 101 | |
| 102 | Args: |
| 103 | baseboard_name: The name of the baseboard, e.g. 'hatch' |
| 104 | We expect the caller to have checked that it is one |
| 105 | that we support |
| 106 | variant_name: The name of the board variant, e.g. 'kohaku' |
| 107 | """ |
| 108 | # Board name for the config section. |
| 109 | uppercase = variant_name.upper() |
| 110 | capitalized = variant_name.lower().capitalize() |
| 111 | |
| 112 | inputname = 'Kconfig.name' |
| 113 | outputname = 'Kconfig.name.new' |
| 114 | with open(outputname, 'w', encoding='utf-8') as outfile, \ |
| 115 | open(inputname, 'r', encoding='utf-8') as infile: |
| 116 | # Copy all input lines to output. |
| 117 | for rawline in infile: |
| 118 | line = rawline.rstrip() |
| 119 | print(line, file=outfile) |
| 120 | |
| 121 | # Now add the new section. |
| 122 | if baseboard_name == 'hatch': |
| 123 | print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile) |
| 124 | print('\tbool "-> ' + capitalized + '"', file=outfile) |
| 125 | print('\tselect BOARD_GOOGLE_BASEBOARD_HATCH', file=outfile) |
| 126 | print('\tselect BOARD_ROMSIZE_KB_16384', file=outfile) |
| 127 | elif baseboard_name == 'volteer': |
| 128 | print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile) |
| 129 | print('\tbool "-> ' + capitalized + '"', file=outfile) |
| 130 | print('\tselect BOARD_GOOGLE_BASEBOARD_VOLTEER', file=outfile) |
| 131 | elif baseboard_name == 'trembyle': |
| 132 | print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile) |
| 133 | print('\tbool "-> ' + capitalized + '"', file=outfile) |
| 134 | print('\tselect BOARD_GOOGLE_BASEBOARD_TREMBYLE', file=outfile) |
Paul Fagerburg | 43a7d89 | 2020-05-19 20:28:11 -0600 | [diff] [blame] | 135 | elif baseboard_name == 'dalboz': |
| 136 | print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile) |
| 137 | print('\tbool "-> ' + capitalized + '"', file=outfile) |
| 138 | print('\tselect BOARD_GOOGLE_BASEBOARD_DALBOZ', file=outfile) |
Paul Fagerburg | f3fe9e8 | 2020-05-21 17:13:09 -0600 | [diff] [blame] | 139 | elif baseboard_name == 'waddledee': |
| 140 | print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile) |
| 141 | print('\tbool "-> ' + capitalized + '"', file=outfile) |
Paul Fagerburg | 5a26094 | 2021-05-04 12:22:41 -0600 | [diff] [blame] | 142 | print('\tselect BOARD_GOOGLE_BASEBOARD_DEDEDE_CR50', file=outfile) |
Paul Fagerburg | f3fe9e8 | 2020-05-21 17:13:09 -0600 | [diff] [blame] | 143 | print('\tselect BASEBOARD_DEDEDE_LAPTOP', file=outfile) |
Paul Fagerburg | f3fe9e8 | 2020-05-21 17:13:09 -0600 | [diff] [blame] | 144 | elif baseboard_name == 'waddledoo': |
| 145 | print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile) |
| 146 | print('\tbool "-> ' + capitalized + '"', file=outfile) |
Paul Fagerburg | 5a26094 | 2021-05-04 12:22:41 -0600 | [diff] [blame] | 147 | print('\tselect BOARD_GOOGLE_BASEBOARD_DEDEDE_CR50', file=outfile) |
Paul Fagerburg | f3fe9e8 | 2020-05-21 17:13:09 -0600 | [diff] [blame] | 148 | print('\tselect BASEBOARD_DEDEDE_LAPTOP', file=outfile) |
Paul Fagerburg | f3fe9e8 | 2020-05-21 17:13:09 -0600 | [diff] [blame] | 149 | print('\tselect DRIVERS_GENERIC_MAX98357A', file=outfile) |
| 150 | print('\tselect DRIVERS_I2C_DA7219', file=outfile) |
Paul Fagerburg | 3ef1cbb | 2020-06-02 16:38:36 -0600 | [diff] [blame] | 151 | elif baseboard_name == 'puff': |
| 152 | print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile) |
| 153 | print('\tbool "-> ' + capitalized + '"', file=outfile) |
| 154 | print('\tselect BOARD_GOOGLE_BASEBOARD_PUFF', file=outfile) |
Paul Fagerburg | 5f794bf | 2020-02-12 13:01:36 -0700 | [diff] [blame] | 155 | else: |
| 156 | raise ValueError(f'Unsupported board {baseboard_name}') |
| 157 | |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | sys.exit(not int(main(sys.argv[1:]))) |