Blame view

tools/buildman/board.py 8.44 KB
fc3fe1c28   Simon Glass   buildman - U-Boot...
1
2
  # Copyright (c) 2012 The Chromium OS Authors.
  #
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
3
  # SPDX-License-Identifier:	GPL-2.0+
fc3fe1c28   Simon Glass   buildman - U-Boot...
4
  #
8426d8b08   Stephen Warren   buildman: make bo...
5
  import re
6131beab6   Simon Glass   buildman: Introdu...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  class Expr:
      """A single regular expression for matching boards to build"""
  
      def __init__(self, expr):
          """Set up a new Expr object.
  
          Args:
              expr: String cotaining regular expression to store
          """
          self._expr = expr
          self._re = re.compile(expr)
  
      def Matches(self, props):
          """Check if any of the properties match the regular expression.
  
          Args:
             props: List of properties to check
          Returns:
             True if any of the properties match the regular expression
          """
          for prop in props:
              if self._re.match(prop):
                  return True
          return False
  
      def __str__(self):
          return self._expr
  
  class Term:
      """A list of expressions each of which must match with properties.
  
      This provides a list of 'AND' expressions, meaning that each must
      match the board properties for that board to be built.
      """
      def __init__(self):
          self._expr_list = []
          self._board_count = 0
  
      def AddExpr(self, expr):
          """Add an Expr object to the list to check.
  
          Args:
              expr: New Expr object to add to the list of those that must
                    match for a board to be built.
          """
          self._expr_list.append(Expr(expr))
  
      def __str__(self):
          """Return some sort of useful string describing the term"""
          return '&'.join([str(expr) for expr in self._expr_list])
  
      def Matches(self, props):
          """Check if any of the properties match this term
  
          Each of the expressions in the term is checked. All must match.
  
          Args:
             props: List of properties to check
          Returns:
             True if all of the expressions in the Term match, else False
          """
          for expr in self._expr_list:
              if not expr.Matches(props):
                  return False
          return True
fc3fe1c28   Simon Glass   buildman - U-Boot...
71
72
  class Board:
      """A particular board that we can build"""
03c1bb242   Andreas Bießmann   buildman: fix boa...
73
      def __init__(self, status, arch, cpu, soc, vendor, board_name, target, options):
fc3fe1c28   Simon Glass   buildman - U-Boot...
74
75
76
          """Create a new board type.
  
          Args:
03c1bb242   Andreas Bießmann   buildman: fix boa...
77
              status: define whether the board is 'Active' or 'Orphaned'
fc3fe1c28   Simon Glass   buildman - U-Boot...
78
79
              arch: Architecture name (e.g. arm)
              cpu: Cpu name (e.g. arm1136)
fc3fe1c28   Simon Glass   buildman - U-Boot...
80
              soc: Name of SOC, or '' if none (e.g. mx31)
03c1bb242   Andreas Bießmann   buildman: fix boa...
81
82
              vendor: Name of vendor (e.g. armltd)
              board_name: Name of board (e.g. integrator)
73f30b9b8   Masahiro Yamada   buildman: adjust ...
83
              target: Target name (use make <target>_defconfig to configure)
fc3fe1c28   Simon Glass   buildman - U-Boot...
84
85
86
87
88
89
90
91
              options: board-specific options (e.g. integratorcp:CM1136)
          """
          self.target = target
          self.arch = arch
          self.cpu = cpu
          self.board_name = board_name
          self.vendor = vendor
          self.soc = soc
fc3fe1c28   Simon Glass   buildman - U-Boot...
92
          self.options = options
e0f2406e7   Tom Rini   buildman: Fix bui...
93
94
          self.props = [self.target, self.arch, self.cpu, self.board_name,
                        self.vendor, self.soc, self.options]
fc3fe1c28   Simon Glass   buildman - U-Boot...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
          self.build_it = False
  
  
  class Boards:
      """Manage a list of boards."""
      def __init__(self):
          # Use a simple list here, sinc OrderedDict requires Python 2.7
          self._boards = []
  
      def AddBoard(self, board):
          """Add a new board to the list.
  
          The board's target member must not already exist in the board list.
  
          Args:
              board: board to add
          """
          self._boards.append(board)
  
      def ReadBoards(self, fname):
          """Read a list of boards from a board file.
  
          Create a board object for each and add it to our _boards list.
  
          Args:
              fname: Filename of boards.cfg file
          """
          with open(fname, 'r') as fd:
              for line in fd:
                  if line[0] == '#':
                      continue
                  fields = line.split()
                  if not fields:
                      continue
                  for upto in range(len(fields)):
                      if fields[upto] == '-':
                          fields[upto] = ''
03c1bb242   Andreas Bießmann   buildman: fix boa...
132
                  while len(fields) < 8:
fc3fe1c28   Simon Glass   buildman - U-Boot...
133
                      fields.append('')
03c1bb242   Andreas Bießmann   buildman: fix boa...
134
135
                  if len(fields) > 8:
                      fields = fields[:8]
fc3fe1c28   Simon Glass   buildman - U-Boot...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  
                  board = Board(*fields)
                  self.AddBoard(board)
  
  
      def GetList(self):
          """Return a list of available boards.
  
          Returns:
              List of Board objects
          """
          return self._boards
  
      def GetDict(self):
          """Build a dictionary containing all the boards.
  
          Returns:
              Dictionary:
                  key is board.target
                  value is board
          """
          board_dict = {}
          for board in self._boards:
              board_dict[board.target] = board
          return board_dict
  
      def GetSelectedDict(self):
          """Return a dictionary containing the selected boards
  
          Returns:
              List of Board objects that are marked selected
          """
          board_dict = {}
          for board in self._boards:
              if board.build_it:
                  board_dict[board.target] = board
          return board_dict
  
      def GetSelected(self):
          """Return a list of selected boards
  
          Returns:
              List of Board objects that are marked selected
          """
          return [board for board in self._boards if board.build_it]
  
      def GetSelectedNames(self):
          """Return a list of selected boards
  
          Returns:
              List of board names that are marked selected
          """
          return [board.target for board in self._boards if board.build_it]
6131beab6   Simon Glass   buildman: Introdu...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
      def _BuildTerms(self, args):
          """Convert command line arguments to a list of terms.
  
          This deals with parsing of the arguments. It handles the '&'
          operator, which joins several expressions into a single Term.
  
          For example:
              ['arm & freescale sandbox', 'tegra']
  
          will produce 3 Terms containing expressions as follows:
              arm, freescale
              sandbox
              tegra
  
          The first Term has two expressions, both of which must match for
          a board to be selected.
  
          Args:
              args: List of command line arguments
          Returns:
              A list of Term objects
          """
          syms = []
          for arg in args:
              for word in arg.split():
                  sym_build = []
                  for term in word.split('&'):
                      if term:
                          sym_build.append(term)
                      sym_build.append('&')
                  syms += sym_build[:-1]
          terms = []
          term = None
          oper = None
          for sym in syms:
              if sym == '&':
                  oper = sym
              elif oper:
                  term.AddExpr(sym)
                  oper = None
              else:
                  if term:
                      terms.append(term)
                  term = Term()
                  term.AddExpr(sym)
          if term:
              terms.append(term)
          return terms
3cf4ae6f8   Simon Glass   buildman: Impleme...
237
      def SelectBoards(self, args, exclude=[]):
fc3fe1c28   Simon Glass   buildman - U-Boot...
238
239
240
          """Mark boards selected based on args
  
          Args:
3cf4ae6f8   Simon Glass   buildman: Impleme...
241
242
243
244
              args: List of strings specifying boards to include, either named,
                    or by their target, architecture, cpu, vendor or soc. If
                    empty, all boards are selected.
              exclude: List of boards to exclude, regardless of 'args'
fc3fe1c28   Simon Glass   buildman - U-Boot...
245
246
  
          Returns:
8d7523c55   Simon Glass   buildman: Allow s...
247
              Dictionary which holds the list of boards which were selected
fc3fe1c28   Simon Glass   buildman - U-Boot...
248
249
250
              due to each argument, arranged by argument.
          """
          result = {}
6131beab6   Simon Glass   buildman: Introdu...
251
          terms = self._BuildTerms(args)
8d7523c55   Simon Glass   buildman: Allow s...
252
          result['all'] = []
6131beab6   Simon Glass   buildman: Introdu...
253
          for term in terms:
8d7523c55   Simon Glass   buildman: Allow s...
254
              result[str(term)] = []
fc3fe1c28   Simon Glass   buildman - U-Boot...
255

3cf4ae6f8   Simon Glass   buildman: Impleme...
256
257
258
          exclude_list = []
          for expr in exclude:
              exclude_list.append(Expr(expr))
fc3fe1c28   Simon Glass   buildman - U-Boot...
259
          for board in self._boards:
3cf4ae6f8   Simon Glass   buildman: Impleme...
260
261
              matching_term = None
              build_it = False
6131beab6   Simon Glass   buildman: Introdu...
262
263
264
265
              if terms:
                  match = False
                  for term in terms:
                      if term.Matches(board.props):
3cf4ae6f8   Simon Glass   buildman: Impleme...
266
267
                          matching_term = str(term)
                          build_it = True
6131beab6   Simon Glass   buildman: Introdu...
268
                          break
fc3fe1c28   Simon Glass   buildman - U-Boot...
269
              else:
3cf4ae6f8   Simon Glass   buildman: Impleme...
270
271
272
273
274
275
276
277
278
                  build_it = True
  
              # Check that it is not specifically excluded
              for expr in exclude_list:
                  if expr.Matches(board.props):
                      build_it = False
                      break
  
              if build_it:
fc3fe1c28   Simon Glass   buildman - U-Boot...
279
                  board.build_it = True
3cf4ae6f8   Simon Glass   buildman: Impleme...
280
                  if matching_term:
8d7523c55   Simon Glass   buildman: Allow s...
281
282
                      result[matching_term].append(board.target)
                  result['all'].append(board.target)
fc3fe1c28   Simon Glass   buildman - U-Boot...
283
284
  
          return result