boards.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2012 The Chromium OS Authors.
  3. # Author: Simon Glass <sjg@chromium.org>
  4. # Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
  5. """Maintains a list of boards and allows them to be selected"""
  6. from collections import OrderedDict
  7. import errno
  8. import fnmatch
  9. import glob
  10. import multiprocessing
  11. import os
  12. import re
  13. import sys
  14. import tempfile
  15. import time
  16. from buildman import board
  17. from buildman import kconfiglib
  18. ### constant variables ###
  19. OUTPUT_FILE = 'boards.cfg'
  20. CONFIG_DIR = 'configs'
  21. SLEEP_TIME = 0.03
  22. COMMENT_BLOCK = f'''#
  23. # List of boards
  24. # Automatically generated by {__file__}: don't edit
  25. #
  26. # Status, Arch, CPU, SoC, Vendor, Board, Target, Config, Maintainers
  27. '''
  28. def try_remove(fname):
  29. """Remove a file ignoring 'No such file or directory' error.
  30. Args:
  31. fname (str): Filename to remove
  32. Raises:
  33. OSError: output file exists but could not be removed
  34. """
  35. try:
  36. os.remove(fname)
  37. except OSError as exception:
  38. # Ignore 'No such file or directory' error
  39. if exception.errno != errno.ENOENT:
  40. raise
  41. def output_is_new(output, config_dir, srcdir):
  42. """Check if the output file is up to date.
  43. Looks at defconfig and Kconfig files to make sure none is newer than the
  44. output file. Also ensures that the boards.cfg does not mention any removed
  45. boards.
  46. Args:
  47. output (str): Filename to check
  48. config_dir (str): Directory containing defconfig files
  49. srcdir (str): Directory containing Kconfig and MAINTAINERS files
  50. Returns:
  51. True if the given output file exists and is newer than any of
  52. *_defconfig, MAINTAINERS and Kconfig*. False otherwise.
  53. Raises:
  54. OSError: output file exists but could not be opened
  55. """
  56. # pylint: disable=too-many-branches
  57. try:
  58. ctime = os.path.getctime(output)
  59. except OSError as exception:
  60. if exception.errno == errno.ENOENT:
  61. # return False on 'No such file or directory' error
  62. return False
  63. raise
  64. for (dirpath, _, filenames) in os.walk(config_dir):
  65. for filename in fnmatch.filter(filenames, '*_defconfig'):
  66. if fnmatch.fnmatch(filename, '.*'):
  67. continue
  68. filepath = os.path.join(dirpath, filename)
  69. if ctime < os.path.getctime(filepath):
  70. return False
  71. for (dirpath, _, filenames) in os.walk(srcdir):
  72. for filename in filenames:
  73. if (fnmatch.fnmatch(filename, '*~') or
  74. not fnmatch.fnmatch(filename, 'Kconfig*') and
  75. not filename == 'MAINTAINERS'):
  76. continue
  77. filepath = os.path.join(dirpath, filename)
  78. if ctime < os.path.getctime(filepath):
  79. return False
  80. # Detect a board that has been removed since the current board database
  81. # was generated
  82. with open(output, encoding="utf-8") as inf:
  83. for line in inf:
  84. if 'Options,' in line:
  85. return False
  86. if line[0] == '#' or line == '\n':
  87. continue
  88. defconfig = line.split()[6] + '_defconfig'
  89. if not os.path.exists(os.path.join(config_dir, defconfig)):
  90. return False
  91. return True
  92. class Expr:
  93. """A single regular expression for matching boards to build"""
  94. def __init__(self, expr):
  95. """Set up a new Expr object.
  96. Args:
  97. expr (str): String cotaining regular expression to store
  98. """
  99. self._expr = expr
  100. self._re = re.compile(expr)
  101. def matches(self, props):
  102. """Check if any of the properties match the regular expression.
  103. Args:
  104. props (list of str): List of properties to check
  105. Returns:
  106. True if any of the properties match the regular expression
  107. """
  108. for prop in props:
  109. if self._re.match(prop):
  110. return True
  111. return False
  112. def __str__(self):
  113. return self._expr
  114. class Term:
  115. """A list of expressions each of which must match with properties.
  116. This provides a list of 'AND' expressions, meaning that each must
  117. match the board properties for that board to be built.
  118. """
  119. def __init__(self):
  120. self._expr_list = []
  121. self._board_count = 0
  122. def add_expr(self, expr):
  123. """Add an Expr object to the list to check.
  124. Args:
  125. expr (Expr): New Expr object to add to the list of those that must
  126. match for a board to be built.
  127. """
  128. self._expr_list.append(Expr(expr))
  129. def __str__(self):
  130. """Return some sort of useful string describing the term"""
  131. return '&'.join([str(expr) for expr in self._expr_list])
  132. def matches(self, props):
  133. """Check if any of the properties match this term
  134. Each of the expressions in the term is checked. All must match.
  135. Args:
  136. props (list of str): List of properties to check
  137. Returns:
  138. True if all of the expressions in the Term match, else False
  139. """
  140. for expr in self._expr_list:
  141. if not expr.matches(props):
  142. return False
  143. return True
  144. class KconfigScanner:
  145. """Kconfig scanner."""
  146. ### constant variable only used in this class ###
  147. _SYMBOL_TABLE = {
  148. 'arch' : 'SYS_ARCH',
  149. 'cpu' : 'SYS_CPU',
  150. 'soc' : 'SYS_SOC',
  151. 'vendor' : 'SYS_VENDOR',
  152. 'board' : 'SYS_BOARD',
  153. 'config' : 'SYS_CONFIG_NAME',
  154. # 'target' is added later
  155. }
  156. def __init__(self, srctree):
  157. """Scan all the Kconfig files and create a Kconfig object."""
  158. # Define environment variables referenced from Kconfig
  159. os.environ['srctree'] = srctree
  160. os.environ['UBOOTVERSION'] = 'dummy'
  161. os.environ['KCONFIG_OBJDIR'] = ''
  162. self._tmpfile = None
  163. self._conf = kconfiglib.Kconfig(warn=False)
  164. def __del__(self):
  165. """Delete a leftover temporary file before exit.
  166. The scan() method of this class creates a temporay file and deletes
  167. it on success. If scan() method throws an exception on the way,
  168. the temporary file might be left over. In that case, it should be
  169. deleted in this destructor.
  170. """
  171. if self._tmpfile:
  172. try_remove(self._tmpfile)
  173. def scan(self, defconfig, warn_targets):
  174. """Load a defconfig file to obtain board parameters.
  175. Args:
  176. defconfig (str): path to the defconfig file to be processed
  177. warn_targets (bool): True to warn about missing or duplicate
  178. CONFIG_TARGET options
  179. Returns:
  180. tuple: dictionary of board parameters. It has a form of:
  181. {
  182. 'arch': <arch_name>,
  183. 'cpu': <cpu_name>,
  184. 'soc': <soc_name>,
  185. 'vendor': <vendor_name>,
  186. 'board': <board_name>,
  187. 'target': <target_name>,
  188. 'config': <config_header_name>,
  189. }
  190. warnings (list of str): list of warnings found
  191. """
  192. leaf = os.path.basename(defconfig)
  193. expect_target, match, rear = leaf.partition('_defconfig')
  194. assert match and not rear, f'{leaf} : invalid defconfig'
  195. self._conf.load_config(defconfig)
  196. self._tmpfile = None
  197. params = {}
  198. warnings = []
  199. # Get the value of CONFIG_SYS_ARCH, CONFIG_SYS_CPU, ... etc.
  200. # Set '-' if the value is empty.
  201. for key, symbol in list(self._SYMBOL_TABLE.items()):
  202. value = self._conf.syms.get(symbol).str_value
  203. if value:
  204. params[key] = value
  205. else:
  206. params[key] = '-'
  207. # Check there is exactly one TARGET_xxx set
  208. if warn_targets:
  209. target = None
  210. for name, sym in self._conf.syms.items():
  211. if name.startswith('TARGET_') and sym.str_value == 'y':
  212. tname = name[7:].lower()
  213. if target:
  214. warnings.append(
  215. f'WARNING: {leaf}: Duplicate TARGET_xxx: {target} and {tname}')
  216. else:
  217. target = tname
  218. if not target:
  219. cfg_name = expect_target.replace('-', '_').upper()
  220. warnings.append(f'WARNING: {leaf}: No TARGET_{cfg_name} enabled')
  221. params['target'] = expect_target
  222. # fix-up for aarch64
  223. if params['arch'] == 'arm' and params['cpu'] == 'armv8':
  224. params['arch'] = 'aarch64'
  225. # fix-up for riscv
  226. if params['arch'] == 'riscv':
  227. try:
  228. value = self._conf.syms.get('ARCH_RV32I').str_value
  229. except:
  230. value = ''
  231. if value == 'y':
  232. params['arch'] = 'riscv32'
  233. else:
  234. params['arch'] = 'riscv64'
  235. return params, warnings
  236. class MaintainersDatabase:
  237. """The database of board status and maintainers.
  238. Properties:
  239. database: dict:
  240. key: Board-target name (e.g. 'snow')
  241. value: tuple:
  242. str: Board status (e.g. 'Active')
  243. str: List of maintainers, separated by :
  244. warnings (list of str): List of warnings due to missing status, etc.
  245. """
  246. def __init__(self):
  247. """Create an empty database."""
  248. self.database = {}
  249. self.warnings = []
  250. def get_status(self, target):
  251. """Return the status of the given board.
  252. The board status is generally either 'Active' or 'Orphan'.
  253. Display a warning message and return '-' if status information
  254. is not found.
  255. Args:
  256. target (str): Build-target name
  257. Returns:
  258. str: 'Active', 'Orphan' or '-'.
  259. """
  260. if not target in self.database:
  261. self.warnings.append(f"WARNING: no status info for '{target}'")
  262. return '-'
  263. tmp = self.database[target][0]
  264. if tmp.startswith('Maintained'):
  265. return 'Active'
  266. if tmp.startswith('Supported'):
  267. return 'Active'
  268. if tmp.startswith('Orphan'):
  269. return 'Orphan'
  270. self.warnings.append(f"WARNING: {tmp}: unknown status for '{target}'")
  271. return '-'
  272. def get_maintainers(self, target):
  273. """Return the maintainers of the given board.
  274. Args:
  275. target (str): Build-target name
  276. Returns:
  277. str: Maintainers of the board. If the board has two or more
  278. maintainers, they are separated with colons.
  279. """
  280. entry = self.database.get(target)
  281. if entry:
  282. status, maint_list = entry
  283. if not status.startswith('Orphan'):
  284. if len(maint_list) > 1 or (maint_list and maint_list[0] != '-'):
  285. return ':'.join(maint_list)
  286. self.warnings.append(f"WARNING: no maintainers for '{target}'")
  287. return ''
  288. def parse_file(self, srcdir, fname):
  289. """Parse a MAINTAINERS file.
  290. Parse a MAINTAINERS file and accumulate board status and maintainers
  291. information in the self.database dict.
  292. defconfig files are used to specify the target, e.g. xxx_defconfig is
  293. used for target 'xxx'. If there is no defconfig file mentioned in the
  294. MAINTAINERS file F: entries, then this function does nothing.
  295. The N: name entries can be used to specify a defconfig file using
  296. wildcards.
  297. Args:
  298. srcdir (str): Directory containing source code (Kconfig files)
  299. fname (str): MAINTAINERS file to be parsed
  300. """
  301. def add_targets(linenum):
  302. """Add any new targets
  303. Args:
  304. linenum (int): Current line number
  305. """
  306. if targets:
  307. for target in targets:
  308. self.database[target] = (status, maintainers)
  309. targets = []
  310. maintainers = []
  311. status = '-'
  312. with open(fname, encoding="utf-8") as inf:
  313. for linenum, line in enumerate(inf):
  314. # Check also commented maintainers
  315. if line[:3] == '#M:':
  316. line = line[1:]
  317. tag, rest = line[:2], line[2:].strip()
  318. if tag == 'M:':
  319. maintainers.append(rest)
  320. elif tag == 'F:':
  321. # expand wildcard and filter by 'configs/*_defconfig'
  322. glob_path = os.path.join(srcdir, rest)
  323. for item in glob.glob(glob_path):
  324. front, match, rear = item.partition('configs/')
  325. if front.endswith('/'):
  326. front = front[:-1]
  327. if front == srcdir and match:
  328. front, match, rear = rear.rpartition('_defconfig')
  329. if match and not rear:
  330. targets.append(front)
  331. elif tag == 'S:':
  332. status = rest
  333. elif tag == 'N:':
  334. # Just scan the configs directory since that's all we care
  335. # about
  336. walk_path = os.walk(os.path.join(srcdir, 'configs'))
  337. for dirpath, _, fnames in walk_path:
  338. for cfg in fnames:
  339. path = os.path.join(dirpath, cfg)[len(srcdir) + 1:]
  340. front, match, rear = path.partition('configs/')
  341. if front or not match:
  342. continue
  343. front, match, rear = rear.rpartition('_defconfig')
  344. # Use this entry if it matches the defconfig file
  345. # without the _defconfig suffix. For example
  346. # 'am335x.*' matches am335x_guardian_defconfig
  347. if match and not rear and re.search(rest, front):
  348. targets.append(front)
  349. elif line == '\n':
  350. add_targets(linenum)
  351. targets = []
  352. maintainers = []
  353. status = '-'
  354. add_targets(linenum)
  355. class Boards:
  356. """Manage a list of boards."""
  357. def __init__(self):
  358. self._boards = []
  359. def add_board(self, brd):
  360. """Add a new board to the list.
  361. The board's target member must not already exist in the board list.
  362. Args:
  363. brd (Board): board to add
  364. """
  365. self._boards.append(brd)
  366. def read_boards(self, fname):
  367. """Read a list of boards from a board file.
  368. Create a Board object for each and add it to our _boards list.
  369. Args:
  370. fname (str): Filename of boards.cfg file
  371. """
  372. with open(fname, 'r', encoding='utf-8') as inf:
  373. for line in inf:
  374. if line[0] == '#':
  375. continue
  376. fields = line.split()
  377. if not fields:
  378. continue
  379. for upto, field in enumerate(fields):
  380. if field == '-':
  381. fields[upto] = ''
  382. while len(fields) < 8:
  383. fields.append('')
  384. if len(fields) > 8:
  385. fields = fields[:8]
  386. brd = board.Board(*fields)
  387. self.add_board(brd)
  388. def get_list(self):
  389. """Return a list of available boards.
  390. Returns:
  391. List of Board objects
  392. """
  393. return self._boards
  394. def get_dict(self):
  395. """Build a dictionary containing all the boards.
  396. Returns:
  397. Dictionary:
  398. key is board.target
  399. value is board
  400. """
  401. board_dict = OrderedDict()
  402. for brd in self._boards:
  403. board_dict[brd.target] = brd
  404. return board_dict
  405. def get_selected_dict(self):
  406. """Return a dictionary containing the selected boards
  407. Returns:
  408. List of Board objects that are marked selected
  409. """
  410. board_dict = OrderedDict()
  411. for brd in self._boards:
  412. if brd.build_it:
  413. board_dict[brd.target] = brd
  414. return board_dict
  415. def get_selected(self):
  416. """Return a list of selected boards
  417. Returns:
  418. List of Board objects that are marked selected
  419. """
  420. return [brd for brd in self._boards if brd.build_it]
  421. def get_selected_names(self):
  422. """Return a list of selected boards
  423. Returns:
  424. List of board names that are marked selected
  425. """
  426. return [brd.target for brd in self._boards if brd.build_it]
  427. @classmethod
  428. def _build_terms(cls, args):
  429. """Convert command line arguments to a list of terms.
  430. This deals with parsing of the arguments. It handles the '&'
  431. operator, which joins several expressions into a single Term.
  432. For example:
  433. ['arm & freescale sandbox', 'tegra']
  434. will produce 3 Terms containing expressions as follows:
  435. arm, freescale
  436. sandbox
  437. tegra
  438. The first Term has two expressions, both of which must match for
  439. a board to be selected.
  440. Args:
  441. args (list of str): List of command line arguments
  442. Returns:
  443. list of Term: A list of Term objects
  444. """
  445. syms = []
  446. for arg in args:
  447. for word in arg.split():
  448. sym_build = []
  449. for term in word.split('&'):
  450. if term:
  451. sym_build.append(term)
  452. sym_build.append('&')
  453. syms += sym_build[:-1]
  454. terms = []
  455. term = None
  456. oper = None
  457. for sym in syms:
  458. if sym == '&':
  459. oper = sym
  460. elif oper:
  461. term.add_expr(sym)
  462. oper = None
  463. else:
  464. if term:
  465. terms.append(term)
  466. term = Term()
  467. term.add_expr(sym)
  468. if term:
  469. terms.append(term)
  470. return terms
  471. def select_boards(self, args, exclude=None, brds=None):
  472. """Mark boards selected based on args
  473. Normally either boards (an explicit list of boards) or args (a list of
  474. terms to match against) is used. It is possible to specify both, in
  475. which case they are additive.
  476. If brds and args are both empty, all boards are selected.
  477. Args:
  478. args (list of str): List of strings specifying boards to include,
  479. either named, or by their target, architecture, cpu, vendor or
  480. soc. If empty, all boards are selected.
  481. exclude (list of str): List of boards to exclude, regardless of
  482. 'args', or None for none
  483. brds (list of Board): List of boards to build, or None/[] for all
  484. Returns:
  485. Tuple
  486. Dictionary which holds the list of boards which were selected
  487. due to each argument, arranged by argument.
  488. List of errors found
  489. """
  490. def _check_board(brd):
  491. """Check whether to include or exclude a board
  492. Checks the various terms and decide whether to build it or not (the
  493. 'build_it' variable).
  494. If it is built, add the board to the result[term] list so we know
  495. which term caused it to be built. Add it to result['all'] also.
  496. Keep a list of boards we found in 'found', so we can report boards
  497. which appear in self._boards but not in brds.
  498. Args:
  499. brd (Board): Board to check
  500. """
  501. matching_term = None
  502. build_it = False
  503. if terms:
  504. for term in terms:
  505. if term.matches(brd.props):
  506. matching_term = str(term)
  507. build_it = True
  508. break
  509. elif brds:
  510. if brd.target in brds:
  511. build_it = True
  512. found.append(brd.target)
  513. else:
  514. build_it = True
  515. # Check that it is not specifically excluded
  516. for expr in exclude_list:
  517. if expr.matches(brd.props):
  518. build_it = False
  519. break
  520. if build_it:
  521. brd.build_it = True
  522. if matching_term:
  523. result[matching_term].append(brd.target)
  524. result['all'].append(brd.target)
  525. result = OrderedDict()
  526. warnings = []
  527. terms = self._build_terms(args)
  528. result['all'] = []
  529. for term in terms:
  530. result[str(term)] = []
  531. exclude_list = []
  532. if exclude:
  533. for expr in exclude:
  534. exclude_list.append(Expr(expr))
  535. found = []
  536. for brd in self._boards:
  537. _check_board(brd)
  538. if brds:
  539. remaining = set(brds) - set(found)
  540. if remaining:
  541. warnings.append(f"Boards not found: {', '.join(remaining)}\n")
  542. return result, warnings
  543. @classmethod
  544. def scan_defconfigs_for_multiprocess(cls, srcdir, queue, defconfigs,
  545. warn_targets):
  546. """Scan defconfig files and queue their board parameters
  547. This function is intended to be passed to multiprocessing.Process()
  548. constructor.
  549. Args:
  550. srcdir (str): Directory containing source code
  551. queue (multiprocessing.Queue): The resulting board parameters are
  552. written into this.
  553. defconfigs (sequence of str): A sequence of defconfig files to be
  554. scanned.
  555. warn_targets (bool): True to warn about missing or duplicate
  556. CONFIG_TARGET options
  557. """
  558. kconf_scanner = KconfigScanner(srcdir)
  559. for defconfig in defconfigs:
  560. queue.put(kconf_scanner.scan(defconfig, warn_targets))
  561. @classmethod
  562. def read_queues(cls, queues, params_list, warnings):
  563. """Read the queues and append the data to the paramers list
  564. Args:
  565. queues (list of multiprocessing.Queue): Queues to read
  566. params_list (list of dict): List to add params too
  567. warnings (set of str): Set to add warnings to
  568. """
  569. for que in queues:
  570. while not que.empty():
  571. params, warn = que.get()
  572. params_list.append(params)
  573. warnings.update(warn)
  574. def scan_defconfigs(self, config_dir, srcdir, jobs=1, warn_targets=False):
  575. """Collect board parameters for all defconfig files.
  576. This function invokes multiple processes for faster processing.
  577. Args:
  578. config_dir (str): Directory containing the defconfig files
  579. srcdir (str): Directory containing source code (Kconfig files)
  580. jobs (int): The number of jobs to run simultaneously
  581. warn_targets (bool): True to warn about missing or duplicate
  582. CONFIG_TARGET options
  583. Returns:
  584. tuple:
  585. list of dict: List of board parameters, each a dict:
  586. key: 'arch', 'cpu', 'soc', 'vendor', 'board', 'target',
  587. 'config'
  588. value: string value of the key
  589. list of str: List of warnings recorded
  590. """
  591. all_defconfigs = []
  592. for (dirpath, _, filenames) in os.walk(config_dir):
  593. for filename in fnmatch.filter(filenames, '*_defconfig'):
  594. if fnmatch.fnmatch(filename, '.*'):
  595. continue
  596. all_defconfigs.append(os.path.join(dirpath, filename))
  597. total_boards = len(all_defconfigs)
  598. processes = []
  599. queues = []
  600. for i in range(jobs):
  601. defconfigs = all_defconfigs[total_boards * i // jobs :
  602. total_boards * (i + 1) // jobs]
  603. que = multiprocessing.Queue(maxsize=-1)
  604. proc = multiprocessing.Process(
  605. target=self.scan_defconfigs_for_multiprocess,
  606. args=(srcdir, que, defconfigs, warn_targets))
  607. proc.start()
  608. processes.append(proc)
  609. queues.append(que)
  610. # The resulting data should be accumulated to these lists
  611. params_list = []
  612. warnings = set()
  613. # Data in the queues should be retrieved preriodically.
  614. # Otherwise, the queues would become full and subprocesses would get stuck.
  615. while any(p.is_alive() for p in processes):
  616. self.read_queues(queues, params_list, warnings)
  617. # sleep for a while until the queues are filled
  618. time.sleep(SLEEP_TIME)
  619. # Joining subprocesses just in case
  620. # (All subprocesses should already have been finished)
  621. for proc in processes:
  622. proc.join()
  623. # retrieve leftover data
  624. self.read_queues(queues, params_list, warnings)
  625. return params_list, sorted(list(warnings))
  626. @classmethod
  627. def insert_maintainers_info(cls, srcdir, params_list):
  628. """Add Status and Maintainers information to the board parameters list.
  629. Args:
  630. params_list (list of dict): A list of the board parameters
  631. Returns:
  632. list of str: List of warnings collected due to missing status, etc.
  633. """
  634. database = MaintainersDatabase()
  635. for (dirpath, _, filenames) in os.walk(srcdir):
  636. if 'MAINTAINERS' in filenames and 'tools/buildman' not in dirpath:
  637. database.parse_file(srcdir,
  638. os.path.join(dirpath, 'MAINTAINERS'))
  639. for i, params in enumerate(params_list):
  640. target = params['target']
  641. maintainers = database.get_maintainers(target)
  642. params['maintainers'] = maintainers
  643. if maintainers:
  644. params['status'] = database.get_status(target)
  645. else:
  646. params['status'] = '-'
  647. params_list[i] = params
  648. return sorted(database.warnings)
  649. @classmethod
  650. def format_and_output(cls, params_list, output):
  651. """Write board parameters into a file.
  652. Columnate the board parameters, sort lines alphabetically,
  653. and then write them to a file.
  654. Args:
  655. params_list (list of dict): The list of board parameters
  656. output (str): The path to the output file
  657. """
  658. fields = ('status', 'arch', 'cpu', 'soc', 'vendor', 'board', 'target',
  659. 'config', 'maintainers')
  660. # First, decide the width of each column
  661. max_length = {f: 0 for f in fields}
  662. for params in params_list:
  663. for field in fields:
  664. max_length[field] = max(max_length[field], len(params[field]))
  665. output_lines = []
  666. for params in params_list:
  667. line = ''
  668. for field in fields:
  669. # insert two spaces between fields like column -t would
  670. line += ' ' + params[field].ljust(max_length[field])
  671. output_lines.append(line.strip())
  672. # ignore case when sorting
  673. output_lines.sort(key=str.lower)
  674. with open(output, 'w', encoding="utf-8") as outf:
  675. outf.write(COMMENT_BLOCK + '\n'.join(output_lines) + '\n')
  676. def build_board_list(self, config_dir=CONFIG_DIR, srcdir='.', jobs=1,
  677. warn_targets=False):
  678. """Generate a board-database file
  679. This works by reading the Kconfig, then loading each board's defconfig
  680. in to get the setting for each option. In particular, CONFIG_TARGET_xxx
  681. is typically set by the defconfig, where xxx is the target to build.
  682. Args:
  683. config_dir (str): Directory containing the defconfig files
  684. srcdir (str): Directory containing source code (Kconfig files)
  685. jobs (int): The number of jobs to run simultaneously
  686. warn_targets (bool): True to warn about missing or duplicate
  687. CONFIG_TARGET options
  688. Returns:
  689. tuple:
  690. list of dict: List of board parameters, each a dict:
  691. key: 'arch', 'cpu', 'soc', 'vendor', 'board', 'config',
  692. 'target'
  693. value: string value of the key
  694. list of str: Warnings that came up
  695. """
  696. params_list, warnings = self.scan_defconfigs(config_dir, srcdir, jobs,
  697. warn_targets)
  698. m_warnings = self.insert_maintainers_info(srcdir, params_list)
  699. return params_list, warnings + m_warnings
  700. def ensure_board_list(self, output, jobs=1, force=False, quiet=False):
  701. """Generate a board database file if needed.
  702. This is intended to check if Kconfig has changed since the boards.cfg
  703. files was generated.
  704. Args:
  705. output (str): The name of the output file
  706. jobs (int): The number of jobs to run simultaneously
  707. force (bool): Force to generate the output even if it is new
  708. quiet (bool): True to avoid printing a message if nothing needs doing
  709. Returns:
  710. bool: True if all is well, False if there were warnings
  711. """
  712. if not force and output_is_new(output, CONFIG_DIR, '.'):
  713. if not quiet:
  714. print(f'{output} is up to date. Nothing to do.')
  715. return True
  716. params_list, warnings = self.build_board_list(CONFIG_DIR, '.', jobs)
  717. for warn in warnings:
  718. print(warn, file=sys.stderr)
  719. self.format_and_output(params_list, output)
  720. return not warnings