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