BA / Mbed OS BaBoRo1
Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 #! /usr/bin/env python
borlanic 0:380207fcb5c1 2 """
borlanic 0:380207fcb5c1 3 mbed SDK
borlanic 0:380207fcb5c1 4 Copyright (c) 2011-2013 ARM Limited
borlanic 0:380207fcb5c1 5
borlanic 0:380207fcb5c1 6 Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:380207fcb5c1 7 you may not use this file except in compliance with the License.
borlanic 0:380207fcb5c1 8 You may obtain a copy of the License at
borlanic 0:380207fcb5c1 9
borlanic 0:380207fcb5c1 10 http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:380207fcb5c1 11
borlanic 0:380207fcb5c1 12 Unless required by applicable law or agreed to in writing, software
borlanic 0:380207fcb5c1 13 distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:380207fcb5c1 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:380207fcb5c1 15 See the License for the specific language governing permissions and
borlanic 0:380207fcb5c1 16 limitations under the License.
borlanic 0:380207fcb5c1 17 """
borlanic 0:380207fcb5c1 18 import sys
borlanic 0:380207fcb5c1 19 from time import time
borlanic 0:380207fcb5c1 20 from os.path import join, abspath, dirname, normpath
borlanic 0:380207fcb5c1 21 from optparse import OptionParser
borlanic 0:380207fcb5c1 22 import json
borlanic 0:380207fcb5c1 23
borlanic 0:380207fcb5c1 24 # Be sure that the tools directory is in the search path
borlanic 0:380207fcb5c1 25 ROOT = abspath(join(dirname(__file__), ".."))
borlanic 0:380207fcb5c1 26 sys.path.insert(0, ROOT)
borlanic 0:380207fcb5c1 27
borlanic 0:380207fcb5c1 28 from tools.build_api import build_library
borlanic 0:380207fcb5c1 29 from tools.build_api import write_build_report
borlanic 0:380207fcb5c1 30 from tools.targets import TARGET_MAP, TARGET_NAMES
borlanic 0:380207fcb5c1 31 from tools.toolchains import TOOLCHAINS
borlanic 0:380207fcb5c1 32 from tools.test_exporters import ReportExporter, ResultExporterType
borlanic 0:380207fcb5c1 33 from tools.test_api import find_tests, build_tests, test_spec_from_test_builds
borlanic 0:380207fcb5c1 34 from tools.build_release import OFFICIAL_MBED_LIBRARY_BUILD
borlanic 0:380207fcb5c1 35
borlanic 0:380207fcb5c1 36 if __name__ == '__main__':
borlanic 0:380207fcb5c1 37 try:
borlanic 0:380207fcb5c1 38 parser = OptionParser()
borlanic 0:380207fcb5c1 39
borlanic 0:380207fcb5c1 40 parser.add_option("--source", dest="source_dir",
borlanic 0:380207fcb5c1 41 default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append")
borlanic 0:380207fcb5c1 42
borlanic 0:380207fcb5c1 43 parser.add_option("--build", dest="build_dir",
borlanic 0:380207fcb5c1 44 default=None, help="The build (output) directory")
borlanic 0:380207fcb5c1 45
borlanic 0:380207fcb5c1 46 parser.add_option('-c', '--clean',
borlanic 0:380207fcb5c1 47 dest='clean',
borlanic 0:380207fcb5c1 48 metavar=False,
borlanic 0:380207fcb5c1 49 action="store_true",
borlanic 0:380207fcb5c1 50 help='Clean the build directory')
borlanic 0:380207fcb5c1 51
borlanic 0:380207fcb5c1 52 parser.add_option('-a', '--all', dest="all", default=False, action="store_true",
borlanic 0:380207fcb5c1 53 help="Build every target (including unofficial targets) and with each of the supported toolchains")
borlanic 0:380207fcb5c1 54
borlanic 0:380207fcb5c1 55 parser.add_option('-o', '--official', dest="official_only", default=False, action="store_true",
borlanic 0:380207fcb5c1 56 help="Build using only the official toolchain for each target")
borlanic 0:380207fcb5c1 57
borlanic 0:380207fcb5c1 58 parser.add_option("-D", "",
borlanic 0:380207fcb5c1 59 action="append",
borlanic 0:380207fcb5c1 60 dest="macros",
borlanic 0:380207fcb5c1 61 help="Add a macro definition")
borlanic 0:380207fcb5c1 62
borlanic 0:380207fcb5c1 63 parser.add_option("-j", "--jobs", type="int", dest="jobs",
borlanic 0:380207fcb5c1 64 default=0, help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
borlanic 0:380207fcb5c1 65
borlanic 0:380207fcb5c1 66 parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
borlanic 0:380207fcb5c1 67 default=False, help="Verbose diagnostic output")
borlanic 0:380207fcb5c1 68
borlanic 0:380207fcb5c1 69 parser.add_option("-t", "--toolchains", dest="toolchains", help="Use toolchains names separated by comma")
borlanic 0:380207fcb5c1 70
borlanic 0:380207fcb5c1 71 parser.add_option("-p", "--platforms", dest="platforms", default="", help="Build only for the platform namesseparated by comma")
borlanic 0:380207fcb5c1 72
borlanic 0:380207fcb5c1 73 parser.add_option("", "--config", action="store_true", dest="list_config",
borlanic 0:380207fcb5c1 74 default=False, help="List the platforms and toolchains in the release in JSON")
borlanic 0:380207fcb5c1 75
borlanic 0:380207fcb5c1 76 parser.add_option("", "--test-spec", dest="test_spec",
borlanic 0:380207fcb5c1 77 default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool")
borlanic 0:380207fcb5c1 78
borlanic 0:380207fcb5c1 79 parser.add_option("", "--build-report-junit", dest="build_report_junit", help="Output the build results to an junit xml file")
borlanic 0:380207fcb5c1 80
borlanic 0:380207fcb5c1 81 parser.add_option("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail",
borlanic 0:380207fcb5c1 82 default=False, help="Continue trying to build all tests if a build failure occurs")
borlanic 0:380207fcb5c1 83
borlanic 0:380207fcb5c1 84 options, args = parser.parse_args()
borlanic 0:380207fcb5c1 85
borlanic 0:380207fcb5c1 86 # Get set of valid targets
borlanic 0:380207fcb5c1 87 all_platforms = set(TARGET_NAMES)
borlanic 0:380207fcb5c1 88 bad_platforms = set()
borlanic 0:380207fcb5c1 89 platforms = set()
borlanic 0:380207fcb5c1 90 if options.platforms != "":
borlanic 0:380207fcb5c1 91 platforms = set(options.platforms.split(","))
borlanic 0:380207fcb5c1 92 bad_platforms = platforms.difference(all_platforms)
borlanic 0:380207fcb5c1 93 platforms = platforms.intersection(all_platforms)
borlanic 0:380207fcb5c1 94 elif options.all:
borlanic 0:380207fcb5c1 95 platforms = all_platforms
borlanic 0:380207fcb5c1 96 else:
borlanic 0:380207fcb5c1 97 platforms = set(x[0] for x in OFFICIAL_MBED_LIBRARY_BUILD)
borlanic 0:380207fcb5c1 98 bad_platforms = platforms.difference(all_platforms)
borlanic 0:380207fcb5c1 99 platforms = platforms.intersection(all_platforms)
borlanic 0:380207fcb5c1 100
borlanic 0:380207fcb5c1 101 for bad_platform in bad_platforms:
borlanic 0:380207fcb5c1 102 print "Platform '%s' is not a valid platform. Skipping." % bad_platform
borlanic 0:380207fcb5c1 103
borlanic 0:380207fcb5c1 104 if options.platforms:
borlanic 0:380207fcb5c1 105 print "Limiting build to the following platforms: %s" % ",".join(platforms)
borlanic 0:380207fcb5c1 106
borlanic 0:380207fcb5c1 107 # Get set of valid toolchains
borlanic 0:380207fcb5c1 108 all_toolchains = set(TOOLCHAINS)
borlanic 0:380207fcb5c1 109 bad_toolchains = set()
borlanic 0:380207fcb5c1 110 toolchains = set()
borlanic 0:380207fcb5c1 111
borlanic 0:380207fcb5c1 112 if options.toolchains:
borlanic 0:380207fcb5c1 113 toolchains = set(options.toolchains.split(","))
borlanic 0:380207fcb5c1 114 bad_toolchains = toolchains.difference(all_toolchains)
borlanic 0:380207fcb5c1 115 toolchains = toolchains.intersection(all_toolchains)
borlanic 0:380207fcb5c1 116 else:
borlanic 0:380207fcb5c1 117 toolchains = all_toolchains
borlanic 0:380207fcb5c1 118
borlanic 0:380207fcb5c1 119 for bad_toolchain in bad_toolchains:
borlanic 0:380207fcb5c1 120 print "Toolchain '%s' is not a valid toolchain. Skipping." % bad_toolchain
borlanic 0:380207fcb5c1 121
borlanic 0:380207fcb5c1 122 if options.toolchains:
borlanic 0:380207fcb5c1 123 print "Limiting build to the following toolchains: %s" % ",".join(toolchains)
borlanic 0:380207fcb5c1 124
borlanic 0:380207fcb5c1 125 build_config = {}
borlanic 0:380207fcb5c1 126
borlanic 0:380207fcb5c1 127 for platform in platforms:
borlanic 0:380207fcb5c1 128 target = TARGET_MAP[platform]
borlanic 0:380207fcb5c1 129
borlanic 0:380207fcb5c1 130 if options.official_only:
borlanic 0:380207fcb5c1 131 default_toolchain = getattr(target, 'default_toolchain', 'ARM')
borlanic 0:380207fcb5c1 132 build_config[platform] = list(toolchains.intersection(set([default_toolchain])))
borlanic 0:380207fcb5c1 133 else:
borlanic 0:380207fcb5c1 134 build_config[platform] = list(toolchains.intersection(set(target.supported_toolchains)))
borlanic 0:380207fcb5c1 135
borlanic 0:380207fcb5c1 136 if options.list_config:
borlanic 0:380207fcb5c1 137 print json.dumps(build_config, indent=4)
borlanic 0:380207fcb5c1 138 sys.exit(0)
borlanic 0:380207fcb5c1 139
borlanic 0:380207fcb5c1 140 # Ensure build directory is set
borlanic 0:380207fcb5c1 141 if not options.build_dir:
borlanic 0:380207fcb5c1 142 print "[ERROR] You must specify a build path"
borlanic 0:380207fcb5c1 143 sys.exit(1)
borlanic 0:380207fcb5c1 144
borlanic 0:380207fcb5c1 145 # Default base source path is the current directory
borlanic 0:380207fcb5c1 146 base_source_paths = options.source_dir
borlanic 0:380207fcb5c1 147 if not base_source_paths:
borlanic 0:380207fcb5c1 148 base_source_paths = ['.']
borlanic 0:380207fcb5c1 149
borlanic 0:380207fcb5c1 150
borlanic 0:380207fcb5c1 151 start = time()
borlanic 0:380207fcb5c1 152 build_report = {}
borlanic 0:380207fcb5c1 153 build_properties = {}
borlanic 0:380207fcb5c1 154 test_builds = {}
borlanic 0:380207fcb5c1 155 total_build_success = True
borlanic 0:380207fcb5c1 156
borlanic 0:380207fcb5c1 157 for target_name, target_toolchains in build_config.items():
borlanic 0:380207fcb5c1 158 target = TARGET_MAP[target_name]
borlanic 0:380207fcb5c1 159
borlanic 0:380207fcb5c1 160 for target_toolchain in target_toolchains:
borlanic 0:380207fcb5c1 161 library_build_success = True
borlanic 0:380207fcb5c1 162
borlanic 0:380207fcb5c1 163 try:
borlanic 0:380207fcb5c1 164 build_directory = join(options.build_dir, target_name, target_toolchain)
borlanic 0:380207fcb5c1 165 # Build sources
borlanic 0:380207fcb5c1 166 build_library(base_source_paths, build_directory, target, target_toolchain,
borlanic 0:380207fcb5c1 167 jobs=options.jobs,
borlanic 0:380207fcb5c1 168 clean=options.clean,
borlanic 0:380207fcb5c1 169 report=build_report,
borlanic 0:380207fcb5c1 170 properties=build_properties,
borlanic 0:380207fcb5c1 171 name="mbed-os",
borlanic 0:380207fcb5c1 172 macros=options.macros,
borlanic 0:380207fcb5c1 173 verbose=options.verbose,
borlanic 0:380207fcb5c1 174 archive=False)
borlanic 0:380207fcb5c1 175 except Exception, e:
borlanic 0:380207fcb5c1 176 library_build_success = False
borlanic 0:380207fcb5c1 177 print "Failed to build library"
borlanic 0:380207fcb5c1 178 print e
borlanic 0:380207fcb5c1 179
borlanic 0:380207fcb5c1 180 if options.continue_on_build_fail or library_build_success:
borlanic 0:380207fcb5c1 181 # Build all the tests
borlanic 0:380207fcb5c1 182 all_tests = find_tests(base_source_paths[0], target_name, toolchain_name)
borlanic 0:380207fcb5c1 183 test_build_success, test_build = build_tests(all_tests, [build_directory], build_directory, target, target_toolchain,
borlanic 0:380207fcb5c1 184 clean=options.clean,
borlanic 0:380207fcb5c1 185 report=build_report,
borlanic 0:380207fcb5c1 186 properties=build_properties,
borlanic 0:380207fcb5c1 187 macros=options.macros,
borlanic 0:380207fcb5c1 188 verbose=options.verbose,
borlanic 0:380207fcb5c1 189 jobs=options.jobs,
borlanic 0:380207fcb5c1 190 continue_on_build_fail=options.continue_on_build_fail)
borlanic 0:380207fcb5c1 191
borlanic 0:380207fcb5c1 192 if not test_build_success:
borlanic 0:380207fcb5c1 193 total_build_success = False
borlanic 0:380207fcb5c1 194 print "Failed to build some tests, check build log for details"
borlanic 0:380207fcb5c1 195
borlanic 0:380207fcb5c1 196 test_builds.update(test_build)
borlanic 0:380207fcb5c1 197 else:
borlanic 0:380207fcb5c1 198 total_build_success = False
borlanic 0:380207fcb5c1 199 break
borlanic 0:380207fcb5c1 200
borlanic 0:380207fcb5c1 201 # If a path to a test spec is provided, write it to a file
borlanic 0:380207fcb5c1 202 if options.test_spec:
borlanic 0:380207fcb5c1 203 test_spec_data = test_spec_from_test_builds(test_builds)
borlanic 0:380207fcb5c1 204
borlanic 0:380207fcb5c1 205 # Create the target dir for the test spec if necessary
borlanic 0:380207fcb5c1 206 # mkdir will not create the dir if it already exists
borlanic 0:380207fcb5c1 207 test_spec_dir = dirname(options.test_spec)
borlanic 0:380207fcb5c1 208 if test_spec_dir:
borlanic 0:380207fcb5c1 209 mkdir(test_spec_dir)
borlanic 0:380207fcb5c1 210
borlanic 0:380207fcb5c1 211 try:
borlanic 0:380207fcb5c1 212 with open(options.test_spec, 'w') as f:
borlanic 0:380207fcb5c1 213 f.write(json.dumps(test_spec_data, indent=2))
borlanic 0:380207fcb5c1 214 except IOError, e:
borlanic 0:380207fcb5c1 215 print "[ERROR] Error writing test spec to file"
borlanic 0:380207fcb5c1 216 print e
borlanic 0:380207fcb5c1 217
borlanic 0:380207fcb5c1 218 # If a path to a JUnit build report spec is provided, write it to a file
borlanic 0:380207fcb5c1 219 if options.build_report_junit:
borlanic 0:380207fcb5c1 220 report_exporter = ReportExporter(ResultExporterType.JUNIT)
borlanic 0:380207fcb5c1 221 report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties)
borlanic 0:380207fcb5c1 222
borlanic 0:380207fcb5c1 223 print "\n\nCompleted in: (%.2f)s" % (time() - start)
borlanic 0:380207fcb5c1 224
borlanic 0:380207fcb5c1 225 print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build")
borlanic 0:380207fcb5c1 226 status = print_report_exporter.report(build_report)
borlanic 0:380207fcb5c1 227
borlanic 0:380207fcb5c1 228 if status:
borlanic 0:380207fcb5c1 229 sys.exit(0)
borlanic 0:380207fcb5c1 230 else:
borlanic 0:380207fcb5c1 231 sys.exit(1)
borlanic 0:380207fcb5c1 232
borlanic 0:380207fcb5c1 233 except KeyboardInterrupt, e:
borlanic 0:380207fcb5c1 234 print "\n[CTRL+c] exit"
borlanic 0:380207fcb5c1 235 except Exception,e:
borlanic 0:380207fcb5c1 236 import traceback
borlanic 0:380207fcb5c1 237 traceback.print_exc(file=sys.stdout)
borlanic 0:380207fcb5c1 238 print "[ERROR] %s" % str(e)
borlanic 0:380207fcb5c1 239 sys.exit(1)