blob: e225ac2ca0f4f6285fb037c5dd084ca8470684ce [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):
Simon Glassc78ed662019-10-31 07:42:53 -060032 settings.readfp(io.StringIO(data))
Simon Glassc05694f2013-04-03 11:07:16 +000033
Simon Glass06b83a52023-07-19 17:49:05 -060034def get_items(section):
Simon Glassc05694f2013-04-03 11:07:16 +000035 """Get the items from a section of the config.
36
37 Args:
38 section: name of section to retrieve
39
40 Returns:
41 List of (name, value) tuples for the section
42 """
43 try:
44 return settings.items(section)
Simon Glassc78ed662019-10-31 07:42:53 -060045 except configparser.NoSectionError as e:
Simon Glassc05694f2013-04-03 11:07:16 +000046 return []
47 except:
48 raise
Simon Glass7e803e12014-12-01 17:34:06 -070049
Simon Glass06b83a52023-07-19 17:49:05 -060050def get_global_item_value(name):
Tom Rini93ebd462022-11-09 19:14:53 -070051 """Get an item from the 'global' section of the config.
52
53 Args:
54 name: name of item to retrieve
55
56 Returns:
57 str: Value of item, or None if not present
58 """
59 return settings.get('global', name, fallback=None)
60
Simon Glass06b83a52023-07-19 17:49:05 -060061def set_item(section, tag, value):
Simon Glass7e803e12014-12-01 17:34:06 -070062 """Set an item and write it back to the settings file"""
63 global settings
64 global config_fname
65
66 settings.set(section, tag, value)
67 if config_fname is not None:
68 with open(config_fname, 'w') as fd:
69 settings.write(fd)
Simon Glassdd195172016-07-27 20:32:59 -060070
Simon Glass06b83a52023-07-19 17:49:05 -060071def create_buildman_config_file(config_fname):
Simon Glassdd195172016-07-27 20:32:59 -060072 """Creates a new config file with no tool chain information.
73
74 Args:
75 config_fname: Config filename to create
76
77 Returns:
78 None
79 """
80 try:
81 f = open(config_fname, 'w')
82 except IOError:
Simon Glassc78ed662019-10-31 07:42:53 -060083 print("Couldn't create buildman config file '%s'\n" % config_fname)
Simon Glassdd195172016-07-27 20:32:59 -060084 raise
85
Simon Glassc78ed662019-10-31 07:42:53 -060086 print('''[toolchain]
Simon Glassdd195172016-07-27 20:32:59 -060087# name = path
88# e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux
Simon Glassfae6acf2022-02-11 13:23:25 -070089other = /
Simon Glassdd195172016-07-27 20:32:59 -060090
91[toolchain-prefix]
92# name = path to prefix
93# e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux/bin/x86_64-linux-
94
95[toolchain-alias]
96# arch = alias
97# Indicates which toolchain should be used to build for that arch
Simon Glass8897c7d2022-11-09 19:14:46 -070098riscv = riscv32
99sh = sh4
Simon Glassdd195172016-07-27 20:32:59 -0600100x86 = i386
Simon Glassdd195172016-07-27 20:32:59 -0600101
102[make-flags]
103# Special flags to pass to 'make' for certain boards, e.g. to pass a test
104# flag and build tag to snapper boards:
105# snapper-boards=ENABLE_AT91_TEST=1
106# snapper9260=${snapper-boards} BUILD_TAG=442
107# snapper9g45=${snapper-boards} BUILD_TAG=443
Simon Glassc78ed662019-10-31 07:42:53 -0600108''', file=f)
Simon Glassdd195172016-07-27 20:32:59 -0600109 f.close();