nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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