From d859182d4117ed1af3559eb81ce6d89364c01d0f Mon Sep 17 00:00:00 2001 From: Huihuang Shi Date: Thu, 11 Oct 2018 10:20:08 +0800 Subject: [PATCH] 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 --- scripts/kconfig/generate_header.py | 59 +++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/scripts/kconfig/generate_header.py b/scripts/kconfig/generate_header.py index 6afec0a31..5fa198046 100644 --- a/scripts/kconfig/generate_header.py +++ b/scripts/kconfig/generate_header.py @@ -10,6 +10,63 @@ import sys, os # SPDX-License-Identifier: ISC # Refer to scripts/kconfig/LICENSE.kconfiglib for the permission notice. import kconfiglib +import re + +class Acrn_config(kconfiglib.Kconfig): + help_regex = re.compile("64-bit[\s\n]+integer") + def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True, + encoding="utf-8"): + kconfiglib.Kconfig.__init__(self, filename, warn, warn_to_stderr, encoding) + + def write_autoconf(self, filename, + header="/* Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib) */\n"): + + guard_begin = "#ifndef __HV_KCONFIG__\n#define __HV_KCONFIG__\n" + guard_end = "#endif" + + with open(filename, "w") as f: + f.write(header) + f.write(guard_begin) + + for sym in self.defined_syms: + if sym.config_string in ("",None): + continue + else: + val = sym.str_value + if sym.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE): + if val != "n": + f.write("#define {}{}{} 1\n" + .format(self.config_prefix, sym.name, + "_MODULE" if val == "m" else "")) + elif sym.orig_type == kconfiglib.STRING: + f.write('#define {}{} "{}"\n' + .format(self.config_prefix, sym.name, + kconfiglib.escape(val))) + elif sym.orig_type in (kconfiglib.INT, kconfiglib.HEX): + if sym.orig_type == kconfiglib.HEX: + val = val + "U" + if not val.startswith(("0x", "0X")): + val = "0x" + val + elif sym.orig_type == kconfiglib.INT and len(sym.ranges) > 0: + left_sym = sym.ranges[0][0] + right_sym = sym.ranges[0][1] + left_value = int(left_sym.str_value) + right_value = int(right_sym.str_value) + if left_value >= 0 and right_value >= 0: + val = val + "U" + + _help = sym.nodes[0].help + if _help not in (None,"") and len(self.help_regex.findall(_help)) > 0: + val = val + "L" + f.write("#define {}{} {}\n" + .format(self.config_prefix, sym.name, val)) + else: + raise Exception("Internal error while creating C " + 'header: unknown type "{}".' + .format(sym.orig_type)) + + f.write(guard_end) + def usage(): sys.stdout.write("%s: <.config file> \n" % sys.argv[0]) @@ -29,7 +86,7 @@ def main(): sys.stderr.write("Cannot find file %s\n" % config_path) sys.exit(1) - kconfig = kconfiglib.Kconfig(kconfig_path) + kconfig = Acrn_config(kconfig_path) kconfig.load_config(config_path) kconfig.write_autoconf(sys.argv[3]) sys.stdout.write("Configuration header written to %s.\n" % sys.argv[3])