Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 3 | |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 4 | import configparser |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 5 | import os |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 6 | import io |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 7 | |
Simon Glass | ccc6906 | 2022-11-09 19:14:51 -0700 | [diff] [blame] | 8 | config_fname = None |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 9 | |
Simon Glass | 06b83a5 | 2023-07-19 17:49:05 -0600 | [diff] [blame] | 10 | def setup(fname=''): |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 11 | """Set up the buildman settings module by reading config files |
| 12 | |
| 13 | Args: |
| 14 | config_fname: Config filename to read ('' for default) |
| 15 | """ |
| 16 | global settings |
| 17 | global config_fname |
| 18 | |
Quentin Schulz | 88c1116d | 2024-01-23 18:57:01 +0100 | [diff] [blame] | 19 | settings = configparser.ConfigParser() |
Simon Glass | 5e0441d | 2014-09-05 19:00:15 -0600 | [diff] [blame] | 20 | if fname is not None: |
| 21 | config_fname = fname |
| 22 | if config_fname == '': |
| 23 | config_fname = '%s/.buildman' % os.getenv('HOME') |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 24 | if not os.path.exists(config_fname): |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 25 | print('No config file found ~/.buildman\nCreating one...\n') |
Simon Glass | 06b83a5 | 2023-07-19 17:49:05 -0600 | [diff] [blame] | 26 | create_buildman_config_file(config_fname) |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 27 | print('To install tool chains, please use the --fetch-arch option') |
Simon Glass | 5e0441d | 2014-09-05 19:00:15 -0600 | [diff] [blame] | 28 | if config_fname: |
| 29 | settings.read(config_fname) |
| 30 | |
Simon Glass | 06b83a5 | 2023-07-19 17:49:05 -0600 | [diff] [blame] | 31 | def add_file(data): |
Brandon Maier | fb726ab | 2024-06-04 16:16:06 +0000 | [diff] [blame] | 32 | settings.read_file(io.StringIO(data)) |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 33 | |
Simon Glass | 038a3dd | 2024-08-15 13:57:44 -0600 | [diff] [blame^] | 34 | def add_section(name): |
| 35 | settings.add_section(name) |
| 36 | |
Simon Glass | 06b83a5 | 2023-07-19 17:49:05 -0600 | [diff] [blame] | 37 | def get_items(section): |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 38 | """Get the items from a section of the config. |
| 39 | |
| 40 | Args: |
| 41 | section: name of section to retrieve |
| 42 | |
| 43 | Returns: |
| 44 | List of (name, value) tuples for the section |
| 45 | """ |
| 46 | try: |
| 47 | return settings.items(section) |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 48 | except configparser.NoSectionError as e: |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 49 | return [] |
| 50 | except: |
| 51 | raise |
Simon Glass | 7e803e1 | 2014-12-01 17:34:06 -0700 | [diff] [blame] | 52 | |
Simon Glass | 06b83a5 | 2023-07-19 17:49:05 -0600 | [diff] [blame] | 53 | def get_global_item_value(name): |
Tom Rini | 93ebd46 | 2022-11-09 19:14:53 -0700 | [diff] [blame] | 54 | """Get an item from the 'global' section of the config. |
| 55 | |
| 56 | Args: |
| 57 | name: name of item to retrieve |
| 58 | |
| 59 | Returns: |
| 60 | str: Value of item, or None if not present |
| 61 | """ |
| 62 | return settings.get('global', name, fallback=None) |
| 63 | |
Simon Glass | 06b83a5 | 2023-07-19 17:49:05 -0600 | [diff] [blame] | 64 | def set_item(section, tag, value): |
Simon Glass | 7e803e1 | 2014-12-01 17:34:06 -0700 | [diff] [blame] | 65 | """Set an item and write it back to the settings file""" |
| 66 | global settings |
| 67 | global config_fname |
| 68 | |
| 69 | settings.set(section, tag, value) |
| 70 | if config_fname is not None: |
| 71 | with open(config_fname, 'w') as fd: |
| 72 | settings.write(fd) |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 73 | |
Simon Glass | 06b83a5 | 2023-07-19 17:49:05 -0600 | [diff] [blame] | 74 | def create_buildman_config_file(config_fname): |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 75 | """Creates a new config file with no tool chain information. |
| 76 | |
| 77 | Args: |
| 78 | config_fname: Config filename to create |
| 79 | |
| 80 | Returns: |
| 81 | None |
| 82 | """ |
| 83 | try: |
| 84 | f = open(config_fname, 'w') |
| 85 | except IOError: |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 86 | print("Couldn't create buildman config file '%s'\n" % config_fname) |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 87 | raise |
| 88 | |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 89 | print('''[toolchain] |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 90 | # name = path |
| 91 | # e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux |
Simon Glass | fae6acf | 2022-02-11 13:23:25 -0700 | [diff] [blame] | 92 | other = / |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 93 | |
| 94 | [toolchain-prefix] |
| 95 | # name = path to prefix |
| 96 | # e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux/bin/x86_64-linux- |
| 97 | |
| 98 | [toolchain-alias] |
| 99 | # arch = alias |
| 100 | # Indicates which toolchain should be used to build for that arch |
Simon Glass | 8897c7d | 2022-11-09 19:14:46 -0700 | [diff] [blame] | 101 | riscv = riscv32 |
| 102 | sh = sh4 |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 103 | x86 = i386 |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 104 | |
| 105 | [make-flags] |
| 106 | # Special flags to pass to 'make' for certain boards, e.g. to pass a test |
| 107 | # flag and build tag to snapper boards: |
| 108 | # snapper-boards=ENABLE_AT91_TEST=1 |
| 109 | # snapper9260=${snapper-boards} BUILD_TAG=442 |
| 110 | # snapper9g45=${snapper-boards} BUILD_TAG=443 |
Simon Glass | c78ed66 | 2019-10-31 07:42:53 -0600 | [diff] [blame] | 111 | ''', file=f) |
Simon Glass | dd19517 | 2016-07-27 20:32:59 -0600 | [diff] [blame] | 112 | f.close(); |