From fe95ea221b2b88e9cb52a6378ff2018ee752094b Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Thu, 22 Dec 2016 23:47:46 -0500 Subject: build: Define DECL_CONSTANT mechanism for defining exported constants Add a DECL_CONSTANT macro to allow the firmware to define constants that are to be exported to the host during the "identify" phase. This replaces the existing hardcoded mechanism of scanning the Kconfig header file for certain constants. Signed-off-by: Kevin O'Connor --- scripts/buildcommands.py | 43 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) (limited to 'scripts/buildcommands.py') diff --git a/scripts/buildcommands.py b/scripts/buildcommands.py index 9b6acb45..c0584d15 100644 --- a/scripts/buildcommands.py +++ b/scripts/buildcommands.py @@ -21,25 +21,6 @@ def error(msg): sys.stderr.write(msg + "\n") sys.exit(-1) -# Parser for constants in simple C header files. -def scan_config(file): - f = open(file, 'r') - opts = {} - for l in f.readlines(): - parts = l.split() - if len(parts) != 3: - continue - if parts[0] != '#define': - continue - value = parts[2] - if value.isdigit() or (value.startswith('0x') and value[2:].isdigit()): - value = int(value, 0) - elif value.startswith('"'): - value = value[1:-1] - opts[parts[1]] = value - f.close() - return opts - ###################################################################### # Command and output parser generation @@ -145,7 +126,7 @@ const uint8_t command_index_size PROGMEM = ARRAY_SIZE(command_index); ###################################################################### def build_identify(cmd_by_id, msg_to_id, responses, static_strings - , config, version): + , constants, version): #commands, messages, static_strings messages = dict((msgid, msg) for msg, msgid in msg_to_id.items()) data = {} @@ -153,9 +134,7 @@ def build_identify(cmd_by_id, msg_to_id, responses, static_strings data['commands'] = sorted(cmd_by_id.keys()) data['responses'] = sorted(responses) data['static_strings'] = static_strings - configlist = ['MCU', 'CLOCK_FREQ', 'SERIAL_BAUD'] - data['config'] = dict((i, config['CONFIG_'+i]) for i in configlist - if 'CONFIG_'+i in config) + data['config'] = constants data['version'] = version # Format compressed info into C code @@ -223,7 +202,7 @@ def build_version(extra): ###################################################################### def main(): - usage = "%prog [options] " + usage = "%prog [options] " opts = optparse.OptionParser(usage) opts.add_option("-e", "--extra", dest="extra", default="", help="extra version string to append to version") @@ -233,9 +212,9 @@ def main(): help="enable debug messages") options, args = opts.parse_args() - if len(args) != 3: + if len(args) != 2: opts.error("Incorrect arguments") - incmdfile, inheader, outcfile = args + incmdfile, outcfile = args if options.verbose: logging.basicConfig(level=logging.DEBUG) @@ -245,6 +224,7 @@ def main(): for m in msgproto.DefaultMessages.values()) parsers = [] static_strings = [] + constants = {} # Parse request file f = open(incmdfile, 'rb') data = f.read() @@ -280,6 +260,14 @@ def main(): parsers.append((None, msg)) elif cmd == '_DECL_STATIC_STR': static_strings.append(req[17:]) + elif cmd == '_DECL_CONSTANT': + name, value = parts[1:] + value = value.strip() + if value.startswith('"') and value.endswith('"'): + value = value[1:-1] + if name in constants and constants[name] != value: + error("Conflicting definition for constant '%s'" % name) + constants[name] = value else: error("Unknown build time command '%s'" % cmd) # Create unique ids for each message type @@ -299,13 +287,12 @@ def main(): cmdcode = build_commands(cmd_by_id, messages_by_name, all_param_types) paramcode = build_param_types(all_param_types) # Create identify information - config = scan_config(inheader) version = build_version(options.extra) sys.stdout.write("Version: %s\n" % (version,)) responses = [msg_to_id[msg] for msgname, msg in messages_by_name.items() if msgname not in commands] datadict, icode = build_identify(cmd_by_id, msg_to_id, responses - , static_strings, config, version) + , static_strings, constants, version) # Write output f = open(outcfile, 'wb') f.write(FILEHEADER + paramcode + parsercode + cmdcode + icode) -- cgit v1.2.3-70-g09d2