-
Notifications
You must be signed in to change notification settings - Fork 63
/
kconfig.go
488 lines (399 loc) · 9.48 KB
/
kconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 syzkaller project authors. All rights reserved.
// Copyright 2022 Unikraft GmbH. All rights reserved.
// Package kconfig implements parsing of the Linux kernel Kconfig and .config
// files and provides some algorithms to work with these files. For Kconfig
// reference see:
// https://www.kernel.org/doc/html/latest/kbuild/kconfig-language.html
package kconfig
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
)
// KConfigFile represents a parsed Kconfig file (including includes).
type KConfigFile struct {
// Root is the main menu
Root *KConfigMenu `json:"root,omitempty"`
// All config/menuconfig entries
Configs map[string]*KConfigMenu `json:"configs,omitempty"`
}
// KConfigMenu represents a single hierarchical menu or config.
type KConfigMenu struct {
// Kind represents the structure type, e.g. config/menu/choice/etc
Kind MenuKind `json:"kind,omitempty"`
// Type of menu element, e.g. tristate/bool/string/etc
Type ConfigType `json:"type,omitempty"`
// Name without CONFIG_
Name string `json:"name,omitempty"`
// Sub-elements for menus
Children []*KConfigMenu `json:"children,omitempty"`
// Prompt is the 1-line description of the menu entry.
Prompt KConfigPrompt `json:"prompt,omitempty"`
// Help information about the menu item.
Help string `json:"help,omitempty"`
// Default value of the entry.
Default DefaultValue `json:"default,omitempty"`
// Parent menu, non-nil for everythign except for mainmenu
parent *KConfigMenu
kconfigFile *KConfigFile // back-link to the owning KConfig
dependsOn expr
visibleIf expr
deps map[string]bool
depsOnce sync.Once
}
type KConfigPrompt struct {
Text string `json:"text,omitempty"`
Condition expr `json:"condition,omitempty"`
}
type DefaultValue struct {
Value expr `json:"value,omitempty"`
Condition expr `json:"condition,omitempty"`
}
type (
MenuKind string
ConfigType string
)
const (
MenuConfig = MenuKind("config")
MenuGroup = MenuKind("group")
MenuChoice = MenuKind("choice")
MenuComment = MenuKind("comment")
)
const (
TypeBool = ConfigType("bool")
TypeTristate = ConfigType("tristate")
TypeString = ConfigType("string")
TypeInt = ConfigType("int")
TypeHex = ConfigType("hex")
)
// DependsOn returns all transitive configs this config depends on.
func (m *KConfigMenu) DependsOn() map[string]bool {
m.depsOnce.Do(func() {
m.deps = make(map[string]bool)
if m.dependsOn != nil {
m.dependsOn.collectDeps(m.deps)
}
if m.visibleIf != nil {
m.visibleIf.collectDeps(m.deps)
}
var indirect []string
for cfg := range m.deps {
dep := m.kconfigFile.Configs[cfg]
if dep == nil {
delete(m.deps, cfg)
continue
}
for cfg1 := range dep.DependsOn() {
indirect = append(indirect, cfg1)
}
}
for _, cfg := range indirect {
m.deps[cfg] = true
}
})
return m.deps
}
type kconfigParser struct {
*parser
includes []*parser
stack []*KConfigMenu
cur *KConfigMenu
baseDir string
helpIdent int
}
func Parse(file string, env ...*KeyValue) (*KConfigFile, error) {
data, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to open Kconfig file %v: %v", file, err)
}
return ParseData(data, file, env...)
}
func ParseData(data []byte, file string, extra ...*KeyValue) (*KConfigFile, error) {
env := KeyValueMap{}
for _, kcv := range extra {
env[kcv.Key] = kcv
}
kp := &kconfigParser{
parser: newParser(data, file, env),
baseDir: filepath.Dir(file),
}
kp.parseFile()
if kp.err != nil {
return nil, kp.err
}
if len(kp.stack) == 0 {
return nil, fmt.Errorf("no mainmenu in config")
}
root := kp.stack[0]
kconf := &KConfigFile{
Root: root,
Configs: make(map[string]*KConfigMenu),
}
kconf.walk(root, nil, nil)
return kconf, nil
}
func (kconf *KConfigFile) walk(m *KConfigMenu, dependsOn, visibleIf expr) {
m.kconfigFile = kconf
m.dependsOn = exprAnd(dependsOn, m.dependsOn)
m.visibleIf = exprAnd(visibleIf, m.visibleIf)
if m.Kind == MenuConfig {
kconf.Configs[m.Name] = m
}
for _, elem := range m.Children {
kconf.walk(elem, m.dependsOn, m.visibleIf)
}
}
func (kp *kconfigParser) parseFile() {
for kp.nextLine() {
kp.parseLine()
if kp.TryConsume("#") {
_ = kp.ConsumeLine()
}
}
kp.endCurrent()
}
func (kp *kconfigParser) parseLine() {
if kp.eol() {
return
}
if kp.helpIdent != 0 {
if kp.identLevel() >= kp.helpIdent {
_ = kp.ConsumeLine()
return
}
kp.helpIdent = 0
}
if kp.TryConsume("#") {
_ = kp.ConsumeLine()
return
}
ident := kp.Ident()
if kp.TryConsume("=") || kp.TryConsume(":=") {
// Macro definition, see:
// https://www.kernel.org/doc/html/latest/kbuild/kconfig-macro-language.html
// We don't use this for anything now.
kp.ConsumeLine()
return
}
kp.parseMenu(ident)
}
func (kp *kconfigParser) parseMenu(cmd string) {
switch cmd {
case "source":
file, ok := kp.TryQuotedString()
if !ok {
file = kp.ConsumeLine()
}
kp.includeSource(file)
case "mainmenu":
kp.pushCurrent(&KConfigMenu{
Kind: MenuConfig,
Prompt: KConfigPrompt{Text: kp.QuotedString()},
})
case "comment":
kp.newCurrent(&KConfigMenu{
Kind: MenuComment,
Prompt: KConfigPrompt{Text: kp.QuotedString()},
})
case "menu":
kp.pushCurrent(&KConfigMenu{
Kind: MenuGroup,
Prompt: KConfigPrompt{Text: kp.QuotedString()},
})
case "if":
kp.pushCurrent(&KConfigMenu{
Kind: MenuGroup,
visibleIf: kp.parseExpr(),
})
case "choice":
kp.pushCurrent(&KConfigMenu{
Kind: MenuChoice,
})
case "endmenu", "endif", "endchoice":
kp.popCurrent()
case "config", "menuconfig":
kp.newCurrent(&KConfigMenu{
Kind: MenuConfig,
Name: kp.Ident(),
})
default:
kp.parseConfigType(cmd)
}
}
func (kp *kconfigParser) parseConfigType(typ string) {
cur := kp.current()
switch typ {
case "tristate":
cur.Type = TypeTristate
kp.tryParsePrompt()
case "def_tristate":
cur.Type = TypeTristate
kp.parseDefaultValue()
case "bool":
cur.Type = TypeBool
kp.tryParsePrompt()
case "def_bool":
cur.Type = TypeBool
kp.parseDefaultValue()
case "int":
cur.Type = TypeInt
kp.tryParsePrompt()
case "def_int":
cur.Type = TypeInt
kp.parseDefaultValue()
case "hex":
cur.Type = TypeHex
kp.tryParsePrompt()
case "def_hex":
cur.Type = TypeHex
kp.parseDefaultValue()
case "string":
cur.Type = TypeString
kp.tryParsePrompt()
case "def_string":
cur.Type = TypeString
kp.parseDefaultValue()
default:
kp.parseProperty(typ)
}
}
func (kp *kconfigParser) parseProperty(prop string) {
cur := kp.current()
switch prop {
case "prompt":
kp.tryParsePrompt()
case "depends":
kp.MustConsume("on")
cur.dependsOn = exprAnd(cur.dependsOn, kp.parseExpr())
case "visible":
kp.MustConsume("if")
cur.visibleIf = exprAnd(cur.visibleIf, kp.parseExpr())
case "select", "imply":
_ = kp.Ident()
if kp.TryConsume("if") {
_ = kp.parseExpr()
}
case "option":
// It can be 'option foo', or 'option bar="BAZ"'.
kp.ConsumeLine()
case "modules":
case "optional":
case "default":
kp.parseDefaultValue()
case "range":
_, _ = kp.parseExpr(), kp.parseExpr() // from, to
if kp.TryConsume("if") {
_ = kp.parseExpr()
}
case "help", "---help---":
kp.tryParseHelp()
default:
kp.failf("unknown line")
}
}
func (kp *kconfigParser) includeSource(file string) {
// ignore blank files
if file == "" {
return
}
kp.newCurrent(nil)
if file[0] != filepath.Separator {
file = filepath.Join(kp.baseDir, file)
}
data, err := os.ReadFile(file)
if err != nil {
kp.failf("%v", err)
return
}
kp.includes = append(kp.includes, kp.parser)
kp.parser = newParser(data, file, kp.env)
kp.parseFile()
err = kp.err
kp.parser = kp.includes[len(kp.includes)-1]
kp.includes = kp.includes[:len(kp.includes)-1]
if kp.err == nil {
kp.err = err
}
}
func (kp *kconfigParser) pushCurrent(m *KConfigMenu) {
kp.endCurrent()
kp.cur = m
kp.stack = append(kp.stack, m)
}
func (kp *kconfigParser) popCurrent() {
kp.endCurrent()
if len(kp.stack) < 2 {
return
}
last := kp.stack[len(kp.stack)-1]
kp.stack = kp.stack[:len(kp.stack)-1]
top := kp.stack[len(kp.stack)-1]
last.parent = top
top.Children = append(top.Children, last)
}
func (kp *kconfigParser) newCurrent(m *KConfigMenu) {
kp.endCurrent()
kp.cur = m
}
func (kp *kconfigParser) current() *KConfigMenu {
if kp.cur == nil {
kp.failf("config property outside of config")
return &KConfigMenu{}
}
return kp.cur
}
func (kp *kconfigParser) endCurrent() {
if kp.cur == nil {
return
}
if len(kp.stack) == 0 {
kp.failf("unbalanced endmenu")
kp.cur = nil
return
}
top := kp.stack[len(kp.stack)-1]
if top != kp.cur {
kp.cur.parent = top
top.Children = append(top.Children, kp.cur)
}
kp.cur = nil
}
func (kp *kconfigParser) tryParsePrompt() {
if str, ok := kp.TryQuotedString(); ok {
prompt := KConfigPrompt{
Text: str,
}
if kp.TryConsume("if") {
prompt.Condition = kp.parseExpr()
}
kp.current().Prompt = prompt
}
}
func (kp *kconfigParser) parseDefaultValue() {
def := DefaultValue{Value: kp.parseExpr()}
if kp.TryConsume("if") {
def.Condition = kp.parseExpr()
}
kp.current().Default = def
}
func (kp *kconfigParser) tryParseHelp() {
var help []string
baseHelpIdent := -1
for kp.nextLine() {
if kp.eol() {
continue
}
if len(help) > 0 && kp.identLevel() < baseHelpIdent {
break
}
if baseHelpIdent == -1 {
baseHelpIdent = kp.identLevel()
}
help = append(help, kp.ConsumeLine())
kp.helpIdent = kp.identLevel()
}
kp.current().Help = strings.Join(help, " ")
}