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