[6043559] | 1 | #!/usr/bin/env python3
|
---|
| 2 |
|
---|
| 3 | # SPDX-License-Identifier: MIT
|
---|
| 4 | # Copyright 2023 The LFS Editors
|
---|
| 5 |
|
---|
| 6 | # Stupid script to render "mconf"-style kernel configuration
|
---|
| 7 | # Usage: kernel-config.py [path to kernel tree] [needed config].toml
|
---|
| 8 | # The toml file should be like:
|
---|
| 9 | # for bool and tristate:
|
---|
| 10 | # EXT4="*"
|
---|
| 11 | # DRM="*M"
|
---|
| 12 | # EXPERT=" "
|
---|
| 13 | # DRM_I915="*M"
|
---|
| 14 | # for choice:
|
---|
| 15 | # HIGHMEM64G="X"
|
---|
[5ca8d70] | 16 | # an entry with comment:
|
---|
| 17 | # DRM_I915 = { value = " *M", comment = "for i915, crocus, or iris" }
|
---|
[6043559] | 18 |
|
---|
| 19 | choice_bit = 1 << 30
|
---|
| 20 | ind0 = 0
|
---|
| 21 | ind1 = 0
|
---|
| 22 | menu_id = 1
|
---|
| 23 | stack = []
|
---|
[831ba20a] | 24 | if_stack = []
|
---|
[6043559] | 25 |
|
---|
| 26 | expand_var_mp = { 'SRCARCH': 'x86' }
|
---|
[1417643] | 27 | main_dep = {}
|
---|
[6043559] | 28 |
|
---|
| 29 | def expand_var(s):
|
---|
| 30 | for k in expand_var_mp:
|
---|
| 31 | s = s.replace('$(' + k + ')', expand_var_mp[k])
|
---|
| 32 | return s
|
---|
| 33 |
|
---|
| 34 | def pop_stack(cond):
|
---|
| 35 | global ind0, ind1, stack
|
---|
| 36 | assert(cond(stack[-1][0]))
|
---|
| 37 | s, i0, i1, _ = stack[-1]
|
---|
| 38 | stack = stack[:-1]
|
---|
| 39 | ind0 -= i0
|
---|
| 40 | ind1 -= i1
|
---|
| 41 |
|
---|
| 42 | def pop_stack_while(cond):
|
---|
[1417643] | 43 | while stack and cond(stack[-1][0]):
|
---|
[6043559] | 44 | pop_stack(cond)
|
---|
| 45 |
|
---|
| 46 | def cur_menu():
|
---|
| 47 | global stack
|
---|
[1417643] | 48 | return stack[-1][3] if stack else 0
|
---|
[6043559] | 49 |
|
---|
[831ba20a] | 50 | def cur_if():
|
---|
| 51 | global if_stack
|
---|
[1417643] | 52 | return if_stack[-1][:] if if_stack else []
|
---|
[831ba20a] | 53 |
|
---|
[d67d543] | 54 | def clean_dep(d):
|
---|
| 55 | d = d.strip()
|
---|
| 56 | if d.endswith('=y') or d.endswith('=M'):
|
---|
| 57 | d = d[:-2]
|
---|
[7ebdf4e] | 58 | elif d.endswith(' != ""'):
|
---|
| 59 | d = d[:-6]
|
---|
[d67d543] | 60 | return d
|
---|
| 61 |
|
---|
[6043559] | 62 | def parse_config(buf):
|
---|
| 63 | global ind0, ind1, stack, menu_id
|
---|
[0d2ef60] | 64 | is_choice = buf[0].strip() == 'choice'
|
---|
| 65 | is_menu = buf[0].startswith('menu') or is_choice
|
---|
| 66 | is_nonconfig_menu = buf[0].startswith('menu ') or is_choice
|
---|
[1417643] | 67 | key = None if is_nonconfig_menu else buf[0].split()[1].strip()
|
---|
| 68 | title = buf[0][len('menu '):] if is_nonconfig_menu else None
|
---|
[831ba20a] | 69 | deps = ['menu'] + cur_if()
|
---|
[6043559] | 70 | klass = None
|
---|
[1417643] | 71 |
|
---|
[6043559] | 72 | for line in buf[1:]:
|
---|
| 73 | line = line.strip()
|
---|
| 74 | if line.startswith('depends on '):
|
---|
| 75 | new_deps = line[len('depends on '):].split('&&')
|
---|
[d67d543] | 76 | deps += [clean_dep(x) for x in new_deps]
|
---|
[1417643] | 77 | elif line.startswith('prompt'):
|
---|
| 78 | title = line[len('prompt '):]
|
---|
[6043559] | 79 | else:
|
---|
| 80 | for prefix in ['tristate', 'bool', 'string']:
|
---|
| 81 | if line.startswith(prefix + ' '):
|
---|
| 82 | title = line[len(prefix) + 1:]
|
---|
| 83 | klass = prefix
|
---|
[1417643] | 84 | elif line == prefix:
|
---|
| 85 | klass = prefix
|
---|
[6043559] | 86 | elif line.startswith('def_' + prefix + ' '):
|
---|
| 87 | klass = prefix
|
---|
[1417643] | 88 | else:
|
---|
| 89 | continue
|
---|
| 90 |
|
---|
| 91 | if '"' in line:
|
---|
| 92 | tail = line[line.rfind('"') + 1:].strip()
|
---|
| 93 | if tail[:3] == 'if ':
|
---|
| 94 | deps += [clean_dep(x) for x in tail[3:].split('&&')]
|
---|
[6043559] | 95 |
|
---|
| 96 | pop_stack_while(lambda x: x not in deps)
|
---|
| 97 |
|
---|
[1417643] | 98 | menu_id += is_menu
|
---|
| 99 | internal_key = key or menu_id
|
---|
| 100 | if stack:
|
---|
| 101 | fa = stack[-1][0]
|
---|
| 102 | if fa == 'menu':
|
---|
| 103 | fa = cur_menu() & ~choice_bit
|
---|
| 104 | main_dep[internal_key] = fa
|
---|
| 105 |
|
---|
| 106 | val = known_config.get(key)
|
---|
[5ca8d70] | 107 | comment = None
|
---|
[831ba20a] | 108 | forced = None
|
---|
[5ca8d70] | 109 |
|
---|
| 110 | if type(val) == dict:
|
---|
[831ba20a] | 111 | comment = val.get('comment')
|
---|
| 112 | forced = val.get('forced')
|
---|
[5ca8d70] | 113 | val = val['value']
|
---|
[6043559] | 114 |
|
---|
[1417643] | 115 | klass = klass or 'string'
|
---|
| 116 | if title:
|
---|
| 117 | title = title.strip().lstrip('"')
|
---|
| 118 | title = title[:title.find('"')]
|
---|
[6043559] | 119 |
|
---|
[1417643] | 120 | if not val:
|
---|
| 121 | pass
|
---|
| 122 | elif klass == 'string':
|
---|
[6043559] | 123 | val = '(' + val + ')'
|
---|
| 124 | else:
|
---|
| 125 | assert((val == 'X') == bool(cur_menu() & choice_bit))
|
---|
| 126 | if (val == 'X'):
|
---|
| 127 | val = '(X)'
|
---|
| 128 | else:
|
---|
[5ca8d70] | 129 | val = list(val)
|
---|
| 130 | val.sort()
|
---|
[6043559] | 131 | for c in val:
|
---|
| 132 | if c not in 'M* ' or (c == 'M' and klass != 'tristate'):
|
---|
| 133 | raise Exception('unknown setting %s for %s' % (c, key))
|
---|
[831ba20a] | 134 | bracket = None
|
---|
[185ffd9] | 135 | if klass == 'tristate' and forced != '*' :
|
---|
| 136 | bracket = '{}' if forced else '<>'
|
---|
| 137 | else:
|
---|
[831ba20a] | 138 | bracket = '--' if forced else '[]'
|
---|
| 139 |
|
---|
| 140 | val = bracket[0] + '/'.join(val) + bracket[1]
|
---|
[6043559] | 141 |
|
---|
| 142 | arrow = ' --->' if is_menu else ''
|
---|
[1417643] | 143 | r = [ind0, val, ind1, title, arrow, internal_key, cur_menu(), comment]
|
---|
| 144 |
|
---|
| 145 | # Don't indent for untitled (internal) entries
|
---|
| 146 | x = 2 if title else 0
|
---|
| 147 |
|
---|
| 148 | key = key or 'menu'
|
---|
[0d2ef60] | 149 | menu = (menu_id if is_menu else cur_menu())
|
---|
| 150 | menu |= choice_bit if is_choice else 0
|
---|
| 151 | stack_ent = (key, 2, 0, menu) if is_menu else (key, 0, x, menu)
|
---|
[6043559] | 152 | ind0 += stack_ent[1]
|
---|
| 153 | ind1 += stack_ent[2]
|
---|
| 154 | stack += [stack_ent]
|
---|
[2fbed80] | 155 |
|
---|
[6043559] | 156 | return r
|
---|
| 157 |
|
---|
| 158 | def load_kconfig(file):
|
---|
[831ba20a] | 159 | global ind0, ind1, stack, path, menu_id, if_stack
|
---|
[6043559] | 160 | r = []
|
---|
| 161 | config_buf = []
|
---|
| 162 | with open(path + file) as f:
|
---|
| 163 | for line in f:
|
---|
[1417643] | 164 | if config_buf:
|
---|
[d67d543] | 165 | if not (line.startswith('\t') or line.startswith(' ')):
|
---|
[0d2ef60] | 166 | r += [parse_config(config_buf)]
|
---|
[6043559] | 167 | config_buf = []
|
---|
| 168 | else:
|
---|
| 169 | config_buf += [line]
|
---|
| 170 | continue
|
---|
[6af847b] | 171 | if line.startswith('source') or line.startswith('\tsource'):
|
---|
| 172 | sub = expand_var(line.strip().split()[1].strip('"'))
|
---|
[6043559] | 173 | r += load_kconfig(sub)
|
---|
[1417643] | 174 | elif line.startswith('config') or line.startswith('menu'):
|
---|
[6043559] | 175 | config_buf = [line]
|
---|
| 176 | elif line.startswith('choice'):
|
---|
| 177 | config_buf = [line]
|
---|
| 178 | elif line.startswith('endmenu') or line.startswith('endchoice'):
|
---|
| 179 | pop_stack_while(lambda x: x != 'menu')
|
---|
| 180 | pop_stack(lambda x: x == 'menu')
|
---|
[831ba20a] | 181 | elif line.startswith('if '):
|
---|
| 182 | line = line[3:]
|
---|
| 183 | top = cur_if()
|
---|
| 184 | top += [x.strip() for x in line.split("&&")]
|
---|
| 185 | if_stack += [top]
|
---|
| 186 | elif line.startswith('endif'):
|
---|
| 187 | if_stack = if_stack[:-1]
|
---|
[5a809f30] | 188 |
|
---|
| 189 | if config_buf:
|
---|
| 190 | r += [parse_config(config_buf)]
|
---|
| 191 |
|
---|
[6043559] | 192 | return r
|
---|
| 193 |
|
---|
| 194 | known_config = {}
|
---|
| 195 |
|
---|
[c41cafb] | 196 | def escape(x):
|
---|
| 197 | return x.replace('<', '<').replace('>', '>')
|
---|
| 198 |
|
---|
[6043559] | 199 | from sys import argv
|
---|
| 200 | import tomllib
|
---|
| 201 |
|
---|
| 202 | path = argv[1]
|
---|
| 203 | if path[-1] != '/':
|
---|
| 204 | path += '/'
|
---|
| 205 | with open(argv[2], 'rb') as f:
|
---|
| 206 | known_config = tomllib.load(f)
|
---|
| 207 |
|
---|
[1417643] | 208 | r = load_kconfig('Kconfig')
|
---|
| 209 |
|
---|
| 210 | # Refcount all menus
|
---|
| 211 |
|
---|
| 212 | index_ikey = {}
|
---|
| 213 | for i in reversed(range(len(r))):
|
---|
| 214 | index_ikey[r[i][5]] = i
|
---|
| 215 |
|
---|
| 216 | for i in reversed(range(len(r))):
|
---|
[2fbed80] | 217 | if r[i][1] != None:
|
---|
[1417643] | 218 | key = r[i][5]
|
---|
| 219 | fa = main_dep.get(key)
|
---|
| 220 | if not fa:
|
---|
| 221 | continue
|
---|
| 222 | j = index_ikey[fa]
|
---|
[2fbed80] | 223 | if type(fa) == int or not r[j][3]:
|
---|
| 224 | # The main dependency is a menu or untitled magic entry,
|
---|
| 225 | # just mark it used
|
---|
[1417643] | 226 | r[j][1] = ''
|
---|
[2fbed80] | 227 | if r[j][1] is None:
|
---|
[1417643] | 228 | raise Exception('[%s] needs unselected [%s]' % (key, fa))
|
---|
| 229 |
|
---|
[2fbed80] | 230 | r = [i for i in r if i[1] != None and i[3]]
|
---|
[6043559] | 231 |
|
---|
| 232 | # Now we are going to pretty-print r
|
---|
| 233 |
|
---|
| 234 | ## Calculate the maximum value length for each menu
|
---|
| 235 | max_val_len = {}
|
---|
[5ca8d70] | 236 | for _, val, _, _, _, _, menu, _ in r:
|
---|
[1417643] | 237 | x = max_val_len.get(menu) or 0
|
---|
[6043559] | 238 | max_val_len[menu] = max(x, len(val))
|
---|
| 239 |
|
---|
| 240 | ## Output
|
---|
| 241 |
|
---|
| 242 | max_line = 80
|
---|
| 243 | buf = []
|
---|
| 244 |
|
---|
[1417643] | 245 | done = [x[5] for x in r] + ['revision']
|
---|
[6043559] | 246 | for i in known_config:
|
---|
[5ca8d70] | 247 | if i not in done:
|
---|
| 248 | raise Exception("%s seems not exist" % i)
|
---|
[6043559] | 249 |
|
---|
[ddf89fff] | 250 | sep = known_config.get('separate_toplevel_menu')
|
---|
| 251 |
|
---|
[5ca8d70] | 252 | for i0, val, i1, title, arrow, key, menu, comment in r:
|
---|
[c41cafb] | 253 | rem = max_line
|
---|
[333d2a84] | 254 | is_choice = (val == '(X)')
|
---|
[c41cafb] | 255 |
|
---|
[6043559] | 256 | if val:
|
---|
| 257 | val += (max_val_len[menu] - len(val)) * ' '
|
---|
| 258 |
|
---|
[c41cafb] | 259 | rem -= i0 + i1 + bool(val) + len(val)
|
---|
| 260 | line = i0 * ' ' + escape(val) + (i1 + bool(val)) * ' '
|
---|
| 261 |
|
---|
| 262 | rem -= len(arrow)
|
---|
[6043559] | 263 |
|
---|
| 264 | if len(title) > rem:
|
---|
| 265 | title = title[:rem - 3] + '...'
|
---|
| 266 |
|
---|
[333d2a84] | 267 | b = title
|
---|
| 268 | if not is_choice:
|
---|
[41eefd36] | 269 | while not (b[0].isalpha() and b[0] not in 'YyMmNnHh'):
|
---|
| 270 | b = b[1:]
|
---|
[c41cafb] | 271 | a = title[:len(title) - len(b)]
|
---|
| 272 | b0 = "<emphasis role='blue'>" + escape(b[0]) + "</emphasis>"
|
---|
| 273 | line += escape(a) + b0 + escape(b[1:]) + escape(arrow)
|
---|
| 274 |
|
---|
| 275 | rem -= len(title)
|
---|
[6043559] | 276 |
|
---|
[1417643] | 277 | key = ' [' + key + ']' if type(key) == str else ''
|
---|
[6043559] | 278 |
|
---|
| 279 | if len(key) <= rem:
|
---|
| 280 | line += (rem - len(key)) * ' ' + key
|
---|
| 281 | else:
|
---|
| 282 | key = '... ' + key
|
---|
| 283 | line += '\n' + ' ' * (max_line - len(key)) + key
|
---|
[1417643] | 284 | if type(comment) == str:
|
---|
| 285 | comment = [comment]
|
---|
[5ca8d70] | 286 | if comment:
|
---|
[1417643] | 287 | comment = '\n'.join([' ' * i0 + '# ' + line for line in comment])
|
---|
[ddf89fff] | 288 | buf += [escape(comment) + ':']
|
---|
| 289 |
|
---|
| 290 | if not menu and buf:
|
---|
| 291 | buf += ['']
|
---|
[c41cafb] | 292 |
|
---|
| 293 | buf += [line.rstrip()]
|
---|
[6043559] | 294 |
|
---|
[1417643] | 295 | from jinja2 import Template
|
---|
| 296 |
|
---|
[865ea30] | 297 | t = Template('''<?xml version="1.0" encoding="UTF-8"?>
|
---|
[6043559] | 298 | <!DOCTYPE note PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
|
---|
[1417643] | 299 | "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
|
---|
| 300 | <!-- Automatically generated by kernel-config.py
|
---|
| 301 | DO NOT EDIT! -->
|
---|
[c41cafb] | 302 | <screen{{ rev }}>{{ '\n'.join(buf) }}</screen>''')
|
---|
[1417643] | 303 |
|
---|
| 304 | rev = known_config.get('revision')
|
---|
| 305 | rev = ' revision="%s"' % rev if rev else ''
|
---|
| 306 | print(t.render(rev = rev, buf = buf))
|
---|