mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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