diff options
| -rw-r--r-- | kconfiglib.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index 4b2ec2c..f31a71c 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -826,6 +826,74 @@ class Kconfig(object): if not choice._was_set: choice.unset_value() + def write_autoconf(self, filename, + header="/* Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib) */\n"): + r""" + Writes out symbol values as a C header file. + + 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 open(filename, "w") as f: + + # Small optimization + write = f.write + write(header) + + # Avoid duplicates + for sym in self.defined_syms: + sym._written = False + + node = self.top_node.list + if not node: + # Empty configuration + return + + while 1: + item = node.item + if isinstance(item, Symbol): + if not item._written: + # Force caculation of the value by means of the hidden + # call triggered by accessing str_value + val = item.str_value + if item._write_to_conf: + if item.orig_type in (BOOL, TRISTATE) and val != "n": + suffix = "_MODULE" if val == "m" else "" + write("#define {}{}{} 1\n" + .format(self.config_prefix, item.name, + suffix)) + elif item.orig_type == STRING: + write('#define {}{} "{}"\n' + .format(self.config_prefix, item.name, + escape(val))) + elif item.orig_type in (INT, HEX): + if item.orig_type == HEX and not val.startswith(("0x", "0X")): + val = "0x" + val + write("#define {}{} {}\n" + .format(self.config_prefix, item.name, + val)) + + item._written = True + + # Iterative tree walk using parent pointers + if node.list: + node = node.list + elif node.next: + node = node.next + else: + while node.parent: + node = node.parent + if node.next: + node = node.next + break + else: + return + def write_config(self, filename, header="# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"): r""" |
