1# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2012 The Chromium OS Authors.
3
4import configparser
5import os
6import io
7
8config_fname = None
9
10def setup(fname=''):
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
19    settings = configparser.ConfigParser()
20    if fname is not None:
21        config_fname = fname
22        if config_fname == '':
23            config_fname = '%s/.buildman' % os.getenv('HOME')
24        if not os.path.exists(config_fname):
25            print('No config file found ~/.buildman\nCreating one...\n')
26            create_buildman_config_file(config_fname)
27            print('To install tool chains, please use the --fetch-arch option')
28        if config_fname:
29            settings.read(config_fname)
30
31def add_file(data):
32    settings.read_file(io.StringIO(data))
33
34def add_section(name):
35    settings.add_section(name)
36
37def get_items(section):
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)
48    except configparser.NoSectionError as e:
49        return []
50    except:
51        raise
52
53def get_global_item_value(name):
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
64def set_item(section, tag, value):
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)
73
74def create_buildman_config_file(config_fname):
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:
86        print("Couldn't create buildman config file '%s'\n" % config_fname)
87        raise
88
89    print('''[toolchain]
90# name = path
91# e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux
92other = /
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
101riscv = riscv32
102sh = sh4
103x86 = i386
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
111''', file=f)
112    f.close();
113