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