Skip to content

Commit d859182

Browse files
Shawnshhwenlingz
authored andcommitted
customize function to generate config.h with proper suffixes
Add new format to parsed by kconfig to support U and UL. When config is 'int',if this symbol have 'range' key words and bigger or qeual to 0,the int macro will have suffix 'U'. When config is 'hex',the suffix is 'U'. When config have 'help' keywords,and the help contents have the string "64-bit integer",it will add suffix 'L'. V1->V2: 1.modified the comments to let it much eaisy to understand. 2.change the values' name protected_foot,protected_tai to guard_begin and guard_end. 3.add regex to identified the '64-bit' and 'integer'. V2->V3: 1.remove kconfiglib internal attribute 2.use config_string to avoid no active config entry Tracked-On: #861 Signed-off-by: Huihuang Shi <huihuang.shi@intel.com>
1 parent 8ccaf3c commit d859182

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

scripts/kconfig/generate_header.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,63 @@
1010
# SPDX-License-Identifier: ISC
1111
# Refer to scripts/kconfig/LICENSE.kconfiglib for the permission notice.
1212
import kconfiglib
13+
import re
14+
15+
class Acrn_config(kconfiglib.Kconfig):
16+
help_regex = re.compile("64-bit[\s\n]+integer")
17+
def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
18+
encoding="utf-8"):
19+
kconfiglib.Kconfig.__init__(self, filename, warn, warn_to_stderr, encoding)
20+
21+
def write_autoconf(self, filename,
22+
header="/* Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib) */\n"):
23+
24+
guard_begin = "#ifndef __HV_KCONFIG__\n#define __HV_KCONFIG__\n"
25+
guard_end = "#endif"
26+
27+
with open(filename, "w") as f:
28+
f.write(header)
29+
f.write(guard_begin)
30+
31+
for sym in self.defined_syms:
32+
if sym.config_string in ("",None):
33+
continue
34+
else:
35+
val = sym.str_value
36+
if sym.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE):
37+
if val != "n":
38+
f.write("#define {}{}{} 1\n"
39+
.format(self.config_prefix, sym.name,
40+
"_MODULE" if val == "m" else ""))
41+
elif sym.orig_type == kconfiglib.STRING:
42+
f.write('#define {}{} "{}"\n'
43+
.format(self.config_prefix, sym.name,
44+
kconfiglib.escape(val)))
45+
elif sym.orig_type in (kconfiglib.INT, kconfiglib.HEX):
46+
if sym.orig_type == kconfiglib.HEX:
47+
val = val + "U"
48+
if not val.startswith(("0x", "0X")):
49+
val = "0x" + val
50+
elif sym.orig_type == kconfiglib.INT and len(sym.ranges) > 0:
51+
left_sym = sym.ranges[0][0]
52+
right_sym = sym.ranges[0][1]
53+
left_value = int(left_sym.str_value)
54+
right_value = int(right_sym.str_value)
55+
if left_value >= 0 and right_value >= 0:
56+
val = val + "U"
57+
58+
_help = sym.nodes[0].help
59+
if _help not in (None,"") and len(self.help_regex.findall(_help)) > 0:
60+
val = val + "L"
61+
f.write("#define {}{} {}\n"
62+
.format(self.config_prefix, sym.name, val))
63+
else:
64+
raise Exception("Internal error while creating C "
65+
'header: unknown type "{}".'
66+
.format(sym.orig_type))
67+
68+
f.write(guard_end)
69+
1370

1471
def usage():
1572
sys.stdout.write("%s: <Kconfig file> <.config file> <path to config.h>\n" % sys.argv[0])
@@ -29,7 +86,7 @@ def main():
2986
sys.stderr.write("Cannot find file %s\n" % config_path)
3087
sys.exit(1)
3188

32-
kconfig = kconfiglib.Kconfig(kconfig_path)
89+
kconfig = Acrn_config(kconfig_path)
3390
kconfig.load_config(config_path)
3491
kconfig.write_autoconf(sys.argv[3])
3592
sys.stdout.write("Configuration header written to %s.\n" % sys.argv[3])

0 commit comments

Comments
 (0)