init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

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