genboardscfg.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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('Orphan'):
  245. return 'Orphan'
  246. else:
  247. print >> sys.stderr, ("WARNING: %s: unknown status for '%s'" %
  248. (tmp, target))
  249. return '-'
  250. def get_maintainers(self, target):
  251. """Return the maintainers of the given board.
  252. Returns:
  253. Maintainers of the board. If the board has two or more maintainers,
  254. they are separated with colons.
  255. """
  256. if not target in self.database:
  257. print >> sys.stderr, "WARNING: no maintainers for '%s'" % target
  258. return ''
  259. return ':'.join(self.database[target][1])
  260. def parse_file(self, file):
  261. """Parse a MAINTAINERS file.
  262. Parse a MAINTAINERS file and accumulates board status and
  263. maintainers information.
  264. Arguments:
  265. file: MAINTAINERS file to be parsed
  266. """
  267. targets = []
  268. maintainers = []
  269. status = '-'
  270. for line in open(file):
  271. # Check also commented maintainers
  272. if line[:3] == '#M:':
  273. line = line[1:]
  274. tag, rest = line[:2], line[2:].strip()
  275. if tag == 'M:':
  276. maintainers.append(rest)
  277. elif tag == 'F:':
  278. # expand wildcard and filter by 'configs/*_defconfig'
  279. for f in glob.glob(rest):
  280. front, match, rear = f.partition('configs/')
  281. if not front and match:
  282. front, match, rear = rear.rpartition('_defconfig')
  283. if match and not rear:
  284. targets.append(front)
  285. elif tag == 'S:':
  286. status = rest
  287. elif line == '\n':
  288. for target in targets:
  289. self.database[target] = (status, maintainers)
  290. targets = []
  291. maintainers = []
  292. status = '-'
  293. if targets:
  294. for target in targets:
  295. self.database[target] = (status, maintainers)
  296. def insert_maintainers_info(params_list):
  297. """Add Status and Maintainers information to the board parameters list.
  298. Arguments:
  299. params_list: A list of the board parameters
  300. """
  301. database = MaintainersDatabase()
  302. for (dirpath, dirnames, filenames) in os.walk('.'):
  303. if 'MAINTAINERS' in filenames:
  304. database.parse_file(os.path.join(dirpath, 'MAINTAINERS'))
  305. for i, params in enumerate(params_list):
  306. target = params['target']
  307. params['status'] = database.get_status(target)
  308. params['maintainers'] = database.get_maintainers(target)
  309. params_list[i] = params
  310. def format_and_output(params_list, output):
  311. """Write board parameters into a file.
  312. Columnate the board parameters, sort lines alphabetically,
  313. and then write them to a file.
  314. Arguments:
  315. params_list: The list of board parameters
  316. output: The path to the output file
  317. """
  318. FIELDS = ('status', 'arch', 'cpu', 'soc', 'vendor', 'board', 'target',
  319. 'options', 'maintainers')
  320. # First, decide the width of each column
  321. max_length = dict([ (f, 0) for f in FIELDS])
  322. for params in params_list:
  323. for f in FIELDS:
  324. max_length[f] = max(max_length[f], len(params[f]))
  325. output_lines = []
  326. for params in params_list:
  327. line = ''
  328. for f in FIELDS:
  329. # insert two spaces between fields like column -t would
  330. line += ' ' + params[f].ljust(max_length[f])
  331. output_lines.append(line.strip())
  332. # ignore case when sorting
  333. output_lines.sort(key=str.lower)
  334. with open(output, 'w') as f:
  335. f.write(COMMENT_BLOCK + '\n'.join(output_lines) + '\n')
  336. def gen_boards_cfg(output, jobs=1, force=False):
  337. """Generate a board database file.
  338. Arguments:
  339. output: The name of the output file
  340. jobs: The number of jobs to run simultaneously
  341. force: Force to generate the output even if it is new
  342. """
  343. check_top_directory()
  344. if not force and output_is_new(output):
  345. print "%s is up to date. Nothing to do." % output
  346. sys.exit(0)
  347. params_list = scan_defconfigs(jobs)
  348. insert_maintainers_info(params_list)
  349. format_and_output(params_list, output)
  350. def main():
  351. try:
  352. cpu_count = multiprocessing.cpu_count()
  353. except NotImplementedError:
  354. cpu_count = 1
  355. parser = optparse.OptionParser()
  356. # Add options here
  357. parser.add_option('-f', '--force', action="store_true", default=False,
  358. help='regenerate the output even if it is new')
  359. parser.add_option('-j', '--jobs', type='int', default=cpu_count,
  360. help='the number of jobs to run simultaneously')
  361. parser.add_option('-o', '--output', default=OUTPUT_FILE,
  362. help='output file [default=%s]' % OUTPUT_FILE)
  363. (options, args) = parser.parse_args()
  364. gen_boards_cfg(options.output, jobs=options.jobs, force=options.force)
  365. if __name__ == '__main__':
  366. main()