def get_makefile_config_string(name, value, orig_type): if orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE): return "{}{}={}\n".format(config.config_prefix, name, '' if value == 'n' else value) elif orig_type in (kconfiglib.INT, kconfiglib.HEX): return "{}{}={}\n".format(config.config_prefix, name, value) elif orig_type == kconfiglib.STRING: return '{}{}="{}"\n'.format(config.config_prefix, name, kconfiglib.escape(value)) else: raise RuntimeError('{}{}: unknown type {}'.format(config.config_prefix, name, orig_type))
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 write_c_header( kconfig, filename, header="/* Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib) */\n" ): r""" Writes out symbol values as a C header file. Based on Kconfig's write_autoconf function. The ordering of the #defines matches the one generated by write_config(). The order in the C implementation depends on the hash table implementation as of writing, and so won't match. filename: Self-explanatory. header (default: "/* Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib) */\n"): Text that will be inserted verbatim at the beginning of the file. You would usually want it enclosed in '/* */' to make it a C comment, and include a final terminating newline. """ with kconfig._open(filename, "w") as f: f.write(header) for sym in kconfig.unique_defined_syms: # Note: _write_to_conf is determined when the value is # calculated. This is a hidden function call due to # property magic. val = sym.str_value if sym._write_to_conf: if re.match('PREDEFINED_', sym.name): continue if sym.orig_type in _BOOL_TRISTATE: f.write("#define {}{}{} {}\n".format( kconfig.config_prefix, sym.name, "_MODULE" if val == "m" else "", "1" if val == "y" else "0")) elif sym.orig_type is STRING: f.write('#define {}{} "{}"\n'.format( kconfig.config_prefix, sym.name, escape(val))) else: # sym.orig_type in _INT_HEX: if sym.orig_type is HEX and \ not val.startswith(("0x", "0X")): val = "0x" + val f.write("#define {}{} {}\n".format(kconfig.config_prefix, sym.name, val))
def write_node(node): sym = node.item if not isinstance(sym, kconfiglib.Symbol): return # Note: str_value calculates _write_to_conf, due to # internal magic in kconfiglib... val = sym.str_value if sym._write_to_conf: if sym.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE) and val == "n": val = "" # write unset values as empty variables elif sym.orig_type == kconfiglib.STRING: val = kconfiglib.escape(val) write("set({}{} \"{}\")\n".format(prefix, sym.name, val))
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 get_makefile_config_string(name, value, orig_type): if orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE): value = '' if value == 'n' else value elif orig_type == kconfiglib.INT: try: value = int(value) except ValueError: value = '' elif orig_type == kconfiglib.HEX: try: value = hex(int(value, 16)) # ensure 0x prefix except ValueError: value = '' elif orig_type == kconfiglib.STRING: value = '"{}"'.format(kconfiglib.escape(value)) else: raise RuntimeError('{}{}: unknown type {}'.format(config.config_prefix, name, orig_type)) return '{}{}={}\n'.format(config.config_prefix, name, value)
def write_node(node): sym = node.item if not isinstance(sym, kconfiglib.Symbol): return if sym.config_string: val = sym.str_value if sym.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE) and val == 'n': val = '' # write unset values as empty variables elif sym.orig_type == kconfiglib.STRING: val = kconfiglib.escape(val) elif sym.orig_type == kconfiglib.HEX: val = hex(int(val, 16)) # ensure 0x prefix write('set({}{} "{}")\n'.format(prefix, sym.name, val)) configs_list.append(prefix + sym.name) dep_opt = deprecated_options.get_deprecated_option(sym.name) if dep_opt: tmp_dep_list.append('set({}{} "{}")\n'.format(prefix, dep_opt, val)) configs_list.append(prefix + dep_opt)
def write_node(node): sym = node.item if not isinstance(sym, kconfiglib.Symbol): return if sym.config_string: val = sym.str_value if sym.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE) and val == "n": val = "" # write unset values as empty variables elif sym.orig_type == kconfiglib.STRING: val = kconfiglib.escape(val) write("set({}{} \"{}\")\n".format(prefix, sym.name, val)) configs_list.append(prefix + sym.name) dep_opt = deprecated_options.get_deprecated_option(sym.name) if dep_opt: tmp_dep_list.append("set({}{} \"{}\")\n".format( prefix, dep_opt, val)) configs_list.append(prefix + dep_opt)