mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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