Skip to content

Commit 7e03d8f

Browse files
Merge pull request #4110 from theotherjimmy/build-metadata
Add --build-data switch to mbed compile and test
2 parents 7f0ed3b + c4c6e13 commit 7e03d8f

File tree

5 files changed

+83
-20
lines changed

5 files changed

+83
-20
lines changed

tools/build_api.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717

1818
import re
1919
import tempfile
20+
import datetime
21+
import uuid
2022
from types import ListType
2123
from shutil import rmtree
2224
from os.path import join, exists, dirname, basename, abspath, normpath, splitext
25+
from os.path import relpath
2326
from os import linesep, remove, makedirs
2427
from time import time
2528
from intelhex import IntelHex
29+
from json import load, dump
2630

2731
from tools.utils import mkdir, run_cmd, run_cmd_ext, NotSupportedException,\
2832
ToolException, InvalidReleaseTargetException, intelhex_offset
@@ -102,6 +106,8 @@ def add_result_to_report(report, result):
102106
report - the report to append to
103107
result - the result to append
104108
"""
109+
result["date"] = datetime.datetime.utcnow().isoformat()
110+
result["uuid"] = str(uuid.uuid1())
105111
target = result["target_name"]
106112
toolchain = result["toolchain_name"]
107113
id_name = result['id']
@@ -552,6 +558,9 @@ def build_project(src_paths, build_path, target, toolchain_name,
552558
cur_result["output"] = toolchain.get_output() + memap_table
553559
cur_result["result"] = "OK"
554560
cur_result["memory_usage"] = toolchain.map_outputs
561+
cur_result["bin"] = res
562+
cur_result["elf"] = splitext(res)[0] + ".elf"
563+
cur_result.update(toolchain.report)
555564

556565
add_result_to_report(report, cur_result)
557566

@@ -653,6 +662,7 @@ def build_library(src_paths, build_path, target, toolchain_name,
653662
prep_report(report, toolchain.target.name, toolchain_name, id_name)
654663
cur_result = create_result(toolchain.target.name, toolchain_name,
655664
id_name, description)
665+
cur_result['type'] = 'library'
656666
if properties != None:
657667
prep_properties(properties, toolchain.target.name, toolchain_name,
658668
vendor_label)
@@ -1362,3 +1372,24 @@ def write_build_report(build_report, template_filename, filename):
13621372
placeholder.write(template.render(
13631373
failing_builds=build_report_failing,
13641374
passing_builds=build_report_passing))
1375+
1376+
1377+
def merge_build_data(filename, toolchain_report, app_type):
1378+
path_to_file = dirname(abspath(filename))
1379+
try:
1380+
build_data = load(open(filename))
1381+
except (IOError, ValueError):
1382+
build_data = {'builds': []}
1383+
for tgt in toolchain_report.values():
1384+
for tc in tgt.values():
1385+
for project in tc.values():
1386+
for build in project:
1387+
try:
1388+
build[0]['elf'] = relpath(build[0]['elf'], path_to_file)
1389+
build[0]['bin'] = relpath(build[0]['bin'], path_to_file)
1390+
except KeyError:
1391+
pass
1392+
if 'type' not in build[0]:
1393+
build[0]['type'] = app_type
1394+
build_data['builds'].append(build[0])
1395+
dump(build_data, open(filename, "wb"), indent=4, separators=(',', ': '))

tools/config/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from os.path import dirname, abspath, exists
2121
import sys
2222
from collections import namedtuple
23-
from os.path import splitext
23+
from os.path import splitext, relpath
2424
from intelhex import IntelHex
2525
from jinja2 import FileSystemLoader, StrictUndefined
2626
from jinja2.environment import Environment
@@ -389,25 +389,25 @@ def __init__(self, tgt, top_level_dirs=None, app_config=None):
389389
search for a configuration file).
390390
"""
391391
config_errors = []
392-
app_config_location = app_config
393-
if app_config_location is None:
392+
self.app_config_location = app_config
393+
if self.app_config_location is None:
394394
for directory in top_level_dirs or []:
395395
full_path = os.path.join(directory, self.__mbed_app_config_name)
396396
if os.path.isfile(full_path):
397-
if app_config_location is not None:
397+
if self.app_config_location is not None:
398398
raise ConfigException("Duplicate '%s' file in '%s' and '%s'"
399399
% (self.__mbed_app_config_name,
400-
app_config_location, full_path))
400+
self.app_config_location, full_path))
401401
else:
402-
app_config_location = full_path
402+
self.app_config_location = full_path
403403
try:
404-
self.app_config_data = json_file_to_dict(app_config_location) \
405-
if app_config_location else {}
404+
self.app_config_data = json_file_to_dict(self.app_config_location) \
405+
if self.app_config_location else {}
406406
except ValueError as exc:
407407
self.app_config_data = {}
408408
config_errors.append(
409409
ConfigException("Could not parse mbed app configuration from %s"
410-
% app_config_location))
410+
% self.app_config_location))
411411

412412
# Check the keys in the application configuration data
413413
unknown_keys = set(self.app_config_data.keys()) - \
@@ -529,6 +529,11 @@ def regions(self):
529529
raise ConfigException("Not enough memory on device to fit all "
530530
"application regions")
531531

532+
@property
533+
def report(self):
534+
return {'app_config': self.app_config_location,
535+
'library_configs': map(relpath, self.processed_configs.keys())}
536+
532537
def _process_config_and_overrides(self, data, params, unit_name, unit_kind):
533538
"""Process "config_parameters" and "target_config_overrides" into a
534539
given dictionary

tools/make.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from time import sleep
2424
from shutil import copy
2525
from os.path import join, abspath, dirname
26+
from json import load, dump
2627

2728
# Be sure that the tools directory is in the search path
2829
ROOT = abspath(join(dirname(__file__), ".."))
@@ -48,6 +49,7 @@
4849
from tools.build_api import mcu_toolchain_matrix
4950
from tools.build_api import mcu_toolchain_list
5051
from tools.build_api import mcu_target_list
52+
from tools.build_api import merge_build_data
5153
from utils import argparse_filestring_type
5254
from utils import argparse_many
5355
from utils import argparse_dir_not_parent
@@ -177,6 +179,11 @@
177179
default=False,
178180
help="Link with mbed test library")
179181

182+
parser.add_argument("--build-data",
183+
dest="build_data",
184+
default=None,
185+
help="Dump build_data to this file")
186+
180187
# Specify a different linker script
181188
parser.add_argument("-l", "--linker", dest="linker_script",
182189
type=argparse_filestring_type,
@@ -249,6 +256,7 @@
249256
%(toolchain,search_path))
250257

251258
# Test
259+
build_data_blob = {} if options.build_data else None
252260
for test_no in p:
253261
test = Test(test_no)
254262
if options.automated is not None: test.automated = options.automated
@@ -287,6 +295,7 @@
287295
clean=options.clean,
288296
verbose=options.verbose,
289297
notify=notify,
298+
report=build_data_blob,
290299
silent=options.silent,
291300
macros=options.macros,
292301
jobs=options.jobs,
@@ -342,3 +351,5 @@
342351
print "[ERROR] %s" % str(e)
343352

344353
sys.exit(1)
354+
if options.build_data:
355+
merge_build_data(options.build_data, build_data_blob, "application")

tools/test.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from tools.options import get_default_options_parser, extract_profile
3232
from tools.build_api import build_project, build_library
3333
from tools.build_api import print_build_memory_usage
34+
from tools.build_api import merge_build_data
3435
from tools.targets import TARGET_MAP
3536
from tools.utils import mkdir, ToolException, NotSupportedException, args_error
3637
from tools.test_exporters import ReportExporter, ResultExporterType
@@ -88,6 +89,10 @@
8889

8990
parser.add_argument("--build-report-junit", dest="build_report_junit",
9091
default=None, help="Destination path for a build report in the JUnit xml format")
92+
parser.add_argument("--build-data",
93+
dest="build_data",
94+
default=None,
95+
help="Dump build_data to this file")
9196

9297
parser.add_argument("-v", "--verbose",
9398
action="store_true",
@@ -175,17 +180,13 @@
175180
profile = extract_profile(parser, options, toolchain)
176181
try:
177182
# Build sources
178-
build_library(base_source_paths, options.build_dir, mcu, toolchain,
179-
jobs=options.jobs,
180-
clean=options.clean,
181-
report=build_report,
182-
properties=build_properties,
183-
name="mbed-build",
184-
macros=options.macros,
185-
verbose=options.verbose,
186-
notify=notify,
187-
archive=False,
188-
app_config=options.app_config,
183+
build_library(base_source_paths, options.build_dir, mcu,
184+
toolchain, jobs=options.jobs,
185+
clean=options.clean, report=build_report,
186+
properties=build_properties, name="mbed-build",
187+
macros=options.macros, verbose=options.verbose,
188+
notify=notify, archive=False,
189+
app_config=options.app_config,
189190
build_profile=profile)
190191

191192
library_build_success = True
@@ -245,6 +246,8 @@
245246

246247
print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build")
247248
status = print_report_exporter.report(build_report)
249+
if options.build_data:
250+
merge_build_data(options.build_data, build_report, "test")
248251

249252
if status:
250253
sys.exit(0)

tools/toolchains/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,19 @@ def redirect_symbol(source, sync, build_dir):
13931393
def get_config_macros(self):
13941394
return Config.config_to_macros(self.config_data) if self.config_data else []
13951395

1396+
@property
1397+
def report(self):
1398+
to_ret = {}
1399+
to_ret['c_compiler'] = {'flags': copy(self.flags['c']),
1400+
'symbols': self.get_symbols()}
1401+
to_ret['cxx_compiler'] = {'flags': copy(self.flags['cxx']),
1402+
'symbols': self.get_symbols()}
1403+
to_ret['assembler'] = {'flags': copy(self.flags['asm']),
1404+
'symbols': self.get_symbols(True)}
1405+
to_ret['linker'] = {'flags': copy(self.flags['ld'])}
1406+
to_ret.update(self.config.report)
1407+
return to_ret
1408+
13961409
from tools.settings import ARM_PATH
13971410
from tools.settings import GCC_ARM_PATH
13981411
from tools.settings import IAR_PATH

0 commit comments

Comments
 (0)