summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarles Cufi <carles.cufi@nordicsemi.no>2018-01-04 22:00:05 +0100
committerCarles Cufi <carles.cufi@nordicsemi.no>2018-01-05 10:57:44 +0100
commitd2a272777f4b877207dbe16fd9c616aad78aa7c9 (patch)
tree9fad6ad496443602fe5fa48015148e5b96add1b1
parentefcd2bda4684ed29951c808c571c4159a7aa6b28 (diff)
Implement autoconf.h generation
Implement the generation of the C header file that mirrors the .config files, commonly named autoconf.h. Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
-rw-r--r--kconfiglib.py68
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"""