genboardscfg.py 14 KB

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