BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 #! /usr/bin/env python2
borlanic 0:fbdae7e6d805 2 """
borlanic 0:fbdae7e6d805 3 mbed SDK
borlanic 0:fbdae7e6d805 4 Copyright (c) 2011-2013 ARM Limited
borlanic 0:fbdae7e6d805 5
borlanic 0:fbdae7e6d805 6 Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:fbdae7e6d805 7 you may not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 8 You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 9
borlanic 0:fbdae7e6d805 10 http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 11
borlanic 0:fbdae7e6d805 12 Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 13 distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:fbdae7e6d805 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 15 See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 16 limitations under the License.
borlanic 0:fbdae7e6d805 17
borlanic 0:fbdae7e6d805 18
borlanic 0:fbdae7e6d805 19 TEST BUILD & RUN
borlanic 0:fbdae7e6d805 20 """
borlanic 0:fbdae7e6d805 21 from __future__ import print_function, division, absolute_import
borlanic 0:fbdae7e6d805 22 import sys
borlanic 0:fbdae7e6d805 23 import os
borlanic 0:fbdae7e6d805 24 import json
borlanic 0:fbdae7e6d805 25 import fnmatch
borlanic 0:fbdae7e6d805 26
borlanic 0:fbdae7e6d805 27 ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
borlanic 0:fbdae7e6d805 28 sys.path.insert(0, ROOT)
borlanic 0:fbdae7e6d805 29
borlanic 0:fbdae7e6d805 30 from tools.config import ConfigException, Config
borlanic 0:fbdae7e6d805 31 from tools.test_api import test_path_to_name, find_tests, get_test_config, print_tests, build_tests, test_spec_from_test_builds
borlanic 0:fbdae7e6d805 32 from tools.test_configs import get_default_config
borlanic 0:fbdae7e6d805 33 from tools.options import get_default_options_parser, extract_profile, extract_mcus
borlanic 0:fbdae7e6d805 34 from tools.build_api import build_project, build_library
borlanic 0:fbdae7e6d805 35 from tools.build_api import print_build_memory_usage
borlanic 0:fbdae7e6d805 36 from tools.build_api import merge_build_data
borlanic 0:fbdae7e6d805 37 from tools.targets import TARGET_MAP
borlanic 0:fbdae7e6d805 38 from tools.notifier.term import TerminalNotifier
borlanic 0:fbdae7e6d805 39 from tools.utils import mkdir, ToolException, NotSupportedException, args_error
borlanic 0:fbdae7e6d805 40 from tools.test_exporters import ReportExporter, ResultExporterType
borlanic 0:fbdae7e6d805 41 from tools.utils import argparse_filestring_type, argparse_lowercase_type, argparse_many
borlanic 0:fbdae7e6d805 42 from tools.utils import argparse_dir_not_parent
borlanic 0:fbdae7e6d805 43 from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS, TOOLCHAIN_CLASSES
borlanic 0:fbdae7e6d805 44 from tools.settings import CLI_COLOR_MAP
borlanic 0:fbdae7e6d805 45
borlanic 0:fbdae7e6d805 46 if __name__ == '__main__':
borlanic 0:fbdae7e6d805 47 try:
borlanic 0:fbdae7e6d805 48 # Parse Options
borlanic 0:fbdae7e6d805 49 parser = get_default_options_parser(add_app_config=True)
borlanic 0:fbdae7e6d805 50
borlanic 0:fbdae7e6d805 51 parser.add_argument("-D",
borlanic 0:fbdae7e6d805 52 action="append",
borlanic 0:fbdae7e6d805 53 dest="macros",
borlanic 0:fbdae7e6d805 54 help="Add a macro definition")
borlanic 0:fbdae7e6d805 55
borlanic 0:fbdae7e6d805 56 parser.add_argument("-j", "--jobs",
borlanic 0:fbdae7e6d805 57 type=int,
borlanic 0:fbdae7e6d805 58 dest="jobs",
borlanic 0:fbdae7e6d805 59 default=0,
borlanic 0:fbdae7e6d805 60 help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
borlanic 0:fbdae7e6d805 61
borlanic 0:fbdae7e6d805 62 parser.add_argument("--source", dest="source_dir",
borlanic 0:fbdae7e6d805 63 type=argparse_filestring_type,
borlanic 0:fbdae7e6d805 64 default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append")
borlanic 0:fbdae7e6d805 65
borlanic 0:fbdae7e6d805 66 parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT),
borlanic 0:fbdae7e6d805 67 default=None, help="The build (output) directory")
borlanic 0:fbdae7e6d805 68
borlanic 0:fbdae7e6d805 69 parser.add_argument("-l", "--list", action="store_true", dest="list",
borlanic 0:fbdae7e6d805 70 default=False, help="List (recursively) available tests in order and exit")
borlanic 0:fbdae7e6d805 71
borlanic 0:fbdae7e6d805 72 parser.add_argument("-p", "--paths", dest="paths",
borlanic 0:fbdae7e6d805 73 type=argparse_many(argparse_filestring_type),
borlanic 0:fbdae7e6d805 74 default=None, help="Limit the tests to those within the specified comma separated list of paths")
borlanic 0:fbdae7e6d805 75
borlanic 0:fbdae7e6d805 76 format_choices = ["list", "json"]
borlanic 0:fbdae7e6d805 77 format_default_choice = "list"
borlanic 0:fbdae7e6d805 78 format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice)
borlanic 0:fbdae7e6d805 79 parser.add_argument("-f", "--format", dest="format",
borlanic 0:fbdae7e6d805 80 type=argparse_lowercase_type(format_choices, "format"),
borlanic 0:fbdae7e6d805 81 default=format_default_choice, help=format_help)
borlanic 0:fbdae7e6d805 82
borlanic 0:fbdae7e6d805 83 parser.add_argument("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail",
borlanic 0:fbdae7e6d805 84 default=None, help="Continue trying to build all tests if a build failure occurs")
borlanic 0:fbdae7e6d805 85
borlanic 0:fbdae7e6d805 86 #TODO validate the names instead of just passing through str
borlanic 0:fbdae7e6d805 87 parser.add_argument("-n", "--names", dest="names", type=argparse_many(str),
borlanic 0:fbdae7e6d805 88 default=None, help="Limit the tests to a comma separated list of names")
borlanic 0:fbdae7e6d805 89
borlanic 0:fbdae7e6d805 90 parser.add_argument("--test-config", dest="test_config", type=str,
borlanic 0:fbdae7e6d805 91 default=None, help="Test config for a module")
borlanic 0:fbdae7e6d805 92
borlanic 0:fbdae7e6d805 93 parser.add_argument("--test-spec", dest="test_spec",
borlanic 0:fbdae7e6d805 94 default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool")
borlanic 0:fbdae7e6d805 95
borlanic 0:fbdae7e6d805 96 parser.add_argument("--build-report-junit", dest="build_report_junit",
borlanic 0:fbdae7e6d805 97 default=None, help="Destination path for a build report in the JUnit xml format")
borlanic 0:fbdae7e6d805 98 parser.add_argument("--build-data",
borlanic 0:fbdae7e6d805 99 dest="build_data",
borlanic 0:fbdae7e6d805 100 default=None,
borlanic 0:fbdae7e6d805 101 help="Dump build_data to this file")
borlanic 0:fbdae7e6d805 102
borlanic 0:fbdae7e6d805 103 parser.add_argument("-v", "--verbose",
borlanic 0:fbdae7e6d805 104 action="store_true",
borlanic 0:fbdae7e6d805 105 dest="verbose",
borlanic 0:fbdae7e6d805 106 default=False,
borlanic 0:fbdae7e6d805 107 help="Verbose diagnostic output")
borlanic 0:fbdae7e6d805 108
borlanic 0:fbdae7e6d805 109 parser.add_argument("--stats-depth",
borlanic 0:fbdae7e6d805 110 type=int,
borlanic 0:fbdae7e6d805 111 dest="stats_depth",
borlanic 0:fbdae7e6d805 112 default=2,
borlanic 0:fbdae7e6d805 113 help="Depth level for static memory report")
borlanic 0:fbdae7e6d805 114
borlanic 0:fbdae7e6d805 115 options = parser.parse_args()
borlanic 0:fbdae7e6d805 116
borlanic 0:fbdae7e6d805 117 # Filter tests by path if specified
borlanic 0:fbdae7e6d805 118 if options.paths:
borlanic 0:fbdae7e6d805 119 all_paths = options.paths
borlanic 0:fbdae7e6d805 120 else:
borlanic 0:fbdae7e6d805 121 all_paths = ["."]
borlanic 0:fbdae7e6d805 122
borlanic 0:fbdae7e6d805 123 all_tests = {}
borlanic 0:fbdae7e6d805 124 tests = {}
borlanic 0:fbdae7e6d805 125
borlanic 0:fbdae7e6d805 126 # Target
borlanic 0:fbdae7e6d805 127 if options.mcu is None :
borlanic 0:fbdae7e6d805 128 args_error(parser, "argument -m/--mcu is required")
borlanic 0:fbdae7e6d805 129 mcu = extract_mcus(parser, options)[0]
borlanic 0:fbdae7e6d805 130
borlanic 0:fbdae7e6d805 131 # Toolchain
borlanic 0:fbdae7e6d805 132 if options.tool is None:
borlanic 0:fbdae7e6d805 133 args_error(parser, "argument -t/--tool is required")
borlanic 0:fbdae7e6d805 134 toolchain = options.tool[0]
borlanic 0:fbdae7e6d805 135
borlanic 0:fbdae7e6d805 136 if not TOOLCHAIN_CLASSES[toolchain].check_executable():
borlanic 0:fbdae7e6d805 137 search_path = TOOLCHAIN_PATHS[toolchain] or "No path set"
borlanic 0:fbdae7e6d805 138 args_error(parser, "Could not find executable for %s.\n"
borlanic 0:fbdae7e6d805 139 "Currently set search path: %s"
borlanic 0:fbdae7e6d805 140 % (toolchain, search_path))
borlanic 0:fbdae7e6d805 141
borlanic 0:fbdae7e6d805 142 # Assign config file. Precedence: test_config>app_config
borlanic 0:fbdae7e6d805 143 # TODO: merge configs if both given
borlanic 0:fbdae7e6d805 144 if options.test_config:
borlanic 0:fbdae7e6d805 145 config = get_test_config(options.test_config, mcu)
borlanic 0:fbdae7e6d805 146 if not config:
borlanic 0:fbdae7e6d805 147 args_error(parser, "argument --test-config contains invalid path or identifier")
borlanic 0:fbdae7e6d805 148 elif options.app_config:
borlanic 0:fbdae7e6d805 149 config = options.app_config
borlanic 0:fbdae7e6d805 150 else:
borlanic 0:fbdae7e6d805 151 config = Config.find_app_config(options.source_dir)
borlanic 0:fbdae7e6d805 152
borlanic 0:fbdae7e6d805 153 if not config:
borlanic 0:fbdae7e6d805 154 config = get_default_config(options.source_dir or ['.'], mcu)
borlanic 0:fbdae7e6d805 155
borlanic 0:fbdae7e6d805 156 # Find all tests in the relevant paths
borlanic 0:fbdae7e6d805 157 for path in all_paths:
borlanic 0:fbdae7e6d805 158 all_tests.update(find_tests(path, mcu, toolchain,
borlanic 0:fbdae7e6d805 159 app_config=config))
borlanic 0:fbdae7e6d805 160
borlanic 0:fbdae7e6d805 161 # Filter tests by name if specified
borlanic 0:fbdae7e6d805 162 if options.names:
borlanic 0:fbdae7e6d805 163 all_names = options.names
borlanic 0:fbdae7e6d805 164 all_names = [x.lower() for x in all_names]
borlanic 0:fbdae7e6d805 165
borlanic 0:fbdae7e6d805 166 for name in all_names:
borlanic 0:fbdae7e6d805 167 if any(fnmatch.fnmatch(testname, name) for testname in all_tests):
borlanic 0:fbdae7e6d805 168 for testname, test in all_tests.items():
borlanic 0:fbdae7e6d805 169 if fnmatch.fnmatch(testname, name):
borlanic 0:fbdae7e6d805 170 tests[testname] = test
borlanic 0:fbdae7e6d805 171 else:
borlanic 0:fbdae7e6d805 172 print("[Warning] Test with name '%s' was not found in the "
borlanic 0:fbdae7e6d805 173 "available tests" % (name))
borlanic 0:fbdae7e6d805 174 else:
borlanic 0:fbdae7e6d805 175 tests = all_tests
borlanic 0:fbdae7e6d805 176
borlanic 0:fbdae7e6d805 177
borlanic 0:fbdae7e6d805 178 if options.list:
borlanic 0:fbdae7e6d805 179 # Print available tests in order and exit
borlanic 0:fbdae7e6d805 180 print_tests(tests, options.format)
borlanic 0:fbdae7e6d805 181 sys.exit(0)
borlanic 0:fbdae7e6d805 182 else:
borlanic 0:fbdae7e6d805 183 # Build all tests
borlanic 0:fbdae7e6d805 184 if not options.build_dir:
borlanic 0:fbdae7e6d805 185 args_error(parser, "argument --build is required")
borlanic 0:fbdae7e6d805 186
borlanic 0:fbdae7e6d805 187 base_source_paths = options.source_dir
borlanic 0:fbdae7e6d805 188
borlanic 0:fbdae7e6d805 189 # Default base source path is the current directory
borlanic 0:fbdae7e6d805 190 if not base_source_paths:
borlanic 0:fbdae7e6d805 191 base_source_paths = ['.']
borlanic 0:fbdae7e6d805 192
borlanic 0:fbdae7e6d805 193 build_report = {}
borlanic 0:fbdae7e6d805 194 build_properties = {}
borlanic 0:fbdae7e6d805 195
borlanic 0:fbdae7e6d805 196 library_build_success = False
borlanic 0:fbdae7e6d805 197 profile = extract_profile(parser, options, toolchain)
borlanic 0:fbdae7e6d805 198 try:
borlanic 0:fbdae7e6d805 199 # Build sources
borlanic 0:fbdae7e6d805 200 notify = TerminalNotifier(options.verbose)
borlanic 0:fbdae7e6d805 201 build_library(base_source_paths, options.build_dir, mcu,
borlanic 0:fbdae7e6d805 202 toolchain, jobs=options.jobs,
borlanic 0:fbdae7e6d805 203 clean=options.clean, report=build_report,
borlanic 0:fbdae7e6d805 204 properties=build_properties, name="mbed-build",
borlanic 0:fbdae7e6d805 205 macros=options.macros,
borlanic 0:fbdae7e6d805 206 notify=notify, archive=False,
borlanic 0:fbdae7e6d805 207 app_config=config,
borlanic 0:fbdae7e6d805 208 build_profile=profile)
borlanic 0:fbdae7e6d805 209
borlanic 0:fbdae7e6d805 210 library_build_success = True
borlanic 0:fbdae7e6d805 211 except ToolException as e:
borlanic 0:fbdae7e6d805 212 # ToolException output is handled by the build log
borlanic 0:fbdae7e6d805 213 pass
borlanic 0:fbdae7e6d805 214 except NotSupportedException as e:
borlanic 0:fbdae7e6d805 215 # NotSupportedException is handled by the build log
borlanic 0:fbdae7e6d805 216 pass
borlanic 0:fbdae7e6d805 217 except Exception as e:
borlanic 0:fbdae7e6d805 218 # Some other exception occurred, print the error message
borlanic 0:fbdae7e6d805 219 print(e)
borlanic 0:fbdae7e6d805 220
borlanic 0:fbdae7e6d805 221 if not library_build_success:
borlanic 0:fbdae7e6d805 222 print("Failed to build library")
borlanic 0:fbdae7e6d805 223 else:
borlanic 0:fbdae7e6d805 224 # Build all the tests
borlanic 0:fbdae7e6d805 225 notify = TerminalNotifier(options.verbose)
borlanic 0:fbdae7e6d805 226 test_build_success, test_build = build_tests(tests, [options.build_dir], options.build_dir, mcu, toolchain,
borlanic 0:fbdae7e6d805 227 clean=options.clean,
borlanic 0:fbdae7e6d805 228 report=build_report,
borlanic 0:fbdae7e6d805 229 properties=build_properties,
borlanic 0:fbdae7e6d805 230 macros=options.macros,
borlanic 0:fbdae7e6d805 231 notify=notify,
borlanic 0:fbdae7e6d805 232 jobs=options.jobs,
borlanic 0:fbdae7e6d805 233 continue_on_build_fail=options.continue_on_build_fail,
borlanic 0:fbdae7e6d805 234 app_config=config,
borlanic 0:fbdae7e6d805 235 build_profile=profile,
borlanic 0:fbdae7e6d805 236 stats_depth=options.stats_depth)
borlanic 0:fbdae7e6d805 237
borlanic 0:fbdae7e6d805 238 # If a path to a test spec is provided, write it to a file
borlanic 0:fbdae7e6d805 239 if options.test_spec:
borlanic 0:fbdae7e6d805 240 test_spec_data = test_spec_from_test_builds(test_build)
borlanic 0:fbdae7e6d805 241
borlanic 0:fbdae7e6d805 242 # Create the target dir for the test spec if necessary
borlanic 0:fbdae7e6d805 243 # mkdir will not create the dir if it already exists
borlanic 0:fbdae7e6d805 244 test_spec_dir = os.path.dirname(options.test_spec)
borlanic 0:fbdae7e6d805 245 if test_spec_dir:
borlanic 0:fbdae7e6d805 246 mkdir(test_spec_dir)
borlanic 0:fbdae7e6d805 247
borlanic 0:fbdae7e6d805 248 try:
borlanic 0:fbdae7e6d805 249 with open(options.test_spec, 'w') as f:
borlanic 0:fbdae7e6d805 250 f.write(json.dumps(test_spec_data, indent=2))
borlanic 0:fbdae7e6d805 251 except IOError as e:
borlanic 0:fbdae7e6d805 252 print("[ERROR] Error writing test spec to file")
borlanic 0:fbdae7e6d805 253 print(e)
borlanic 0:fbdae7e6d805 254
borlanic 0:fbdae7e6d805 255 # If a path to a JUnit build report spec is provided, write it to a file
borlanic 0:fbdae7e6d805 256 if options.build_report_junit:
borlanic 0:fbdae7e6d805 257 report_exporter = ReportExporter(ResultExporterType.JUNIT, package="build")
borlanic 0:fbdae7e6d805 258 report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties)
borlanic 0:fbdae7e6d805 259
borlanic 0:fbdae7e6d805 260 # Print memory map summary on screen
borlanic 0:fbdae7e6d805 261 if build_report:
borlanic 0:fbdae7e6d805 262 print
borlanic 0:fbdae7e6d805 263 print(print_build_memory_usage(build_report))
borlanic 0:fbdae7e6d805 264
borlanic 0:fbdae7e6d805 265 print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build")
borlanic 0:fbdae7e6d805 266 status = print_report_exporter.report(build_report)
borlanic 0:fbdae7e6d805 267 if options.build_data:
borlanic 0:fbdae7e6d805 268 merge_build_data(options.build_data, build_report, "test")
borlanic 0:fbdae7e6d805 269
borlanic 0:fbdae7e6d805 270 if status:
borlanic 0:fbdae7e6d805 271 sys.exit(0)
borlanic 0:fbdae7e6d805 272 else:
borlanic 0:fbdae7e6d805 273 sys.exit(1)
borlanic 0:fbdae7e6d805 274
borlanic 0:fbdae7e6d805 275 except KeyboardInterrupt as e:
borlanic 0:fbdae7e6d805 276 print("\n[CTRL+c] exit")
borlanic 0:fbdae7e6d805 277 except ConfigException as e:
borlanic 0:fbdae7e6d805 278 # Catching ConfigException here to prevent a traceback
borlanic 0:fbdae7e6d805 279 print("[ERROR] %s" % str(e))
borlanic 0:fbdae7e6d805 280 except Exception as e:
borlanic 0:fbdae7e6d805 281 import traceback
borlanic 0:fbdae7e6d805 282 traceback.print_exc(file=sys.stdout)
borlanic 0:fbdae7e6d805 283 print("[ERROR] %s" % str(e))
borlanic 0:fbdae7e6d805 284 sys.exit(1)