board.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright (c) 2012 The Chromium OS Authors.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0+
  4. #
  5. import re
  6. class Board:
  7. """A particular board that we can build"""
  8. def __init__(self, status, arch, cpu, soc, vendor, board_name, target, options):
  9. """Create a new board type.
  10. Args:
  11. status: define whether the board is 'Active' or 'Orphaned'
  12. arch: Architecture name (e.g. arm)
  13. cpu: Cpu name (e.g. arm1136)
  14. soc: Name of SOC, or '' if none (e.g. mx31)
  15. vendor: Name of vendor (e.g. armltd)
  16. board_name: Name of board (e.g. integrator)
  17. target: Target name (use make <target>_defconfig to configure)
  18. options: board-specific options (e.g. integratorcp:CM1136)
  19. """
  20. self.target = target
  21. self.arch = arch
  22. self.cpu = cpu
  23. self.board_name = board_name
  24. self.vendor = vendor
  25. self.soc = soc
  26. self.props = [self.target, self.arch, self.cpu, self.board_name,
  27. self.vendor, self.soc]
  28. self.options = options
  29. self.build_it = False
  30. class Boards:
  31. """Manage a list of boards."""
  32. def __init__(self):
  33. # Use a simple list here, sinc OrderedDict requires Python 2.7
  34. self._boards = []
  35. def AddBoard(self, board):
  36. """Add a new board to the list.
  37. The board's target member must not already exist in the board list.
  38. Args:
  39. board: board to add
  40. """
  41. self._boards.append(board)
  42. def ReadBoards(self, fname):
  43. """Read a list of boards from a board file.
  44. Create a board object for each and add it to our _boards list.
  45. Args:
  46. fname: Filename of boards.cfg file
  47. """
  48. with open(fname, 'r') as fd:
  49. for line in fd:
  50. if line[0] == '#':
  51. continue
  52. fields = line.split()
  53. if not fields:
  54. continue
  55. for upto in range(len(fields)):
  56. if fields[upto] == '-':
  57. fields[upto] = ''
  58. while len(fields) < 8:
  59. fields.append('')
  60. if len(fields) > 8:
  61. fields = fields[:8]
  62. board = Board(*fields)
  63. self.AddBoard(board)
  64. def GetList(self):
  65. """Return a list of available boards.
  66. Returns:
  67. List of Board objects
  68. """
  69. return self._boards
  70. def GetDict(self):
  71. """Build a dictionary containing all the boards.
  72. Returns:
  73. Dictionary:
  74. key is board.target
  75. value is board
  76. """
  77. board_dict = {}
  78. for board in self._boards:
  79. board_dict[board.target] = board
  80. return board_dict
  81. def GetSelectedDict(self):
  82. """Return a dictionary containing the selected boards
  83. Returns:
  84. List of Board objects that are marked selected
  85. """
  86. board_dict = {}
  87. for board in self._boards:
  88. if board.build_it:
  89. board_dict[board.target] = board
  90. return board_dict
  91. def GetSelected(self):
  92. """Return a list of selected boards
  93. Returns:
  94. List of Board objects that are marked selected
  95. """
  96. return [board for board in self._boards if board.build_it]
  97. def GetSelectedNames(self):
  98. """Return a list of selected boards
  99. Returns:
  100. List of board names that are marked selected
  101. """
  102. return [board.target for board in self._boards if board.build_it]
  103. def SelectBoards(self, args):
  104. """Mark boards selected based on args
  105. Args:
  106. List of strings specifying boards to include, either named, or
  107. by their target, architecture, cpu, vendor or soc. If empty, all
  108. boards are selected.
  109. Returns:
  110. Dictionary which holds the number of boards which were selected
  111. due to each argument, arranged by argument.
  112. """
  113. result = {}
  114. argres = {}
  115. for arg in args:
  116. result[arg] = 0
  117. argres[arg] = re.compile(arg)
  118. result['all'] = 0
  119. for board in self._boards:
  120. if args:
  121. for arg in args:
  122. argre = argres[arg]
  123. match = False
  124. for prop in board.props:
  125. match = argre.match(prop)
  126. if match:
  127. break
  128. if match:
  129. if not board.build_it:
  130. board.build_it = True
  131. result[arg] += 1
  132. result['all'] += 1
  133. else:
  134. board.build_it = True
  135. result['all'] += 1
  136. return result