Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1 """
switches 0:0e018d759a2a 2 mbed SDK
switches 0:0e018d759a2a 3 Copyright (c) 2011-2013 ARM Limited
switches 0:0e018d759a2a 4
switches 0:0e018d759a2a 5 Licensed under the Apache License, Version 2.0 (the "License");
switches 0:0e018d759a2a 6 you may not use this file except in compliance with the License.
switches 0:0e018d759a2a 7 You may obtain a copy of the License at
switches 0:0e018d759a2a 8
switches 0:0e018d759a2a 9 http://www.apache.org/licenses/LICENSE-2.0
switches 0:0e018d759a2a 10
switches 0:0e018d759a2a 11 Unless required by applicable law or agreed to in writing, software
switches 0:0e018d759a2a 12 distributed under the License is distributed on an "AS IS" BASIS,
switches 0:0e018d759a2a 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:0e018d759a2a 14 See the License for the specific language governing permissions and
switches 0:0e018d759a2a 15 limitations under the License.
switches 0:0e018d759a2a 16 """
switches 0:0e018d759a2a 17
switches 0:0e018d759a2a 18 import re
switches 0:0e018d759a2a 19 import sys
switches 0:0e018d759a2a 20 from os import stat, walk, getcwd, sep, remove
switches 0:0e018d759a2a 21 from copy import copy
switches 0:0e018d759a2a 22 from time import time, sleep
switches 0:0e018d759a2a 23 from types import ListType
switches 0:0e018d759a2a 24 from shutil import copyfile
switches 0:0e018d759a2a 25 from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath, isfile, isdir
switches 0:0e018d759a2a 26 from inspect import getmro
switches 0:0e018d759a2a 27 from copy import deepcopy
switches 0:0e018d759a2a 28 from tools.config import Config
switches 0:0e018d759a2a 29 from abc import ABCMeta, abstractmethod
switches 0:0e018d759a2a 30 from distutils.spawn import find_executable
switches 0:0e018d759a2a 31
switches 0:0e018d759a2a 32 from multiprocessing import Pool, cpu_count
switches 0:0e018d759a2a 33 from tools.utils import run_cmd, mkdir, rel_path, ToolException, NotSupportedException, split_path, compile_worker
switches 0:0e018d759a2a 34 from tools.settings import MBED_ORG_USER
switches 0:0e018d759a2a 35 import tools.hooks as hooks
switches 0:0e018d759a2a 36 from tools.memap import MemapParser
switches 0:0e018d759a2a 37 from hashlib import md5
switches 0:0e018d759a2a 38 import fnmatch
switches 0:0e018d759a2a 39
switches 0:0e018d759a2a 40
switches 0:0e018d759a2a 41 #Disables multiprocessing if set to higher number than the host machine CPUs
switches 0:0e018d759a2a 42 CPU_COUNT_MIN = 1
switches 0:0e018d759a2a 43 CPU_COEF = 1
switches 0:0e018d759a2a 44
switches 0:0e018d759a2a 45 class Resources:
switches 0:0e018d759a2a 46 def __init__(self, base_path=None):
switches 0:0e018d759a2a 47 self.base_path = base_path
switches 0:0e018d759a2a 48
switches 0:0e018d759a2a 49 self.file_basepath = {}
switches 0:0e018d759a2a 50
switches 0:0e018d759a2a 51 self.inc_dirs = []
switches 0:0e018d759a2a 52 self.headers = []
switches 0:0e018d759a2a 53
switches 0:0e018d759a2a 54 self.s_sources = []
switches 0:0e018d759a2a 55 self.c_sources = []
switches 0:0e018d759a2a 56 self.cpp_sources = []
switches 0:0e018d759a2a 57
switches 0:0e018d759a2a 58 self.lib_dirs = set([])
switches 0:0e018d759a2a 59 self.objects = []
switches 0:0e018d759a2a 60 self.libraries = []
switches 0:0e018d759a2a 61
switches 0:0e018d759a2a 62 # mbed special files
switches 0:0e018d759a2a 63 self.lib_builds = []
switches 0:0e018d759a2a 64 self.lib_refs = []
switches 0:0e018d759a2a 65
switches 0:0e018d759a2a 66 self.repo_dirs = []
switches 0:0e018d759a2a 67 self.repo_files = []
switches 0:0e018d759a2a 68
switches 0:0e018d759a2a 69 self.linker_script = None
switches 0:0e018d759a2a 70
switches 0:0e018d759a2a 71 # Other files
switches 0:0e018d759a2a 72 self.hex_files = []
switches 0:0e018d759a2a 73 self.bin_files = []
switches 0:0e018d759a2a 74 self.json_files = []
switches 0:0e018d759a2a 75
switches 0:0e018d759a2a 76 # Features
switches 0:0e018d759a2a 77 self.features = {}
switches 0:0e018d759a2a 78
switches 0:0e018d759a2a 79 def __add__(self, resources):
switches 0:0e018d759a2a 80 if resources is None:
switches 0:0e018d759a2a 81 return self
switches 0:0e018d759a2a 82 else:
switches 0:0e018d759a2a 83 return self.add(resources)
switches 0:0e018d759a2a 84
switches 0:0e018d759a2a 85 def __radd__(self, resources):
switches 0:0e018d759a2a 86 if resources is None:
switches 0:0e018d759a2a 87 return self
switches 0:0e018d759a2a 88 else:
switches 0:0e018d759a2a 89 return self.add(resources)
switches 0:0e018d759a2a 90
switches 0:0e018d759a2a 91 def add(self, resources):
switches 0:0e018d759a2a 92 for f,p in resources.file_basepath.items():
switches 0:0e018d759a2a 93 self.file_basepath[f] = p
switches 0:0e018d759a2a 94
switches 0:0e018d759a2a 95 self.inc_dirs += resources.inc_dirs
switches 0:0e018d759a2a 96 self.headers += resources.headers
switches 0:0e018d759a2a 97
switches 0:0e018d759a2a 98 self.s_sources += resources.s_sources
switches 0:0e018d759a2a 99 self.c_sources += resources.c_sources
switches 0:0e018d759a2a 100 self.cpp_sources += resources.cpp_sources
switches 0:0e018d759a2a 101
switches 0:0e018d759a2a 102 self.lib_dirs |= resources.lib_dirs
switches 0:0e018d759a2a 103 self.objects += resources.objects
switches 0:0e018d759a2a 104 self.libraries += resources.libraries
switches 0:0e018d759a2a 105
switches 0:0e018d759a2a 106 self.lib_builds += resources.lib_builds
switches 0:0e018d759a2a 107 self.lib_refs += resources.lib_refs
switches 0:0e018d759a2a 108
switches 0:0e018d759a2a 109 self.repo_dirs += resources.repo_dirs
switches 0:0e018d759a2a 110 self.repo_files += resources.repo_files
switches 0:0e018d759a2a 111
switches 0:0e018d759a2a 112 if resources.linker_script is not None:
switches 0:0e018d759a2a 113 self.linker_script = resources.linker_script
switches 0:0e018d759a2a 114
switches 0:0e018d759a2a 115 self.hex_files += resources.hex_files
switches 0:0e018d759a2a 116 self.bin_files += resources.bin_files
switches 0:0e018d759a2a 117 self.json_files += resources.json_files
switches 0:0e018d759a2a 118
switches 0:0e018d759a2a 119 self.features.update(resources.features)
switches 0:0e018d759a2a 120
switches 0:0e018d759a2a 121 return self
switches 0:0e018d759a2a 122
switches 0:0e018d759a2a 123 def _collect_duplicates(self, dupe_dict, dupe_headers):
switches 0:0e018d759a2a 124 for filename in self.s_sources + self.c_sources + self.cpp_sources:
switches 0:0e018d759a2a 125 objname, _ = splitext(basename(filename))
switches 0:0e018d759a2a 126 dupe_dict.setdefault(objname, set())
switches 0:0e018d759a2a 127 dupe_dict[objname] |= set([filename])
switches 0:0e018d759a2a 128 for filename in self.headers:
switches 0:0e018d759a2a 129 headername = basename(filename)
switches 0:0e018d759a2a 130 dupe_headers.setdefault(headername, set())
switches 0:0e018d759a2a 131 dupe_headers[headername] |= set([headername])
switches 0:0e018d759a2a 132 for res in self.features.values():
switches 0:0e018d759a2a 133 res._collect_duplicates(dupe_dict, dupe_headers)
switches 0:0e018d759a2a 134 return dupe_dict, dupe_headers
switches 0:0e018d759a2a 135
switches 0:0e018d759a2a 136 def detect_duplicates(self, toolchain):
switches 0:0e018d759a2a 137 """Detect all potential ambiguities in filenames and report them with
switches 0:0e018d759a2a 138 a toolchain notification
switches 0:0e018d759a2a 139
switches 0:0e018d759a2a 140 Positional Arguments:
switches 0:0e018d759a2a 141 toolchain - used for notifications
switches 0:0e018d759a2a 142 """
switches 0:0e018d759a2a 143 count = 0
switches 0:0e018d759a2a 144 dupe_dict, dupe_headers = self._collect_duplicates(dict(), dict())
switches 0:0e018d759a2a 145 for objname, filenames in dupe_dict.iteritems():
switches 0:0e018d759a2a 146 if len(filenames) > 1:
switches 0:0e018d759a2a 147 count+=1
switches 0:0e018d759a2a 148 toolchain.tool_error(
switches 0:0e018d759a2a 149 "Object file %s.o is not unique! It could be made from: %s"\
switches 0:0e018d759a2a 150 % (objname, " ".join(filenames)))
switches 0:0e018d759a2a 151 for headername, locations in dupe_headers.iteritems():
switches 0:0e018d759a2a 152 if len(locations) > 1:
switches 0:0e018d759a2a 153 count+=1
switches 0:0e018d759a2a 154 toolchain.tool_error(
switches 0:0e018d759a2a 155 "Header file %s is not unique! It could be: %s" %\
switches 0:0e018d759a2a 156 (headername, " ".join(locations)))
switches 0:0e018d759a2a 157 return count
switches 0:0e018d759a2a 158
switches 0:0e018d759a2a 159
switches 0:0e018d759a2a 160 def relative_to(self, base, dot=False):
switches 0:0e018d759a2a 161 for field in ['inc_dirs', 'headers', 's_sources', 'c_sources',
switches 0:0e018d759a2a 162 'cpp_sources', 'lib_dirs', 'objects', 'libraries',
switches 0:0e018d759a2a 163 'lib_builds', 'lib_refs', 'repo_dirs', 'repo_files',
switches 0:0e018d759a2a 164 'hex_files', 'bin_files', 'json_files']:
switches 0:0e018d759a2a 165 v = [rel_path(f, base, dot) for f in getattr(self, field)]
switches 0:0e018d759a2a 166 setattr(self, field, v)
switches 0:0e018d759a2a 167
switches 0:0e018d759a2a 168 self.features = {k: f.relative_to(base, dot) for k, f in self.features.iteritems() if f}
switches 0:0e018d759a2a 169
switches 0:0e018d759a2a 170 if self.linker_script is not None:
switches 0:0e018d759a2a 171 self.linker_script = rel_path(self.linker_script, base, dot)
switches 0:0e018d759a2a 172
switches 0:0e018d759a2a 173 def win_to_unix(self):
switches 0:0e018d759a2a 174 for field in ['inc_dirs', 'headers', 's_sources', 'c_sources',
switches 0:0e018d759a2a 175 'cpp_sources', 'lib_dirs', 'objects', 'libraries',
switches 0:0e018d759a2a 176 'lib_builds', 'lib_refs', 'repo_dirs', 'repo_files',
switches 0:0e018d759a2a 177 'hex_files', 'bin_files', 'json_files']:
switches 0:0e018d759a2a 178 v = [f.replace('\\', '/') for f in getattr(self, field)]
switches 0:0e018d759a2a 179 setattr(self, field, v)
switches 0:0e018d759a2a 180
switches 0:0e018d759a2a 181 self.features = {k: f.win_to_unix() for k, f in self.features.iteritems() if f}
switches 0:0e018d759a2a 182
switches 0:0e018d759a2a 183 if self.linker_script is not None:
switches 0:0e018d759a2a 184 self.linker_script = self.linker_script.replace('\\', '/')
switches 0:0e018d759a2a 185
switches 0:0e018d759a2a 186 def __str__(self):
switches 0:0e018d759a2a 187 s = []
switches 0:0e018d759a2a 188
switches 0:0e018d759a2a 189 for (label, resources) in (
switches 0:0e018d759a2a 190 ('Include Directories', self.inc_dirs),
switches 0:0e018d759a2a 191 ('Headers', self.headers),
switches 0:0e018d759a2a 192
switches 0:0e018d759a2a 193 ('Assembly sources', self.s_sources),
switches 0:0e018d759a2a 194 ('C sources', self.c_sources),
switches 0:0e018d759a2a 195 ('C++ sources', self.cpp_sources),
switches 0:0e018d759a2a 196
switches 0:0e018d759a2a 197 ('Library directories', self.lib_dirs),
switches 0:0e018d759a2a 198 ('Objects', self.objects),
switches 0:0e018d759a2a 199 ('Libraries', self.libraries),
switches 0:0e018d759a2a 200
switches 0:0e018d759a2a 201 ('Hex files', self.hex_files),
switches 0:0e018d759a2a 202 ('Bin files', self.bin_files),
switches 0:0e018d759a2a 203
switches 0:0e018d759a2a 204 ('Features', self.features),
switches 0:0e018d759a2a 205 ):
switches 0:0e018d759a2a 206 if resources:
switches 0:0e018d759a2a 207 s.append('%s:\n ' % label + '\n '.join(resources))
switches 0:0e018d759a2a 208
switches 0:0e018d759a2a 209 if self.linker_script:
switches 0:0e018d759a2a 210 s.append('Linker Script: ' + self.linker_script)
switches 0:0e018d759a2a 211
switches 0:0e018d759a2a 212 return '\n'.join(s)
switches 0:0e018d759a2a 213
switches 0:0e018d759a2a 214 # Support legacy build conventions: the original mbed build system did not have
switches 0:0e018d759a2a 215 # standard labels for the "TARGET_" and "TOOLCHAIN_" specific directories, but
switches 0:0e018d759a2a 216 # had the knowledge of a list of these directories to be ignored.
switches 0:0e018d759a2a 217 LEGACY_IGNORE_DIRS = set([
switches 0:0e018d759a2a 218 'LPC11U24', 'LPC1768', 'LPC2368', 'LPC4088', 'LPC812', 'KL25Z',
switches 0:0e018d759a2a 219 'ARM', 'uARM', 'IAR',
switches 0:0e018d759a2a 220 'GCC_ARM', 'GCC_CS', 'GCC_CR', 'GCC_CW', 'GCC_CW_EWL', 'GCC_CW_NEWLIB',
switches 0:0e018d759a2a 221 ])
switches 0:0e018d759a2a 222 LEGACY_TOOLCHAIN_NAMES = {
switches 0:0e018d759a2a 223 'ARM_STD':'ARM', 'ARM_MICRO': 'uARM',
switches 0:0e018d759a2a 224 'GCC_ARM': 'GCC_ARM', 'GCC_CR': 'GCC_CR',
switches 0:0e018d759a2a 225 'IAR': 'IAR',
switches 0:0e018d759a2a 226 }
switches 0:0e018d759a2a 227
switches 0:0e018d759a2a 228
switches 0:0e018d759a2a 229 class mbedToolchain:
switches 0:0e018d759a2a 230 # Verbose logging
switches 0:0e018d759a2a 231 VERBOSE = True
switches 0:0e018d759a2a 232
switches 0:0e018d759a2a 233 # Compile C files as CPP
switches 0:0e018d759a2a 234 COMPILE_C_AS_CPP = False
switches 0:0e018d759a2a 235
switches 0:0e018d759a2a 236 # Response files for compiling, includes, linking and archiving.
switches 0:0e018d759a2a 237 # Not needed on posix systems where the typical arg limit is 2 megabytes
switches 0:0e018d759a2a 238 RESPONSE_FILES = True
switches 0:0e018d759a2a 239
switches 0:0e018d759a2a 240 CORTEX_SYMBOLS = {
switches 0:0e018d759a2a 241 "Cortex-M0" : ["__CORTEX_M0", "ARM_MATH_CM0", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 242 "Cortex-M0+": ["__CORTEX_M0PLUS", "ARM_MATH_CM0PLUS", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 243 "Cortex-M1" : ["__CORTEX_M3", "ARM_MATH_CM1", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 244 "Cortex-M3" : ["__CORTEX_M3", "ARM_MATH_CM3", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 245 "Cortex-M4" : ["__CORTEX_M4", "ARM_MATH_CM4", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 246 "Cortex-M4F" : ["__CORTEX_M4", "ARM_MATH_CM4", "__FPU_PRESENT=1", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 247 "Cortex-M7" : ["__CORTEX_M7", "ARM_MATH_CM7", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 248 "Cortex-M7F" : ["__CORTEX_M7", "ARM_MATH_CM7", "__FPU_PRESENT=1", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 249 "Cortex-M7FD" : ["__CORTEX_M7", "ARM_MATH_CM7", "__FPU_PRESENT=1", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
switches 0:0e018d759a2a 250 "Cortex-A9" : ["__CORTEX_A9", "ARM_MATH_CA9", "__FPU_PRESENT", "__CMSIS_RTOS", "__EVAL", "__MBED_CMSIS_RTOS_CA9"],
switches 0:0e018d759a2a 251 }
switches 0:0e018d759a2a 252
switches 0:0e018d759a2a 253 MBED_CONFIG_FILE_NAME="mbed_config.h"
switches 0:0e018d759a2a 254
switches 0:0e018d759a2a 255 __metaclass__ = ABCMeta
switches 0:0e018d759a2a 256
switches 0:0e018d759a2a 257 profile_template = {'common':[], 'c':[], 'cxx':[], 'asm':[], 'ld':[]}
switches 0:0e018d759a2a 258
switches 0:0e018d759a2a 259 def __init__(self, target, notify=None, macros=None, silent=False, extra_verbose=False, build_profile=None):
switches 0:0e018d759a2a 260 self.target = target
switches 0:0e018d759a2a 261 self.name = self.__class__.__name__
switches 0:0e018d759a2a 262
switches 0:0e018d759a2a 263 # compile/assemble/link/binary hooks
switches 0:0e018d759a2a 264 self.hook = hooks.Hook(target, self)
switches 0:0e018d759a2a 265
switches 0:0e018d759a2a 266 # Toolchain flags
switches 0:0e018d759a2a 267 self.flags = deepcopy(build_profile or self.profile_template)
switches 0:0e018d759a2a 268
switches 0:0e018d759a2a 269 # User-defined macros
switches 0:0e018d759a2a 270 self.macros = macros or []
switches 0:0e018d759a2a 271
switches 0:0e018d759a2a 272 # Macros generated from toolchain and target rules/features
switches 0:0e018d759a2a 273 self.asm_symbols = None
switches 0:0e018d759a2a 274 self.cxx_symbols = None
switches 0:0e018d759a2a 275
switches 0:0e018d759a2a 276 # Labels generated from toolchain and target rules/features (used for selective build)
switches 0:0e018d759a2a 277 self.labels = None
switches 0:0e018d759a2a 278
switches 0:0e018d759a2a 279 # This will hold the initialized config object
switches 0:0e018d759a2a 280 self.config = None
switches 0:0e018d759a2a 281
switches 0:0e018d759a2a 282 # This will hold the configuration data (as returned by Config.get_config_data())
switches 0:0e018d759a2a 283 self.config_data = None
switches 0:0e018d759a2a 284
switches 0:0e018d759a2a 285 # This will hold the location of the configuration file or None if there's no configuration available
switches 0:0e018d759a2a 286 self.config_file = None
switches 0:0e018d759a2a 287
switches 0:0e018d759a2a 288 # Call guard for "get_config_data" (see the comments of get_config_data for details)
switches 0:0e018d759a2a 289 self.config_processed = False
switches 0:0e018d759a2a 290
switches 0:0e018d759a2a 291 # Non-incremental compile
switches 0:0e018d759a2a 292 self.build_all = False
switches 0:0e018d759a2a 293
switches 0:0e018d759a2a 294 # Build output dir
switches 0:0e018d759a2a 295 self.build_dir = None
switches 0:0e018d759a2a 296 self.timestamp = time()
switches 0:0e018d759a2a 297
switches 0:0e018d759a2a 298 # Output build naming based on target+toolchain combo (mbed 2.0 builds)
switches 0:0e018d759a2a 299 self.obj_path = join("TARGET_"+target.name, "TOOLCHAIN_"+self.name)
switches 0:0e018d759a2a 300
switches 0:0e018d759a2a 301 # Number of concurrent build jobs. 0 means auto (based on host system cores)
switches 0:0e018d759a2a 302 self.jobs = 0
switches 0:0e018d759a2a 303
switches 0:0e018d759a2a 304 # Ignore patterns from .mbedignore files
switches 0:0e018d759a2a 305 self.ignore_patterns = []
switches 0:0e018d759a2a 306
switches 0:0e018d759a2a 307 # Pre-mbed 2.0 ignore dirs
switches 0:0e018d759a2a 308 self.legacy_ignore_dirs = (LEGACY_IGNORE_DIRS | TOOLCHAINS) - set([target.name, LEGACY_TOOLCHAIN_NAMES[self.name]])
switches 0:0e018d759a2a 309
switches 0:0e018d759a2a 310 # Output notify function
switches 0:0e018d759a2a 311 # This function is passed all events, and expected to handle notification of the
switches 0:0e018d759a2a 312 # user, emit the events to a log, etc.
switches 0:0e018d759a2a 313 # The API for all notify methods passed into the notify parameter is as follows:
switches 0:0e018d759a2a 314 # def notify(Event, Silent)
switches 0:0e018d759a2a 315 # Where *Event* is a dict representing the toolchain event that was generated
switches 0:0e018d759a2a 316 # e.g.: a compile succeeded, or a warning was emitted by the compiler
switches 0:0e018d759a2a 317 # or an application was linked
switches 0:0e018d759a2a 318 # *Silent* is a boolean
switches 0:0e018d759a2a 319 if notify:
switches 0:0e018d759a2a 320 self.notify_fun = notify
switches 0:0e018d759a2a 321 elif extra_verbose:
switches 0:0e018d759a2a 322 self.notify_fun = self.print_notify_verbose
switches 0:0e018d759a2a 323 else:
switches 0:0e018d759a2a 324 self.notify_fun = self.print_notify
switches 0:0e018d759a2a 325
switches 0:0e018d759a2a 326 # Silent builds (no output)
switches 0:0e018d759a2a 327 self.silent = silent
switches 0:0e018d759a2a 328
switches 0:0e018d759a2a 329 # Print output buffer
switches 0:0e018d759a2a 330 self.output = str()
switches 0:0e018d759a2a 331 self.map_outputs = list() # Place to store memmap scan results in JSON like data structures
switches 0:0e018d759a2a 332
switches 0:0e018d759a2a 333 # uVisor spepcific rules
switches 0:0e018d759a2a 334 if 'UVISOR' in self.target.features and 'UVISOR_SUPPORTED' in self.target.extra_labels:
switches 0:0e018d759a2a 335 self.target.core = re.sub(r"F$", '', self.target.core)
switches 0:0e018d759a2a 336
switches 0:0e018d759a2a 337 # Stats cache is used to reduce the amount of IO requests to stat
switches 0:0e018d759a2a 338 # header files during dependency change. See need_update()
switches 0:0e018d759a2a 339 self.stat_cache = {}
switches 0:0e018d759a2a 340
switches 0:0e018d759a2a 341 # Used by the mbed Online Build System to build in chrooted environment
switches 0:0e018d759a2a 342 self.CHROOT = None
switches 0:0e018d759a2a 343
switches 0:0e018d759a2a 344 # Call post __init__() hooks before the ARM/GCC_ARM/IAR toolchain __init__() takes over
switches 0:0e018d759a2a 345 self.init()
switches 0:0e018d759a2a 346
switches 0:0e018d759a2a 347 # Used for post __init__() hooks
switches 0:0e018d759a2a 348 # THIS METHOD IS BEING OVERRIDDEN BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 349 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 350 def init(self):
switches 0:0e018d759a2a 351 return True
switches 0:0e018d759a2a 352
switches 0:0e018d759a2a 353 def get_output(self):
switches 0:0e018d759a2a 354 return self.output
switches 0:0e018d759a2a 355
switches 0:0e018d759a2a 356 def print_notify(self, event, silent=False):
switches 0:0e018d759a2a 357 """ Default command line notification
switches 0:0e018d759a2a 358 """
switches 0:0e018d759a2a 359 msg = None
switches 0:0e018d759a2a 360
switches 0:0e018d759a2a 361 if not self.VERBOSE and event['type'] == 'tool_error':
switches 0:0e018d759a2a 362 msg = event['message']
switches 0:0e018d759a2a 363
switches 0:0e018d759a2a 364 elif event['type'] in ['info', 'debug']:
switches 0:0e018d759a2a 365 msg = event['message']
switches 0:0e018d759a2a 366
switches 0:0e018d759a2a 367 elif event['type'] == 'cc':
switches 0:0e018d759a2a 368 event['severity'] = event['severity'].title()
switches 0:0e018d759a2a 369 event['file'] = basename(event['file'])
switches 0:0e018d759a2a 370 msg = '[%(severity)s] %(file)s@%(line)s,%(col)s: %(message)s' % event
switches 0:0e018d759a2a 371
switches 0:0e018d759a2a 372 elif event['type'] == 'progress':
switches 0:0e018d759a2a 373 if 'percent' in event:
switches 0:0e018d759a2a 374 msg = '{} [{:>5.1f}%]: {}'.format(event['action'].title(),
switches 0:0e018d759a2a 375 event['percent'],
switches 0:0e018d759a2a 376 basename(event['file']))
switches 0:0e018d759a2a 377 else:
switches 0:0e018d759a2a 378 msg = '{}: {}'.format(event['action'].title(),
switches 0:0e018d759a2a 379 basename(event['file']))
switches 0:0e018d759a2a 380
switches 0:0e018d759a2a 381 if msg:
switches 0:0e018d759a2a 382 if not silent:
switches 0:0e018d759a2a 383 print msg
switches 0:0e018d759a2a 384 self.output += msg + "\n"
switches 0:0e018d759a2a 385
switches 0:0e018d759a2a 386 def print_notify_verbose(self, event, silent=False):
switches 0:0e018d759a2a 387 """ Default command line notification with more verbose mode
switches 0:0e018d759a2a 388 """
switches 0:0e018d759a2a 389 if event['type'] in ['info', 'debug']:
switches 0:0e018d759a2a 390 self.print_notify(event, silent=silent) # standard handle
switches 0:0e018d759a2a 391
switches 0:0e018d759a2a 392 elif event['type'] == 'cc':
switches 0:0e018d759a2a 393 event['severity'] = event['severity'].title()
switches 0:0e018d759a2a 394 event['file'] = basename(event['file'])
switches 0:0e018d759a2a 395 event['mcu_name'] = "None"
switches 0:0e018d759a2a 396 event['target_name'] = event['target_name'].upper() if event['target_name'] else "Unknown"
switches 0:0e018d759a2a 397 event['toolchain_name'] = event['toolchain_name'].upper() if event['toolchain_name'] else "Unknown"
switches 0:0e018d759a2a 398 msg = '[%(severity)s] %(target_name)s::%(toolchain_name)s::%(file)s@%(line)s: %(message)s' % event
switches 0:0e018d759a2a 399 if not silent:
switches 0:0e018d759a2a 400 print msg
switches 0:0e018d759a2a 401 self.output += msg + "\n"
switches 0:0e018d759a2a 402
switches 0:0e018d759a2a 403 elif event['type'] == 'progress':
switches 0:0e018d759a2a 404 self.print_notify(event) # standard handle
switches 0:0e018d759a2a 405
switches 0:0e018d759a2a 406 # THIS METHOD IS BEING OVERRIDDEN BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 407 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 408 def notify(self, event):
switches 0:0e018d759a2a 409 """ Little closure for notify functions
switches 0:0e018d759a2a 410 """
switches 0:0e018d759a2a 411 event['toolchain'] = self
switches 0:0e018d759a2a 412 return self.notify_fun(event, self.silent)
switches 0:0e018d759a2a 413
switches 0:0e018d759a2a 414 def get_symbols(self, for_asm=False):
switches 0:0e018d759a2a 415 if for_asm:
switches 0:0e018d759a2a 416 if self.asm_symbols is None:
switches 0:0e018d759a2a 417 self.asm_symbols = []
switches 0:0e018d759a2a 418
switches 0:0e018d759a2a 419 # Cortex CPU symbols
switches 0:0e018d759a2a 420 if self.target.core in mbedToolchain.CORTEX_SYMBOLS:
switches 0:0e018d759a2a 421 self.asm_symbols.extend(mbedToolchain.CORTEX_SYMBOLS[self.target.core])
switches 0:0e018d759a2a 422
switches 0:0e018d759a2a 423 # Add target's symbols
switches 0:0e018d759a2a 424 self.asm_symbols += self.target.macros
switches 0:0e018d759a2a 425 # Add extra symbols passed via 'macros' parameter
switches 0:0e018d759a2a 426 self.asm_symbols += self.macros
switches 0:0e018d759a2a 427 return list(set(self.asm_symbols)) # Return only unique symbols
switches 0:0e018d759a2a 428 else:
switches 0:0e018d759a2a 429 if self.cxx_symbols is None:
switches 0:0e018d759a2a 430 # Target and Toolchain symbols
switches 0:0e018d759a2a 431 labels = self.get_labels()
switches 0:0e018d759a2a 432 self.cxx_symbols = ["TARGET_%s" % t for t in labels['TARGET']]
switches 0:0e018d759a2a 433 self.cxx_symbols.extend(["TOOLCHAIN_%s" % t for t in labels['TOOLCHAIN']])
switches 0:0e018d759a2a 434
switches 0:0e018d759a2a 435 # Cortex CPU symbols
switches 0:0e018d759a2a 436 if self.target.core in mbedToolchain.CORTEX_SYMBOLS:
switches 0:0e018d759a2a 437 self.cxx_symbols.extend(mbedToolchain.CORTEX_SYMBOLS[self.target.core])
switches 0:0e018d759a2a 438
switches 0:0e018d759a2a 439 # Symbols defined by the on-line build.system
switches 0:0e018d759a2a 440 self.cxx_symbols.extend(['MBED_BUILD_TIMESTAMP=%s' % self.timestamp, 'TARGET_LIKE_MBED', '__MBED__=1'])
switches 0:0e018d759a2a 441 if MBED_ORG_USER:
switches 0:0e018d759a2a 442 self.cxx_symbols.append('MBED_USERNAME=' + MBED_ORG_USER)
switches 0:0e018d759a2a 443
switches 0:0e018d759a2a 444 # Add target's symbols
switches 0:0e018d759a2a 445 self.cxx_symbols += self.target.macros
switches 0:0e018d759a2a 446 # Add target's hardware
switches 0:0e018d759a2a 447 self.cxx_symbols += ["DEVICE_" + data + "=1" for data in self.target.device_has]
switches 0:0e018d759a2a 448 # Add target's features
switches 0:0e018d759a2a 449 self.cxx_symbols += ["FEATURE_" + data + "=1" for data in self.target.features]
switches 0:0e018d759a2a 450 # Add extra symbols passed via 'macros' parameter
switches 0:0e018d759a2a 451 self.cxx_symbols += self.macros
switches 0:0e018d759a2a 452
switches 0:0e018d759a2a 453 # Form factor variables
switches 0:0e018d759a2a 454 if hasattr(self.target, 'supported_form_factors'):
switches 0:0e018d759a2a 455 self.cxx_symbols.extend(["TARGET_FF_%s" % t for t in self.target.supported_form_factors])
switches 0:0e018d759a2a 456
switches 0:0e018d759a2a 457 return list(set(self.cxx_symbols)) # Return only unique symbols
switches 0:0e018d759a2a 458
switches 0:0e018d759a2a 459 # Extend the internal list of macros
switches 0:0e018d759a2a 460 def add_macros(self, new_macros):
switches 0:0e018d759a2a 461 self.macros.extend(new_macros)
switches 0:0e018d759a2a 462
switches 0:0e018d759a2a 463 def get_labels(self):
switches 0:0e018d759a2a 464 if self.labels is None:
switches 0:0e018d759a2a 465 toolchain_labels = [c.__name__ for c in getmro(self.__class__)]
switches 0:0e018d759a2a 466 toolchain_labels.remove('mbedToolchain')
switches 0:0e018d759a2a 467 self.labels = {
switches 0:0e018d759a2a 468 'TARGET': self.target.labels,
switches 0:0e018d759a2a 469 'FEATURE': self.target.features,
switches 0:0e018d759a2a 470 'TOOLCHAIN': toolchain_labels
switches 0:0e018d759a2a 471 }
switches 0:0e018d759a2a 472
switches 0:0e018d759a2a 473 # This is a policy decision and it should /really/ be in the config system
switches 0:0e018d759a2a 474 # ATM it's here for backward compatibility
switches 0:0e018d759a2a 475 if (("-g" in self.flags['common'] and
switches 0:0e018d759a2a 476 "-O0") in self.flags['common'] or
switches 0:0e018d759a2a 477 ("-r" in self.flags['common'] and
switches 0:0e018d759a2a 478 "-On" in self.flags['common'])):
switches 0:0e018d759a2a 479 self.labels['TARGET'].append("DEBUG")
switches 0:0e018d759a2a 480 else:
switches 0:0e018d759a2a 481 self.labels['TARGET'].append("RELEASE")
switches 0:0e018d759a2a 482 return self.labels
switches 0:0e018d759a2a 483
switches 0:0e018d759a2a 484
switches 0:0e018d759a2a 485 # Determine whether a source file needs updating/compiling
switches 0:0e018d759a2a 486 def need_update(self, target, dependencies):
switches 0:0e018d759a2a 487 if self.build_all:
switches 0:0e018d759a2a 488 return True
switches 0:0e018d759a2a 489
switches 0:0e018d759a2a 490 if not exists(target):
switches 0:0e018d759a2a 491 return True
switches 0:0e018d759a2a 492
switches 0:0e018d759a2a 493 target_mod_time = stat(target).st_mtime
switches 0:0e018d759a2a 494
switches 0:0e018d759a2a 495 for d in dependencies:
switches 0:0e018d759a2a 496 # Some objects are not provided with full path and here we do not have
switches 0:0e018d759a2a 497 # information about the library paths. Safe option: assume an update
switches 0:0e018d759a2a 498 if not d or not exists(d):
switches 0:0e018d759a2a 499 return True
switches 0:0e018d759a2a 500
switches 0:0e018d759a2a 501 if not self.stat_cache.has_key(d):
switches 0:0e018d759a2a 502 self.stat_cache[d] = stat(d).st_mtime
switches 0:0e018d759a2a 503
switches 0:0e018d759a2a 504 if self.stat_cache[d] >= target_mod_time:
switches 0:0e018d759a2a 505 return True
switches 0:0e018d759a2a 506
switches 0:0e018d759a2a 507 return False
switches 0:0e018d759a2a 508
switches 0:0e018d759a2a 509 def is_ignored(self, file_path):
switches 0:0e018d759a2a 510 for pattern in self.ignore_patterns:
switches 0:0e018d759a2a 511 if fnmatch.fnmatch(file_path, pattern):
switches 0:0e018d759a2a 512 return True
switches 0:0e018d759a2a 513 return False
switches 0:0e018d759a2a 514
switches 0:0e018d759a2a 515 # Create a Resources object from the path pointed to by *path* by either traversing a
switches 0:0e018d759a2a 516 # a directory structure, when *path* is a directory, or adding *path* to the resources,
switches 0:0e018d759a2a 517 # when *path* is a file.
switches 0:0e018d759a2a 518 # The parameter *base_path* is used to set the base_path attribute of the Resources
switches 0:0e018d759a2a 519 # object and the parameter *exclude_paths* is used by the directory traversal to
switches 0:0e018d759a2a 520 # exclude certain paths from the traversal.
switches 0:0e018d759a2a 521 def scan_resources(self, path, exclude_paths=None, base_path=None):
switches 0:0e018d759a2a 522 self.progress("scan", path)
switches 0:0e018d759a2a 523
switches 0:0e018d759a2a 524 resources = Resources(path)
switches 0:0e018d759a2a 525 if not base_path:
switches 0:0e018d759a2a 526 if isfile(path):
switches 0:0e018d759a2a 527 base_path = dirname(path)
switches 0:0e018d759a2a 528 else:
switches 0:0e018d759a2a 529 base_path = path
switches 0:0e018d759a2a 530 resources.base_path = base_path
switches 0:0e018d759a2a 531
switches 0:0e018d759a2a 532 if isfile(path):
switches 0:0e018d759a2a 533 self._add_file(path, resources, base_path, exclude_paths=exclude_paths)
switches 0:0e018d759a2a 534 else:
switches 0:0e018d759a2a 535 self._add_dir(path, resources, base_path, exclude_paths=exclude_paths)
switches 0:0e018d759a2a 536 return resources
switches 0:0e018d759a2a 537
switches 0:0e018d759a2a 538 # A helper function for scan_resources. _add_dir traverses *path* (assumed to be a
switches 0:0e018d759a2a 539 # directory) and heeds the ".mbedignore" files along the way. _add_dir calls _add_file
switches 0:0e018d759a2a 540 # on every file it considers adding to the resources object.
switches 0:0e018d759a2a 541 def _add_dir(self, path, resources, base_path, exclude_paths=None):
switches 0:0e018d759a2a 542 """ os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
switches 0:0e018d759a2a 543 When topdown is True, the caller can modify the dirnames list in-place
switches 0:0e018d759a2a 544 (perhaps using del or slice assignment), and walk() will only recurse into
switches 0:0e018d759a2a 545 the subdirectories whose names remain in dirnames; this can be used to prune
switches 0:0e018d759a2a 546 the search, impose a specific order of visiting, or even to inform walk()
switches 0:0e018d759a2a 547 about directories the caller creates or renames before it resumes walk()
switches 0:0e018d759a2a 548 again. Modifying dirnames when topdown is False is ineffective, because in
switches 0:0e018d759a2a 549 bottom-up mode the directories in dirnames are generated before dirpath
switches 0:0e018d759a2a 550 itself is generated.
switches 0:0e018d759a2a 551 """
switches 0:0e018d759a2a 552 labels = self.get_labels()
switches 0:0e018d759a2a 553 for root, dirs, files in walk(path, followlinks=True):
switches 0:0e018d759a2a 554 # Check if folder contains .mbedignore
switches 0:0e018d759a2a 555 if ".mbedignore" in files:
switches 0:0e018d759a2a 556 with open (join(root,".mbedignore"), "r") as f:
switches 0:0e018d759a2a 557 lines=f.readlines()
switches 0:0e018d759a2a 558 lines = [l.strip() for l in lines] # Strip whitespaces
switches 0:0e018d759a2a 559 lines = [l for l in lines if l != ""] # Strip empty lines
switches 0:0e018d759a2a 560 lines = [l for l in lines if not re.match("^#",l)] # Strip comment lines
switches 0:0e018d759a2a 561 # Append root path to glob patterns and append patterns to ignore_patterns
switches 0:0e018d759a2a 562 self.ignore_patterns.extend([join(root,line.strip()) for line in lines])
switches 0:0e018d759a2a 563
switches 0:0e018d759a2a 564 # Skip the whole folder if ignored, e.g. .mbedignore containing '*'
switches 0:0e018d759a2a 565 if self.is_ignored(join(root,"")):
switches 0:0e018d759a2a 566 continue
switches 0:0e018d759a2a 567
switches 0:0e018d759a2a 568 for d in copy(dirs):
switches 0:0e018d759a2a 569 dir_path = join(root, d)
switches 0:0e018d759a2a 570 # Add internal repo folders/files. This is needed for exporters
switches 0:0e018d759a2a 571 if d == '.hg' or d == '.git':
switches 0:0e018d759a2a 572 resources.repo_dirs.append(dir_path)
switches 0:0e018d759a2a 573
switches 0:0e018d759a2a 574 if ((d.startswith('.') or d in self.legacy_ignore_dirs) or
switches 0:0e018d759a2a 575 # Ignore targets that do not match the TARGET in extra_labels list
switches 0:0e018d759a2a 576 (d.startswith('TARGET_') and d[7:] not in labels['TARGET']) or
switches 0:0e018d759a2a 577 # Ignore toolchain that do not match the current TOOLCHAIN
switches 0:0e018d759a2a 578 (d.startswith('TOOLCHAIN_') and d[10:] not in labels['TOOLCHAIN']) or
switches 0:0e018d759a2a 579 # Ignore .mbedignore files
switches 0:0e018d759a2a 580 self.is_ignored(join(dir_path,"")) or
switches 0:0e018d759a2a 581 # Ignore TESTS dir
switches 0:0e018d759a2a 582 (d == 'TESTS')):
switches 0:0e018d759a2a 583 dirs.remove(d)
switches 0:0e018d759a2a 584 elif d.startswith('FEATURE_'):
switches 0:0e018d759a2a 585 # Recursively scan features but ignore them in the current scan.
switches 0:0e018d759a2a 586 # These are dynamically added by the config system if the conditions are matched
switches 0:0e018d759a2a 587 resources.features[d[8:]] = self.scan_resources(dir_path, base_path=base_path)
switches 0:0e018d759a2a 588 dirs.remove(d)
switches 0:0e018d759a2a 589 elif exclude_paths:
switches 0:0e018d759a2a 590 for exclude_path in exclude_paths:
switches 0:0e018d759a2a 591 rel_path = relpath(dir_path, exclude_path)
switches 0:0e018d759a2a 592 if not (rel_path.startswith('..')):
switches 0:0e018d759a2a 593 dirs.remove(d)
switches 0:0e018d759a2a 594 break
switches 0:0e018d759a2a 595
switches 0:0e018d759a2a 596 # Add root to include paths
switches 0:0e018d759a2a 597 resources.inc_dirs.append(root)
switches 0:0e018d759a2a 598 resources.file_basepath[root] = base_path
switches 0:0e018d759a2a 599
switches 0:0e018d759a2a 600 for file in files:
switches 0:0e018d759a2a 601 file_path = join(root, file)
switches 0:0e018d759a2a 602 self._add_file(file_path, resources, base_path)
switches 0:0e018d759a2a 603
switches 0:0e018d759a2a 604 # A helper function for both scan_resources and _add_dir. _add_file adds one file
switches 0:0e018d759a2a 605 # (*file_path*) to the resources object based on the file type.
switches 0:0e018d759a2a 606 def _add_file(self, file_path, resources, base_path, exclude_paths=None):
switches 0:0e018d759a2a 607 resources.file_basepath[file_path] = base_path
switches 0:0e018d759a2a 608
switches 0:0e018d759a2a 609 if self.is_ignored(file_path):
switches 0:0e018d759a2a 610 return
switches 0:0e018d759a2a 611
switches 0:0e018d759a2a 612 _, ext = splitext(file_path)
switches 0:0e018d759a2a 613 ext = ext.lower()
switches 0:0e018d759a2a 614
switches 0:0e018d759a2a 615 if ext == '.s':
switches 0:0e018d759a2a 616 resources.s_sources.append(file_path)
switches 0:0e018d759a2a 617
switches 0:0e018d759a2a 618 elif ext == '.c':
switches 0:0e018d759a2a 619 resources.c_sources.append(file_path)
switches 0:0e018d759a2a 620
switches 0:0e018d759a2a 621 elif ext == '.cpp':
switches 0:0e018d759a2a 622 resources.cpp_sources.append(file_path)
switches 0:0e018d759a2a 623
switches 0:0e018d759a2a 624 elif ext == '.h' or ext == '.hpp':
switches 0:0e018d759a2a 625 resources.headers.append(file_path)
switches 0:0e018d759a2a 626
switches 0:0e018d759a2a 627 elif ext == '.o':
switches 0:0e018d759a2a 628 resources.objects.append(file_path)
switches 0:0e018d759a2a 629
switches 0:0e018d759a2a 630 elif ext == self.LIBRARY_EXT:
switches 0:0e018d759a2a 631 resources.libraries.append(file_path)
switches 0:0e018d759a2a 632 resources.lib_dirs.add(dirname(file_path))
switches 0:0e018d759a2a 633
switches 0:0e018d759a2a 634 elif ext == self.LINKER_EXT:
switches 0:0e018d759a2a 635 if resources.linker_script is not None:
switches 0:0e018d759a2a 636 self.info("Warning: Multiple linker scripts detected: %s -> %s" % (resources.linker_script, file_path))
switches 0:0e018d759a2a 637 resources.linker_script = file_path
switches 0:0e018d759a2a 638
switches 0:0e018d759a2a 639 elif ext == '.lib':
switches 0:0e018d759a2a 640 resources.lib_refs.append(file_path)
switches 0:0e018d759a2a 641
switches 0:0e018d759a2a 642 elif ext == '.bld':
switches 0:0e018d759a2a 643 resources.lib_builds.append(file_path)
switches 0:0e018d759a2a 644
switches 0:0e018d759a2a 645 elif basename(file_path) == '.hgignore':
switches 0:0e018d759a2a 646 resources.repo_files.append(file_path)
switches 0:0e018d759a2a 647
switches 0:0e018d759a2a 648 elif basename(file_path) == '.gitignore':
switches 0:0e018d759a2a 649 resources.repo_files.append(file_path)
switches 0:0e018d759a2a 650
switches 0:0e018d759a2a 651 elif ext == '.hex':
switches 0:0e018d759a2a 652 resources.hex_files.append(file_path)
switches 0:0e018d759a2a 653
switches 0:0e018d759a2a 654 elif ext == '.bin':
switches 0:0e018d759a2a 655 resources.bin_files.append(file_path)
switches 0:0e018d759a2a 656
switches 0:0e018d759a2a 657 elif ext == '.json':
switches 0:0e018d759a2a 658 resources.json_files.append(file_path)
switches 0:0e018d759a2a 659
switches 0:0e018d759a2a 660
switches 0:0e018d759a2a 661 def scan_repository(self, path):
switches 0:0e018d759a2a 662 resources = []
switches 0:0e018d759a2a 663
switches 0:0e018d759a2a 664 for root, dirs, files in walk(path):
switches 0:0e018d759a2a 665 # Remove ignored directories
switches 0:0e018d759a2a 666 for d in copy(dirs):
switches 0:0e018d759a2a 667 if d == '.' or d == '..':
switches 0:0e018d759a2a 668 dirs.remove(d)
switches 0:0e018d759a2a 669
switches 0:0e018d759a2a 670 for file in files:
switches 0:0e018d759a2a 671 file_path = join(root, file)
switches 0:0e018d759a2a 672 resources.append(file_path)
switches 0:0e018d759a2a 673
switches 0:0e018d759a2a 674 return resources
switches 0:0e018d759a2a 675
switches 0:0e018d759a2a 676 def copy_files(self, files_paths, trg_path, resources=None, rel_path=None):
switches 0:0e018d759a2a 677 # Handle a single file
switches 0:0e018d759a2a 678 if type(files_paths) != ListType: files_paths = [files_paths]
switches 0:0e018d759a2a 679
switches 0:0e018d759a2a 680 for source in files_paths:
switches 0:0e018d759a2a 681 if source is None:
switches 0:0e018d759a2a 682 files_paths.remove(source)
switches 0:0e018d759a2a 683
switches 0:0e018d759a2a 684 for source in files_paths:
switches 0:0e018d759a2a 685 if resources is not None and resources.file_basepath.has_key(source):
switches 0:0e018d759a2a 686 relative_path = relpath(source, resources.file_basepath[source])
switches 0:0e018d759a2a 687 elif rel_path is not None:
switches 0:0e018d759a2a 688 relative_path = relpath(source, rel_path)
switches 0:0e018d759a2a 689 else:
switches 0:0e018d759a2a 690 _, relative_path = split(source)
switches 0:0e018d759a2a 691
switches 0:0e018d759a2a 692 target = join(trg_path, relative_path)
switches 0:0e018d759a2a 693
switches 0:0e018d759a2a 694 if (target != source) and (self.need_update(target, [source])):
switches 0:0e018d759a2a 695 self.progress("copy", relative_path)
switches 0:0e018d759a2a 696 mkdir(dirname(target))
switches 0:0e018d759a2a 697 copyfile(source, target)
switches 0:0e018d759a2a 698
switches 0:0e018d759a2a 699 # THIS METHOD IS BEING OVERRIDDEN BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 700 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 701 def relative_object_path(self, build_path, base_dir, source):
switches 0:0e018d759a2a 702 source_dir, name, _ = split_path(source)
switches 0:0e018d759a2a 703
switches 0:0e018d759a2a 704 obj_dir = join(build_path, relpath(source_dir, base_dir))
switches 0:0e018d759a2a 705 if obj_dir is not self.prev_dir:
switches 0:0e018d759a2a 706 self.prev_dir = obj_dir
switches 0:0e018d759a2a 707 mkdir(obj_dir)
switches 0:0e018d759a2a 708 return join(obj_dir, name + '.o')
switches 0:0e018d759a2a 709
switches 0:0e018d759a2a 710 # Generate response file for all includes.
switches 0:0e018d759a2a 711 # ARM, GCC, IAR cross compatible
switches 0:0e018d759a2a 712 def get_inc_file(self, includes):
switches 0:0e018d759a2a 713 include_file = join(self.build_dir, ".includes_%s.txt" % self.inc_md5)
switches 0:0e018d759a2a 714 if not exists(include_file):
switches 0:0e018d759a2a 715 with open(include_file, "wb") as f:
switches 0:0e018d759a2a 716 cmd_list = []
switches 0:0e018d759a2a 717 for c in includes:
switches 0:0e018d759a2a 718 if c:
switches 0:0e018d759a2a 719 c = c.replace("\\", "/")
switches 0:0e018d759a2a 720 if self.CHROOT:
switches 0:0e018d759a2a 721 c = c.replace(self.CHROOT, '')
switches 0:0e018d759a2a 722 cmd_list.append('-I%s' % c)
switches 0:0e018d759a2a 723 string = " ".join(cmd_list)
switches 0:0e018d759a2a 724 f.write(string)
switches 0:0e018d759a2a 725 return include_file
switches 0:0e018d759a2a 726
switches 0:0e018d759a2a 727 # Generate response file for all objects when linking.
switches 0:0e018d759a2a 728 # ARM, GCC, IAR cross compatible
switches 0:0e018d759a2a 729 def get_link_file(self, cmd):
switches 0:0e018d759a2a 730 link_file = join(self.build_dir, ".link_files.txt")
switches 0:0e018d759a2a 731 with open(link_file, "wb") as f:
switches 0:0e018d759a2a 732 cmd_list = []
switches 0:0e018d759a2a 733 for c in cmd:
switches 0:0e018d759a2a 734 if c:
switches 0:0e018d759a2a 735 c = c.replace("\\", "/")
switches 0:0e018d759a2a 736 if self.CHROOT:
switches 0:0e018d759a2a 737 c = c.replace(self.CHROOT, '')
switches 0:0e018d759a2a 738 cmd_list.append(('"%s"' % c) if not c.startswith('-') else c)
switches 0:0e018d759a2a 739 string = " ".join(cmd_list)
switches 0:0e018d759a2a 740 f.write(string)
switches 0:0e018d759a2a 741 return link_file
switches 0:0e018d759a2a 742
switches 0:0e018d759a2a 743 # Generate response file for all objects when archiving.
switches 0:0e018d759a2a 744 # ARM, GCC, IAR cross compatible
switches 0:0e018d759a2a 745 def get_arch_file(self, objects):
switches 0:0e018d759a2a 746 archive_file = join(self.build_dir, ".archive_files.txt")
switches 0:0e018d759a2a 747 with open(archive_file, "wb") as f:
switches 0:0e018d759a2a 748 o_list = []
switches 0:0e018d759a2a 749 for o in objects:
switches 0:0e018d759a2a 750 o_list.append('"%s"' % o)
switches 0:0e018d759a2a 751 string = " ".join(o_list).replace("\\", "/")
switches 0:0e018d759a2a 752 f.write(string)
switches 0:0e018d759a2a 753 return archive_file
switches 0:0e018d759a2a 754
switches 0:0e018d759a2a 755 # THIS METHOD IS BEING CALLED BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 756 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 757 def compile_sources(self, resources, build_path, inc_dirs=None):
switches 0:0e018d759a2a 758 # Web IDE progress bar for project build
switches 0:0e018d759a2a 759 files_to_compile = resources.s_sources + resources.c_sources + resources.cpp_sources
switches 0:0e018d759a2a 760 self.to_be_compiled = len(files_to_compile)
switches 0:0e018d759a2a 761 self.compiled = 0
switches 0:0e018d759a2a 762
switches 0:0e018d759a2a 763 self.cc_verbose("Macros: "+' '.join(['-D%s' % s for s in self.get_symbols()]))
switches 0:0e018d759a2a 764
switches 0:0e018d759a2a 765 inc_paths = resources.inc_dirs
switches 0:0e018d759a2a 766 if inc_dirs is not None:
switches 0:0e018d759a2a 767 inc_paths.extend(inc_dirs)
switches 0:0e018d759a2a 768 # De-duplicate include paths
switches 0:0e018d759a2a 769 inc_paths = set(inc_paths)
switches 0:0e018d759a2a 770 # Sort include paths for consistency
switches 0:0e018d759a2a 771 inc_paths = sorted(set(inc_paths))
switches 0:0e018d759a2a 772 # Unique id of all include paths
switches 0:0e018d759a2a 773 self.inc_md5 = md5(' '.join(inc_paths)).hexdigest()
switches 0:0e018d759a2a 774 # Where to store response files
switches 0:0e018d759a2a 775 self.build_dir = build_path
switches 0:0e018d759a2a 776
switches 0:0e018d759a2a 777 objects = []
switches 0:0e018d759a2a 778 queue = []
switches 0:0e018d759a2a 779 work_dir = getcwd()
switches 0:0e018d759a2a 780 self.prev_dir = None
switches 0:0e018d759a2a 781
switches 0:0e018d759a2a 782 # Generate configuration header (this will update self.build_all if needed)
switches 0:0e018d759a2a 783 self.get_config_header()
switches 0:0e018d759a2a 784
switches 0:0e018d759a2a 785 # Sort compile queue for consistency
switches 0:0e018d759a2a 786 files_to_compile.sort()
switches 0:0e018d759a2a 787 for source in files_to_compile:
switches 0:0e018d759a2a 788 object = self.relative_object_path(build_path, resources.file_basepath[source], source)
switches 0:0e018d759a2a 789
switches 0:0e018d759a2a 790 # Queue mode (multiprocessing)
switches 0:0e018d759a2a 791 commands = self.compile_command(source, object, inc_paths)
switches 0:0e018d759a2a 792 if commands is not None:
switches 0:0e018d759a2a 793 queue.append({
switches 0:0e018d759a2a 794 'source': source,
switches 0:0e018d759a2a 795 'object': object,
switches 0:0e018d759a2a 796 'commands': commands,
switches 0:0e018d759a2a 797 'work_dir': work_dir,
switches 0:0e018d759a2a 798 'chroot': self.CHROOT
switches 0:0e018d759a2a 799 })
switches 0:0e018d759a2a 800 else:
switches 0:0e018d759a2a 801 self.compiled += 1
switches 0:0e018d759a2a 802 objects.append(object)
switches 0:0e018d759a2a 803
switches 0:0e018d759a2a 804 # Use queues/multiprocessing if cpu count is higher than setting
switches 0:0e018d759a2a 805 jobs = self.jobs if self.jobs else cpu_count()
switches 0:0e018d759a2a 806 if jobs > CPU_COUNT_MIN and len(queue) > jobs:
switches 0:0e018d759a2a 807 return self.compile_queue(queue, objects)
switches 0:0e018d759a2a 808 else:
switches 0:0e018d759a2a 809 return self.compile_seq(queue, objects)
switches 0:0e018d759a2a 810
switches 0:0e018d759a2a 811 # Compile source files queue in sequential order
switches 0:0e018d759a2a 812 def compile_seq(self, queue, objects):
switches 0:0e018d759a2a 813 for item in queue:
switches 0:0e018d759a2a 814 result = compile_worker(item)
switches 0:0e018d759a2a 815
switches 0:0e018d759a2a 816 self.compiled += 1
switches 0:0e018d759a2a 817 self.progress("compile", item['source'], build_update=True)
switches 0:0e018d759a2a 818 for res in result['results']:
switches 0:0e018d759a2a 819 self.cc_verbose("Compile: %s" % ' '.join(res['command']), result['source'])
switches 0:0e018d759a2a 820 self.compile_output([
switches 0:0e018d759a2a 821 res['code'],
switches 0:0e018d759a2a 822 res['output'],
switches 0:0e018d759a2a 823 res['command']
switches 0:0e018d759a2a 824 ])
switches 0:0e018d759a2a 825 objects.append(result['object'])
switches 0:0e018d759a2a 826 return objects
switches 0:0e018d759a2a 827
switches 0:0e018d759a2a 828 # Compile source files queue in parallel by creating pool of worker threads
switches 0:0e018d759a2a 829 def compile_queue(self, queue, objects):
switches 0:0e018d759a2a 830 jobs_count = int(self.jobs if self.jobs else cpu_count() * CPU_COEF)
switches 0:0e018d759a2a 831 p = Pool(processes=jobs_count)
switches 0:0e018d759a2a 832
switches 0:0e018d759a2a 833 results = []
switches 0:0e018d759a2a 834 for i in range(len(queue)):
switches 0:0e018d759a2a 835 results.append(p.apply_async(compile_worker, [queue[i]]))
switches 0:0e018d759a2a 836 p.close()
switches 0:0e018d759a2a 837
switches 0:0e018d759a2a 838 itr = 0
switches 0:0e018d759a2a 839 while len(results):
switches 0:0e018d759a2a 840 itr += 1
switches 0:0e018d759a2a 841 if itr > 180000:
switches 0:0e018d759a2a 842 p.terminate()
switches 0:0e018d759a2a 843 p.join()
switches 0:0e018d759a2a 844 raise ToolException("Compile did not finish in 5 minutes")
switches 0:0e018d759a2a 845
switches 0:0e018d759a2a 846 sleep(0.01)
switches 0:0e018d759a2a 847 pending = 0
switches 0:0e018d759a2a 848 for r in results:
switches 0:0e018d759a2a 849 if r._ready is True:
switches 0:0e018d759a2a 850 try:
switches 0:0e018d759a2a 851 result = r.get()
switches 0:0e018d759a2a 852 results.remove(r)
switches 0:0e018d759a2a 853
switches 0:0e018d759a2a 854 self.compiled += 1
switches 0:0e018d759a2a 855 self.progress("compile", result['source'], build_update=True)
switches 0:0e018d759a2a 856 for res in result['results']:
switches 0:0e018d759a2a 857 self.cc_verbose("Compile: %s" % ' '.join(res['command']), result['source'])
switches 0:0e018d759a2a 858 self.compile_output([
switches 0:0e018d759a2a 859 res['code'],
switches 0:0e018d759a2a 860 res['output'],
switches 0:0e018d759a2a 861 res['command']
switches 0:0e018d759a2a 862 ])
switches 0:0e018d759a2a 863 objects.append(result['object'])
switches 0:0e018d759a2a 864 except ToolException, err:
switches 0:0e018d759a2a 865 if p._taskqueue.queue:
switches 0:0e018d759a2a 866 p._taskqueue.queue.clear()
switches 0:0e018d759a2a 867 sleep(0.5)
switches 0:0e018d759a2a 868 p.terminate()
switches 0:0e018d759a2a 869 p.join()
switches 0:0e018d759a2a 870 raise ToolException(err)
switches 0:0e018d759a2a 871 else:
switches 0:0e018d759a2a 872 pending += 1
switches 0:0e018d759a2a 873 if pending >= jobs_count:
switches 0:0e018d759a2a 874 break
switches 0:0e018d759a2a 875
switches 0:0e018d759a2a 876 results = None
switches 0:0e018d759a2a 877 p.join()
switches 0:0e018d759a2a 878
switches 0:0e018d759a2a 879 return objects
switches 0:0e018d759a2a 880
switches 0:0e018d759a2a 881 # Determine the compile command based on type of source file
switches 0:0e018d759a2a 882 def compile_command(self, source, object, includes):
switches 0:0e018d759a2a 883 # Check dependencies
switches 0:0e018d759a2a 884 _, ext = splitext(source)
switches 0:0e018d759a2a 885 ext = ext.lower()
switches 0:0e018d759a2a 886
switches 0:0e018d759a2a 887 if ext == '.c' or ext == '.cpp':
switches 0:0e018d759a2a 888 base, _ = splitext(object)
switches 0:0e018d759a2a 889 dep_path = base + '.d'
switches 0:0e018d759a2a 890 deps = self.parse_dependencies(dep_path) if (exists(dep_path)) else []
switches 0:0e018d759a2a 891 if len(deps) == 0 or self.need_update(object, deps):
switches 0:0e018d759a2a 892 if ext == '.cpp' or self.COMPILE_C_AS_CPP:
switches 0:0e018d759a2a 893 return self.compile_cpp(source, object, includes)
switches 0:0e018d759a2a 894 else:
switches 0:0e018d759a2a 895 return self.compile_c(source, object, includes)
switches 0:0e018d759a2a 896 elif ext == '.s':
switches 0:0e018d759a2a 897 deps = [source]
switches 0:0e018d759a2a 898 if self.need_update(object, deps):
switches 0:0e018d759a2a 899 return self.assemble(source, object, includes)
switches 0:0e018d759a2a 900 else:
switches 0:0e018d759a2a 901 return False
switches 0:0e018d759a2a 902
switches 0:0e018d759a2a 903 return None
switches 0:0e018d759a2a 904
switches 0:0e018d759a2a 905 @abstractmethod
switches 0:0e018d759a2a 906 def parse_dependencies(self, dep_path):
switches 0:0e018d759a2a 907 """Parse the dependency information generated by the compiler.
switches 0:0e018d759a2a 908
switches 0:0e018d759a2a 909 Positional arguments:
switches 0:0e018d759a2a 910 dep_path -- the path to a file generated by a previous run of the compiler
switches 0:0e018d759a2a 911
switches 0:0e018d759a2a 912 Return value:
switches 0:0e018d759a2a 913 A list of all source files that the dependency file indicated were dependencies
switches 0:0e018d759a2a 914
switches 0:0e018d759a2a 915 Side effects:
switches 0:0e018d759a2a 916 None
switches 0:0e018d759a2a 917 """
switches 0:0e018d759a2a 918 raise NotImplemented
switches 0:0e018d759a2a 919
switches 0:0e018d759a2a 920 def is_not_supported_error(self, output):
switches 0:0e018d759a2a 921 return "#error directive: [NOT_SUPPORTED]" in output
switches 0:0e018d759a2a 922
switches 0:0e018d759a2a 923 @abstractmethod
switches 0:0e018d759a2a 924 def parse_output(self, output):
switches 0:0e018d759a2a 925 """Take in compiler output and extract sinlge line warnings and errors from it.
switches 0:0e018d759a2a 926
switches 0:0e018d759a2a 927 Positional arguments:
switches 0:0e018d759a2a 928 output -- a string of all the messages emitted by a run of the compiler
switches 0:0e018d759a2a 929
switches 0:0e018d759a2a 930 Return value:
switches 0:0e018d759a2a 931 None
switches 0:0e018d759a2a 932
switches 0:0e018d759a2a 933 Side effects:
switches 0:0e018d759a2a 934 call self.cc_info or self.notify with a description of the event generated by the compiler
switches 0:0e018d759a2a 935 """
switches 0:0e018d759a2a 936 raise NotImplemented
switches 0:0e018d759a2a 937
switches 0:0e018d759a2a 938 def compile_output(self, output=[]):
switches 0:0e018d759a2a 939 _rc = output[0]
switches 0:0e018d759a2a 940 _stderr = output[1]
switches 0:0e018d759a2a 941 command = output[2]
switches 0:0e018d759a2a 942
switches 0:0e018d759a2a 943 # Parse output for Warnings and Errors
switches 0:0e018d759a2a 944 self.parse_output(_stderr)
switches 0:0e018d759a2a 945 self.debug("Return: %s"% _rc)
switches 0:0e018d759a2a 946 for error_line in _stderr.splitlines():
switches 0:0e018d759a2a 947 self.debug("Output: %s"% error_line)
switches 0:0e018d759a2a 948
switches 0:0e018d759a2a 949 # Check return code
switches 0:0e018d759a2a 950 if _rc != 0:
switches 0:0e018d759a2a 951 if self.is_not_supported_error(_stderr):
switches 0:0e018d759a2a 952 raise NotSupportedException(_stderr)
switches 0:0e018d759a2a 953 else:
switches 0:0e018d759a2a 954 raise ToolException(_stderr)
switches 0:0e018d759a2a 955
switches 0:0e018d759a2a 956 def build_library(self, objects, dir, name):
switches 0:0e018d759a2a 957 needed_update = False
switches 0:0e018d759a2a 958 lib = self.STD_LIB_NAME % name
switches 0:0e018d759a2a 959 fout = join(dir, lib)
switches 0:0e018d759a2a 960 if self.need_update(fout, objects):
switches 0:0e018d759a2a 961 self.info("Library: %s" % lib)
switches 0:0e018d759a2a 962 self.archive(objects, fout)
switches 0:0e018d759a2a 963 needed_update = True
switches 0:0e018d759a2a 964
switches 0:0e018d759a2a 965 return needed_update
switches 0:0e018d759a2a 966
switches 0:0e018d759a2a 967 def link_program(self, r, tmp_path, name):
switches 0:0e018d759a2a 968 needed_update = False
switches 0:0e018d759a2a 969 ext = 'bin'
switches 0:0e018d759a2a 970 if hasattr(self.target, 'OUTPUT_EXT'):
switches 0:0e018d759a2a 971 ext = self.target.OUTPUT_EXT
switches 0:0e018d759a2a 972
switches 0:0e018d759a2a 973 if hasattr(self.target, 'OUTPUT_NAMING'):
switches 0:0e018d759a2a 974 self.var("binary_naming", self.target.OUTPUT_NAMING)
switches 0:0e018d759a2a 975 if self.target.OUTPUT_NAMING == "8.3":
switches 0:0e018d759a2a 976 name = name[0:8]
switches 0:0e018d759a2a 977 ext = ext[0:3]
switches 0:0e018d759a2a 978
switches 0:0e018d759a2a 979 # Create destination directory
switches 0:0e018d759a2a 980 head, tail = split(name)
switches 0:0e018d759a2a 981 new_path = join(tmp_path, head)
switches 0:0e018d759a2a 982 mkdir(new_path)
switches 0:0e018d759a2a 983
switches 0:0e018d759a2a 984 filename = name+'.'+ext
switches 0:0e018d759a2a 985 elf = join(tmp_path, name + '.elf')
switches 0:0e018d759a2a 986 bin = join(tmp_path, filename)
switches 0:0e018d759a2a 987 map = join(tmp_path, name + '.map')
switches 0:0e018d759a2a 988
switches 0:0e018d759a2a 989 r.objects = sorted(set(r.objects))
switches 0:0e018d759a2a 990 if self.need_update(elf, r.objects + r.libraries + [r.linker_script]):
switches 0:0e018d759a2a 991 needed_update = True
switches 0:0e018d759a2a 992 self.progress("link", name)
switches 0:0e018d759a2a 993 self.link(elf, r.objects, r.libraries, r.lib_dirs, r.linker_script)
switches 0:0e018d759a2a 994
switches 0:0e018d759a2a 995 if self.need_update(bin, [elf]):
switches 0:0e018d759a2a 996 needed_update = True
switches 0:0e018d759a2a 997 self.progress("elf2bin", name)
switches 0:0e018d759a2a 998 self.binary(r, elf, bin)
switches 0:0e018d759a2a 999
switches 0:0e018d759a2a 1000 self.map_outputs = self.mem_stats(map)
switches 0:0e018d759a2a 1001
switches 0:0e018d759a2a 1002 self.var("compile_succeded", True)
switches 0:0e018d759a2a 1003 self.var("binary", filename)
switches 0:0e018d759a2a 1004
switches 0:0e018d759a2a 1005 return bin, needed_update
switches 0:0e018d759a2a 1006
switches 0:0e018d759a2a 1007 # THIS METHOD IS BEING OVERRIDDEN BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 1008 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 1009 def default_cmd(self, command):
switches 0:0e018d759a2a 1010 _stdout, _stderr, _rc = run_cmd(command, work_dir=getcwd(), chroot=self.CHROOT)
switches 0:0e018d759a2a 1011 self.debug("Return: %s"% _rc)
switches 0:0e018d759a2a 1012
switches 0:0e018d759a2a 1013 for output_line in _stdout.splitlines():
switches 0:0e018d759a2a 1014 self.debug("Output: %s"% output_line)
switches 0:0e018d759a2a 1015 for error_line in _stderr.splitlines():
switches 0:0e018d759a2a 1016 self.debug("Errors: %s"% error_line)
switches 0:0e018d759a2a 1017
switches 0:0e018d759a2a 1018 if _rc != 0:
switches 0:0e018d759a2a 1019 for line in _stderr.splitlines():
switches 0:0e018d759a2a 1020 self.tool_error(line)
switches 0:0e018d759a2a 1021 raise ToolException(_stderr)
switches 0:0e018d759a2a 1022
switches 0:0e018d759a2a 1023 ### NOTIFICATIONS ###
switches 0:0e018d759a2a 1024 def info(self, message):
switches 0:0e018d759a2a 1025 self.notify({'type': 'info', 'message': message})
switches 0:0e018d759a2a 1026
switches 0:0e018d759a2a 1027 # THIS METHOD IS BEING OVERRIDDEN BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 1028 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 1029 def debug(self, message):
switches 0:0e018d759a2a 1030 if self.VERBOSE:
switches 0:0e018d759a2a 1031 if type(message) is ListType:
switches 0:0e018d759a2a 1032 message = ' '.join(message)
switches 0:0e018d759a2a 1033 message = "[DEBUG] " + message
switches 0:0e018d759a2a 1034 self.notify({'type': 'debug', 'message': message})
switches 0:0e018d759a2a 1035
switches 0:0e018d759a2a 1036 # THIS METHOD IS BEING OVERRIDDEN BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 1037 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 1038 def cc_info(self, info=None):
switches 0:0e018d759a2a 1039 if info is not None:
switches 0:0e018d759a2a 1040 info['type'] = 'cc'
switches 0:0e018d759a2a 1041 self.notify(info)
switches 0:0e018d759a2a 1042
switches 0:0e018d759a2a 1043 # THIS METHOD IS BEING OVERRIDDEN BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 1044 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 1045 def cc_verbose(self, message, file=""):
switches 0:0e018d759a2a 1046 self.debug(message)
switches 0:0e018d759a2a 1047
switches 0:0e018d759a2a 1048 def progress(self, action, file, build_update=False):
switches 0:0e018d759a2a 1049 msg = {'type': 'progress', 'action': action, 'file': file}
switches 0:0e018d759a2a 1050 if build_update:
switches 0:0e018d759a2a 1051 msg['percent'] = 100. * float(self.compiled) / float(self.to_be_compiled)
switches 0:0e018d759a2a 1052 self.notify(msg)
switches 0:0e018d759a2a 1053
switches 0:0e018d759a2a 1054 def tool_error(self, message):
switches 0:0e018d759a2a 1055 self.notify({'type': 'tool_error', 'message': message})
switches 0:0e018d759a2a 1056
switches 0:0e018d759a2a 1057 def var(self, key, value):
switches 0:0e018d759a2a 1058 self.notify({'type': 'var', 'key': key, 'val': value})
switches 0:0e018d759a2a 1059
switches 0:0e018d759a2a 1060 # THIS METHOD IS BEING OVERRIDDEN BY THE MBED ONLINE BUILD SYSTEM
switches 0:0e018d759a2a 1061 # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
switches 0:0e018d759a2a 1062 def mem_stats(self, map):
switches 0:0e018d759a2a 1063 """! Creates parser object
switches 0:0e018d759a2a 1064 @param map Path to linker map file to parse and decode
switches 0:0e018d759a2a 1065 @return Memory summary structure with memory usage statistics
switches 0:0e018d759a2a 1066 None if map file can't be opened and processed
switches 0:0e018d759a2a 1067 """
switches 0:0e018d759a2a 1068 toolchain = self.__class__.__name__
switches 0:0e018d759a2a 1069
switches 0:0e018d759a2a 1070 # Create memap object
switches 0:0e018d759a2a 1071 memap = MemapParser()
switches 0:0e018d759a2a 1072
switches 0:0e018d759a2a 1073 # Parse and decode a map file
switches 0:0e018d759a2a 1074 if memap.parse(abspath(map), toolchain) is False:
switches 0:0e018d759a2a 1075 self.info("Unknown toolchain for memory statistics %s" % toolchain)
switches 0:0e018d759a2a 1076 return None
switches 0:0e018d759a2a 1077
switches 0:0e018d759a2a 1078 # Store the memap instance for later use
switches 0:0e018d759a2a 1079 self.memap_instance = memap
switches 0:0e018d759a2a 1080
switches 0:0e018d759a2a 1081 # Here we return memory statistics structure (constructed after
switches 0:0e018d759a2a 1082 # call to generate_output) which contains raw data in bytes
switches 0:0e018d759a2a 1083 # about sections + summary
switches 0:0e018d759a2a 1084 return memap.mem_report
switches 0:0e018d759a2a 1085
switches 0:0e018d759a2a 1086 # Set the configuration data
switches 0:0e018d759a2a 1087 def set_config_data(self, config_data):
switches 0:0e018d759a2a 1088 self.config_data = config_data
switches 0:0e018d759a2a 1089
switches 0:0e018d759a2a 1090 # Creates the configuration header if needed:
switches 0:0e018d759a2a 1091 # - if there is no configuration data, "mbed_config.h" is not create (or deleted if it exists).
switches 0:0e018d759a2a 1092 # - if there is configuration data and "mbed_config.h" does not exist, it is created.
switches 0:0e018d759a2a 1093 # - if there is configuration data similar to the previous configuration data,
switches 0:0e018d759a2a 1094 # "mbed_config.h" is left untouched.
switches 0:0e018d759a2a 1095 # - if there is new configuration data, "mbed_config.h" is overriden.
switches 0:0e018d759a2a 1096 # The function needs to be called exactly once for the lifetime of this toolchain instance.
switches 0:0e018d759a2a 1097 # The "config_processed" variable (below) ensures this behaviour.
switches 0:0e018d759a2a 1098 # The function returns the location of the configuration file, or None if there is no
switches 0:0e018d759a2a 1099 # configuration data available (and thus no configuration file)
switches 0:0e018d759a2a 1100 def get_config_header(self):
switches 0:0e018d759a2a 1101 if self.config_processed: # this function was already called, return its result
switches 0:0e018d759a2a 1102 return self.config_file
switches 0:0e018d759a2a 1103 # The config file is located in the build directory
switches 0:0e018d759a2a 1104 self.config_file = join(self.build_dir, self.MBED_CONFIG_FILE_NAME)
switches 0:0e018d759a2a 1105 # If the file exists, read its current content in prev_data
switches 0:0e018d759a2a 1106 if exists(self.config_file):
switches 0:0e018d759a2a 1107 with open(self.config_file, "rt") as f:
switches 0:0e018d759a2a 1108 prev_data = f.read()
switches 0:0e018d759a2a 1109 else:
switches 0:0e018d759a2a 1110 prev_data = None
switches 0:0e018d759a2a 1111 # Get the current configuration data
switches 0:0e018d759a2a 1112 crt_data = Config.config_to_header(self.config_data) if self.config_data else None
switches 0:0e018d759a2a 1113 # "changed" indicates if a configuration change was detected
switches 0:0e018d759a2a 1114 changed = False
switches 0:0e018d759a2a 1115 if prev_data is not None: # a previous mbed_config.h exists
switches 0:0e018d759a2a 1116 if crt_data is None: # no configuration data, so "mbed_config.h" needs to be removed
switches 0:0e018d759a2a 1117 remove(self.config_file)
switches 0:0e018d759a2a 1118 self.config_file = None # this means "config file not present"
switches 0:0e018d759a2a 1119 changed = True
switches 0:0e018d759a2a 1120 elif crt_data != prev_data: # different content of config file
switches 0:0e018d759a2a 1121 with open(self.config_file, "wt") as f:
switches 0:0e018d759a2a 1122 f.write(crt_data)
switches 0:0e018d759a2a 1123 changed = True
switches 0:0e018d759a2a 1124 else: # a previous mbed_config.h does not exist
switches 0:0e018d759a2a 1125 if crt_data is not None: # there's configuration data available
switches 0:0e018d759a2a 1126 with open(self.config_file, "wt") as f:
switches 0:0e018d759a2a 1127 f.write(crt_data)
switches 0:0e018d759a2a 1128 changed = True
switches 0:0e018d759a2a 1129 else:
switches 0:0e018d759a2a 1130 self.config_file = None # this means "config file not present"
switches 0:0e018d759a2a 1131 # If there was a change in configuration, rebuild everything
switches 0:0e018d759a2a 1132 self.build_all = changed
switches 0:0e018d759a2a 1133 # Make sure that this function will only return the location of the configuration
switches 0:0e018d759a2a 1134 # file for subsequent calls, without trying to manipulate its content in any way.
switches 0:0e018d759a2a 1135 self.config_processed = True
switches 0:0e018d759a2a 1136 return self.config_file
switches 0:0e018d759a2a 1137
switches 0:0e018d759a2a 1138 @staticmethod
switches 0:0e018d759a2a 1139 def generic_check_executable(tool_key, executable_name, levels_up,
switches 0:0e018d759a2a 1140 nested_dir=None):
switches 0:0e018d759a2a 1141 """
switches 0:0e018d759a2a 1142 Positional args:
switches 0:0e018d759a2a 1143 tool_key: the key to index TOOLCHAIN_PATHS
switches 0:0e018d759a2a 1144 executable_name: the toolchain's named executable (ex. armcc)
switches 0:0e018d759a2a 1145 levels_up: each toolchain joins the toolchain_path, some
switches 0:0e018d759a2a 1146 variable directories (bin, include), and the executable name,
switches 0:0e018d759a2a 1147 so the TOOLCHAIN_PATH value must be appropriately distanced
switches 0:0e018d759a2a 1148
switches 0:0e018d759a2a 1149 Keyword args:
switches 0:0e018d759a2a 1150 nested_dir: the directory within TOOLCHAIN_PATHS where the executable
switches 0:0e018d759a2a 1151 is found (ex: 'bin' for ARM\bin\armcc (necessary to check for path
switches 0:0e018d759a2a 1152 that will be used by toolchain's compile)
switches 0:0e018d759a2a 1153
switches 0:0e018d759a2a 1154 Returns True if the executable location specified by the user
switches 0:0e018d759a2a 1155 exists and is valid OR the executable can be found on the PATH.
switches 0:0e018d759a2a 1156 Returns False otherwise.
switches 0:0e018d759a2a 1157 """
switches 0:0e018d759a2a 1158 # Search PATH if user did not specify a path or specified path doesn't
switches 0:0e018d759a2a 1159 # exist.
switches 0:0e018d759a2a 1160 if not TOOLCHAIN_PATHS[tool_key] or not exists(TOOLCHAIN_PATHS[tool_key]):
switches 0:0e018d759a2a 1161 exe = find_executable(executable_name)
switches 0:0e018d759a2a 1162 if not exe:
switches 0:0e018d759a2a 1163 return False
switches 0:0e018d759a2a 1164 for level in range(levels_up):
switches 0:0e018d759a2a 1165 # move up the specified number of directories
switches 0:0e018d759a2a 1166 exe = dirname(exe)
switches 0:0e018d759a2a 1167 TOOLCHAIN_PATHS[tool_key] = exe
switches 0:0e018d759a2a 1168 if nested_dir:
switches 0:0e018d759a2a 1169 subdir = join(TOOLCHAIN_PATHS[tool_key], nested_dir,
switches 0:0e018d759a2a 1170 executable_name)
switches 0:0e018d759a2a 1171 else:
switches 0:0e018d759a2a 1172 subdir = join(TOOLCHAIN_PATHS[tool_key],executable_name)
switches 0:0e018d759a2a 1173 # User could have specified a path that exists but does not contain exe
switches 0:0e018d759a2a 1174 return exists(subdir) or exists(subdir +'.exe')
switches 0:0e018d759a2a 1175
switches 0:0e018d759a2a 1176 @abstractmethod
switches 0:0e018d759a2a 1177 def check_executable(self):
switches 0:0e018d759a2a 1178 """Returns True if the executable (armcc) location specified by the
switches 0:0e018d759a2a 1179 user exists OR the executable can be found on the PATH.
switches 0:0e018d759a2a 1180 Returns False otherwise."""
switches 0:0e018d759a2a 1181 raise NotImplemented
switches 0:0e018d759a2a 1182
switches 0:0e018d759a2a 1183 @abstractmethod
switches 0:0e018d759a2a 1184 def get_config_option(self, config_header):
switches 0:0e018d759a2a 1185 """Generate the compiler option that forces the inclusion of the configuration
switches 0:0e018d759a2a 1186 header file.
switches 0:0e018d759a2a 1187
switches 0:0e018d759a2a 1188 Positional arguments:
switches 0:0e018d759a2a 1189 config_header -- The configuration header that will be included within all source files
switches 0:0e018d759a2a 1190
switches 0:0e018d759a2a 1191 Return value:
switches 0:0e018d759a2a 1192 A list of the command line arguments that will force the inclusion the specified header
switches 0:0e018d759a2a 1193
switches 0:0e018d759a2a 1194 Side effects:
switches 0:0e018d759a2a 1195 None
switches 0:0e018d759a2a 1196 """
switches 0:0e018d759a2a 1197 raise NotImplemented
switches 0:0e018d759a2a 1198
switches 0:0e018d759a2a 1199 @abstractmethod
switches 0:0e018d759a2a 1200 def assemble(self, source, object, includes):
switches 0:0e018d759a2a 1201 """Generate the command line that assembles.
switches 0:0e018d759a2a 1202
switches 0:0e018d759a2a 1203 Positional arguments:
switches 0:0e018d759a2a 1204 source -- a file path that is the file to assemble
switches 0:0e018d759a2a 1205 object -- a file path that is the destination object
switches 0:0e018d759a2a 1206 includes -- a list of all directories where header files may be found
switches 0:0e018d759a2a 1207
switches 0:0e018d759a2a 1208 Return value:
switches 0:0e018d759a2a 1209 The complete command line, as a list, that would invoke the assembler
switches 0:0e018d759a2a 1210 on the source file, include all the include paths, and generate
switches 0:0e018d759a2a 1211 the specified object file.
switches 0:0e018d759a2a 1212
switches 0:0e018d759a2a 1213 Side effects:
switches 0:0e018d759a2a 1214 None
switches 0:0e018d759a2a 1215
switches 0:0e018d759a2a 1216 Note:
switches 0:0e018d759a2a 1217 This method should be decorated with @hook_tool.
switches 0:0e018d759a2a 1218 """
switches 0:0e018d759a2a 1219 raise NotImplemented
switches 0:0e018d759a2a 1220
switches 0:0e018d759a2a 1221 @abstractmethod
switches 0:0e018d759a2a 1222 def compile_c(self, source, object, includes):
switches 0:0e018d759a2a 1223 """Generate the command line that compiles a C source file.
switches 0:0e018d759a2a 1224
switches 0:0e018d759a2a 1225 Positional arguments:
switches 0:0e018d759a2a 1226 source -- the C source file to compile
switches 0:0e018d759a2a 1227 object -- the destination object file
switches 0:0e018d759a2a 1228 includes -- a list of all the directories where header files may be found
switches 0:0e018d759a2a 1229
switches 0:0e018d759a2a 1230 Return value:
switches 0:0e018d759a2a 1231 The complete command line, as a list, that would invoke the C compiler
switches 0:0e018d759a2a 1232 on the source file, include all the include paths, and generate the
switches 0:0e018d759a2a 1233 specified object file.
switches 0:0e018d759a2a 1234
switches 0:0e018d759a2a 1235 Side effects:
switches 0:0e018d759a2a 1236 None
switches 0:0e018d759a2a 1237
switches 0:0e018d759a2a 1238 Note:
switches 0:0e018d759a2a 1239 This method should be decorated with @hook_tool.
switches 0:0e018d759a2a 1240 """
switches 0:0e018d759a2a 1241 raise NotImplemented
switches 0:0e018d759a2a 1242
switches 0:0e018d759a2a 1243 @abstractmethod
switches 0:0e018d759a2a 1244 def compile_cpp(self, source, object, includes):
switches 0:0e018d759a2a 1245 """Generate the command line that compiles a C++ source file.
switches 0:0e018d759a2a 1246
switches 0:0e018d759a2a 1247 Positional arguments:
switches 0:0e018d759a2a 1248 source -- the C++ source file to compile
switches 0:0e018d759a2a 1249 object -- the destination object file
switches 0:0e018d759a2a 1250 includes -- a list of all the directories where header files may be found
switches 0:0e018d759a2a 1251
switches 0:0e018d759a2a 1252 Return value:
switches 0:0e018d759a2a 1253 The complete command line, as a list, that would invoke the C++ compiler
switches 0:0e018d759a2a 1254 on the source file, include all the include paths, and generate the
switches 0:0e018d759a2a 1255 specified object file.
switches 0:0e018d759a2a 1256
switches 0:0e018d759a2a 1257 Side effects:
switches 0:0e018d759a2a 1258 None
switches 0:0e018d759a2a 1259
switches 0:0e018d759a2a 1260 Note:
switches 0:0e018d759a2a 1261 This method should be decorated with @hook_tool.
switches 0:0e018d759a2a 1262 """
switches 0:0e018d759a2a 1263 raise NotImplemented
switches 0:0e018d759a2a 1264
switches 0:0e018d759a2a 1265 @abstractmethod
switches 0:0e018d759a2a 1266 def link(self, output, objects, libraries, lib_dirs, mem_map):
switches 0:0e018d759a2a 1267 """Run the linker to create an executable and memory map.
switches 0:0e018d759a2a 1268
switches 0:0e018d759a2a 1269 Positional arguments:
switches 0:0e018d759a2a 1270 output -- the file name to place the executable in
switches 0:0e018d759a2a 1271 objects -- all of the object files to link
switches 0:0e018d759a2a 1272 libraries -- all of the required libraries
switches 0:0e018d759a2a 1273 lib_dirs -- where the required libraries are located
switches 0:0e018d759a2a 1274 mem_map -- the location where the memory map file should be stored
switches 0:0e018d759a2a 1275
switches 0:0e018d759a2a 1276 Return value:
switches 0:0e018d759a2a 1277 None
switches 0:0e018d759a2a 1278
switches 0:0e018d759a2a 1279 Side effect:
switches 0:0e018d759a2a 1280 Runs the linker to produce the executable.
switches 0:0e018d759a2a 1281
switches 0:0e018d759a2a 1282 Note:
switches 0:0e018d759a2a 1283 This method should be decorated with @hook_tool.
switches 0:0e018d759a2a 1284 """
switches 0:0e018d759a2a 1285 raise NotImplemented
switches 0:0e018d759a2a 1286
switches 0:0e018d759a2a 1287 @abstractmethod
switches 0:0e018d759a2a 1288 def archive(self, objects, lib_path):
switches 0:0e018d759a2a 1289 """Run the command line that creates an archive.
switches 0:0e018d759a2a 1290
switches 0:0e018d759a2a 1291 Positional arguhments:
switches 0:0e018d759a2a 1292 objects -- a list of all the object files that should be archived
switches 0:0e018d759a2a 1293 lib_path -- the file name of the resulting library file
switches 0:0e018d759a2a 1294
switches 0:0e018d759a2a 1295 Return value:
switches 0:0e018d759a2a 1296 None
switches 0:0e018d759a2a 1297
switches 0:0e018d759a2a 1298 Side effect:
switches 0:0e018d759a2a 1299 Runs the archiving tool to produce the library file.
switches 0:0e018d759a2a 1300
switches 0:0e018d759a2a 1301 Note:
switches 0:0e018d759a2a 1302 This method should be decorated with @hook_tool.
switches 0:0e018d759a2a 1303 """
switches 0:0e018d759a2a 1304 raise NotImplemented
switches 0:0e018d759a2a 1305
switches 0:0e018d759a2a 1306 @abstractmethod
switches 0:0e018d759a2a 1307 def binary(self, resources, elf, bin):
switches 0:0e018d759a2a 1308 """Run the command line that will Extract a simplified binary file.
switches 0:0e018d759a2a 1309
switches 0:0e018d759a2a 1310 Positional arguments:
switches 0:0e018d759a2a 1311 resources -- A resources object (Is not used in any of the toolchains)
switches 0:0e018d759a2a 1312 elf -- the executable file that is to be converted
switches 0:0e018d759a2a 1313 bin -- the file name of the to be created simplified binary file
switches 0:0e018d759a2a 1314
switches 0:0e018d759a2a 1315 Return value:
switches 0:0e018d759a2a 1316 None
switches 0:0e018d759a2a 1317
switches 0:0e018d759a2a 1318 Side effect:
switches 0:0e018d759a2a 1319 Runs the elf2bin tool to produce the simplified binary file.
switches 0:0e018d759a2a 1320
switches 0:0e018d759a2a 1321 Note:
switches 0:0e018d759a2a 1322 This method should be decorated with @hook_tool.
switches 0:0e018d759a2a 1323 """
switches 0:0e018d759a2a 1324 raise NotImplemented
switches 0:0e018d759a2a 1325
switches 0:0e018d759a2a 1326 # Return the list of macros geenrated by the build system
switches 0:0e018d759a2a 1327 def get_config_macros(self):
switches 0:0e018d759a2a 1328 return Config.config_to_macros(self.config_data) if self.config_data else []
switches 0:0e018d759a2a 1329
switches 0:0e018d759a2a 1330 from tools.settings import ARM_PATH
switches 0:0e018d759a2a 1331 from tools.settings import GCC_ARM_PATH, GCC_CR_PATH
switches 0:0e018d759a2a 1332 from tools.settings import IAR_PATH
switches 0:0e018d759a2a 1333
switches 0:0e018d759a2a 1334 TOOLCHAIN_PATHS = {
switches 0:0e018d759a2a 1335 'ARM': ARM_PATH,
switches 0:0e018d759a2a 1336 'uARM': ARM_PATH,
switches 0:0e018d759a2a 1337 'GCC_ARM': GCC_ARM_PATH,
switches 0:0e018d759a2a 1338 'GCC_CR': GCC_CR_PATH,
switches 0:0e018d759a2a 1339 'IAR': IAR_PATH
switches 0:0e018d759a2a 1340 }
switches 0:0e018d759a2a 1341
switches 0:0e018d759a2a 1342 from tools.toolchains.arm import ARM_STD, ARM_MICRO
switches 0:0e018d759a2a 1343 from tools.toolchains.gcc import GCC_ARM, GCC_CR
switches 0:0e018d759a2a 1344 from tools.toolchains.iar import IAR
switches 0:0e018d759a2a 1345
switches 0:0e018d759a2a 1346 TOOLCHAIN_CLASSES = {
switches 0:0e018d759a2a 1347 'ARM': ARM_STD,
switches 0:0e018d759a2a 1348 'uARM': ARM_MICRO,
switches 0:0e018d759a2a 1349 'GCC_ARM': GCC_ARM,
switches 0:0e018d759a2a 1350 'GCC_CR': GCC_CR,
switches 0:0e018d759a2a 1351 'IAR': IAR
switches 0:0e018d759a2a 1352 }
switches 0:0e018d759a2a 1353
switches 0:0e018d759a2a 1354 TOOLCHAINS = set(TOOLCHAIN_CLASSES.keys())