Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

446 lines
14 KiB

  1. #!/usr/bin/env python2
  2. #
  3. # Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
  4. #
  5. # SPDX-License-Identifier: GPL-2.0+
  6. #
  7. """
  8. Converter from Kconfig and MAINTAINERS to a board database.
  9. Run 'tools/genboardscfg.py' to create a board database.
  10. Run 'tools/genboardscfg.py -h' for available options.
  11. Python 2.6 or later, but not Python 3.x is necessary to run this script.
  12. """
  13. import errno
  14. import fnmatch
  15. import glob
  16. import multiprocessing
  17. import optparse
  18. import os
  19. import sys
  20. import tempfile
  21. import time
  22. sys.path.append(os.path.join(os.path.dirname(__file__), 'buildman'))
  23. import kconfiglib
  24. ### constant variables ###
  25. OUTPUT_FILE = 'boards.cfg'
  26. CONFIG_DIR = 'configs'
  27. SLEEP_TIME = 0.03
  28. COMMENT_BLOCK = '''#
  29. # List of boards
  30. # Automatically generated by %s: don't edit
  31. #
  32. # Status, Arch, CPU, SoC, Vendor, Board, Target, Options, Maintainers
  33. ''' % __file__
  34. ### helper functions ###
  35. def try_remove(f):
  36. """Remove a file ignoring 'No such file or directory' error."""
  37. try:
  38. os.remove(f)
  39. except OSError as exception:
  40. # Ignore 'No such file or directory' error
  41. if exception.errno != errno.ENOENT:
  42. raise
  43. def check_top_directory():
  44. """Exit if we are not at the top of source directory."""
  45. for f in ('README', 'Licenses'):
  46. if not os.path.exists(f):
  47. sys.exit('Please run at the top of source directory.')
  48. def output_is_new(output):
  49. """Check if the output file is up to date.
  50. Returns:
  51. True if the given output file exists and is newer than any of
  52. *_defconfig, MAINTAINERS and Kconfig*. False otherwise.
  53. """
  54. try:
  55. ctime = os.path.getctime(output)
  56. except OSError as exception:
  57. if exception.errno == errno.ENOENT:
  58. # return False on 'No such file or directory' error
  59. return False
  60. else:
  61. raise
  62. for (dirpath, dirnames, filenames) in os.walk(CONFIG_DIR):
  63. for filename in fnmatch.filter(filenames, '*_defconfig'):
  64. if fnmatch.fnmatch(filename, '.*'):
  65. continue
  66. filepath = os.path.join(dirpath, filename)
  67. if ctime < os.path.getctime(filepath):
  68. return False
  69. for (dirpath, dirnames, filenames) in os.walk('.'):
  70. for filename in filenames:
  71. if (fnmatch.fnmatch(filename, '*~') or
  72. not fnmatch.fnmatch(filename, 'Kconfig*') and
  73. not filename == 'MAINTAINERS'):
  74. continue
  75. filepath = os.path.join(dirpath, filename)
  76. if ctime < os.path.getctime(filepath):
  77. return False
  78. # Detect a board that has been removed since the current board database
  79. # was generated
  80. with open(output) as f:
  81. for line in f:
  82. if line[0] == '#' or line == '\n':
  83. continue
  84. defconfig = line.split()[6] + '_defconfig'
  85. if not os.path.exists(os.path.join(CONFIG_DIR, defconfig)):
  86. return False
  87. return True
  88. ### classes ###
  89. class KconfigScanner:
  90. """Kconfig scanner."""
  91. ### constant variable only used in this class ###
  92. _SYMBOL_TABLE = {
  93. 'arch' : 'SYS_ARCH',
  94. 'cpu' : 'SYS_CPU',
  95. 'soc' : 'SYS_SOC',
  96. 'vendor' : 'SYS_VENDOR',
  97. 'board' : 'SYS_BOARD',
  98. 'config' : 'SYS_CONFIG_NAME',
  99. 'options' : 'SYS_EXTRA_OPTIONS'
  100. }
  101. def __init__(self):
  102. """Scan all the Kconfig files and create a Config object."""
  103. # Define environment variables referenced from Kconfig
  104. os.environ['srctree'] = os.getcwd()
  105. os.environ['UBOOTVERSION'] = 'dummy'
  106. os.environ['KCONFIG_OBJDIR'] = ''
  107. self._conf = kconfiglib.Config()
  108. def __del__(self):
  109. """Delete a leftover temporary file before exit.
  110. The scan() method of this class creates a temporay file and deletes
  111. it on success. If scan() method throws an exception on the way,
  112. the temporary file might be left over. In that case, it should be
  113. deleted in this destructor.
  114. """
  115. if hasattr(self, '_tmpfile') and self._tmpfile:
  116. try_remove(self._tmpfile)
  117. def scan(self, defconfig):
  118. """Load a defconfig file to obtain board parameters.
  119. Arguments:
  120. defconfig: path to the defconfig file to be processed
  121. Returns:
  122. A dictionary of board parameters. It has a form of:
  123. {
  124. 'arch': <arch_name>,
  125. 'cpu': <cpu_name>,
  126. 'soc': <soc_name>,
  127. 'vendor': <vendor_name>,
  128. 'board': <board_name>,
  129. 'target': <target_name>,
  130. 'config': <config_header_name>,
  131. 'options': <extra_options>
  132. }
  133. """
  134. # strip special prefixes and save it in a temporary file
  135. fd, self._tmpfile = tempfile.mkstemp()
  136. with os.fdopen(fd, 'w') as f:
  137. for line in open(defconfig):
  138. colon = line.find(':CONFIG_')
  139. if colon == -1:
  140. f.write(line)
  141. else:
  142. f.write(line[colon + 1:])
  143. self._conf.load_config(self._tmpfile)
  144. try_remove(self._tmpfile)
  145. self._tmpfile = None
  146. params = {}
  147. # Get the value of CONFIG_SYS_ARCH, CONFIG_SYS_CPU, ... etc.
  148. # Set '-' if the value is empty.
  149. for key, symbol in self._SYMBOL_TABLE.items():
  150. value = self._conf.get_symbol(symbol).get_value()
  151. if value:
  152. params[key] = value
  153. else:
  154. params[key] = '-'
  155. defconfig = os.path.basename(defconfig)
  156. params['target'], match, rear = defconfig.partition('_defconfig')
  157. assert match and not rear, '%s : invalid defconfig' % defconfig
  158. # fix-up for aarch64
  159. if params['arch'] == 'arm' and params['cpu'] == 'armv8':
  160. params['arch'] = 'aarch64'
  161. # fix-up options field. It should have the form:
  162. # <config name>[:comma separated config options]
  163. if params['options'] != '-':
  164. params['options'] = params['config'] + ':' + \
  165. params['options'].replace(r'\"', '"')
  166. elif params['config'] != params['target']:
  167. params['options'] = params['config']
  168. return params
  169. def scan_defconfigs_for_multiprocess(queue, defconfigs):
  170. """Scan defconfig files and queue their board parameters
  171. This function is intended to be passed to
  172. multiprocessing.Process() constructor.
  173. Arguments:
  174. queue: An instance of multiprocessing.Queue().
  175. The resulting board parameters are written into it.
  176. defconfigs: A sequence of defconfig files to be scanned.
  177. """
  178. kconf_scanner = KconfigScanner()
  179. for defconfig in defconfigs:
  180. queue.put(kconf_scanner.scan(defconfig))
  181. def read_queues(queues, params_list):
  182. """Read the queues and append the data to the paramers list"""
  183. for q in queues:
  184. while not q.empty():
  185. params_list.append(q.get())
  186. def scan_defconfigs(jobs=1):
  187. """Collect board parameters for all defconfig files.
  188. This function invokes multiple processes for faster processing.
  189. Arguments:
  190. jobs: The number of jobs to run simultaneously
  191. """
  192. all_defconfigs = []
  193. for (dirpath, dirnames, filenames) in os.walk(CONFIG_DIR):
  194. for filename in fnmatch.filter(filenames, '*_defconfig'):
  195. if fnmatch.fnmatch(filename, '.*'):
  196. continue
  197. all_defconfigs.append(os.path.join(dirpath, filename))
  198. total_boards = len(all_defconfigs)
  199. processes = []
  200. queues = []
  201. for i in range(jobs):
  202. defconfigs = all_defconfigs[total_boards * i / jobs :
  203. total_boards * (i + 1) / jobs]
  204. q = multiprocessing.Queue(maxsize=-1)
  205. p = multiprocessing.Process(target=scan_defconfigs_for_multiprocess,
  206. args=(q, defconfigs))
  207. p.start()
  208. processes.append(p)
  209. queues.append(q)
  210. # The resulting data should be accumulated to this list
  211. params_list = []
  212. # Data in the queues should be retrieved preriodically.
  213. # Otherwise, the queues would become full and subprocesses would get stuck.
  214. while any([p.is_alive() for p in processes]):
  215. read_queues(queues, params_list)
  216. # sleep for a while until the queues are filled
  217. time.sleep(SLEEP_TIME)
  218. # Joining subprocesses just in case
  219. # (All subprocesses should already have been finished)
  220. for p in processes:
  221. p.join()
  222. # retrieve leftover data
  223. read_queues(queues, params_list)
  224. return params_list
  225. class MaintainersDatabase:
  226. """The database of board status and maintainers."""
  227. def __init__(self):
  228. """Create an empty database."""
  229. self.database = {}
  230. def get_status(self, target):
  231. """Return the status of the given board.
  232. The board status is generally either 'Active' or 'Orphan'.
  233. Display a warning message and return '-' if status information
  234. is not found.
  235. Returns:
  236. 'Active', 'Orphan' or '-'.
  237. """
  238. if not target in self.database:
  239. print >> sys.stderr, "WARNING: no status info for '%s'" % target
  240. return '-'
  241. tmp = self.database[target][0]
  242. if tmp.startswith('Maintained'):
  243. return 'Active'
  244. elif tmp.startswith('Supported'):
  245. return 'Active'
  246. elif tmp.startswith('Orphan'):
  247. return 'Orphan'
  248. else:
  249. print >> sys.stderr, ("WARNING: %s: unknown status for '%s'" %
  250. (tmp, target))
  251. return '-'
  252. def get_maintainers(self, target):
  253. """Return the maintainers of the given board.
  254. Returns:
  255. Maintainers of the board. If the board has two or more maintainers,
  256. they are separated with colons.
  257. """
  258. if not target in self.database:
  259. print >> sys.stderr, "WARNING: no maintainers for '%s'" % target
  260. return ''
  261. return ':'.join(self.database[target][1])
  262. def parse_file(self, file):
  263. """Parse a MAINTAINERS file.
  264. Parse a MAINTAINERS file and accumulates board status and
  265. maintainers information.
  266. Arguments:
  267. file: MAINTAINERS file to be parsed
  268. """
  269. targets = []
  270. maintainers = []
  271. status = '-'
  272. for line in open(file):
  273. # Check also commented maintainers
  274. if line[:3] == '#M:':
  275. line = line[1:]
  276. tag, rest = line[:2], line[2:].strip()
  277. if tag == 'M:':
  278. maintainers.append(rest)
  279. elif tag == 'F:':
  280. # expand wildcard and filter by 'configs/*_defconfig'
  281. for f in glob.glob(rest):
  282. front, match, rear = f.partition('configs/')
  283. if not front and match:
  284. front, match, rear = rear.rpartition('_defconfig')
  285. if match and not rear:
  286. targets.append(front)
  287. elif tag == 'S:':
  288. status = rest
  289. elif line == '\n':
  290. for target in targets:
  291. self.database[target] = (status, maintainers)
  292. targets = []
  293. maintainers = []
  294. status = '-'
  295. if targets:
  296. for target in targets:
  297. self.database[target] = (status, maintainers)
  298. def insert_maintainers_info(params_list):
  299. """Add Status and Maintainers information to the board parameters list.
  300. Arguments:
  301. params_list: A list of the board parameters
  302. """
  303. database = MaintainersDatabase()
  304. for (dirpath, dirnames, filenames) in os.walk('.'):
  305. if 'MAINTAINERS' in filenames:
  306. database.parse_file(os.path.join(dirpath, 'MAINTAINERS'))
  307. for i, params in enumerate(params_list):
  308. target = params['target']
  309. params['status'] = database.get_status(target)
  310. params['maintainers'] = database.get_maintainers(target)
  311. params_list[i] = params
  312. def format_and_output(params_list, output):
  313. """Write board parameters into a file.
  314. Columnate the board parameters, sort lines alphabetically,
  315. and then write them to a file.
  316. Arguments:
  317. params_list: The list of board parameters
  318. output: The path to the output file
  319. """
  320. FIELDS = ('status', 'arch', 'cpu', 'soc', 'vendor', 'board', 'target',
  321. 'options', 'maintainers')
  322. # First, decide the width of each column
  323. max_length = dict([ (f, 0) for f in FIELDS])
  324. for params in params_list:
  325. for f in FIELDS:
  326. max_length[f] = max(max_length[f], len(params[f]))
  327. output_lines = []
  328. for params in params_list:
  329. line = ''
  330. for f in FIELDS:
  331. # insert two spaces between fields like column -t would
  332. line += ' ' + params[f].ljust(max_length[f])
  333. output_lines.append(line.strip())
  334. # ignore case when sorting
  335. output_lines.sort(key=str.lower)
  336. with open(output, 'w') as f:
  337. f.write(COMMENT_BLOCK + '\n'.join(output_lines) + '\n')
  338. def gen_boards_cfg(output, jobs=1, force=False):
  339. """Generate a board database file.
  340. Arguments:
  341. output: The name of the output file
  342. jobs: The number of jobs to run simultaneously
  343. force: Force to generate the output even if it is new
  344. """
  345. check_top_directory()
  346. if not force and output_is_new(output):
  347. print "%s is up to date. Nothing to do." % output
  348. sys.exit(0)
  349. params_list = scan_defconfigs(jobs)
  350. insert_maintainers_info(params_list)
  351. format_and_output(params_list, output)
  352. def main():
  353. try:
  354. cpu_count = multiprocessing.cpu_count()
  355. except NotImplementedError:
  356. cpu_count = 1
  357. parser = optparse.OptionParser()
  358. # Add options here
  359. parser.add_option('-f', '--force', action="store_true", default=False,
  360. help='regenerate the output even if it is new')
  361. parser.add_option('-j', '--jobs', type='int', default=cpu_count,
  362. help='the number of jobs to run simultaneously')
  363. parser.add_option('-o', '--output', default=OUTPUT_FILE,
  364. help='output file [default=%s]' % OUTPUT_FILE)
  365. (options, args) = parser.parse_args()
  366. gen_boards_cfg(options.output, jobs=options.jobs, force=options.force)
  367. if __name__ == '__main__':
  368. main()