Nicolas Borla / Mbed OS BBR_1Ebene
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers build.py Source File

build.py

00001 #! /usr/bin/env python2
00002 """
00003 mbed SDK
00004 Copyright (c) 2011-2013 ARM Limited
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may not use this file except in compliance with the License.
00008 You may obtain a copy of the License at
00009 
00010     http://www.apache.org/licenses/LICENSE-2.0
00011 
00012 Unless required by applicable law or agreed to in writing, software
00013 distributed under the License is distributed on an "AS IS" BASIS,
00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 See the License for the specific language governing permissions and
00016 limitations under the License.
00017 
00018 LIBRARIES BUILD
00019 """
00020 from __future__ import print_function, division, absolute_import
00021 
00022 import sys
00023 from time import time
00024 from os.path import join, abspath, dirname
00025 
00026 
00027 # Be sure that the tools directory is in the search path
00028 ROOT = abspath(join(dirname(__file__), ".."))
00029 sys.path.insert(0, ROOT)
00030 
00031 
00032 from tools.toolchains import TOOLCHAINS, TOOLCHAIN_CLASSES, TOOLCHAIN_PATHS
00033 from tools.toolchains import mbedToolchain
00034 from tools.targets import TARGET_NAMES, TARGET_MAP
00035 from tools.options import get_default_options_parser
00036 from tools.options import extract_profile
00037 from tools.options import extract_mcus
00038 from tools.build_api import build_library, build_mbed_libs, build_lib
00039 from tools.build_api import mcu_toolchain_matrix
00040 from tools.build_api import print_build_results
00041 from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT
00042 from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT, CLI_COLOR_MAP
00043 from tools.notifier.term import TerminalNotifier
00044 from tools.utils import argparse_filestring_type, args_error
00045 from tools.utils import argparse_filestring_type, argparse_dir_not_parent
00046 
00047 if __name__ == '__main__':
00048     start = time()
00049 
00050     # Parse Options
00051     parser = get_default_options_parser()
00052 
00053     parser.add_argument("--source", dest="source_dir", type=argparse_filestring_type,
00054                         default=None, help="The source (input) directory", action="append")
00055 
00056     parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT),
00057                       default=None, help="The build (output) directory")
00058 
00059     parser.add_argument("--no-archive", dest="no_archive", action="store_true",
00060                       default=False, help="Do not produce archive (.ar) file, but rather .o")
00061 
00062     # Extra libraries
00063     parser.add_argument("-r", "--rtos",
00064                       action="store_true",
00065                       dest="rtos",
00066                       default=False,
00067                       help="Compile the rtos")
00068 
00069     parser.add_argument("--rpc",
00070                       action="store_true",
00071                       dest="rpc",
00072                       default=False,
00073                       help="Compile the rpc library")
00074 
00075     parser.add_argument("-u", "--usb",
00076                       action="store_true",
00077                       dest="usb",
00078                       default=False,
00079                       help="Compile the USB Device library")
00080 
00081     parser.add_argument("-d", "--dsp",
00082                       action="store_true",
00083                       dest="dsp",
00084                       default=False,
00085                       help="Compile the DSP library")
00086 
00087     parser.add_argument( "--cpputest",
00088                       action="store_true",
00089                       dest="cpputest_lib",
00090                       default=False,
00091                       help="Compiles 'cpputest' unit test library (library should be on the same directory level as mbed repository)")
00092 
00093     parser.add_argument("-D",
00094                       action="append",
00095                       dest="macros",
00096                       help="Add a macro definition")
00097 
00098     parser.add_argument("-S", "--supported-toolchains",
00099                       action="store_true",
00100                       dest="supported_toolchains",
00101                       default=False,
00102                       help="Displays supported matrix of MCUs and toolchains")
00103 
00104     parser.add_argument('-f', '--filter',
00105                       dest='general_filter_regex',
00106                       default=None,
00107                       help='For some commands you can use filter to filter out results')
00108 
00109     parser.add_argument("-j", "--jobs", type=int, dest="jobs",
00110                       default=0, help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
00111     parser.add_argument("-N", "--artifact-name", dest="artifact_name",
00112                       default=None, help="The built project's name")
00113 
00114     parser.add_argument("-v", "--verbose",
00115                       action="store_true",
00116                       dest="verbose",
00117                       default=False,
00118                       help="Verbose diagnostic output")
00119 
00120     parser.add_argument("--silent",
00121                       action="store_true",
00122                       dest="silent",
00123                       default=False,
00124                       help="Silent diagnostic output (no copy, compile notification)")
00125 
00126     parser.add_argument("-x", "--extra-verbose-notifications",
00127                       action="store_true",
00128                       dest="extra_verbose_notify",
00129                       default=False,
00130                       help="Makes compiler more verbose, CI friendly.")
00131 
00132     options = parser.parse_args()
00133 
00134     # Only prints matrix of supported toolchains
00135     if options.supported_toolchains:
00136         print(mcu_toolchain_matrix(platform_filter=options.general_filter_regex))
00137         exit(0)
00138 
00139 
00140     # Get target list
00141     targets = extract_mcus(parser, options) if options.mcu else TARGET_NAMES
00142 
00143     # Get toolchains list
00144     toolchains = options.tool if options.tool else TOOLCHAINS
00145 
00146     if options.source_dir and not options.build_dir:
00147         args_error(parser, "argument --build is required by argument --source")
00148 
00149 
00150     # Get libraries list
00151     libraries = []
00152 
00153     # Additional Libraries
00154     if options.rpc:
00155         libraries.extend(["rpc"])
00156     if options.usb:
00157         libraries.append("usb")
00158     if options.dsp:
00159         libraries.extend(["dsp"])
00160     if options.cpputest_lib:
00161         libraries.extend(["cpputest"])
00162 
00163     # Build results
00164     failures = []
00165     successes = []
00166     skipped = []
00167 
00168     for toolchain in toolchains:
00169         if not TOOLCHAIN_CLASSES[toolchain].check_executable():
00170             search_path = TOOLCHAIN_PATHS[toolchain] or "No path set"
00171             args_error(parser, "Could not find executable for %s.\n"
00172                                "Currently set search path: %s"
00173                        % (toolchain, search_path))
00174 
00175     for toolchain in toolchains:
00176         for target in targets:
00177             tt_id = "%s::%s" % (toolchain, target)
00178             if toolchain not in TARGET_MAP[target].supported_toolchains:
00179                 # Log this later
00180                 print("%s skipped: toolchain not supported" % tt_id)
00181                 skipped.append(tt_id)
00182             else:
00183                 try:
00184                     notify = TerminalNotifer(options.verbose, options.silent)
00185                     mcu = TARGET_MAP[target]
00186                     profile = extract_profile(parser, options, toolchain)
00187                     if options.source_dir:
00188                         lib_build_res = build_library(options.source_dir, options.build_dir, mcu, toolchain,
00189                                                     extra_verbose=options.extra_verbose_notify,
00190                                                     verbose=options.verbose,
00191                                                     silent=options.silent,
00192                                                     jobs=options.jobs,
00193                                                     clean=options.clean,
00194                                                     archive=(not options.no_archive),
00195                                                     macros=options.macros,
00196                                                     name=options.artifact_name,
00197                                                     build_profile=profile)
00198                     else:
00199                         lib_build_res = build_mbed_libs(mcu, toolchain,
00200                                                     extra_verbose=options.extra_verbose_notify,
00201                                                     verbose=options.verbose,
00202                                                     silent=options.silent,
00203                                                     jobs=options.jobs,
00204                                                     clean=options.clean,
00205                                                         macros=options.macros,
00206                                                         build_profile=profile)
00207 
00208                     for lib_id in libraries:
00209                         build_lib(lib_id, mcu, toolchain,
00210                                 extra_verbose=options.extra_verbose_notify,
00211                                 verbose=options.verbose,
00212                                 silent=options.silent,
00213                                 clean=options.clean,
00214                                 macros=options.macros,
00215                                     jobs=options.jobs,
00216                                     build_profile=profile)
00217                     if lib_build_res:
00218                         successes.append(tt_id)
00219                     else:
00220                         skipped.append(tt_id)
00221                 except Exception as e:
00222                     if options.verbose:
00223                         import traceback
00224                         traceback.print_exc(file=sys.stdout)
00225                         sys.exit(1)
00226                     failures.append(tt_id)
00227                     print(e)
00228 
00229 
00230     # Write summary of the builds
00231     print("\nCompleted in: (%.2f)s\n" % (time() - start))
00232 
00233     for report, report_name in [(successes, "Build successes:"),
00234                                 (skipped, "Build skipped:"),
00235                                 (failures, "Build failures:"),
00236                                ]:
00237         if report:
00238             print(print_build_results(report, report_name))
00239 
00240     if failures:
00241         sys.exit(1)