Skip to content

Commit

Permalink
build: Add gcc and binutils version to mcu data dictionary
Browse files Browse the repository at this point in the history
Store the gcc and binutils versions used in the compilation of the
firmware in the firmware data dictionary.  Forward that information to
the log so it is available during debugging.

Signed-off-by: Kevin O'Connor <[email protected]>
  • Loading branch information
KevinOConnor committed Dec 22, 2017
1 parent 522093e commit d778ae1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ $(OUT)%.o.ctr: $(OUT)%.o
$(OUT)compile_time_request.o: $(patsubst %.c, $(OUT)src/%.o.ctr,$(src-y)) ./scripts/buildcommands.py
@echo " Building $@"
$(Q)cat $(patsubst %.c, $(OUT)src/%.o.ctr,$(src-y)) > $(OUT)klipper.compile_time_request
$(Q)$(PYTHON) ./scripts/buildcommands.py -d $(OUT)klipper.dict $(OUT)klipper.compile_time_request $(OUT)compile_time_request.c
$(Q)$(PYTHON) ./scripts/buildcommands.py -d $(OUT)klipper.dict -t "$(CC);$(AS);$(LD);$(OBJCOPY);$(OBJDUMP);$(STRIP)" $(OUT)klipper.compile_time_request $(OUT)compile_time_request.c
$(Q)$(CC) $(CFLAGS) -c $(OUT)compile_time_request.c -o $@

$(OUT)klipper.elf: $(patsubst %.c, $(OUT)src/%.o,$(src-y)) $(OUT)compile_time_request.o
Expand Down
4 changes: 2 additions & 2 deletions klippy/mcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@ def _send_config(self):
msgparser = self._serial.msgparser
info = [
"Configured MCU '%s' (%d moves)" % (self._name, move_count),
"Loaded MCU '%s' %d commands (%s)" % (
"Loaded MCU '%s' %d commands (%s / %s)" % (
self._name, len(msgparser.messages_by_id),
msgparser.version),
msgparser.version, msgparser.build_versions),
"MCU '%s' config: %s" % (self._name, " ".join(
["%s=%s" % (k, v) for k, v in msgparser.config.items()]))]
self._printer.bglogger.set_rollover_info(self._name, "\n".join(info))
Expand Down
3 changes: 2 additions & 1 deletion klippy/msgproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def __init__(self):
self.messages_by_name = {}
self.static_strings = {}
self.config = {}
self.version = ""
self.version = self.build_versions = ""
self.raw_identify_data = ""
self._init_messages(DefaultMessages, DefaultMessages.keys())
def check_packet(self, s):
Expand Down Expand Up @@ -318,6 +318,7 @@ def process_identify(self, data, decompress=True):
self.static_strings = { int(k): v for k, v in static_strings.items() }
self.config.update(data.get('config', {}))
self.version = data.get('version', '')
self.build_versions = data.get('build_versions', '')
except error as e:
raise
except Exception as e:
Expand Down
5 changes: 3 additions & 2 deletions klippy/serialhdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ def connect(self):
msgparser.process_identify(identify_data)
self.msgparser = msgparser
self.register_callback(self.handle_unknown, '#unknown')
logging.info("Loaded %d commands (%s)",
len(msgparser.messages_by_id), msgparser.version)
logging.info("Loaded %d commands (%s / %s)",
len(msgparser.messages_by_id),
msgparser.version, msgparser.build_versions)
logging.info("MCU config: %s", " ".join(
["%s=%s" % (k, v) for k, v in msgparser.config.items()]))
# Setup baud adjust
Expand Down
49 changes: 43 additions & 6 deletions scripts/buildcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# Copyright (C) 2016 Kevin O'Connor <[email protected]>
#
# This file may be distributed under the terms of the GNU GPLv3 license.

import sys, os, subprocess, optparse, logging, shlex, socket, time
import sys, os, subprocess, optparse, logging, shlex, socket, time, traceback
import json, zlib
sys.path.append('./klippy')
import msgproto
Expand Down Expand Up @@ -182,7 +181,7 @@ def build_commands(cmd_by_id, messages_by_name, all_param_types):
######################################################################

def build_identify(cmd_by_id, msg_to_id, responses, static_strings
, constants, version):
, constants, version, toolstr):
#commands, messages, static_strings
messages = dict((msgid, msg) for msg, msgid in msg_to_id.items())
data = {}
Expand All @@ -193,6 +192,7 @@ def build_identify(cmd_by_id, msg_to_id, responses, static_strings
for i in range(len(static_strings)) }
data['config'] = constants
data['version'] = version
data['build_versions'] = toolstr

# Format compressed info into C code
data = json.dumps(data)
Expand All @@ -203,14 +203,17 @@ def build_identify(cmd_by_id, msg_to_id, responses, static_strings
out.append('\n ')
out.append(" 0x%02x," % (ord(zdata[i]),))
fmt = """
// version: %s
// build_versions: %s
const uint8_t command_identify_data[] PROGMEM = {%s
};
// Identify size = %d (%d uncompressed)
const uint32_t command_identify_size PROGMEM
= ARRAY_SIZE(command_identify_data);
"""
return data, fmt % (''.join(out), len(zdata), len(data))
return data, fmt % (version, toolstr, ''.join(out), len(zdata), len(data))


######################################################################
Expand Down Expand Up @@ -254,6 +257,36 @@ def build_version(extra):
version = "%s-%s-%s%s" % (version, btime, hostname, extra)
return version

# Run "tool --version" for each specified tool and extract versions
def tool_versions(tools):
tools = [t.strip() for t in tools.split(';')]
versions = ['', '']
success = 0
for tool in tools:
# Extract first line from "tool --version" output
verstr = check_output("%s --version" % (tool,)).split('\n')[0]
# Check if this tool looks like a binutils program
isbinutils = 0
if verstr.startswith('GNU '):
isbinutils = 1
verstr = verstr[4:]
# Extract version information and exclude program name
if ' ' not in verstr:
continue
prog, ver = verstr.split(' ', 1)
if not prog or not ver:
continue
# Check for any version conflicts
if versions[isbinutils] and versions[isbinutils] != ver:
logging.debug("Mixed version %s vs %s" % (
repr(versions[isbinutils]), repr(ver)))
versions[isbinutils] = "mixed"
continue
versions[isbinutils] = ver
success += 1
cleanbuild = versions[0] and versions[1] and success == len(tools)
return cleanbuild, "gcc: %s binutils: %s" % (versions[0], versions[1])


######################################################################
# Main code
Expand All @@ -266,6 +299,8 @@ def main():
help="extra version string to append to version")
opts.add_option("-d", dest="write_dictionary",
help="file to write mcu protocol dictionary")
opts.add_option("-t", "--tools", dest="tools", default="",
help="list of build programs to extract version from")
opts.add_option("-v", action="store_true", dest="verbose",
help="enable debug messages")

Expand Down Expand Up @@ -349,12 +384,14 @@ def main():
cmdcode = build_commands(cmd_by_id, messages_by_name, all_param_types)
paramcode = build_param_types(all_param_types)
# Create identify information
cleanbuild, toolstr = tool_versions(options.tools)
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, constants, version)
datadict, icode = build_identify(
cmd_by_id, msg_to_id, responses,
static_strings, constants, version, toolstr)
# Write output
f = open(outcfile, 'wb')
f.write(FILEHEADER + call_lists_code + static_strings_code
Expand Down

0 comments on commit d778ae1

Please sign in to comment.