mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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