Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 """
switches 0:5c4d7b2438d3 2 mbed SDK
switches 0:5c4d7b2438d3 3 Copyright (c) 2011-2016 ARM Limited
switches 0:5c4d7b2438d3 4
switches 0:5c4d7b2438d3 5 Licensed under the Apache License, Version 2.0 (the "License");
switches 0:5c4d7b2438d3 6 you may not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 7 You may obtain a copy of the License at
switches 0:5c4d7b2438d3 8
switches 0:5c4d7b2438d3 9 http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 10
switches 0:5c4d7b2438d3 11 Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 12 distributed under the License is distributed on an "AS IS" BASIS,
switches 0:5c4d7b2438d3 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 14 See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 15 limitations under the License.
switches 0:5c4d7b2438d3 16 """
switches 0:5c4d7b2438d3 17
switches 0:5c4d7b2438d3 18 import re
switches 0:5c4d7b2438d3 19 import tempfile
switches 0:5c4d7b2438d3 20 from types import ListType
switches 0:5c4d7b2438d3 21 from shutil import rmtree
switches 0:5c4d7b2438d3 22 from os.path import join, exists, dirname, basename, abspath, normpath
switches 0:5c4d7b2438d3 23 from os import linesep, remove
switches 0:5c4d7b2438d3 24 from time import time
switches 0:5c4d7b2438d3 25
switches 0:5c4d7b2438d3 26 from tools.utils import mkdir, run_cmd, run_cmd_ext, NotSupportedException,\
switches 0:5c4d7b2438d3 27 ToolException, InvalidReleaseTargetException
switches 0:5c4d7b2438d3 28 from tools.paths import MBED_CMSIS_PATH, MBED_TARGETS_PATH, MBED_LIBRARIES,\
switches 0:5c4d7b2438d3 29 MBED_HEADER, MBED_DRIVERS, MBED_PLATFORM, MBED_HAL, MBED_CONFIG_FILE,\
switches 0:5c4d7b2438d3 30 MBED_LIBRARIES_DRIVERS, MBED_LIBRARIES_PLATFORM, MBED_LIBRARIES_HAL,\
switches 0:5c4d7b2438d3 31 BUILD_DIR
switches 0:5c4d7b2438d3 32 from tools.targets import TARGET_NAMES, TARGET_MAP
switches 0:5c4d7b2438d3 33 from tools.libraries import Library
switches 0:5c4d7b2438d3 34 from tools.toolchains import TOOLCHAIN_CLASSES
switches 0:5c4d7b2438d3 35 from jinja2 import FileSystemLoader
switches 0:5c4d7b2438d3 36 from jinja2.environment import Environment
switches 0:5c4d7b2438d3 37 from tools.config import Config
switches 0:5c4d7b2438d3 38
switches 0:5c4d7b2438d3 39 RELEASE_VERSIONS = ['2', '5']
switches 0:5c4d7b2438d3 40
switches 0:5c4d7b2438d3 41 def prep_report(report, target_name, toolchain_name, id_name):
switches 0:5c4d7b2438d3 42 """Setup report keys
switches 0:5c4d7b2438d3 43
switches 0:5c4d7b2438d3 44 Positional arguments:
switches 0:5c4d7b2438d3 45 report - the report to fill
switches 0:5c4d7b2438d3 46 target_name - the target being used
switches 0:5c4d7b2438d3 47 toolchain_name - the toolchain being used
switches 0:5c4d7b2438d3 48 id_name - the name of the executable or library being built
switches 0:5c4d7b2438d3 49 """
switches 0:5c4d7b2438d3 50 if not target_name in report:
switches 0:5c4d7b2438d3 51 report[target_name] = {}
switches 0:5c4d7b2438d3 52
switches 0:5c4d7b2438d3 53 if not toolchain_name in report[target_name]:
switches 0:5c4d7b2438d3 54 report[target_name][toolchain_name] = {}
switches 0:5c4d7b2438d3 55
switches 0:5c4d7b2438d3 56 if not id_name in report[target_name][toolchain_name]:
switches 0:5c4d7b2438d3 57 report[target_name][toolchain_name][id_name] = []
switches 0:5c4d7b2438d3 58
switches 0:5c4d7b2438d3 59 def prep_properties(properties, target_name, toolchain_name, vendor_label):
switches 0:5c4d7b2438d3 60 """Setup test properties
switches 0:5c4d7b2438d3 61
switches 0:5c4d7b2438d3 62 Positional arguments:
switches 0:5c4d7b2438d3 63 properties - the dict to fill
switches 0:5c4d7b2438d3 64 target_name - the target the test is targeting
switches 0:5c4d7b2438d3 65 toolchain_name - the toolchain that will compile the test
switches 0:5c4d7b2438d3 66 vendor_label - the vendor
switches 0:5c4d7b2438d3 67 """
switches 0:5c4d7b2438d3 68 if not target_name in properties:
switches 0:5c4d7b2438d3 69 properties[target_name] = {}
switches 0:5c4d7b2438d3 70
switches 0:5c4d7b2438d3 71 if not toolchain_name in properties[target_name]:
switches 0:5c4d7b2438d3 72 properties[target_name][toolchain_name] = {}
switches 0:5c4d7b2438d3 73
switches 0:5c4d7b2438d3 74 properties[target_name][toolchain_name]["target"] = target_name
switches 0:5c4d7b2438d3 75 properties[target_name][toolchain_name]["vendor"] = vendor_label
switches 0:5c4d7b2438d3 76 properties[target_name][toolchain_name]["toolchain"] = toolchain_name
switches 0:5c4d7b2438d3 77
switches 0:5c4d7b2438d3 78 def create_result(target_name, toolchain_name, id_name, description):
switches 0:5c4d7b2438d3 79 """Create a result dictionary
switches 0:5c4d7b2438d3 80
switches 0:5c4d7b2438d3 81 Positional arguments:
switches 0:5c4d7b2438d3 82 target_name - the target being built for
switches 0:5c4d7b2438d3 83 toolchain_name - the toolchain doing the building
switches 0:5c4d7b2438d3 84 id_name - the name of the executable or library being built
switches 0:5c4d7b2438d3 85 description - a human readable description of what's going on
switches 0:5c4d7b2438d3 86 """
switches 0:5c4d7b2438d3 87 cur_result = {}
switches 0:5c4d7b2438d3 88 cur_result["target_name"] = target_name
switches 0:5c4d7b2438d3 89 cur_result["toolchain_name"] = toolchain_name
switches 0:5c4d7b2438d3 90 cur_result["id"] = id_name
switches 0:5c4d7b2438d3 91 cur_result["description"] = description
switches 0:5c4d7b2438d3 92 cur_result["elapsed_time"] = 0
switches 0:5c4d7b2438d3 93 cur_result["output"] = ""
switches 0:5c4d7b2438d3 94
switches 0:5c4d7b2438d3 95 return cur_result
switches 0:5c4d7b2438d3 96
switches 0:5c4d7b2438d3 97 def add_result_to_report(report, result):
switches 0:5c4d7b2438d3 98 """Add a single result to a report dictionary
switches 0:5c4d7b2438d3 99
switches 0:5c4d7b2438d3 100 Positional arguments:
switches 0:5c4d7b2438d3 101 report - the report to append to
switches 0:5c4d7b2438d3 102 result - the result to append
switches 0:5c4d7b2438d3 103 """
switches 0:5c4d7b2438d3 104 target = result["target_name"]
switches 0:5c4d7b2438d3 105 toolchain = result["toolchain_name"]
switches 0:5c4d7b2438d3 106 id_name = result['id']
switches 0:5c4d7b2438d3 107 result_wrap = {0: result}
switches 0:5c4d7b2438d3 108 report[target][toolchain][id_name].append(result_wrap)
switches 0:5c4d7b2438d3 109
switches 0:5c4d7b2438d3 110 def get_config(src_paths, target, toolchain_name):
switches 0:5c4d7b2438d3 111 """Get the configuration object for a target-toolchain combination
switches 0:5c4d7b2438d3 112
switches 0:5c4d7b2438d3 113 Positional arguments:
switches 0:5c4d7b2438d3 114 src_paths - paths to scan for the configuration files
switches 0:5c4d7b2438d3 115 target - the device we are building for
switches 0:5c4d7b2438d3 116 toolchain_name - the string that identifies the build tools
switches 0:5c4d7b2438d3 117 """
switches 0:5c4d7b2438d3 118 # Convert src_paths to a list if needed
switches 0:5c4d7b2438d3 119 if type(src_paths) != ListType:
switches 0:5c4d7b2438d3 120 src_paths = [src_paths]
switches 0:5c4d7b2438d3 121
switches 0:5c4d7b2438d3 122 # Pass all params to the unified prepare_resources()
switches 0:5c4d7b2438d3 123 toolchain = prepare_toolchain(src_paths, target, toolchain_name)
switches 0:5c4d7b2438d3 124
switches 0:5c4d7b2438d3 125 # Scan src_path for config files
switches 0:5c4d7b2438d3 126 resources = toolchain.scan_resources(src_paths[0])
switches 0:5c4d7b2438d3 127 for path in src_paths[1:]:
switches 0:5c4d7b2438d3 128 resources.add(toolchain.scan_resources(path))
switches 0:5c4d7b2438d3 129
switches 0:5c4d7b2438d3 130 # Update configuration files until added features creates no changes
switches 0:5c4d7b2438d3 131 prev_features = set()
switches 0:5c4d7b2438d3 132 while True:
switches 0:5c4d7b2438d3 133 # Update the configuration with any .json files found while scanning
switches 0:5c4d7b2438d3 134 toolchain.config.add_config_files(resources.json_files)
switches 0:5c4d7b2438d3 135
switches 0:5c4d7b2438d3 136 # Add features while we find new ones
switches 0:5c4d7b2438d3 137 features = toolchain.config.get_features()
switches 0:5c4d7b2438d3 138 if features == prev_features:
switches 0:5c4d7b2438d3 139 break
switches 0:5c4d7b2438d3 140
switches 0:5c4d7b2438d3 141 for feature in features:
switches 0:5c4d7b2438d3 142 if feature in resources.features:
switches 0:5c4d7b2438d3 143 resources += resources.features[feature]
switches 0:5c4d7b2438d3 144
switches 0:5c4d7b2438d3 145 prev_features = features
switches 0:5c4d7b2438d3 146 toolchain.config.validate_config()
switches 0:5c4d7b2438d3 147
switches 0:5c4d7b2438d3 148 cfg, macros = toolchain.config.get_config_data()
switches 0:5c4d7b2438d3 149 features = toolchain.config.get_features()
switches 0:5c4d7b2438d3 150 return cfg, macros, features
switches 0:5c4d7b2438d3 151
switches 0:5c4d7b2438d3 152 def is_official_target(target_name, version):
switches 0:5c4d7b2438d3 153 """ Returns True, None if a target is part of the official release for the
switches 0:5c4d7b2438d3 154 given version. Return False, 'reason' if a target is not part of the
switches 0:5c4d7b2438d3 155 official release for the given version.
switches 0:5c4d7b2438d3 156
switches 0:5c4d7b2438d3 157 Positional arguments:
switches 0:5c4d7b2438d3 158 target_name - Name if the target (ex. 'K64F')
switches 0:5c4d7b2438d3 159 version - The release version string. Should be a string contained within
switches 0:5c4d7b2438d3 160 RELEASE_VERSIONS
switches 0:5c4d7b2438d3 161 """
switches 0:5c4d7b2438d3 162
switches 0:5c4d7b2438d3 163 result = True
switches 0:5c4d7b2438d3 164 reason = None
switches 0:5c4d7b2438d3 165 target = TARGET_MAP[target_name]
switches 0:5c4d7b2438d3 166
switches 0:5c4d7b2438d3 167 if hasattr(target, 'release_versions') \
switches 0:5c4d7b2438d3 168 and version in target.release_versions:
switches 0:5c4d7b2438d3 169 if version == '2':
switches 0:5c4d7b2438d3 170 # For version 2, either ARM or uARM toolchain support is required
switches 0:5c4d7b2438d3 171 required_toolchains = set(['ARM', 'uARM'])
switches 0:5c4d7b2438d3 172
switches 0:5c4d7b2438d3 173 if not len(required_toolchains.intersection(
switches 0:5c4d7b2438d3 174 set(target.supported_toolchains))) > 0:
switches 0:5c4d7b2438d3 175 result = False
switches 0:5c4d7b2438d3 176 reason = ("Target '%s' must support " % target.name) + \
switches 0:5c4d7b2438d3 177 ("one of the folowing toolchains to be included in the") + \
switches 0:5c4d7b2438d3 178 ((" mbed 2.0 official release: %s" + linesep) %
switches 0:5c4d7b2438d3 179 ", ".join(required_toolchains)) + \
switches 0:5c4d7b2438d3 180 ("Currently it is only configured to support the ") + \
switches 0:5c4d7b2438d3 181 ("following toolchains: %s" %
switches 0:5c4d7b2438d3 182 ", ".join(target.supported_toolchains))
switches 0:5c4d7b2438d3 183
switches 0:5c4d7b2438d3 184 elif version == '5':
switches 0:5c4d7b2438d3 185 # For version 5, ARM, GCC_ARM, and IAR toolchain support is required
switches 0:5c4d7b2438d3 186 required_toolchains = set(['ARM', 'GCC_ARM', 'IAR'])
switches 0:5c4d7b2438d3 187 required_toolchains_sorted = list(required_toolchains)
switches 0:5c4d7b2438d3 188 required_toolchains_sorted.sort()
switches 0:5c4d7b2438d3 189 supported_toolchains = set(target.supported_toolchains)
switches 0:5c4d7b2438d3 190 supported_toolchains_sorted = list(supported_toolchains)
switches 0:5c4d7b2438d3 191 supported_toolchains_sorted.sort()
switches 0:5c4d7b2438d3 192
switches 0:5c4d7b2438d3 193 if not required_toolchains.issubset(supported_toolchains):
switches 0:5c4d7b2438d3 194 result = False
switches 0:5c4d7b2438d3 195 reason = ("Target '%s' must support " % target.name) + \
switches 0:5c4d7b2438d3 196 ("ALL of the folowing toolchains to be included in the") + \
switches 0:5c4d7b2438d3 197 ((" mbed OS 5.0 official release: %s" + linesep) %
switches 0:5c4d7b2438d3 198 ", ".join(required_toolchains_sorted)) + \
switches 0:5c4d7b2438d3 199 ("Currently it is only configured to support the ") + \
switches 0:5c4d7b2438d3 200 ("following toolchains: %s" %
switches 0:5c4d7b2438d3 201 ", ".join(supported_toolchains_sorted))
switches 0:5c4d7b2438d3 202
switches 0:5c4d7b2438d3 203 elif not target.default_lib == 'std':
switches 0:5c4d7b2438d3 204 result = False
switches 0:5c4d7b2438d3 205 reason = ("Target '%s' must set the " % target.name) + \
switches 0:5c4d7b2438d3 206 ("'default_lib' to 'std' to be included in the ") + \
switches 0:5c4d7b2438d3 207 ("mbed OS 5.0 official release." + linesep) + \
switches 0:5c4d7b2438d3 208 ("Currently it is set to '%s'" % target.default_lib)
switches 0:5c4d7b2438d3 209
switches 0:5c4d7b2438d3 210 else:
switches 0:5c4d7b2438d3 211 result = False
switches 0:5c4d7b2438d3 212 reason = ("Target '%s' has set an invalid release version of '%s'" %
switches 0:5c4d7b2438d3 213 version) + \
switches 0:5c4d7b2438d3 214 ("Please choose from the following release versions: %s" %
switches 0:5c4d7b2438d3 215 ', '.join(RELEASE_VERSIONS))
switches 0:5c4d7b2438d3 216
switches 0:5c4d7b2438d3 217 else:
switches 0:5c4d7b2438d3 218 result = False
switches 0:5c4d7b2438d3 219 if not hasattr(target, 'release_versions'):
switches 0:5c4d7b2438d3 220 reason = "Target '%s' " % target.name
switches 0:5c4d7b2438d3 221 reason += "does not have the 'release_versions' key set"
switches 0:5c4d7b2438d3 222 elif not version in target.release_versions:
switches 0:5c4d7b2438d3 223 reason = "Target '%s' does not contain the version '%s' " % \
switches 0:5c4d7b2438d3 224 (target.name, version)
switches 0:5c4d7b2438d3 225 reason += "in its 'release_versions' key"
switches 0:5c4d7b2438d3 226
switches 0:5c4d7b2438d3 227 return result, reason
switches 0:5c4d7b2438d3 228
switches 0:5c4d7b2438d3 229 def transform_release_toolchains(toolchains, version):
switches 0:5c4d7b2438d3 230 """ Given a list of toolchains and a release version, return a list of
switches 0:5c4d7b2438d3 231 only the supported toolchains for that release
switches 0:5c4d7b2438d3 232
switches 0:5c4d7b2438d3 233 Positional arguments:
switches 0:5c4d7b2438d3 234 toolchains - The list of toolchains
switches 0:5c4d7b2438d3 235 version - The release version string. Should be a string contained within
switches 0:5c4d7b2438d3 236 RELEASE_VERSIONS
switches 0:5c4d7b2438d3 237 """
switches 0:5c4d7b2438d3 238 if version == '5':
switches 0:5c4d7b2438d3 239 return ['ARM', 'GCC_ARM', 'IAR']
switches 0:5c4d7b2438d3 240 else:
switches 0:5c4d7b2438d3 241 return toolchains
switches 0:5c4d7b2438d3 242
switches 0:5c4d7b2438d3 243
switches 0:5c4d7b2438d3 244 def get_mbed_official_release(version):
switches 0:5c4d7b2438d3 245 """ Given a release version string, return a tuple that contains a target
switches 0:5c4d7b2438d3 246 and the supported toolchains for that release.
switches 0:5c4d7b2438d3 247 Ex. Given '2', return (('LPC1768', ('ARM', 'GCC_ARM')),
switches 0:5c4d7b2438d3 248 ('K64F', ('ARM', 'GCC_ARM')), ...)
switches 0:5c4d7b2438d3 249
switches 0:5c4d7b2438d3 250 Positional arguments:
switches 0:5c4d7b2438d3 251 version - The version string. Should be a string contained within
switches 0:5c4d7b2438d3 252 RELEASE_VERSIONS
switches 0:5c4d7b2438d3 253 """
switches 0:5c4d7b2438d3 254
switches 0:5c4d7b2438d3 255 mbed_official_release = (
switches 0:5c4d7b2438d3 256 tuple(
switches 0:5c4d7b2438d3 257 tuple(
switches 0:5c4d7b2438d3 258 [
switches 0:5c4d7b2438d3 259 TARGET_MAP[target].name,
switches 0:5c4d7b2438d3 260 tuple(transform_release_toolchains(
switches 0:5c4d7b2438d3 261 TARGET_MAP[target].supported_toolchains, version))
switches 0:5c4d7b2438d3 262 ]
switches 0:5c4d7b2438d3 263 ) for target in TARGET_NAMES \
switches 0:5c4d7b2438d3 264 if (hasattr(TARGET_MAP[target], 'release_versions')
switches 0:5c4d7b2438d3 265 and version in TARGET_MAP[target].release_versions)
switches 0:5c4d7b2438d3 266 )
switches 0:5c4d7b2438d3 267 )
switches 0:5c4d7b2438d3 268
switches 0:5c4d7b2438d3 269 for target in mbed_official_release:
switches 0:5c4d7b2438d3 270 is_official, reason = is_official_target(target[0], version)
switches 0:5c4d7b2438d3 271
switches 0:5c4d7b2438d3 272 if not is_official:
switches 0:5c4d7b2438d3 273 raise InvalidReleaseTargetException(reason)
switches 0:5c4d7b2438d3 274
switches 0:5c4d7b2438d3 275 return mbed_official_release
switches 0:5c4d7b2438d3 276
switches 0:5c4d7b2438d3 277
switches 0:5c4d7b2438d3 278 def prepare_toolchain(src_paths, target, toolchain_name,
switches 0:5c4d7b2438d3 279 macros=None, clean=False, jobs=1,
switches 0:5c4d7b2438d3 280 notify=None, silent=False, verbose=False,
switches 0:5c4d7b2438d3 281 extra_verbose=False, config=None,
switches 0:5c4d7b2438d3 282 app_config=None, build_profile=None):
switches 0:5c4d7b2438d3 283 """ Prepares resource related objects - toolchain, target, config
switches 0:5c4d7b2438d3 284
switches 0:5c4d7b2438d3 285 Positional arguments:
switches 0:5c4d7b2438d3 286 src_paths - the paths to source directories
switches 0:5c4d7b2438d3 287 target - ['LPC1768', 'LPC11U24', 'LPC2368', etc.]
switches 0:5c4d7b2438d3 288 toolchain_name - ['ARM', 'uARM', 'GCC_ARM', 'GCC_CR']
switches 0:5c4d7b2438d3 289
switches 0:5c4d7b2438d3 290 Keyword arguments:
switches 0:5c4d7b2438d3 291 macros - additional macros
switches 0:5c4d7b2438d3 292 clean - Rebuild everything if True
switches 0:5c4d7b2438d3 293 jobs - how many compilers we can run at once
switches 0:5c4d7b2438d3 294 notify - Notify function for logs
switches 0:5c4d7b2438d3 295 silent - suppress printing of progress indicators
switches 0:5c4d7b2438d3 296 verbose - Write the actual tools command lines used if True
switches 0:5c4d7b2438d3 297 extra_verbose - even more output!
switches 0:5c4d7b2438d3 298 config - a Config object to use instead of creating one
switches 0:5c4d7b2438d3 299 app_config - location of a chosen mbed_app.json file
switches 0:5c4d7b2438d3 300 build_profile - a dict of flags that will be passed to the compiler
switches 0:5c4d7b2438d3 301 """
switches 0:5c4d7b2438d3 302
switches 0:5c4d7b2438d3 303 # We need to remove all paths which are repeated to avoid
switches 0:5c4d7b2438d3 304 # multiple compilations and linking with the same objects
switches 0:5c4d7b2438d3 305 src_paths = [src_paths[0]] + list(set(src_paths[1:]))
switches 0:5c4d7b2438d3 306
switches 0:5c4d7b2438d3 307 # If the configuration object was not yet created, create it now
switches 0:5c4d7b2438d3 308 config = config or Config(target, src_paths, app_config=app_config)
switches 0:5c4d7b2438d3 309 target = config.target
switches 0:5c4d7b2438d3 310
switches 0:5c4d7b2438d3 311 # Toolchain instance
switches 0:5c4d7b2438d3 312 try:
switches 0:5c4d7b2438d3 313 toolchain = TOOLCHAIN_CLASSES[toolchain_name](
switches 0:5c4d7b2438d3 314 target, notify, macros, silent,
switches 0:5c4d7b2438d3 315 extra_verbose=extra_verbose, build_profile=build_profile)
switches 0:5c4d7b2438d3 316 except KeyError:
switches 0:5c4d7b2438d3 317 raise KeyError("Toolchain %s not supported" % toolchain_name)
switches 0:5c4d7b2438d3 318
switches 0:5c4d7b2438d3 319 toolchain.config = config
switches 0:5c4d7b2438d3 320 toolchain.jobs = jobs
switches 0:5c4d7b2438d3 321 toolchain.build_all = clean
switches 0:5c4d7b2438d3 322 toolchain.VERBOSE = verbose
switches 0:5c4d7b2438d3 323
switches 0:5c4d7b2438d3 324 return toolchain
switches 0:5c4d7b2438d3 325
switches 0:5c4d7b2438d3 326 def scan_resources(src_paths, toolchain, dependencies_paths=None,
switches 0:5c4d7b2438d3 327 inc_dirs=None, base_path=None):
switches 0:5c4d7b2438d3 328 """ Scan resources using initialized toolcain
switches 0:5c4d7b2438d3 329
switches 0:5c4d7b2438d3 330 Positional arguments
switches 0:5c4d7b2438d3 331 src_paths - the paths to source directories
switches 0:5c4d7b2438d3 332 toolchain - valid toolchain object
switches 0:5c4d7b2438d3 333 dependencies_paths - dependency paths that we should scan for include dirs
switches 0:5c4d7b2438d3 334 inc_dirs - additional include directories which should be added to
switches 0:5c4d7b2438d3 335 the scanner resources
switches 0:5c4d7b2438d3 336 """
switches 0:5c4d7b2438d3 337
switches 0:5c4d7b2438d3 338 # Scan src_path
switches 0:5c4d7b2438d3 339 resources = toolchain.scan_resources(src_paths[0], base_path=base_path)
switches 0:5c4d7b2438d3 340 for path in src_paths[1:]:
switches 0:5c4d7b2438d3 341 resources.add(toolchain.scan_resources(path, base_path=base_path))
switches 0:5c4d7b2438d3 342
switches 0:5c4d7b2438d3 343 # Scan dependency paths for include dirs
switches 0:5c4d7b2438d3 344 if dependencies_paths is not None:
switches 0:5c4d7b2438d3 345 for path in dependencies_paths:
switches 0:5c4d7b2438d3 346 lib_resources = toolchain.scan_resources(path)
switches 0:5c4d7b2438d3 347 resources.inc_dirs.extend(lib_resources.inc_dirs)
switches 0:5c4d7b2438d3 348
switches 0:5c4d7b2438d3 349 # Add additional include directories if passed
switches 0:5c4d7b2438d3 350 if inc_dirs:
switches 0:5c4d7b2438d3 351 if type(inc_dirs) == ListType:
switches 0:5c4d7b2438d3 352 resources.inc_dirs.extend(inc_dirs)
switches 0:5c4d7b2438d3 353 else:
switches 0:5c4d7b2438d3 354 resources.inc_dirs.append(inc_dirs)
switches 0:5c4d7b2438d3 355
switches 0:5c4d7b2438d3 356 # Load resources into the config system which might expand/modify resources
switches 0:5c4d7b2438d3 357 # based on config data
switches 0:5c4d7b2438d3 358 resources = toolchain.config.load_resources(resources)
switches 0:5c4d7b2438d3 359
switches 0:5c4d7b2438d3 360 # Set the toolchain's configuration data
switches 0:5c4d7b2438d3 361 toolchain.set_config_data(toolchain.config.get_config_data())
switches 0:5c4d7b2438d3 362
switches 0:5c4d7b2438d3 363 return resources
switches 0:5c4d7b2438d3 364
switches 0:5c4d7b2438d3 365 def build_project(src_paths, build_path, target, toolchain_name,
switches 0:5c4d7b2438d3 366 libraries_paths=None, linker_script=None,
switches 0:5c4d7b2438d3 367 clean=False, notify=None, verbose=False, name=None,
switches 0:5c4d7b2438d3 368 macros=None, inc_dirs=None, jobs=1, silent=False,
switches 0:5c4d7b2438d3 369 report=None, properties=None, project_id=None,
switches 0:5c4d7b2438d3 370 project_description=None, extra_verbose=False, config=None,
switches 0:5c4d7b2438d3 371 app_config=None, build_profile=None):
switches 0:5c4d7b2438d3 372 """ Build a project. A project may be a test or a user program.
switches 0:5c4d7b2438d3 373
switches 0:5c4d7b2438d3 374 Positional arguments:
switches 0:5c4d7b2438d3 375 src_paths - a path or list of paths that contain all files needed to build
switches 0:5c4d7b2438d3 376 the project
switches 0:5c4d7b2438d3 377 build_path - the directory where all of the object files will be placed
switches 0:5c4d7b2438d3 378 target - the MCU or board that the project will compile for
switches 0:5c4d7b2438d3 379 toolchain_name - the name of the build tools
switches 0:5c4d7b2438d3 380
switches 0:5c4d7b2438d3 381 Keyword arguments:
switches 0:5c4d7b2438d3 382 libraries_paths - The location of libraries to include when linking
switches 0:5c4d7b2438d3 383 linker_script - the file that drives the linker to do it's job
switches 0:5c4d7b2438d3 384 clean - Rebuild everything if True
switches 0:5c4d7b2438d3 385 notify - Notify function for logs
switches 0:5c4d7b2438d3 386 verbose - Write the actual tools command lines used if True
switches 0:5c4d7b2438d3 387 name - the name of the project
switches 0:5c4d7b2438d3 388 macros - additional macros
switches 0:5c4d7b2438d3 389 inc_dirs - additional directories where include files may be found
switches 0:5c4d7b2438d3 390 jobs - how many compilers we can run at once
switches 0:5c4d7b2438d3 391 silent - suppress printing of progress indicators
switches 0:5c4d7b2438d3 392 report - a dict where a result may be appended
switches 0:5c4d7b2438d3 393 properties - UUUUHHHHH beats me
switches 0:5c4d7b2438d3 394 project_id - the name put in the report
switches 0:5c4d7b2438d3 395 project_description - the human-readable version of what this thing does
switches 0:5c4d7b2438d3 396 extra_verbose - even more output!
switches 0:5c4d7b2438d3 397 config - a Config object to use instead of creating one
switches 0:5c4d7b2438d3 398 app_config - location of a chosen mbed_app.json file
switches 0:5c4d7b2438d3 399 build_profile - a dict of flags that will be passed to the compiler
switches 0:5c4d7b2438d3 400 """
switches 0:5c4d7b2438d3 401
switches 0:5c4d7b2438d3 402 # Convert src_path to a list if needed
switches 0:5c4d7b2438d3 403 if type(src_paths) != ListType:
switches 0:5c4d7b2438d3 404 src_paths = [src_paths]
switches 0:5c4d7b2438d3 405 # Extend src_paths wiht libraries_paths
switches 0:5c4d7b2438d3 406 if libraries_paths is not None:
switches 0:5c4d7b2438d3 407 src_paths.extend(libraries_paths)
switches 0:5c4d7b2438d3 408 inc_dirs.extend(map(dirname, libraries_paths))
switches 0:5c4d7b2438d3 409
switches 0:5c4d7b2438d3 410 # Build Directory
switches 0:5c4d7b2438d3 411 if clean and exists(build_path):
switches 0:5c4d7b2438d3 412 rmtree(build_path)
switches 0:5c4d7b2438d3 413 mkdir(build_path)
switches 0:5c4d7b2438d3 414
switches 0:5c4d7b2438d3 415 # Pass all params to the unified prepare_toolchain()
switches 0:5c4d7b2438d3 416 toolchain = prepare_toolchain(
switches 0:5c4d7b2438d3 417 src_paths, target, toolchain_name, macros=macros, clean=clean,
switches 0:5c4d7b2438d3 418 jobs=jobs, notify=notify, silent=silent, verbose=verbose,
switches 0:5c4d7b2438d3 419 extra_verbose=extra_verbose, config=config, app_config=app_config,
switches 0:5c4d7b2438d3 420 build_profile=build_profile)
switches 0:5c4d7b2438d3 421
switches 0:5c4d7b2438d3 422 # The first path will give the name to the library
switches 0:5c4d7b2438d3 423 if name is None:
switches 0:5c4d7b2438d3 424 name = basename(normpath(abspath(src_paths[0])))
switches 0:5c4d7b2438d3 425 toolchain.info("Building project %s (%s, %s)" %
switches 0:5c4d7b2438d3 426 (name, toolchain.target.name, toolchain_name))
switches 0:5c4d7b2438d3 427
switches 0:5c4d7b2438d3 428 # Initialize reporting
switches 0:5c4d7b2438d3 429 if report != None:
switches 0:5c4d7b2438d3 430 start = time()
switches 0:5c4d7b2438d3 431 # If project_id is specified, use that over the default name
switches 0:5c4d7b2438d3 432 id_name = project_id.upper() if project_id else name.upper()
switches 0:5c4d7b2438d3 433 description = project_description if project_description else name
switches 0:5c4d7b2438d3 434 vendor_label = toolchain.target.extra_labels[0]
switches 0:5c4d7b2438d3 435 prep_report(report, toolchain.target.name, toolchain_name, id_name)
switches 0:5c4d7b2438d3 436 cur_result = create_result(toolchain.target.name, toolchain_name,
switches 0:5c4d7b2438d3 437 id_name, description)
switches 0:5c4d7b2438d3 438 if properties != None:
switches 0:5c4d7b2438d3 439 prep_properties(properties, toolchain.target.name, toolchain_name,
switches 0:5c4d7b2438d3 440 vendor_label)
switches 0:5c4d7b2438d3 441
switches 0:5c4d7b2438d3 442 try:
switches 0:5c4d7b2438d3 443 # Call unified scan_resources
switches 0:5c4d7b2438d3 444 resources = scan_resources(src_paths, toolchain, inc_dirs=inc_dirs)
switches 0:5c4d7b2438d3 445
switches 0:5c4d7b2438d3 446 # Change linker script if specified
switches 0:5c4d7b2438d3 447 if linker_script is not None:
switches 0:5c4d7b2438d3 448 resources.linker_script = linker_script
switches 0:5c4d7b2438d3 449
switches 0:5c4d7b2438d3 450 # Compile Sources
switches 0:5c4d7b2438d3 451 objects = toolchain.compile_sources(resources, build_path,
switches 0:5c4d7b2438d3 452 resources.inc_dirs)
switches 0:5c4d7b2438d3 453 resources.objects.extend(objects)
switches 0:5c4d7b2438d3 454
switches 0:5c4d7b2438d3 455 # Link Program
switches 0:5c4d7b2438d3 456 res, _ = toolchain.link_program(resources, build_path, name)
switches 0:5c4d7b2438d3 457
switches 0:5c4d7b2438d3 458 memap_instance = getattr(toolchain, 'memap_instance', None)
switches 0:5c4d7b2438d3 459 memap_table = ''
switches 0:5c4d7b2438d3 460 if memap_instance:
switches 0:5c4d7b2438d3 461 # Write output to stdout in text (pretty table) format
switches 0:5c4d7b2438d3 462 memap_table = memap_instance.generate_output('table')
switches 0:5c4d7b2438d3 463
switches 0:5c4d7b2438d3 464 if not silent:
switches 0:5c4d7b2438d3 465 print memap_table
switches 0:5c4d7b2438d3 466
switches 0:5c4d7b2438d3 467 # Write output to file in JSON format
switches 0:5c4d7b2438d3 468 map_out = join(build_path, name + "_map.json")
switches 0:5c4d7b2438d3 469 memap_instance.generate_output('json', map_out)
switches 0:5c4d7b2438d3 470
switches 0:5c4d7b2438d3 471 # Write output to file in CSV format for the CI
switches 0:5c4d7b2438d3 472 map_csv = join(build_path, name + "_map.csv")
switches 0:5c4d7b2438d3 473 memap_instance.generate_output('csv-ci', map_csv)
switches 0:5c4d7b2438d3 474
switches 0:5c4d7b2438d3 475 resources.detect_duplicates(toolchain)
switches 0:5c4d7b2438d3 476
switches 0:5c4d7b2438d3 477 if report != None:
switches 0:5c4d7b2438d3 478 end = time()
switches 0:5c4d7b2438d3 479 cur_result["elapsed_time"] = end - start
switches 0:5c4d7b2438d3 480 cur_result["output"] = toolchain.get_output() + memap_table
switches 0:5c4d7b2438d3 481 cur_result["result"] = "OK"
switches 0:5c4d7b2438d3 482 cur_result["memory_usage"] = toolchain.map_outputs
switches 0:5c4d7b2438d3 483
switches 0:5c4d7b2438d3 484 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 485
switches 0:5c4d7b2438d3 486 return res
switches 0:5c4d7b2438d3 487
switches 0:5c4d7b2438d3 488 except Exception as exc:
switches 0:5c4d7b2438d3 489 if report != None:
switches 0:5c4d7b2438d3 490 end = time()
switches 0:5c4d7b2438d3 491
switches 0:5c4d7b2438d3 492 if isinstance(exc, NotSupportedException):
switches 0:5c4d7b2438d3 493 cur_result["result"] = "NOT_SUPPORTED"
switches 0:5c4d7b2438d3 494 else:
switches 0:5c4d7b2438d3 495 cur_result["result"] = "FAIL"
switches 0:5c4d7b2438d3 496
switches 0:5c4d7b2438d3 497 cur_result["elapsed_time"] = end - start
switches 0:5c4d7b2438d3 498
switches 0:5c4d7b2438d3 499 toolchain_output = toolchain.get_output()
switches 0:5c4d7b2438d3 500 if toolchain_output:
switches 0:5c4d7b2438d3 501 cur_result["output"] += toolchain_output
switches 0:5c4d7b2438d3 502
switches 0:5c4d7b2438d3 503 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 504
switches 0:5c4d7b2438d3 505 # Let Exception propagate
switches 0:5c4d7b2438d3 506 raise
switches 0:5c4d7b2438d3 507
switches 0:5c4d7b2438d3 508 def build_library(src_paths, build_path, target, toolchain_name,
switches 0:5c4d7b2438d3 509 dependencies_paths=None, name=None, clean=False,
switches 0:5c4d7b2438d3 510 archive=True, notify=None, verbose=False, macros=None,
switches 0:5c4d7b2438d3 511 inc_dirs=None, jobs=1, silent=False, report=None,
switches 0:5c4d7b2438d3 512 properties=None, extra_verbose=False, project_id=None,
switches 0:5c4d7b2438d3 513 remove_config_header_file=False, app_config=None,
switches 0:5c4d7b2438d3 514 build_profile=None):
switches 0:5c4d7b2438d3 515 """ Build a library
switches 0:5c4d7b2438d3 516
switches 0:5c4d7b2438d3 517 Positional arguments:
switches 0:5c4d7b2438d3 518 src_paths - a path or list of paths that contain all files needed to build
switches 0:5c4d7b2438d3 519 the library
switches 0:5c4d7b2438d3 520 build_path - the directory where all of the object files will be placed
switches 0:5c4d7b2438d3 521 target - the MCU or board that the project will compile for
switches 0:5c4d7b2438d3 522 toolchain_name - the name of the build tools
switches 0:5c4d7b2438d3 523
switches 0:5c4d7b2438d3 524 Keyword arguments:
switches 0:5c4d7b2438d3 525 dependencies_paths - The location of libraries to include when linking
switches 0:5c4d7b2438d3 526 name - the name of the library
switches 0:5c4d7b2438d3 527 clean - Rebuild everything if True
switches 0:5c4d7b2438d3 528 archive - whether the library will create an archive file
switches 0:5c4d7b2438d3 529 notify - Notify function for logs
switches 0:5c4d7b2438d3 530 verbose - Write the actual tools command lines used if True
switches 0:5c4d7b2438d3 531 macros - additional macros
switches 0:5c4d7b2438d3 532 inc_dirs - additional directories where include files may be found
switches 0:5c4d7b2438d3 533 jobs - how many compilers we can run at once
switches 0:5c4d7b2438d3 534 silent - suppress printing of progress indicators
switches 0:5c4d7b2438d3 535 report - a dict where a result may be appended
switches 0:5c4d7b2438d3 536 properties - UUUUHHHHH beats me
switches 0:5c4d7b2438d3 537 extra_verbose - even more output!
switches 0:5c4d7b2438d3 538 project_id - the name that goes in the report
switches 0:5c4d7b2438d3 539 remove_config_header_file - delete config header file when done building
switches 0:5c4d7b2438d3 540 app_config - location of a chosen mbed_app.json file
switches 0:5c4d7b2438d3 541 build_profile - a dict of flags that will be passed to the compiler
switches 0:5c4d7b2438d3 542 """
switches 0:5c4d7b2438d3 543
switches 0:5c4d7b2438d3 544 # Convert src_path to a list if needed
switches 0:5c4d7b2438d3 545 if type(src_paths) != ListType:
switches 0:5c4d7b2438d3 546 src_paths = [src_paths]
switches 0:5c4d7b2438d3 547
switches 0:5c4d7b2438d3 548 # Build path
switches 0:5c4d7b2438d3 549 if archive:
switches 0:5c4d7b2438d3 550 # Use temp path when building archive
switches 0:5c4d7b2438d3 551 tmp_path = join(build_path, '.temp')
switches 0:5c4d7b2438d3 552 mkdir(tmp_path)
switches 0:5c4d7b2438d3 553 else:
switches 0:5c4d7b2438d3 554 tmp_path = build_path
switches 0:5c4d7b2438d3 555
switches 0:5c4d7b2438d3 556 # Clean the build directory
switches 0:5c4d7b2438d3 557 if clean and exists(tmp_path):
switches 0:5c4d7b2438d3 558 rmtree(tmp_path)
switches 0:5c4d7b2438d3 559 mkdir(tmp_path)
switches 0:5c4d7b2438d3 560
switches 0:5c4d7b2438d3 561 # Pass all params to the unified prepare_toolchain()
switches 0:5c4d7b2438d3 562 toolchain = prepare_toolchain(
switches 0:5c4d7b2438d3 563 src_paths, target, toolchain_name, macros=macros, clean=clean,
switches 0:5c4d7b2438d3 564 jobs=jobs, notify=notify, silent=silent, verbose=verbose,
switches 0:5c4d7b2438d3 565 extra_verbose=extra_verbose, app_config=app_config,
switches 0:5c4d7b2438d3 566 build_profile=build_profile)
switches 0:5c4d7b2438d3 567
switches 0:5c4d7b2438d3 568 # The first path will give the name to the library
switches 0:5c4d7b2438d3 569 if name is None:
switches 0:5c4d7b2438d3 570 name = basename(normpath(abspath(src_paths[0])))
switches 0:5c4d7b2438d3 571 toolchain.info("Building library %s (%s, %s)" %
switches 0:5c4d7b2438d3 572 (name, toolchain.target.name, toolchain_name))
switches 0:5c4d7b2438d3 573
switches 0:5c4d7b2438d3 574 # Initialize reporting
switches 0:5c4d7b2438d3 575 if report != None:
switches 0:5c4d7b2438d3 576 start = time()
switches 0:5c4d7b2438d3 577 # If project_id is specified, use that over the default name
switches 0:5c4d7b2438d3 578 id_name = project_id.upper() if project_id else name.upper()
switches 0:5c4d7b2438d3 579 description = name
switches 0:5c4d7b2438d3 580 vendor_label = toolchain.target.extra_labels[0]
switches 0:5c4d7b2438d3 581 prep_report(report, toolchain.target.name, toolchain_name, id_name)
switches 0:5c4d7b2438d3 582 cur_result = create_result(toolchain.target.name, toolchain_name,
switches 0:5c4d7b2438d3 583 id_name, description)
switches 0:5c4d7b2438d3 584 if properties != None:
switches 0:5c4d7b2438d3 585 prep_properties(properties, toolchain.target.name, toolchain_name,
switches 0:5c4d7b2438d3 586 vendor_label)
switches 0:5c4d7b2438d3 587
switches 0:5c4d7b2438d3 588 for src_path in src_paths:
switches 0:5c4d7b2438d3 589 if not exists(src_path):
switches 0:5c4d7b2438d3 590 error_msg = "The library source folder does not exist: %s", src_path
switches 0:5c4d7b2438d3 591 if report != None:
switches 0:5c4d7b2438d3 592 cur_result["output"] = error_msg
switches 0:5c4d7b2438d3 593 cur_result["result"] = "FAIL"
switches 0:5c4d7b2438d3 594 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 595 raise Exception(error_msg)
switches 0:5c4d7b2438d3 596
switches 0:5c4d7b2438d3 597 try:
switches 0:5c4d7b2438d3 598 # Call unified scan_resources
switches 0:5c4d7b2438d3 599 resources = scan_resources(src_paths, toolchain,
switches 0:5c4d7b2438d3 600 dependencies_paths=dependencies_paths,
switches 0:5c4d7b2438d3 601 inc_dirs=inc_dirs)
switches 0:5c4d7b2438d3 602
switches 0:5c4d7b2438d3 603
switches 0:5c4d7b2438d3 604 # Copy headers, objects and static libraries - all files needed for
switches 0:5c4d7b2438d3 605 # static lib
switches 0:5c4d7b2438d3 606 toolchain.copy_files(resources.headers, build_path, resources=resources)
switches 0:5c4d7b2438d3 607 toolchain.copy_files(resources.objects, build_path, resources=resources)
switches 0:5c4d7b2438d3 608 toolchain.copy_files(resources.libraries, build_path,
switches 0:5c4d7b2438d3 609 resources=resources)
switches 0:5c4d7b2438d3 610 toolchain.copy_files(resources.json_files, build_path,
switches 0:5c4d7b2438d3 611 resources=resources)
switches 0:5c4d7b2438d3 612 if resources.linker_script:
switches 0:5c4d7b2438d3 613 toolchain.copy_files(resources.linker_script, build_path,
switches 0:5c4d7b2438d3 614 resources=resources)
switches 0:5c4d7b2438d3 615
switches 0:5c4d7b2438d3 616 if resources.hex_files:
switches 0:5c4d7b2438d3 617 toolchain.copy_files(resources.hex_files, build_path,
switches 0:5c4d7b2438d3 618 resources=resources)
switches 0:5c4d7b2438d3 619
switches 0:5c4d7b2438d3 620 # Compile Sources
switches 0:5c4d7b2438d3 621 objects = toolchain.compile_sources(resources, abspath(tmp_path),
switches 0:5c4d7b2438d3 622 resources.inc_dirs)
switches 0:5c4d7b2438d3 623 resources.objects.extend(objects)
switches 0:5c4d7b2438d3 624
switches 0:5c4d7b2438d3 625 if archive:
switches 0:5c4d7b2438d3 626 toolchain.build_library(objects, build_path, name)
switches 0:5c4d7b2438d3 627
switches 0:5c4d7b2438d3 628 if remove_config_header_file:
switches 0:5c4d7b2438d3 629 config_header_path = toolchain.get_config_header()
switches 0:5c4d7b2438d3 630 if config_header_path:
switches 0:5c4d7b2438d3 631 remove(config_header_path)
switches 0:5c4d7b2438d3 632
switches 0:5c4d7b2438d3 633 if report != None:
switches 0:5c4d7b2438d3 634 end = time()
switches 0:5c4d7b2438d3 635 cur_result["elapsed_time"] = end - start
switches 0:5c4d7b2438d3 636 cur_result["output"] = toolchain.get_output()
switches 0:5c4d7b2438d3 637 cur_result["result"] = "OK"
switches 0:5c4d7b2438d3 638
switches 0:5c4d7b2438d3 639
switches 0:5c4d7b2438d3 640 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 641 return True
switches 0:5c4d7b2438d3 642
switches 0:5c4d7b2438d3 643 except Exception as exc:
switches 0:5c4d7b2438d3 644 if report != None:
switches 0:5c4d7b2438d3 645 end = time()
switches 0:5c4d7b2438d3 646
switches 0:5c4d7b2438d3 647 if isinstance(exc, ToolException):
switches 0:5c4d7b2438d3 648 cur_result["result"] = "FAIL"
switches 0:5c4d7b2438d3 649 elif isinstance(exc, NotSupportedException):
switches 0:5c4d7b2438d3 650 cur_result["result"] = "NOT_SUPPORTED"
switches 0:5c4d7b2438d3 651
switches 0:5c4d7b2438d3 652 cur_result["elapsed_time"] = end - start
switches 0:5c4d7b2438d3 653
switches 0:5c4d7b2438d3 654 toolchain_output = toolchain.get_output()
switches 0:5c4d7b2438d3 655 if toolchain_output:
switches 0:5c4d7b2438d3 656 cur_result["output"] += toolchain_output
switches 0:5c4d7b2438d3 657
switches 0:5c4d7b2438d3 658 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 659
switches 0:5c4d7b2438d3 660 # Let Exception propagate
switches 0:5c4d7b2438d3 661 raise
switches 0:5c4d7b2438d3 662
switches 0:5c4d7b2438d3 663 ######################
switches 0:5c4d7b2438d3 664 ### Legacy methods ###
switches 0:5c4d7b2438d3 665 ######################
switches 0:5c4d7b2438d3 666
switches 0:5c4d7b2438d3 667 def build_lib(lib_id, target, toolchain_name, verbose=False,
switches 0:5c4d7b2438d3 668 clean=False, macros=None, notify=None, jobs=1, silent=False,
switches 0:5c4d7b2438d3 669 report=None, properties=None, extra_verbose=False,
switches 0:5c4d7b2438d3 670 build_profile=None):
switches 0:5c4d7b2438d3 671 """ Legacy method for building mbed libraries
switches 0:5c4d7b2438d3 672
switches 0:5c4d7b2438d3 673 Positional arguments:
switches 0:5c4d7b2438d3 674 lib_id - the library's unique identifier
switches 0:5c4d7b2438d3 675 target - the MCU or board that the project will compile for
switches 0:5c4d7b2438d3 676 toolchain_name - the name of the build tools
switches 0:5c4d7b2438d3 677
switches 0:5c4d7b2438d3 678 Keyword arguments:
switches 0:5c4d7b2438d3 679 clean - Rebuild everything if True
switches 0:5c4d7b2438d3 680 verbose - Write the actual tools command lines used if True
switches 0:5c4d7b2438d3 681 macros - additional macros
switches 0:5c4d7b2438d3 682 notify - Notify function for logs
switches 0:5c4d7b2438d3 683 jobs - how many compilers we can run at once
switches 0:5c4d7b2438d3 684 silent - suppress printing of progress indicators
switches 0:5c4d7b2438d3 685 report - a dict where a result may be appended
switches 0:5c4d7b2438d3 686 properties - UUUUHHHHH beats me
switches 0:5c4d7b2438d3 687 extra_verbose - even more output!
switches 0:5c4d7b2438d3 688 build_profile - a dict of flags that will be passed to the compiler
switches 0:5c4d7b2438d3 689 """
switches 0:5c4d7b2438d3 690 lib = Library(lib_id)
switches 0:5c4d7b2438d3 691 if not lib.is_supported(target, toolchain_name):
switches 0:5c4d7b2438d3 692 print('Library "%s" is not yet supported on target %s with toolchain %s'
switches 0:5c4d7b2438d3 693 % (lib_id, target.name, toolchain_name))
switches 0:5c4d7b2438d3 694 return False
switches 0:5c4d7b2438d3 695
switches 0:5c4d7b2438d3 696 # We need to combine macros from parameter list with macros from library
switches 0:5c4d7b2438d3 697 # definition
switches 0:5c4d7b2438d3 698 lib_macros = lib.macros if lib.macros else []
switches 0:5c4d7b2438d3 699 if macros:
switches 0:5c4d7b2438d3 700 macros.extend(lib_macros)
switches 0:5c4d7b2438d3 701 else:
switches 0:5c4d7b2438d3 702 macros = lib_macros
switches 0:5c4d7b2438d3 703
switches 0:5c4d7b2438d3 704 src_paths = lib.source_dir
switches 0:5c4d7b2438d3 705 build_path = lib.build_dir
switches 0:5c4d7b2438d3 706 dependencies_paths = lib.dependencies
switches 0:5c4d7b2438d3 707 inc_dirs = lib.inc_dirs
switches 0:5c4d7b2438d3 708 inc_dirs_ext = lib.inc_dirs_ext
switches 0:5c4d7b2438d3 709
switches 0:5c4d7b2438d3 710 if type(src_paths) != ListType:
switches 0:5c4d7b2438d3 711 src_paths = [src_paths]
switches 0:5c4d7b2438d3 712
switches 0:5c4d7b2438d3 713 # The first path will give the name to the library
switches 0:5c4d7b2438d3 714 name = basename(src_paths[0])
switches 0:5c4d7b2438d3 715
switches 0:5c4d7b2438d3 716 if report != None:
switches 0:5c4d7b2438d3 717 start = time()
switches 0:5c4d7b2438d3 718 id_name = name.upper()
switches 0:5c4d7b2438d3 719 description = name
switches 0:5c4d7b2438d3 720 vendor_label = target.extra_labels[0]
switches 0:5c4d7b2438d3 721 cur_result = None
switches 0:5c4d7b2438d3 722 prep_report(report, target.name, toolchain_name, id_name)
switches 0:5c4d7b2438d3 723 cur_result = create_result(target.name, toolchain_name, id_name,
switches 0:5c4d7b2438d3 724 description)
switches 0:5c4d7b2438d3 725
switches 0:5c4d7b2438d3 726 if properties != None:
switches 0:5c4d7b2438d3 727 prep_properties(properties, target.name, toolchain_name,
switches 0:5c4d7b2438d3 728 vendor_label)
switches 0:5c4d7b2438d3 729
switches 0:5c4d7b2438d3 730 for src_path in src_paths:
switches 0:5c4d7b2438d3 731 if not exists(src_path):
switches 0:5c4d7b2438d3 732 error_msg = "The library source folder does not exist: %s", src_path
switches 0:5c4d7b2438d3 733
switches 0:5c4d7b2438d3 734 if report != None:
switches 0:5c4d7b2438d3 735 cur_result["output"] = error_msg
switches 0:5c4d7b2438d3 736 cur_result["result"] = "FAIL"
switches 0:5c4d7b2438d3 737 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 738
switches 0:5c4d7b2438d3 739 raise Exception(error_msg)
switches 0:5c4d7b2438d3 740
switches 0:5c4d7b2438d3 741 try:
switches 0:5c4d7b2438d3 742 # Toolchain instance
switches 0:5c4d7b2438d3 743 toolchain = TOOLCHAIN_CLASSES[toolchain_name](
switches 0:5c4d7b2438d3 744 target, macros=macros, notify=notify, silent=silent,
switches 0:5c4d7b2438d3 745 extra_verbose=extra_verbose, build_profile=build_profile)
switches 0:5c4d7b2438d3 746 toolchain.VERBOSE = verbose
switches 0:5c4d7b2438d3 747 toolchain.jobs = jobs
switches 0:5c4d7b2438d3 748 toolchain.build_all = clean
switches 0:5c4d7b2438d3 749
switches 0:5c4d7b2438d3 750 toolchain.info("Building library %s (%s, %s)" %
switches 0:5c4d7b2438d3 751 (name.upper(), target.name, toolchain_name))
switches 0:5c4d7b2438d3 752
switches 0:5c4d7b2438d3 753 # Take into account the library configuration (MBED_CONFIG_FILE)
switches 0:5c4d7b2438d3 754 config = Config(target)
switches 0:5c4d7b2438d3 755 toolchain.config = config
switches 0:5c4d7b2438d3 756 config.add_config_files([MBED_CONFIG_FILE])
switches 0:5c4d7b2438d3 757
switches 0:5c4d7b2438d3 758 # Scan Resources
switches 0:5c4d7b2438d3 759 resources = []
switches 0:5c4d7b2438d3 760 for src_path in src_paths:
switches 0:5c4d7b2438d3 761 resources.append(toolchain.scan_resources(src_path))
switches 0:5c4d7b2438d3 762
switches 0:5c4d7b2438d3 763 # Add extra include directories / files which are required by library
switches 0:5c4d7b2438d3 764 # This files usually are not in the same directory as source files so
switches 0:5c4d7b2438d3 765 # previous scan will not include them
switches 0:5c4d7b2438d3 766 if inc_dirs_ext is not None:
switches 0:5c4d7b2438d3 767 for inc_ext in inc_dirs_ext:
switches 0:5c4d7b2438d3 768 resources.append(toolchain.scan_resources(inc_ext))
switches 0:5c4d7b2438d3 769
switches 0:5c4d7b2438d3 770 # Dependencies Include Paths
switches 0:5c4d7b2438d3 771 dependencies_include_dir = []
switches 0:5c4d7b2438d3 772 if dependencies_paths is not None:
switches 0:5c4d7b2438d3 773 for path in dependencies_paths:
switches 0:5c4d7b2438d3 774 lib_resources = toolchain.scan_resources(path)
switches 0:5c4d7b2438d3 775 dependencies_include_dir.extend(lib_resources.inc_dirs)
switches 0:5c4d7b2438d3 776 dependencies_include_dir.extend(map(dirname, lib_resources.inc_dirs))
switches 0:5c4d7b2438d3 777
switches 0:5c4d7b2438d3 778 if inc_dirs:
switches 0:5c4d7b2438d3 779 dependencies_include_dir.extend(inc_dirs)
switches 0:5c4d7b2438d3 780
switches 0:5c4d7b2438d3 781 # Add other discovered configuration data to the configuration object
switches 0:5c4d7b2438d3 782 for res in resources:
switches 0:5c4d7b2438d3 783 config.load_resources(res)
switches 0:5c4d7b2438d3 784 toolchain.set_config_data(toolchain.config.get_config_data())
switches 0:5c4d7b2438d3 785
switches 0:5c4d7b2438d3 786 # Create the desired build directory structure
switches 0:5c4d7b2438d3 787 bin_path = join(build_path, toolchain.obj_path)
switches 0:5c4d7b2438d3 788 mkdir(bin_path)
switches 0:5c4d7b2438d3 789 tmp_path = join(build_path, '.temp', toolchain.obj_path)
switches 0:5c4d7b2438d3 790 mkdir(tmp_path)
switches 0:5c4d7b2438d3 791
switches 0:5c4d7b2438d3 792 # Copy Headers
switches 0:5c4d7b2438d3 793 for resource in resources:
switches 0:5c4d7b2438d3 794 toolchain.copy_files(resource.headers, build_path,
switches 0:5c4d7b2438d3 795 resources=resource)
switches 0:5c4d7b2438d3 796
switches 0:5c4d7b2438d3 797 dependencies_include_dir.extend(
switches 0:5c4d7b2438d3 798 toolchain.scan_resources(build_path).inc_dirs)
switches 0:5c4d7b2438d3 799
switches 0:5c4d7b2438d3 800 # Compile Sources
switches 0:5c4d7b2438d3 801 objects = []
switches 0:5c4d7b2438d3 802 for resource in resources:
switches 0:5c4d7b2438d3 803 objects.extend(toolchain.compile_sources(resource, tmp_path,
switches 0:5c4d7b2438d3 804 dependencies_include_dir))
switches 0:5c4d7b2438d3 805
switches 0:5c4d7b2438d3 806 needed_update = toolchain.build_library(objects, bin_path, name)
switches 0:5c4d7b2438d3 807
switches 0:5c4d7b2438d3 808 if report != None and needed_update:
switches 0:5c4d7b2438d3 809 end = time()
switches 0:5c4d7b2438d3 810 cur_result["elapsed_time"] = end - start
switches 0:5c4d7b2438d3 811 cur_result["output"] = toolchain.get_output()
switches 0:5c4d7b2438d3 812 cur_result["result"] = "OK"
switches 0:5c4d7b2438d3 813
switches 0:5c4d7b2438d3 814 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 815 return True
switches 0:5c4d7b2438d3 816
switches 0:5c4d7b2438d3 817 except Exception:
switches 0:5c4d7b2438d3 818 if report != None:
switches 0:5c4d7b2438d3 819 end = time()
switches 0:5c4d7b2438d3 820 cur_result["result"] = "FAIL"
switches 0:5c4d7b2438d3 821 cur_result["elapsed_time"] = end - start
switches 0:5c4d7b2438d3 822
switches 0:5c4d7b2438d3 823 toolchain_output = toolchain.get_output()
switches 0:5c4d7b2438d3 824 if toolchain_output:
switches 0:5c4d7b2438d3 825 cur_result["output"] += toolchain_output
switches 0:5c4d7b2438d3 826
switches 0:5c4d7b2438d3 827 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 828
switches 0:5c4d7b2438d3 829 # Let Exception propagate
switches 0:5c4d7b2438d3 830 raise
switches 0:5c4d7b2438d3 831
switches 0:5c4d7b2438d3 832 # We do have unique legacy conventions about how we build and package the mbed
switches 0:5c4d7b2438d3 833 # library
switches 0:5c4d7b2438d3 834 def build_mbed_libs(target, toolchain_name, verbose=False,
switches 0:5c4d7b2438d3 835 clean=False, macros=None, notify=None, jobs=1, silent=False,
switches 0:5c4d7b2438d3 836 report=None, properties=None, extra_verbose=False,
switches 0:5c4d7b2438d3 837 build_profile=None):
switches 0:5c4d7b2438d3 838 """ Function returns True is library was built and false if building was
switches 0:5c4d7b2438d3 839 skipped
switches 0:5c4d7b2438d3 840
switches 0:5c4d7b2438d3 841 Positional arguments:
switches 0:5c4d7b2438d3 842 target - the MCU or board that the project will compile for
switches 0:5c4d7b2438d3 843 toolchain_name - the name of the build tools
switches 0:5c4d7b2438d3 844
switches 0:5c4d7b2438d3 845 Keyword arguments:
switches 0:5c4d7b2438d3 846 verbose - Write the actual tools command lines used if True
switches 0:5c4d7b2438d3 847 clean - Rebuild everything if True
switches 0:5c4d7b2438d3 848 macros - additional macros
switches 0:5c4d7b2438d3 849 notify - Notify function for logs
switches 0:5c4d7b2438d3 850 jobs - how many compilers we can run at once
switches 0:5c4d7b2438d3 851 silent - suppress printing of progress indicators
switches 0:5c4d7b2438d3 852 report - a dict where a result may be appended
switches 0:5c4d7b2438d3 853 properties - UUUUHHHHH beats me
switches 0:5c4d7b2438d3 854 extra_verbose - even more output!
switches 0:5c4d7b2438d3 855 build_profile - a dict of flags that will be passed to the compiler
switches 0:5c4d7b2438d3 856 """
switches 0:5c4d7b2438d3 857
switches 0:5c4d7b2438d3 858 if report != None:
switches 0:5c4d7b2438d3 859 start = time()
switches 0:5c4d7b2438d3 860 id_name = "MBED"
switches 0:5c4d7b2438d3 861 description = "mbed SDK"
switches 0:5c4d7b2438d3 862 vendor_label = target.extra_labels[0]
switches 0:5c4d7b2438d3 863 cur_result = None
switches 0:5c4d7b2438d3 864 prep_report(report, target.name, toolchain_name, id_name)
switches 0:5c4d7b2438d3 865 cur_result = create_result(target.name, toolchain_name, id_name,
switches 0:5c4d7b2438d3 866 description)
switches 0:5c4d7b2438d3 867
switches 0:5c4d7b2438d3 868 if properties != None:
switches 0:5c4d7b2438d3 869 prep_properties(properties, target.name, toolchain_name,
switches 0:5c4d7b2438d3 870 vendor_label)
switches 0:5c4d7b2438d3 871
switches 0:5c4d7b2438d3 872 # Check toolchain support
switches 0:5c4d7b2438d3 873 if toolchain_name not in target.supported_toolchains:
switches 0:5c4d7b2438d3 874 supported_toolchains_text = ", ".join(target.supported_toolchains)
switches 0:5c4d7b2438d3 875 print('%s target is not yet supported by toolchain %s' %
switches 0:5c4d7b2438d3 876 (target.name, toolchain_name))
switches 0:5c4d7b2438d3 877 print('%s target supports %s toolchain%s' %
switches 0:5c4d7b2438d3 878 (target.name, supported_toolchains_text, 's'
switches 0:5c4d7b2438d3 879 if len(target.supported_toolchains) > 1 else ''))
switches 0:5c4d7b2438d3 880
switches 0:5c4d7b2438d3 881 if report != None:
switches 0:5c4d7b2438d3 882 cur_result["result"] = "SKIP"
switches 0:5c4d7b2438d3 883 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 884
switches 0:5c4d7b2438d3 885 return False
switches 0:5c4d7b2438d3 886
switches 0:5c4d7b2438d3 887 try:
switches 0:5c4d7b2438d3 888 # Toolchain
switches 0:5c4d7b2438d3 889 toolchain = TOOLCHAIN_CLASSES[toolchain_name](
switches 0:5c4d7b2438d3 890 target, macros=macros, notify=notify, silent=silent,
switches 0:5c4d7b2438d3 891 extra_verbose=extra_verbose, build_profile=build_profile)
switches 0:5c4d7b2438d3 892 toolchain.VERBOSE = verbose
switches 0:5c4d7b2438d3 893 toolchain.jobs = jobs
switches 0:5c4d7b2438d3 894 toolchain.build_all = clean
switches 0:5c4d7b2438d3 895
switches 0:5c4d7b2438d3 896 # Take into account the library configuration (MBED_CONFIG_FILE)
switches 0:5c4d7b2438d3 897 config = Config(target)
switches 0:5c4d7b2438d3 898 toolchain.config = config
switches 0:5c4d7b2438d3 899 config.add_config_files([MBED_CONFIG_FILE])
switches 0:5c4d7b2438d3 900 toolchain.set_config_data(toolchain.config.get_config_data())
switches 0:5c4d7b2438d3 901
switches 0:5c4d7b2438d3 902 # Source and Build Paths
switches 0:5c4d7b2438d3 903 build_target = join(MBED_LIBRARIES, "TARGET_" + target.name)
switches 0:5c4d7b2438d3 904 build_toolchain = join(build_target, "TOOLCHAIN_" + toolchain.name)
switches 0:5c4d7b2438d3 905 mkdir(build_toolchain)
switches 0:5c4d7b2438d3 906
switches 0:5c4d7b2438d3 907 tmp_path = join(MBED_LIBRARIES, '.temp', toolchain.obj_path)
switches 0:5c4d7b2438d3 908 mkdir(tmp_path)
switches 0:5c4d7b2438d3 909
switches 0:5c4d7b2438d3 910 # CMSIS
switches 0:5c4d7b2438d3 911 toolchain.info("Building library %s (%s, %s)" %
switches 0:5c4d7b2438d3 912 ('CMSIS', target.name, toolchain_name))
switches 0:5c4d7b2438d3 913 cmsis_src = MBED_CMSIS_PATH
switches 0:5c4d7b2438d3 914 resources = toolchain.scan_resources(cmsis_src)
switches 0:5c4d7b2438d3 915
switches 0:5c4d7b2438d3 916 toolchain.copy_files(resources.headers, build_target)
switches 0:5c4d7b2438d3 917 toolchain.copy_files(resources.linker_script, build_toolchain)
switches 0:5c4d7b2438d3 918 toolchain.copy_files(resources.bin_files, build_toolchain)
switches 0:5c4d7b2438d3 919
switches 0:5c4d7b2438d3 920 objects = toolchain.compile_sources(resources, tmp_path)
switches 0:5c4d7b2438d3 921 toolchain.copy_files(objects, build_toolchain)
switches 0:5c4d7b2438d3 922
switches 0:5c4d7b2438d3 923 # mbed
switches 0:5c4d7b2438d3 924 toolchain.info("Building library %s (%s, %s)" %
switches 0:5c4d7b2438d3 925 ('MBED', target.name, toolchain_name))
switches 0:5c4d7b2438d3 926
switches 0:5c4d7b2438d3 927 # Common Headers
switches 0:5c4d7b2438d3 928 toolchain.copy_files([MBED_HEADER], MBED_LIBRARIES)
switches 0:5c4d7b2438d3 929 library_incdirs = [dirname(MBED_LIBRARIES), MBED_LIBRARIES]
switches 0:5c4d7b2438d3 930
switches 0:5c4d7b2438d3 931 for dir, dest in [(MBED_DRIVERS, MBED_LIBRARIES_DRIVERS),
switches 0:5c4d7b2438d3 932 (MBED_PLATFORM, MBED_LIBRARIES_PLATFORM),
switches 0:5c4d7b2438d3 933 (MBED_HAL, MBED_LIBRARIES_HAL)]:
switches 0:5c4d7b2438d3 934 resources = toolchain.scan_resources(dir)
switches 0:5c4d7b2438d3 935 toolchain.copy_files(resources.headers, dest)
switches 0:5c4d7b2438d3 936 library_incdirs.append(dest)
switches 0:5c4d7b2438d3 937
switches 0:5c4d7b2438d3 938 # Target specific sources
switches 0:5c4d7b2438d3 939 hal_src = MBED_TARGETS_PATH
switches 0:5c4d7b2438d3 940 hal_implementation = toolchain.scan_resources(hal_src)
switches 0:5c4d7b2438d3 941 toolchain.copy_files(hal_implementation.headers +
switches 0:5c4d7b2438d3 942 hal_implementation.hex_files +
switches 0:5c4d7b2438d3 943 hal_implementation.libraries +
switches 0:5c4d7b2438d3 944 [MBED_CONFIG_FILE],
switches 0:5c4d7b2438d3 945 build_target, resources=hal_implementation)
switches 0:5c4d7b2438d3 946 toolchain.copy_files(hal_implementation.linker_script, build_toolchain)
switches 0:5c4d7b2438d3 947 toolchain.copy_files(hal_implementation.bin_files, build_toolchain)
switches 0:5c4d7b2438d3 948 incdirs = toolchain.scan_resources(build_target).inc_dirs
switches 0:5c4d7b2438d3 949 objects = toolchain.compile_sources(hal_implementation, tmp_path,
switches 0:5c4d7b2438d3 950 library_incdirs + incdirs)
switches 0:5c4d7b2438d3 951 toolchain.copy_files(objects, build_toolchain)
switches 0:5c4d7b2438d3 952
switches 0:5c4d7b2438d3 953 # Common Sources
switches 0:5c4d7b2438d3 954 mbed_resources = None
switches 0:5c4d7b2438d3 955 for dir in [MBED_DRIVERS, MBED_PLATFORM, MBED_HAL]:
switches 0:5c4d7b2438d3 956 mbed_resources += toolchain.scan_resources(dir)
switches 0:5c4d7b2438d3 957
switches 0:5c4d7b2438d3 958 objects = toolchain.compile_sources(mbed_resources, tmp_path,
switches 0:5c4d7b2438d3 959 library_incdirs + incdirs)
switches 0:5c4d7b2438d3 960
switches 0:5c4d7b2438d3 961 # A number of compiled files need to be copied as objects as opposed to
switches 0:5c4d7b2438d3 962 # way the linker search for symbols in archives. These are:
switches 0:5c4d7b2438d3 963 # - retarget.o: to make sure that the C standard lib symbols get
switches 0:5c4d7b2438d3 964 # overridden
switches 0:5c4d7b2438d3 965 # - board.o: mbed_die is weak
switches 0:5c4d7b2438d3 966 # - mbed_overrides.o: this contains platform overrides of various
switches 0:5c4d7b2438d3 967 # weak SDK functions
switches 0:5c4d7b2438d3 968 separate_names, separate_objects = ['retarget.o', 'board.o',
switches 0:5c4d7b2438d3 969 'mbed_overrides.o'], []
switches 0:5c4d7b2438d3 970
switches 0:5c4d7b2438d3 971 for obj in objects:
switches 0:5c4d7b2438d3 972 for name in separate_names:
switches 0:5c4d7b2438d3 973 if obj.endswith(name):
switches 0:5c4d7b2438d3 974 separate_objects.append(obj)
switches 0:5c4d7b2438d3 975
switches 0:5c4d7b2438d3 976 for obj in separate_objects:
switches 0:5c4d7b2438d3 977 objects.remove(obj)
switches 0:5c4d7b2438d3 978
switches 0:5c4d7b2438d3 979 toolchain.build_library(objects, build_toolchain, "mbed")
switches 0:5c4d7b2438d3 980
switches 0:5c4d7b2438d3 981 for obj in separate_objects:
switches 0:5c4d7b2438d3 982 toolchain.copy_files(obj, build_toolchain)
switches 0:5c4d7b2438d3 983
switches 0:5c4d7b2438d3 984 if report != None:
switches 0:5c4d7b2438d3 985 end = time()
switches 0:5c4d7b2438d3 986 cur_result["elapsed_time"] = end - start
switches 0:5c4d7b2438d3 987 cur_result["output"] = toolchain.get_output()
switches 0:5c4d7b2438d3 988 cur_result["result"] = "OK"
switches 0:5c4d7b2438d3 989
switches 0:5c4d7b2438d3 990 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 991
switches 0:5c4d7b2438d3 992 return True
switches 0:5c4d7b2438d3 993
switches 0:5c4d7b2438d3 994 except Exception as exc:
switches 0:5c4d7b2438d3 995 if report != None:
switches 0:5c4d7b2438d3 996 end = time()
switches 0:5c4d7b2438d3 997 cur_result["result"] = "FAIL"
switches 0:5c4d7b2438d3 998 cur_result["elapsed_time"] = end - start
switches 0:5c4d7b2438d3 999
switches 0:5c4d7b2438d3 1000 toolchain_output = toolchain.get_output()
switches 0:5c4d7b2438d3 1001 if toolchain_output:
switches 0:5c4d7b2438d3 1002 cur_result["output"] += toolchain_output
switches 0:5c4d7b2438d3 1003
switches 0:5c4d7b2438d3 1004 cur_result["output"] += str(exc)
switches 0:5c4d7b2438d3 1005
switches 0:5c4d7b2438d3 1006 add_result_to_report(report, cur_result)
switches 0:5c4d7b2438d3 1007
switches 0:5c4d7b2438d3 1008 # Let Exception propagate
switches 0:5c4d7b2438d3 1009 raise
switches 0:5c4d7b2438d3 1010
switches 0:5c4d7b2438d3 1011
switches 0:5c4d7b2438d3 1012 def get_unique_supported_toolchains(release_targets=None):
switches 0:5c4d7b2438d3 1013 """ Get list of all unique toolchains supported by targets
switches 0:5c4d7b2438d3 1014
switches 0:5c4d7b2438d3 1015 Keyword arguments:
switches 0:5c4d7b2438d3 1016 release_targets - tuple structure returned from get_mbed_official_release().
switches 0:5c4d7b2438d3 1017 If release_targets is not specified, then it queries all
switches 0:5c4d7b2438d3 1018 known targets
switches 0:5c4d7b2438d3 1019 """
switches 0:5c4d7b2438d3 1020 unique_supported_toolchains = []
switches 0:5c4d7b2438d3 1021
switches 0:5c4d7b2438d3 1022 if not release_targets:
switches 0:5c4d7b2438d3 1023 for target in TARGET_NAMES:
switches 0:5c4d7b2438d3 1024 for toolchain in TARGET_MAP[target].supported_toolchains:
switches 0:5c4d7b2438d3 1025 if toolchain not in unique_supported_toolchains:
switches 0:5c4d7b2438d3 1026 unique_supported_toolchains.append(toolchain)
switches 0:5c4d7b2438d3 1027 else:
switches 0:5c4d7b2438d3 1028 for target in release_targets:
switches 0:5c4d7b2438d3 1029 for toolchain in target[1]:
switches 0:5c4d7b2438d3 1030 if toolchain not in unique_supported_toolchains:
switches 0:5c4d7b2438d3 1031 unique_supported_toolchains.append(toolchain)
switches 0:5c4d7b2438d3 1032
switches 0:5c4d7b2438d3 1033 return unique_supported_toolchains
switches 0:5c4d7b2438d3 1034
switches 0:5c4d7b2438d3 1035
switches 0:5c4d7b2438d3 1036 def mcu_toolchain_matrix(verbose_html=False, platform_filter=None,
switches 0:5c4d7b2438d3 1037 release_version='5'):
switches 0:5c4d7b2438d3 1038 """ Shows target map using prettytable
switches 0:5c4d7b2438d3 1039
switches 0:5c4d7b2438d3 1040 Keyword arguments:
switches 0:5c4d7b2438d3 1041 verbose_html - emit html instead of a simple table
switches 0:5c4d7b2438d3 1042 platform_filter - remove results that match the string
switches 0:5c4d7b2438d3 1043 release_version - get the matrix for this major version number
switches 0:5c4d7b2438d3 1044 """
switches 0:5c4d7b2438d3 1045 # Only use it in this function so building works without extra modules
switches 0:5c4d7b2438d3 1046 from prettytable import PrettyTable
switches 0:5c4d7b2438d3 1047
switches 0:5c4d7b2438d3 1048 if isinstance(release_version, basestring):
switches 0:5c4d7b2438d3 1049 # Force release_version to lowercase if it is a string
switches 0:5c4d7b2438d3 1050 release_version = release_version.lower()
switches 0:5c4d7b2438d3 1051 else:
switches 0:5c4d7b2438d3 1052 # Otherwise default to printing all known targets and toolchains
switches 0:5c4d7b2438d3 1053 release_version = 'all'
switches 0:5c4d7b2438d3 1054
switches 0:5c4d7b2438d3 1055
switches 0:5c4d7b2438d3 1056 version_release_targets = {}
switches 0:5c4d7b2438d3 1057 version_release_target_names = {}
switches 0:5c4d7b2438d3 1058
switches 0:5c4d7b2438d3 1059 for version in RELEASE_VERSIONS:
switches 0:5c4d7b2438d3 1060 version_release_targets[version] = get_mbed_official_release(version)
switches 0:5c4d7b2438d3 1061 version_release_target_names[version] = [x[0] for x in
switches 0:5c4d7b2438d3 1062 version_release_targets[
switches 0:5c4d7b2438d3 1063 version]]
switches 0:5c4d7b2438d3 1064
switches 0:5c4d7b2438d3 1065 if release_version in RELEASE_VERSIONS:
switches 0:5c4d7b2438d3 1066 release_targets = version_release_targets[release_version]
switches 0:5c4d7b2438d3 1067 else:
switches 0:5c4d7b2438d3 1068 release_targets = None
switches 0:5c4d7b2438d3 1069
switches 0:5c4d7b2438d3 1070 unique_supported_toolchains = get_unique_supported_toolchains(
switches 0:5c4d7b2438d3 1071 release_targets)
switches 0:5c4d7b2438d3 1072 prepend_columns = ["Target"] + ["mbed OS %s" % x for x in RELEASE_VERSIONS]
switches 0:5c4d7b2438d3 1073
switches 0:5c4d7b2438d3 1074 # All tests status table print
switches 0:5c4d7b2438d3 1075 columns = prepend_columns + unique_supported_toolchains
switches 0:5c4d7b2438d3 1076 table_printer = PrettyTable(columns)
switches 0:5c4d7b2438d3 1077 # Align table
switches 0:5c4d7b2438d3 1078 for col in columns:
switches 0:5c4d7b2438d3 1079 table_printer.align[col] = "c"
switches 0:5c4d7b2438d3 1080 table_printer.align["Target"] = "l"
switches 0:5c4d7b2438d3 1081
switches 0:5c4d7b2438d3 1082 perm_counter = 0
switches 0:5c4d7b2438d3 1083 target_counter = 0
switches 0:5c4d7b2438d3 1084
switches 0:5c4d7b2438d3 1085 target_names = []
switches 0:5c4d7b2438d3 1086
switches 0:5c4d7b2438d3 1087 if release_targets:
switches 0:5c4d7b2438d3 1088 target_names = [x[0] for x in release_targets]
switches 0:5c4d7b2438d3 1089 else:
switches 0:5c4d7b2438d3 1090 target_names = TARGET_NAMES
switches 0:5c4d7b2438d3 1091
switches 0:5c4d7b2438d3 1092 for target in sorted(target_names):
switches 0:5c4d7b2438d3 1093 if platform_filter is not None:
switches 0:5c4d7b2438d3 1094 # FIlter out platforms using regex
switches 0:5c4d7b2438d3 1095 if re.search(platform_filter, target) is None:
switches 0:5c4d7b2438d3 1096 continue
switches 0:5c4d7b2438d3 1097 target_counter += 1
switches 0:5c4d7b2438d3 1098
switches 0:5c4d7b2438d3 1099 row = [target] # First column is platform name
switches 0:5c4d7b2438d3 1100
switches 0:5c4d7b2438d3 1101 for version in RELEASE_VERSIONS:
switches 0:5c4d7b2438d3 1102 if target in version_release_target_names[version]:
switches 0:5c4d7b2438d3 1103 text = "Supported"
switches 0:5c4d7b2438d3 1104 else:
switches 0:5c4d7b2438d3 1105 text = "-"
switches 0:5c4d7b2438d3 1106 row.append(text)
switches 0:5c4d7b2438d3 1107
switches 0:5c4d7b2438d3 1108 for unique_toolchain in unique_supported_toolchains:
switches 0:5c4d7b2438d3 1109 if unique_toolchain in TARGET_MAP[target].supported_toolchains:
switches 0:5c4d7b2438d3 1110 text = "Supported"
switches 0:5c4d7b2438d3 1111 perm_counter += 1
switches 0:5c4d7b2438d3 1112 else:
switches 0:5c4d7b2438d3 1113 text = "-"
switches 0:5c4d7b2438d3 1114
switches 0:5c4d7b2438d3 1115 row.append(text)
switches 0:5c4d7b2438d3 1116 table_printer.add_row(row)
switches 0:5c4d7b2438d3 1117
switches 0:5c4d7b2438d3 1118 result = table_printer.get_html_string() if verbose_html \
switches 0:5c4d7b2438d3 1119 else table_printer.get_string()
switches 0:5c4d7b2438d3 1120 result += "\n"
switches 0:5c4d7b2438d3 1121 result += "Supported targets: %d\n"% (target_counter)
switches 0:5c4d7b2438d3 1122 if target_counter == 1:
switches 0:5c4d7b2438d3 1123 result += "Supported toolchains: %d"% (perm_counter)
switches 0:5c4d7b2438d3 1124 return result
switches 0:5c4d7b2438d3 1125
switches 0:5c4d7b2438d3 1126
switches 0:5c4d7b2438d3 1127 def get_target_supported_toolchains(target):
switches 0:5c4d7b2438d3 1128 """ Returns target supported toolchains list
switches 0:5c4d7b2438d3 1129
switches 0:5c4d7b2438d3 1130 Positional arguments:
switches 0:5c4d7b2438d3 1131 target - the target to get the supported toolchains of
switches 0:5c4d7b2438d3 1132 """
switches 0:5c4d7b2438d3 1133 return TARGET_MAP[target].supported_toolchains if target in TARGET_MAP \
switches 0:5c4d7b2438d3 1134 else None
switches 0:5c4d7b2438d3 1135
switches 0:5c4d7b2438d3 1136
switches 0:5c4d7b2438d3 1137 def static_analysis_scan(target, toolchain_name, cppcheck_cmd,
switches 0:5c4d7b2438d3 1138 cppcheck_msg_format, verbose=False,
switches 0:5c4d7b2438d3 1139 clean=False, macros=None, notify=None, jobs=1,
switches 0:5c4d7b2438d3 1140 extra_verbose=False, build_profile=None):
switches 0:5c4d7b2438d3 1141 """Perform static analysis on a target and toolchain combination
switches 0:5c4d7b2438d3 1142
switches 0:5c4d7b2438d3 1143 Positional arguments:
switches 0:5c4d7b2438d3 1144 target - the target to fake the build for
switches 0:5c4d7b2438d3 1145 toolchain_name - pretend you would compile with this toolchain
switches 0:5c4d7b2438d3 1146 cppcheck_cmd - the command used to do static analysis
switches 0:5c4d7b2438d3 1147 cppcheck_msg_format - the format of the check messages
switches 0:5c4d7b2438d3 1148
switches 0:5c4d7b2438d3 1149 Keyword arguments:
switches 0:5c4d7b2438d3 1150 verbose - more printing!
switches 0:5c4d7b2438d3 1151 clean - start from a clean slate
switches 0:5c4d7b2438d3 1152 macros - extra macros to compile with
switches 0:5c4d7b2438d3 1153 notify - the notification event handling function
switches 0:5c4d7b2438d3 1154 jobs - number of commands to run at once
switches 0:5c4d7b2438d3 1155 extra_verbose - even moar printing
switches 0:5c4d7b2438d3 1156 build_profile - a dict of flags that will be passed to the compiler
switches 0:5c4d7b2438d3 1157 """
switches 0:5c4d7b2438d3 1158 # Toolchain
switches 0:5c4d7b2438d3 1159 toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, macros=macros,
switches 0:5c4d7b2438d3 1160 notify=notify,
switches 0:5c4d7b2438d3 1161 extra_verbose=extra_verbose,
switches 0:5c4d7b2438d3 1162 build_profile=build_profile)
switches 0:5c4d7b2438d3 1163 toolchain.VERBOSE = verbose
switches 0:5c4d7b2438d3 1164 toolchain.jobs = jobs
switches 0:5c4d7b2438d3 1165 toolchain.build_all = clean
switches 0:5c4d7b2438d3 1166
switches 0:5c4d7b2438d3 1167 # Source and Build Paths
switches 0:5c4d7b2438d3 1168 build_target = join(MBED_LIBRARIES, "TARGET_" + target.name)
switches 0:5c4d7b2438d3 1169 build_toolchain = join(build_target, "TOOLCHAIN_" + toolchain.name)
switches 0:5c4d7b2438d3 1170 mkdir(build_toolchain)
switches 0:5c4d7b2438d3 1171
switches 0:5c4d7b2438d3 1172 tmp_path = join(MBED_LIBRARIES, '.temp', toolchain.obj_path)
switches 0:5c4d7b2438d3 1173 mkdir(tmp_path)
switches 0:5c4d7b2438d3 1174
switches 0:5c4d7b2438d3 1175 # CMSIS
switches 0:5c4d7b2438d3 1176 toolchain.info("Static analysis for %s (%s, %s)" %
switches 0:5c4d7b2438d3 1177 ('CMSIS', target.name, toolchain_name))
switches 0:5c4d7b2438d3 1178 cmsis_src = MBED_CMSIS_PATH
switches 0:5c4d7b2438d3 1179 resources = toolchain.scan_resources(cmsis_src)
switches 0:5c4d7b2438d3 1180
switches 0:5c4d7b2438d3 1181 # Copy files before analysis
switches 0:5c4d7b2438d3 1182 toolchain.copy_files(resources.headers, build_target)
switches 0:5c4d7b2438d3 1183 toolchain.copy_files(resources.linker_script, build_toolchain)
switches 0:5c4d7b2438d3 1184
switches 0:5c4d7b2438d3 1185 # Gather include paths, c, cpp sources and macros to transfer to cppcheck
switches 0:5c4d7b2438d3 1186 # command line
switches 0:5c4d7b2438d3 1187 includes = ["-I%s"% i for i in resources.inc_dirs]
switches 0:5c4d7b2438d3 1188 includes.append("-I%s"% str(build_target))
switches 0:5c4d7b2438d3 1189 c_sources = " ".join(resources.c_sources)
switches 0:5c4d7b2438d3 1190 cpp_sources = " ".join(resources.cpp_sources)
switches 0:5c4d7b2438d3 1191 macros = ["-D%s"% s for s in toolchain.get_symbols() + toolchain.macros]
switches 0:5c4d7b2438d3 1192
switches 0:5c4d7b2438d3 1193 includes = [inc.strip() for inc in includes]
switches 0:5c4d7b2438d3 1194 macros = [mac.strip() for mac in macros]
switches 0:5c4d7b2438d3 1195
switches 0:5c4d7b2438d3 1196 check_cmd = cppcheck_cmd
switches 0:5c4d7b2438d3 1197 check_cmd += cppcheck_msg_format
switches 0:5c4d7b2438d3 1198 check_cmd += includes
switches 0:5c4d7b2438d3 1199 check_cmd += macros
switches 0:5c4d7b2438d3 1200
switches 0:5c4d7b2438d3 1201 # We need to pass some params via file to avoid "command line too long in
switches 0:5c4d7b2438d3 1202 # some OSs"
switches 0:5c4d7b2438d3 1203 tmp_file = tempfile.NamedTemporaryFile(delete=False)
switches 0:5c4d7b2438d3 1204 tmp_file.writelines(line + '\n' for line in c_sources.split())
switches 0:5c4d7b2438d3 1205 tmp_file.writelines(line + '\n' for line in cpp_sources.split())
switches 0:5c4d7b2438d3 1206 tmp_file.close()
switches 0:5c4d7b2438d3 1207 check_cmd += ["--file-list=%s"% tmp_file.name]
switches 0:5c4d7b2438d3 1208
switches 0:5c4d7b2438d3 1209 _stdout, _stderr, _ = run_cmd(check_cmd)
switches 0:5c4d7b2438d3 1210 if verbose:
switches 0:5c4d7b2438d3 1211 print _stdout
switches 0:5c4d7b2438d3 1212 print _stderr
switches 0:5c4d7b2438d3 1213
switches 0:5c4d7b2438d3 1214 # =========================================================================
switches 0:5c4d7b2438d3 1215
switches 0:5c4d7b2438d3 1216 # MBED
switches 0:5c4d7b2438d3 1217 toolchain.info("Static analysis for %s (%s, %s)" %
switches 0:5c4d7b2438d3 1218 ('MBED', target.name, toolchain_name))
switches 0:5c4d7b2438d3 1219
switches 0:5c4d7b2438d3 1220 # Common Headers
switches 0:5c4d7b2438d3 1221 toolchain.copy_files([MBED_HEADER], MBED_LIBRARIES)
switches 0:5c4d7b2438d3 1222 toolchain.copy_files(toolchain.scan_resources(MBED_DRIVERS).headers,
switches 0:5c4d7b2438d3 1223 MBED_LIBRARIES)
switches 0:5c4d7b2438d3 1224 toolchain.copy_files(toolchain.scan_resources(MBED_PLATFORM).headers,
switches 0:5c4d7b2438d3 1225 MBED_LIBRARIES)
switches 0:5c4d7b2438d3 1226 toolchain.copy_files(toolchain.scan_resources(MBED_HAL).headers,
switches 0:5c4d7b2438d3 1227 MBED_LIBRARIES)
switches 0:5c4d7b2438d3 1228
switches 0:5c4d7b2438d3 1229 # Target specific sources
switches 0:5c4d7b2438d3 1230 hal_src = join(MBED_TARGETS_PATH, "hal")
switches 0:5c4d7b2438d3 1231 hal_implementation = toolchain.scan_resources(hal_src)
switches 0:5c4d7b2438d3 1232
switches 0:5c4d7b2438d3 1233 # Copy files before analysis
switches 0:5c4d7b2438d3 1234 toolchain.copy_files(hal_implementation.headers +
switches 0:5c4d7b2438d3 1235 hal_implementation.hex_files, build_target,
switches 0:5c4d7b2438d3 1236 resources=hal_implementation)
switches 0:5c4d7b2438d3 1237 incdirs = toolchain.scan_resources(build_target)
switches 0:5c4d7b2438d3 1238
switches 0:5c4d7b2438d3 1239 target_includes = ["-I%s" % i for i in incdirs.inc_dirs]
switches 0:5c4d7b2438d3 1240 target_includes.append("-I%s"% str(build_target))
switches 0:5c4d7b2438d3 1241 target_includes.append("-I%s"% str(hal_src))
switches 0:5c4d7b2438d3 1242 target_c_sources = " ".join(incdirs.c_sources)
switches 0:5c4d7b2438d3 1243 target_cpp_sources = " ".join(incdirs.cpp_sources)
switches 0:5c4d7b2438d3 1244 target_macros = ["-D%s"% s for s in
switches 0:5c4d7b2438d3 1245 toolchain.get_symbols() + toolchain.macros]
switches 0:5c4d7b2438d3 1246
switches 0:5c4d7b2438d3 1247 # Common Sources
switches 0:5c4d7b2438d3 1248 mbed_resources = toolchain.scan_resources(MBED_COMMON)
switches 0:5c4d7b2438d3 1249
switches 0:5c4d7b2438d3 1250 # Gather include paths, c, cpp sources and macros to transfer to cppcheck
switches 0:5c4d7b2438d3 1251 # command line
switches 0:5c4d7b2438d3 1252 mbed_includes = ["-I%s" % i for i in mbed_resources.inc_dirs]
switches 0:5c4d7b2438d3 1253 mbed_includes.append("-I%s"% str(build_target))
switches 0:5c4d7b2438d3 1254 mbed_includes.append("-I%s"% str(MBED_DRIVERS))
switches 0:5c4d7b2438d3 1255 mbed_includes.append("-I%s"% str(MBED_PLATFORM))
switches 0:5c4d7b2438d3 1256 mbed_includes.append("-I%s"% str(MBED_HAL))
switches 0:5c4d7b2438d3 1257 mbed_c_sources = " ".join(mbed_resources.c_sources)
switches 0:5c4d7b2438d3 1258 mbed_cpp_sources = " ".join(mbed_resources.cpp_sources)
switches 0:5c4d7b2438d3 1259
switches 0:5c4d7b2438d3 1260 target_includes = [inc.strip() for inc in target_includes]
switches 0:5c4d7b2438d3 1261 mbed_includes = [inc.strip() for inc in mbed_includes]
switches 0:5c4d7b2438d3 1262 target_macros = [mac.strip() for mac in target_macros]
switches 0:5c4d7b2438d3 1263
switches 0:5c4d7b2438d3 1264 check_cmd = cppcheck_cmd
switches 0:5c4d7b2438d3 1265 check_cmd += cppcheck_msg_format
switches 0:5c4d7b2438d3 1266 check_cmd += target_includes
switches 0:5c4d7b2438d3 1267 check_cmd += mbed_includes
switches 0:5c4d7b2438d3 1268 check_cmd += target_macros
switches 0:5c4d7b2438d3 1269
switches 0:5c4d7b2438d3 1270 # We need to pass some parames via file to avoid "command line too long in
switches 0:5c4d7b2438d3 1271 # some OSs"
switches 0:5c4d7b2438d3 1272 tmp_file = tempfile.NamedTemporaryFile(delete=False)
switches 0:5c4d7b2438d3 1273 tmp_file.writelines(line + '\n' for line in target_c_sources.split())
switches 0:5c4d7b2438d3 1274 tmp_file.writelines(line + '\n' for line in target_cpp_sources.split())
switches 0:5c4d7b2438d3 1275 tmp_file.writelines(line + '\n' for line in mbed_c_sources.split())
switches 0:5c4d7b2438d3 1276 tmp_file.writelines(line + '\n' for line in mbed_cpp_sources.split())
switches 0:5c4d7b2438d3 1277 tmp_file.close()
switches 0:5c4d7b2438d3 1278 check_cmd += ["--file-list=%s"% tmp_file.name]
switches 0:5c4d7b2438d3 1279
switches 0:5c4d7b2438d3 1280 _stdout, _stderr, _ = run_cmd_ext(check_cmd)
switches 0:5c4d7b2438d3 1281 if verbose:
switches 0:5c4d7b2438d3 1282 print _stdout
switches 0:5c4d7b2438d3 1283 print _stderr
switches 0:5c4d7b2438d3 1284
switches 0:5c4d7b2438d3 1285
switches 0:5c4d7b2438d3 1286 def static_analysis_scan_lib(lib_id, target, toolchain, cppcheck_cmd,
switches 0:5c4d7b2438d3 1287 cppcheck_msg_format, verbose=False,
switches 0:5c4d7b2438d3 1288 clean=False, macros=None, notify=None, jobs=1,
switches 0:5c4d7b2438d3 1289 extra_verbose=False, build_profile=None):
switches 0:5c4d7b2438d3 1290 """Perform static analysis on a library as if it were to be compiled for a
switches 0:5c4d7b2438d3 1291 particular target and toolchain combination
switches 0:5c4d7b2438d3 1292 """
switches 0:5c4d7b2438d3 1293 lib = Library(lib_id)
switches 0:5c4d7b2438d3 1294 if lib.is_supported(target, toolchain):
switches 0:5c4d7b2438d3 1295 static_analysis_scan_library(
switches 0:5c4d7b2438d3 1296 lib.source_dir, lib.build_dir, target, toolchain, cppcheck_cmd,
switches 0:5c4d7b2438d3 1297 cppcheck_msg_format, lib.dependencies, verbose=verbose,
switches 0:5c4d7b2438d3 1298 clean=clean, macros=macros, notify=notify, jobs=jobs,
switches 0:5c4d7b2438d3 1299 extra_verbose=extra_verbose, build_profile=build_profile)
switches 0:5c4d7b2438d3 1300 else:
switches 0:5c4d7b2438d3 1301 print('Library "%s" is not yet supported on target %s with toolchain %s'
switches 0:5c4d7b2438d3 1302 % (lib_id, target.name, toolchain))
switches 0:5c4d7b2438d3 1303
switches 0:5c4d7b2438d3 1304
switches 0:5c4d7b2438d3 1305 def static_analysis_scan_library(src_paths, build_path, target, toolchain_name,
switches 0:5c4d7b2438d3 1306 cppcheck_cmd, cppcheck_msg_format,
switches 0:5c4d7b2438d3 1307 dependencies_paths=None,
switches 0:5c4d7b2438d3 1308 name=None, clean=False, notify=None,
switches 0:5c4d7b2438d3 1309 verbose=False, macros=None, jobs=1,
switches 0:5c4d7b2438d3 1310 extra_verbose=False, build_profile=None):
switches 0:5c4d7b2438d3 1311 """ Function scans library for statically detectable defects
switches 0:5c4d7b2438d3 1312
switches 0:5c4d7b2438d3 1313 Positional arguments:
switches 0:5c4d7b2438d3 1314 src_paths - the list of library paths to scan
switches 0:5c4d7b2438d3 1315 build_path - the location directory of result files
switches 0:5c4d7b2438d3 1316 target - the target to fake the build for
switches 0:5c4d7b2438d3 1317 toolchain_name - pretend you would compile with this toolchain
switches 0:5c4d7b2438d3 1318 cppcheck_cmd - the command used to do static analysis
switches 0:5c4d7b2438d3 1319 cppcheck_msg_format - the format of the check messages
switches 0:5c4d7b2438d3 1320
switches 0:5c4d7b2438d3 1321 Keyword arguments:
switches 0:5c4d7b2438d3 1322 dependencies_paths - the paths to sources that this library depends on
switches 0:5c4d7b2438d3 1323 name - the name of this library
switches 0:5c4d7b2438d3 1324 clean - start from a clean slate
switches 0:5c4d7b2438d3 1325 notify - the notification event handling function
switches 0:5c4d7b2438d3 1326 verbose - more printing!
switches 0:5c4d7b2438d3 1327 macros - extra macros to compile with
switches 0:5c4d7b2438d3 1328 jobs - number of commands to run at once
switches 0:5c4d7b2438d3 1329 extra_verbose - even moar printing
switches 0:5c4d7b2438d3 1330 build_profile - a dict of flags that will be passed to the compiler
switches 0:5c4d7b2438d3 1331 """
switches 0:5c4d7b2438d3 1332 if type(src_paths) != ListType:
switches 0:5c4d7b2438d3 1333 src_paths = [src_paths]
switches 0:5c4d7b2438d3 1334
switches 0:5c4d7b2438d3 1335 for src_path in src_paths:
switches 0:5c4d7b2438d3 1336 if not exists(src_path):
switches 0:5c4d7b2438d3 1337 raise Exception("The library source folder does not exist: %s",
switches 0:5c4d7b2438d3 1338 src_path)
switches 0:5c4d7b2438d3 1339
switches 0:5c4d7b2438d3 1340 # Toolchain instance
switches 0:5c4d7b2438d3 1341 toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, macros=macros,
switches 0:5c4d7b2438d3 1342 notify=notify,
switches 0:5c4d7b2438d3 1343 extra_verbose=extra_verbose,
switches 0:5c4d7b2438d3 1344 build_profile=build_profile)
switches 0:5c4d7b2438d3 1345 toolchain.VERBOSE = verbose
switches 0:5c4d7b2438d3 1346 toolchain.jobs = jobs
switches 0:5c4d7b2438d3 1347
switches 0:5c4d7b2438d3 1348 # The first path will give the name to the library
switches 0:5c4d7b2438d3 1349 name = basename(src_paths[0])
switches 0:5c4d7b2438d3 1350 toolchain.info("Static analysis for library %s (%s, %s)" %
switches 0:5c4d7b2438d3 1351 (name.upper(), target.name, toolchain_name))
switches 0:5c4d7b2438d3 1352
switches 0:5c4d7b2438d3 1353 # Scan Resources
switches 0:5c4d7b2438d3 1354 resources = []
switches 0:5c4d7b2438d3 1355 for src_path in src_paths:
switches 0:5c4d7b2438d3 1356 resources.append(toolchain.scan_resources(src_path))
switches 0:5c4d7b2438d3 1357
switches 0:5c4d7b2438d3 1358 # Dependencies Include Paths
switches 0:5c4d7b2438d3 1359 dependencies_include_dir = []
switches 0:5c4d7b2438d3 1360 if dependencies_paths is not None:
switches 0:5c4d7b2438d3 1361 for path in dependencies_paths:
switches 0:5c4d7b2438d3 1362 lib_resources = toolchain.scan_resources(path)
switches 0:5c4d7b2438d3 1363 dependencies_include_dir.extend(lib_resources.inc_dirs)
switches 0:5c4d7b2438d3 1364
switches 0:5c4d7b2438d3 1365 # Create the desired build directory structure
switches 0:5c4d7b2438d3 1366 bin_path = join(build_path, toolchain.obj_path)
switches 0:5c4d7b2438d3 1367 mkdir(bin_path)
switches 0:5c4d7b2438d3 1368 tmp_path = join(build_path, '.temp', toolchain.obj_path)
switches 0:5c4d7b2438d3 1369 mkdir(tmp_path)
switches 0:5c4d7b2438d3 1370
switches 0:5c4d7b2438d3 1371 # Gather include paths, c, cpp sources and macros to transfer to cppcheck
switches 0:5c4d7b2438d3 1372 # command line
switches 0:5c4d7b2438d3 1373 includes = ["-I%s" % i for i in dependencies_include_dir + src_paths]
switches 0:5c4d7b2438d3 1374 c_sources = " "
switches 0:5c4d7b2438d3 1375 cpp_sources = " "
switches 0:5c4d7b2438d3 1376 macros = ['-D%s' % s for s in toolchain.get_symbols() + toolchain.macros]
switches 0:5c4d7b2438d3 1377
switches 0:5c4d7b2438d3 1378 # Copy Headers
switches 0:5c4d7b2438d3 1379 for resource in resources:
switches 0:5c4d7b2438d3 1380 toolchain.copy_files(resource.headers, build_path, resources=resource)
switches 0:5c4d7b2438d3 1381 includes += ["-I%s" % i for i in resource.inc_dirs]
switches 0:5c4d7b2438d3 1382 c_sources += " ".join(resource.c_sources) + " "
switches 0:5c4d7b2438d3 1383 cpp_sources += " ".join(resource.cpp_sources) + " "
switches 0:5c4d7b2438d3 1384
switches 0:5c4d7b2438d3 1385 dependencies_include_dir.extend(
switches 0:5c4d7b2438d3 1386 toolchain.scan_resources(build_path).inc_dirs)
switches 0:5c4d7b2438d3 1387
switches 0:5c4d7b2438d3 1388 includes = [inc.strip() for inc in includes]
switches 0:5c4d7b2438d3 1389 macros = [mac.strip() for mac in macros]
switches 0:5c4d7b2438d3 1390
switches 0:5c4d7b2438d3 1391 check_cmd = cppcheck_cmd
switches 0:5c4d7b2438d3 1392 check_cmd += cppcheck_msg_format
switches 0:5c4d7b2438d3 1393 check_cmd += includes
switches 0:5c4d7b2438d3 1394 check_cmd += macros
switches 0:5c4d7b2438d3 1395
switches 0:5c4d7b2438d3 1396 # We need to pass some parameters via file to avoid "command line too long
switches 0:5c4d7b2438d3 1397 # in some OSs". A temporary file is created to store e.g. cppcheck list of
switches 0:5c4d7b2438d3 1398 # files for command line
switches 0:5c4d7b2438d3 1399 tmp_file = tempfile.NamedTemporaryFile(delete=False)
switches 0:5c4d7b2438d3 1400 tmp_file.writelines(line + '\n' for line in c_sources.split())
switches 0:5c4d7b2438d3 1401 tmp_file.writelines(line + '\n' for line in cpp_sources.split())
switches 0:5c4d7b2438d3 1402 tmp_file.close()
switches 0:5c4d7b2438d3 1403 check_cmd += ["--file-list=%s"% tmp_file.name]
switches 0:5c4d7b2438d3 1404
switches 0:5c4d7b2438d3 1405 # This will allow us to grab result from both stdio and stderr outputs (so
switches 0:5c4d7b2438d3 1406 # we can show them) We assume static code analysis tool is outputting
switches 0:5c4d7b2438d3 1407 # defects on STDERR
switches 0:5c4d7b2438d3 1408 _stdout, _stderr, _ = run_cmd_ext(check_cmd)
switches 0:5c4d7b2438d3 1409 if verbose:
switches 0:5c4d7b2438d3 1410 print _stdout
switches 0:5c4d7b2438d3 1411 print _stderr
switches 0:5c4d7b2438d3 1412
switches 0:5c4d7b2438d3 1413
switches 0:5c4d7b2438d3 1414 def print_build_results(result_list, build_name):
switches 0:5c4d7b2438d3 1415 """ Generate result string for build results
switches 0:5c4d7b2438d3 1416
switches 0:5c4d7b2438d3 1417 Positional arguments:
switches 0:5c4d7b2438d3 1418 result_list - the list of results to print
switches 0:5c4d7b2438d3 1419 build_name - the name of the build we are printing result for
switches 0:5c4d7b2438d3 1420 """
switches 0:5c4d7b2438d3 1421 result = ""
switches 0:5c4d7b2438d3 1422 if len(result_list) > 0:
switches 0:5c4d7b2438d3 1423 result += build_name + "\n"
switches 0:5c4d7b2438d3 1424 result += "\n".join([" * %s" % f for f in result_list])
switches 0:5c4d7b2438d3 1425 result += "\n"
switches 0:5c4d7b2438d3 1426 return result
switches 0:5c4d7b2438d3 1427
switches 0:5c4d7b2438d3 1428 def print_build_memory_usage(report):
switches 0:5c4d7b2438d3 1429 """ Generate result table with memory usage values for build results
switches 0:5c4d7b2438d3 1430 Aggregates (puts together) reports obtained from self.get_memory_summary()
switches 0:5c4d7b2438d3 1431
switches 0:5c4d7b2438d3 1432 Positional arguments:
switches 0:5c4d7b2438d3 1433 report - Report generated during build procedure.
switches 0:5c4d7b2438d3 1434 """
switches 0:5c4d7b2438d3 1435 from prettytable import PrettyTable
switches 0:5c4d7b2438d3 1436 columns_text = ['name', 'target', 'toolchain']
switches 0:5c4d7b2438d3 1437 columns_int = ['static_ram', 'stack', 'heap', 'total_ram', 'total_flash']
switches 0:5c4d7b2438d3 1438 table = PrettyTable(columns_text + columns_int)
switches 0:5c4d7b2438d3 1439
switches 0:5c4d7b2438d3 1440 for col in columns_text:
switches 0:5c4d7b2438d3 1441 table.align[col] = 'l'
switches 0:5c4d7b2438d3 1442
switches 0:5c4d7b2438d3 1443 for col in columns_int:
switches 0:5c4d7b2438d3 1444 table.align[col] = 'r'
switches 0:5c4d7b2438d3 1445
switches 0:5c4d7b2438d3 1446 for target in report:
switches 0:5c4d7b2438d3 1447 for toolchain in report[target]:
switches 0:5c4d7b2438d3 1448 for name in report[target][toolchain]:
switches 0:5c4d7b2438d3 1449 for dlist in report[target][toolchain][name]:
switches 0:5c4d7b2438d3 1450 for dlistelem in dlist:
switches 0:5c4d7b2438d3 1451 # Get 'memory_usage' record and build table with
switches 0:5c4d7b2438d3 1452 # statistics
switches 0:5c4d7b2438d3 1453 record = dlist[dlistelem]
switches 0:5c4d7b2438d3 1454 if 'memory_usage' in record and record['memory_usage']:
switches 0:5c4d7b2438d3 1455 # Note that summary should be in the last record of
switches 0:5c4d7b2438d3 1456 # 'memory_usage' section. This is why we are
switches 0:5c4d7b2438d3 1457 # grabbing last "[-1]" record.
switches 0:5c4d7b2438d3 1458 row = [
switches 0:5c4d7b2438d3 1459 record['description'],
switches 0:5c4d7b2438d3 1460 record['target_name'],
switches 0:5c4d7b2438d3 1461 record['toolchain_name'],
switches 0:5c4d7b2438d3 1462 record['memory_usage'][-1]['summary'][
switches 0:5c4d7b2438d3 1463 'static_ram'],
switches 0:5c4d7b2438d3 1464 record['memory_usage'][-1]['summary']['stack'],
switches 0:5c4d7b2438d3 1465 record['memory_usage'][-1]['summary']['heap'],
switches 0:5c4d7b2438d3 1466 record['memory_usage'][-1]['summary'][
switches 0:5c4d7b2438d3 1467 'total_ram'],
switches 0:5c4d7b2438d3 1468 record['memory_usage'][-1]['summary'][
switches 0:5c4d7b2438d3 1469 'total_flash'],
switches 0:5c4d7b2438d3 1470 ]
switches 0:5c4d7b2438d3 1471 table.add_row(row)
switches 0:5c4d7b2438d3 1472
switches 0:5c4d7b2438d3 1473 result = "Memory map breakdown for built projects (values in Bytes):\n"
switches 0:5c4d7b2438d3 1474 result += table.get_string(sortby='name')
switches 0:5c4d7b2438d3 1475 return result
switches 0:5c4d7b2438d3 1476
switches 0:5c4d7b2438d3 1477 def write_build_report(build_report, template_filename, filename):
switches 0:5c4d7b2438d3 1478 """Write a build report to disk using a template file
switches 0:5c4d7b2438d3 1479
switches 0:5c4d7b2438d3 1480 Positional arguments:
switches 0:5c4d7b2438d3 1481 build_report - a report generated by the build system
switches 0:5c4d7b2438d3 1482 template_filename - a file that contains the template for the style of build
switches 0:5c4d7b2438d3 1483 report
switches 0:5c4d7b2438d3 1484 filename - the location on disk to write the file to
switches 0:5c4d7b2438d3 1485 """
switches 0:5c4d7b2438d3 1486 build_report_failing = []
switches 0:5c4d7b2438d3 1487 build_report_passing = []
switches 0:5c4d7b2438d3 1488
switches 0:5c4d7b2438d3 1489 for report in build_report:
switches 0:5c4d7b2438d3 1490 if len(report["failing"]) > 0:
switches 0:5c4d7b2438d3 1491 build_report_failing.append(report)
switches 0:5c4d7b2438d3 1492 else:
switches 0:5c4d7b2438d3 1493 build_report_passing.append(report)
switches 0:5c4d7b2438d3 1494
switches 0:5c4d7b2438d3 1495 env = Environment(extensions=['jinja2.ext.with_'])
switches 0:5c4d7b2438d3 1496 env.loader = FileSystemLoader('ci_templates')
switches 0:5c4d7b2438d3 1497 template = env.get_template(template_filename)
switches 0:5c4d7b2438d3 1498
switches 0:5c4d7b2438d3 1499 with open(filename, 'w+') as placeholder:
switches 0:5c4d7b2438d3 1500 placeholder.write(template.render(
switches 0:5c4d7b2438d3 1501 failing_builds=build_report_failing,
switches 0:5c4d7b2438d3 1502 passing_builds=build_report_passing))