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