blob: a7358cfc08a5ac4b3cbbe5a9a6d3a99d5f29ee68 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassc05694f2013-04-03 11:07:16 +00002# Copyright (c) 2012 The Chromium OS Authors.
Simon Glassc05694f2013-04-03 11:07:16 +00003
Simon Glassc78ed662019-10-31 07:42:53 -06004import configparser
Simon Glassc05694f2013-04-03 11:07:16 +00005import os
Simon Glassc78ed662019-10-31 07:42:53 -06006import io
Simon Glassc05694f2013-04-03 11:07:16 +00007
Simon Glassccc69062022-11-09 19:14:51 -07008config_fname = None
Simon Glassc05694f2013-04-03 11:07:16 +00009
Simon Glass06b83a52023-07-19 17:49:05 -060010def setup(fname=''):
Simon Glassc05694f2013-04-03 11:07:16 +000011 """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 Schulz88c1116d2024-01-23 18:57:01 +010019 settings = configparser.ConfigParser()
Simon Glass5e0441d2014-09-05 19:00:15 -060020 if fname is not None:
21 config_fname = fname
22 if config_fname == '':
23 config_fname = '%s/.buildman' % os.getenv('HOME')
Simon Glassdd195172016-07-27 20:32:59 -060024 if not os.path.exists(config_fname):
Simon Glassc78ed662019-10-31 07:42:53 -060025 print('No config file found ~/.buildman\nCreating one...\n')
Simon Glass06b83a52023-07-19 17:49:05 -060026 create_buildman_config_file(config_fname)
Simon Glassc78ed662019-10-31 07:42:53 -060027 print('To install tool chains, please use the --fetch-arch option')
Simon Glass5e0441d2014-09-05 19:00:15 -060028 if config_fname:
29 settings.read(config_fname)
30
Simon Glass06b83a52023-07-19 17:49:05 -060031def add_file(data):
Brandon Maierfb726ab2024-06-04 16:16:06 +000032 settings.read_file(io.StringIO(data))
Simon Glassc05694f2013-04-03 11:07:16 +000033
Simon Glass038a3dd2024-08-15 13:57:44 -060034def add_section(name):
35 settings.add_section(name)
36
Simon Glass06b83a52023-07-19 17:49:05 -060037def get_items(section):
Simon Glassc05694f2013-04-03 11:07:16 +000038 """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 Glassc78ed662019-10-31 07:42:53 -060048 except configparser.NoSectionError as e:
Simon Glassc05694f2013-04-03 11:07:16 +000049 return []
50 except:
51 raise
Simon Glass7e803e12014-12-01 17:34:06 -070052
Simon Glass06b83a52023-07-19 17:49:05 -060053def get_global_item_value(name):
Tom Rini93ebd462022-11-09 19:14:53 -070054 """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 Glass06b83a52023-07-19 17:49:05 -060064def set_item(section, tag, value):
Simon Glass7e803e12014-12-01 17:34:06 -070065 """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 Glassdd195172016-07-27 20:32:59 -060073
Simon Glass06b83a52023-07-19 17:49:05 -060074def create_buildman_config_file(config_fname):
Simon Glassdd195172016-07-27 20:32:59 -060075 """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 Glassc78ed662019-10-31 07:42:53 -060086 print("Couldn't create buildman config file '%s'\n" % config_fname)
Simon Glassdd195172016-07-27 20:32:59 -060087 raise
88
Simon Glassc78ed662019-10-31 07:42:53 -060089 print('''[toolchain]
Simon Glassdd195172016-07-27 20:32:59 -060090# name = path
91# e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux
Simon Glassfae6acf2022-02-11 13:23:25 -070092other = /
Simon Glassdd195172016-07-27 20:32:59 -060093
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 Glass8897c7d2022-11-09 19:14:46 -0700101riscv = riscv32
102sh = sh4
Simon Glassdd195172016-07-27 20:32:59 -0600103x86 = i386
Simon Glassdd195172016-07-27 20:32:59 -0600104
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 Glassc78ed662019-10-31 07:42:53 -0600111''', file=f)
Simon Glassdd195172016-07-27 20:32:59 -0600112 f.close();