Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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