bsettings.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2012 The Chromium OS Authors.
  3. import configparser
  4. import os
  5. import io
  6. config_fname = None
  7. def setup(fname=''):
  8. """Set up the buildman settings module by reading config files
  9. Args:
  10. config_fname: Config filename to read ('' for default)
  11. """
  12. global settings
  13. global config_fname
  14. settings = configparser.SafeConfigParser()
  15. if fname is not None:
  16. config_fname = fname
  17. if config_fname == '':
  18. config_fname = '%s/.buildman' % os.getenv('HOME')
  19. if not os.path.exists(config_fname):
  20. print('No config file found ~/.buildman\nCreating one...\n')
  21. create_buildman_config_file(config_fname)
  22. print('To install tool chains, please use the --fetch-arch option')
  23. if config_fname:
  24. settings.read(config_fname)
  25. def add_file(data):
  26. settings.readfp(io.StringIO(data))
  27. def get_items(section):
  28. """Get the items from a section of the config.
  29. Args:
  30. section: name of section to retrieve
  31. Returns:
  32. List of (name, value) tuples for the section
  33. """
  34. try:
  35. return settings.items(section)
  36. except configparser.NoSectionError as e:
  37. return []
  38. except:
  39. raise
  40. def get_global_item_value(name):
  41. """Get an item from the 'global' section of the config.
  42. Args:
  43. name: name of item to retrieve
  44. Returns:
  45. str: Value of item, or None if not present
  46. """
  47. return settings.get('global', name, fallback=None)
  48. def set_item(section, tag, value):
  49. """Set an item and write it back to the settings file"""
  50. global settings
  51. global config_fname
  52. settings.set(section, tag, value)
  53. if config_fname is not None:
  54. with open(config_fname, 'w') as fd:
  55. settings.write(fd)
  56. def create_buildman_config_file(config_fname):
  57. """Creates a new config file with no tool chain information.
  58. Args:
  59. config_fname: Config filename to create
  60. Returns:
  61. None
  62. """
  63. try:
  64. f = open(config_fname, 'w')
  65. except IOError:
  66. print("Couldn't create buildman config file '%s'\n" % config_fname)
  67. raise
  68. print('''[toolchain]
  69. # name = path
  70. # e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux
  71. other = /
  72. [toolchain-prefix]
  73. # name = path to prefix
  74. # e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux/bin/x86_64-linux-
  75. [toolchain-alias]
  76. # arch = alias
  77. # Indicates which toolchain should be used to build for that arch
  78. riscv = riscv32
  79. sh = sh4
  80. x86 = i386
  81. [make-flags]
  82. # Special flags to pass to 'make' for certain boards, e.g. to pass a test
  83. # flag and build tag to snapper boards:
  84. # snapper-boards=ENABLE_AT91_TEST=1
  85. # snapper9260=${snapper-boards} BUILD_TAG=442
  86. # snapper9g45=${snapper-boards} BUILD_TAG=443
  87. ''', file=f)
  88. f.close();