Maxim mbed development library
tools/targets.py@0:0e018d759a2a, 2016-11-08 (annotated)
- Committer:
- switches
- Date:
- Tue Nov 08 18:27:11 2016 +0000
- Revision:
- 0:0e018d759a2a
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:0e018d759a2a | 1 | """ |
switches | 0:0e018d759a2a | 2 | mbed SDK |
switches | 0:0e018d759a2a | 3 | Copyright (c) 2011-2016 ARM Limited |
switches | 0:0e018d759a2a | 4 | |
switches | 0:0e018d759a2a | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
switches | 0:0e018d759a2a | 6 | you may not use this file except in compliance with the License. |
switches | 0:0e018d759a2a | 7 | You may obtain a copy of the License at |
switches | 0:0e018d759a2a | 8 | |
switches | 0:0e018d759a2a | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
switches | 0:0e018d759a2a | 10 | |
switches | 0:0e018d759a2a | 11 | Unless required by applicable law or agreed to in writing, software |
switches | 0:0e018d759a2a | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
switches | 0:0e018d759a2a | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
switches | 0:0e018d759a2a | 14 | See the License for the specific language governing permissions and |
switches | 0:0e018d759a2a | 15 | limitations under the License. |
switches | 0:0e018d759a2a | 16 | """ |
switches | 0:0e018d759a2a | 17 | |
switches | 0:0e018d759a2a | 18 | import os |
switches | 0:0e018d759a2a | 19 | import binascii |
switches | 0:0e018d759a2a | 20 | import struct |
switches | 0:0e018d759a2a | 21 | import shutil |
switches | 0:0e018d759a2a | 22 | import inspect |
switches | 0:0e018d759a2a | 23 | import sys |
switches | 0:0e018d759a2a | 24 | from collections import namedtuple |
switches | 0:0e018d759a2a | 25 | from tools.patch import patch |
switches | 0:0e018d759a2a | 26 | from tools.paths import TOOLS_BOOTLOADERS |
switches | 0:0e018d759a2a | 27 | from tools.utils import json_file_to_dict |
switches | 0:0e018d759a2a | 28 | |
switches | 0:0e018d759a2a | 29 | __all__ = ["target", "TARGETS", "TARGET_MAP", "TARGET_NAMES", "CORE_LABELS", |
switches | 0:0e018d759a2a | 30 | "HookError", "generate_py_target", "Target", |
switches | 0:0e018d759a2a | 31 | "CUMULATIVE_ATTRIBUTES", "get_resolution_order"] |
switches | 0:0e018d759a2a | 32 | |
switches | 0:0e018d759a2a | 33 | CORE_LABELS = { |
switches | 0:0e018d759a2a | 34 | "ARM7TDMI-S": ["ARM7", "LIKE_CORTEX_ARM7"], |
switches | 0:0e018d759a2a | 35 | "Cortex-M0" : ["M0", "CORTEX_M", "LIKE_CORTEX_M0"], |
switches | 0:0e018d759a2a | 36 | "Cortex-M0+": ["M0P", "CORTEX_M", "LIKE_CORTEX_M0"], |
switches | 0:0e018d759a2a | 37 | "Cortex-M1" : ["M1", "CORTEX_M", "LIKE_CORTEX_M1"], |
switches | 0:0e018d759a2a | 38 | "Cortex-M3" : ["M3", "CORTEX_M", "LIKE_CORTEX_M3"], |
switches | 0:0e018d759a2a | 39 | "Cortex-M4" : ["M4", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M4"], |
switches | 0:0e018d759a2a | 40 | "Cortex-M4F" : ["M4", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M4"], |
switches | 0:0e018d759a2a | 41 | "Cortex-M7" : ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7"], |
switches | 0:0e018d759a2a | 42 | "Cortex-M7F" : ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7"], |
switches | 0:0e018d759a2a | 43 | "Cortex-M7FD" : ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7"], |
switches | 0:0e018d759a2a | 44 | "Cortex-A9" : ["A9", "CORTEX_A", "LIKE_CORTEX_A9"] |
switches | 0:0e018d759a2a | 45 | } |
switches | 0:0e018d759a2a | 46 | |
switches | 0:0e018d759a2a | 47 | ################################################################################ |
switches | 0:0e018d759a2a | 48 | # Generic Target class that reads and interprets the data in targets.json |
switches | 0:0e018d759a2a | 49 | |
switches | 0:0e018d759a2a | 50 | class HookError(Exception): |
switches | 0:0e018d759a2a | 51 | """ A simple class that represents all the exceptions associated with |
switches | 0:0e018d759a2a | 52 | hooking |
switches | 0:0e018d759a2a | 53 | """ |
switches | 0:0e018d759a2a | 54 | pass |
switches | 0:0e018d759a2a | 55 | |
switches | 0:0e018d759a2a | 56 | CACHES = {} |
switches | 0:0e018d759a2a | 57 | def cached(func): |
switches | 0:0e018d759a2a | 58 | """A simple decorator used for automatically caching data returned by a |
switches | 0:0e018d759a2a | 59 | function |
switches | 0:0e018d759a2a | 60 | """ |
switches | 0:0e018d759a2a | 61 | def wrapper(*args, **kwargs): |
switches | 0:0e018d759a2a | 62 | """The wrapped function itself""" |
switches | 0:0e018d759a2a | 63 | if not CACHES.has_key((func.__name__, args)): |
switches | 0:0e018d759a2a | 64 | CACHES[(func.__name__, args)] = func(*args, **kwargs) |
switches | 0:0e018d759a2a | 65 | return CACHES[(func.__name__, args)] |
switches | 0:0e018d759a2a | 66 | return wrapper |
switches | 0:0e018d759a2a | 67 | |
switches | 0:0e018d759a2a | 68 | |
switches | 0:0e018d759a2a | 69 | # Cumulative attributes can have values appended to them, so they |
switches | 0:0e018d759a2a | 70 | # need to be computed differently than regular attributes |
switches | 0:0e018d759a2a | 71 | CUMULATIVE_ATTRIBUTES = ['extra_labels', 'macros', 'device_has', 'features'] |
switches | 0:0e018d759a2a | 72 | |
switches | 0:0e018d759a2a | 73 | |
switches | 0:0e018d759a2a | 74 | def get_resolution_order(json_data, target_name, order, level=0): |
switches | 0:0e018d759a2a | 75 | """ Return the order in which target descriptions are searched for |
switches | 0:0e018d759a2a | 76 | attributes. This mimics the Python 2.2 method resolution order, which |
switches | 0:0e018d759a2a | 77 | is what the old targets.py module used. For more details, check |
switches | 0:0e018d759a2a | 78 | http://makina-corpus.com/blog/metier/2014/python-tutorial-understanding-python-mro-class-search-path |
switches | 0:0e018d759a2a | 79 | The resolution order contains (name, level) tuples, where "name" is the |
switches | 0:0e018d759a2a | 80 | name of the class and "level" is the level in the inheritance hierarchy |
switches | 0:0e018d759a2a | 81 | (the target itself is at level 0, its first parent at level 1, its |
switches | 0:0e018d759a2a | 82 | parent's parent at level 2 and so on) |
switches | 0:0e018d759a2a | 83 | """ |
switches | 0:0e018d759a2a | 84 | # the resolution order can't contain duplicate target names |
switches | 0:0e018d759a2a | 85 | if target_name not in [l[0] for l in order]: |
switches | 0:0e018d759a2a | 86 | order.append((target_name, level)) |
switches | 0:0e018d759a2a | 87 | parents = json_data[target_name].get("inherits", []) |
switches | 0:0e018d759a2a | 88 | for par in parents: |
switches | 0:0e018d759a2a | 89 | order = get_resolution_order(json_data, par, order, level + 1) |
switches | 0:0e018d759a2a | 90 | return order |
switches | 0:0e018d759a2a | 91 | |
switches | 0:0e018d759a2a | 92 | |
switches | 0:0e018d759a2a | 93 | def target(name, json_data): |
switches | 0:0e018d759a2a | 94 | """Construct a target object""" |
switches | 0:0e018d759a2a | 95 | resolution_order = get_resolution_order(json_data, name, []) |
switches | 0:0e018d759a2a | 96 | resolution_order_names = [tgt for tgt, _ in resolution_order] |
switches | 0:0e018d759a2a | 97 | return Target(name=name, |
switches | 0:0e018d759a2a | 98 | json_data={key: value for key, value in json_data.items() |
switches | 0:0e018d759a2a | 99 | if key in resolution_order_names}, |
switches | 0:0e018d759a2a | 100 | resolution_order=resolution_order, |
switches | 0:0e018d759a2a | 101 | resolution_order_names=resolution_order_names) |
switches | 0:0e018d759a2a | 102 | |
switches | 0:0e018d759a2a | 103 | def generate_py_target(new_targets, name): |
switches | 0:0e018d759a2a | 104 | """Add one or more new target(s) represented as a Python dictionary |
switches | 0:0e018d759a2a | 105 | in 'new_targets'. It is an error to add a target with a name that |
switches | 0:0e018d759a2a | 106 | already exists. |
switches | 0:0e018d759a2a | 107 | """ |
switches | 0:0e018d759a2a | 108 | base_targets = Target.get_json_target_data() |
switches | 0:0e018d759a2a | 109 | for new_target in new_targets.keys(): |
switches | 0:0e018d759a2a | 110 | if new_target in base_targets: |
switches | 0:0e018d759a2a | 111 | raise Exception("Attempt to add target '%s' that already exists" |
switches | 0:0e018d759a2a | 112 | % new_target) |
switches | 0:0e018d759a2a | 113 | total_data = {} |
switches | 0:0e018d759a2a | 114 | total_data.update(new_targets) |
switches | 0:0e018d759a2a | 115 | total_data.update(base_targets) |
switches | 0:0e018d759a2a | 116 | return target(name, total_data) |
switches | 0:0e018d759a2a | 117 | |
switches | 0:0e018d759a2a | 118 | class Target(namedtuple("Target", "name json_data resolution_order resolution_order_names")): |
switches | 0:0e018d759a2a | 119 | """An object to represent a Target (MCU/Board)""" |
switches | 0:0e018d759a2a | 120 | |
switches | 0:0e018d759a2a | 121 | # Default location of the 'targets.json' file |
switches | 0:0e018d759a2a | 122 | __targets_json_location_default = os.path.join( |
switches | 0:0e018d759a2a | 123 | os.path.dirname(os.path.abspath(__file__)), '..', 'targets', 'targets.json') |
switches | 0:0e018d759a2a | 124 | |
switches | 0:0e018d759a2a | 125 | # Current/new location of the 'targets.json' file |
switches | 0:0e018d759a2a | 126 | __targets_json_location = None |
switches | 0:0e018d759a2a | 127 | |
switches | 0:0e018d759a2a | 128 | @staticmethod |
switches | 0:0e018d759a2a | 129 | @cached |
switches | 0:0e018d759a2a | 130 | def get_json_target_data(): |
switches | 0:0e018d759a2a | 131 | """Load the description of JSON target data""" |
switches | 0:0e018d759a2a | 132 | return json_file_to_dict(Target.__targets_json_location or |
switches | 0:0e018d759a2a | 133 | Target.__targets_json_location_default) |
switches | 0:0e018d759a2a | 134 | |
switches | 0:0e018d759a2a | 135 | @staticmethod |
switches | 0:0e018d759a2a | 136 | def set_targets_json_location(location=None): |
switches | 0:0e018d759a2a | 137 | """Set the location of the targets.json file""" |
switches | 0:0e018d759a2a | 138 | Target.__targets_json_location = (location or |
switches | 0:0e018d759a2a | 139 | Target.__targets_json_location_default) |
switches | 0:0e018d759a2a | 140 | # Invalidate caches, since the location of the JSON file changed |
switches | 0:0e018d759a2a | 141 | CACHES.clear() |
switches | 0:0e018d759a2a | 142 | |
switches | 0:0e018d759a2a | 143 | @staticmethod |
switches | 0:0e018d759a2a | 144 | @cached |
switches | 0:0e018d759a2a | 145 | def get_module_data(): |
switches | 0:0e018d759a2a | 146 | """Get the members of this module using Python's "inspect" module""" |
switches | 0:0e018d759a2a | 147 | return dict([(m[0], m[1]) for m in |
switches | 0:0e018d759a2a | 148 | inspect.getmembers(sys.modules[__name__])]) |
switches | 0:0e018d759a2a | 149 | |
switches | 0:0e018d759a2a | 150 | @staticmethod |
switches | 0:0e018d759a2a | 151 | def __add_paths_to_progen(data): |
switches | 0:0e018d759a2a | 152 | """Modify the exporter specification ("progen") by changing all |
switches | 0:0e018d759a2a | 153 | "template" keys to full paths |
switches | 0:0e018d759a2a | 154 | """ |
switches | 0:0e018d759a2a | 155 | out = {} |
switches | 0:0e018d759a2a | 156 | for key, val in data.items(): |
switches | 0:0e018d759a2a | 157 | if isinstance(val, dict): |
switches | 0:0e018d759a2a | 158 | out[key] = Target.__add_paths_to_progen(val) |
switches | 0:0e018d759a2a | 159 | elif key == "template": |
switches | 0:0e018d759a2a | 160 | out[key] = [os.path.join(os.path.dirname(__file__), 'export', v) |
switches | 0:0e018d759a2a | 161 | for v in val] |
switches | 0:0e018d759a2a | 162 | else: |
switches | 0:0e018d759a2a | 163 | out[key] = val |
switches | 0:0e018d759a2a | 164 | return out |
switches | 0:0e018d759a2a | 165 | |
switches | 0:0e018d759a2a | 166 | def __getattr_cumulative(self, attrname): |
switches | 0:0e018d759a2a | 167 | """Look for the attribute in the class and its parents, as defined by |
switches | 0:0e018d759a2a | 168 | the resolution order |
switches | 0:0e018d759a2a | 169 | """ |
switches | 0:0e018d759a2a | 170 | tdata = self.json_data |
switches | 0:0e018d759a2a | 171 | # For a cumulative attribute, figure out when it was defined the |
switches | 0:0e018d759a2a | 172 | # last time (in attribute resolution order) then follow the "_add" |
switches | 0:0e018d759a2a | 173 | # and "_remove" data fields |
switches | 0:0e018d759a2a | 174 | for idx, tgt in enumerate(self.resolution_order): |
switches | 0:0e018d759a2a | 175 | # the attribute was defined at this level in the resolution |
switches | 0:0e018d759a2a | 176 | # order |
switches | 0:0e018d759a2a | 177 | if attrname in tdata[tgt[0]]: |
switches | 0:0e018d759a2a | 178 | def_idx = idx |
switches | 0:0e018d759a2a | 179 | break |
switches | 0:0e018d759a2a | 180 | else: |
switches | 0:0e018d759a2a | 181 | raise AttributeError("Attribute '%s' not found in target '%s'" |
switches | 0:0e018d759a2a | 182 | % (attrname, self.name)) |
switches | 0:0e018d759a2a | 183 | # Get the starting value of the attribute |
switches | 0:0e018d759a2a | 184 | starting_value = (tdata[self.resolution_order[def_idx][0]][attrname] |
switches | 0:0e018d759a2a | 185 | or [])[:] |
switches | 0:0e018d759a2a | 186 | # Traverse the resolution list in high inheritance to low |
switches | 0:0e018d759a2a | 187 | # inheritance level, left to right order to figure out all the |
switches | 0:0e018d759a2a | 188 | # other classes that change the definition by adding or removing |
switches | 0:0e018d759a2a | 189 | # elements |
switches | 0:0e018d759a2a | 190 | for idx in xrange(self.resolution_order[def_idx][1] - 1, -1, -1): |
switches | 0:0e018d759a2a | 191 | same_level_targets = [tar[0] for tar in self.resolution_order |
switches | 0:0e018d759a2a | 192 | if tar[1] == idx] |
switches | 0:0e018d759a2a | 193 | for tar in same_level_targets: |
switches | 0:0e018d759a2a | 194 | data = tdata[tar] |
switches | 0:0e018d759a2a | 195 | # Do we have anything to add ? |
switches | 0:0e018d759a2a | 196 | if data.has_key(attrname + "_add"): |
switches | 0:0e018d759a2a | 197 | starting_value.extend(data[attrname + "_add"]) |
switches | 0:0e018d759a2a | 198 | # Do we have anything to remove ? |
switches | 0:0e018d759a2a | 199 | if data.has_key(attrname + "_remove"): |
switches | 0:0e018d759a2a | 200 | # Macros can be defined either without a value (MACRO) |
switches | 0:0e018d759a2a | 201 | # or with a value (MACRO=10). When removing, we specify |
switches | 0:0e018d759a2a | 202 | # only the name of the macro, without the value. So we |
switches | 0:0e018d759a2a | 203 | # need to create a mapping between the macro name and |
switches | 0:0e018d759a2a | 204 | # its value. This will work for extra_labels and other |
switches | 0:0e018d759a2a | 205 | # type of arrays as well, since they fall into the |
switches | 0:0e018d759a2a | 206 | # "macros without a value" category (simple definitions |
switches | 0:0e018d759a2a | 207 | # without a value). |
switches | 0:0e018d759a2a | 208 | name_def_map = {} |
switches | 0:0e018d759a2a | 209 | for crtv in starting_value: |
switches | 0:0e018d759a2a | 210 | if crtv.find('=') != -1: |
switches | 0:0e018d759a2a | 211 | temp = crtv.split('=') |
switches | 0:0e018d759a2a | 212 | if len(temp) != 2: |
switches | 0:0e018d759a2a | 213 | raise ValueError( |
switches | 0:0e018d759a2a | 214 | "Invalid macro definition '%s'" % crtv) |
switches | 0:0e018d759a2a | 215 | name_def_map[temp[0]] = crtv |
switches | 0:0e018d759a2a | 216 | else: |
switches | 0:0e018d759a2a | 217 | name_def_map[crtv] = crtv |
switches | 0:0e018d759a2a | 218 | for element in data[attrname + "_remove"]: |
switches | 0:0e018d759a2a | 219 | if element not in name_def_map: |
switches | 0:0e018d759a2a | 220 | raise ValueError( |
switches | 0:0e018d759a2a | 221 | ("Unable to remove '%s' in '%s.%s' since " |
switches | 0:0e018d759a2a | 222 | % (element, self.name, attrname)) + |
switches | 0:0e018d759a2a | 223 | "it doesn't exist") |
switches | 0:0e018d759a2a | 224 | starting_value.remove(name_def_map[element]) |
switches | 0:0e018d759a2a | 225 | return starting_value |
switches | 0:0e018d759a2a | 226 | |
switches | 0:0e018d759a2a | 227 | def __getattr_helper(self, attrname): |
switches | 0:0e018d759a2a | 228 | """Compute the value of a given target attribute""" |
switches | 0:0e018d759a2a | 229 | if attrname in CUMULATIVE_ATTRIBUTES: |
switches | 0:0e018d759a2a | 230 | return self.__getattr_cumulative(attrname) |
switches | 0:0e018d759a2a | 231 | else: |
switches | 0:0e018d759a2a | 232 | tdata = self.json_data |
switches | 0:0e018d759a2a | 233 | starting_value = None |
switches | 0:0e018d759a2a | 234 | for tgt in self.resolution_order: |
switches | 0:0e018d759a2a | 235 | data = tdata[tgt[0]] |
switches | 0:0e018d759a2a | 236 | if data.has_key(attrname): |
switches | 0:0e018d759a2a | 237 | starting_value = data[attrname] |
switches | 0:0e018d759a2a | 238 | break |
switches | 0:0e018d759a2a | 239 | else: # Attribute not found |
switches | 0:0e018d759a2a | 240 | raise AttributeError( |
switches | 0:0e018d759a2a | 241 | "Attribute '%s' not found in target '%s'" |
switches | 0:0e018d759a2a | 242 | % (attrname, self.name)) |
switches | 0:0e018d759a2a | 243 | # 'progen' needs the full path to the template (the path in JSON is |
switches | 0:0e018d759a2a | 244 | # relative to tools/export) |
switches | 0:0e018d759a2a | 245 | if attrname == "progen": |
switches | 0:0e018d759a2a | 246 | return self.__add_paths_to_progen(starting_value) |
switches | 0:0e018d759a2a | 247 | else: |
switches | 0:0e018d759a2a | 248 | return starting_value |
switches | 0:0e018d759a2a | 249 | |
switches | 0:0e018d759a2a | 250 | def __getattr__(self, attrname): |
switches | 0:0e018d759a2a | 251 | """ Return the value of an attribute. This function only computes the |
switches | 0:0e018d759a2a | 252 | attribute's value once, then adds it to the instance attributes (in |
switches | 0:0e018d759a2a | 253 | __dict__), so the next time it is returned directly |
switches | 0:0e018d759a2a | 254 | """ |
switches | 0:0e018d759a2a | 255 | result = self.__getattr_helper(attrname) |
switches | 0:0e018d759a2a | 256 | self.__dict__[attrname] = result |
switches | 0:0e018d759a2a | 257 | return result |
switches | 0:0e018d759a2a | 258 | |
switches | 0:0e018d759a2a | 259 | @staticmethod |
switches | 0:0e018d759a2a | 260 | @cached |
switches | 0:0e018d759a2a | 261 | def get_target(target_name): |
switches | 0:0e018d759a2a | 262 | """ Return the target instance starting from the target name """ |
switches | 0:0e018d759a2a | 263 | return target(target_name, Target.get_json_target_data()) |
switches | 0:0e018d759a2a | 264 | |
switches | 0:0e018d759a2a | 265 | |
switches | 0:0e018d759a2a | 266 | @property |
switches | 0:0e018d759a2a | 267 | def program_cycle_s(self): |
switches | 0:0e018d759a2a | 268 | """Special override for program_cycle_s as it's default value depends |
switches | 0:0e018d759a2a | 269 | upon is_disk_virtual |
switches | 0:0e018d759a2a | 270 | """ |
switches | 0:0e018d759a2a | 271 | try: |
switches | 0:0e018d759a2a | 272 | return self.__getattr__("program_cycle_s") |
switches | 0:0e018d759a2a | 273 | except AttributeError: |
switches | 0:0e018d759a2a | 274 | return 4 if self.is_disk_virtual else 1.5 |
switches | 0:0e018d759a2a | 275 | |
switches | 0:0e018d759a2a | 276 | @property |
switches | 0:0e018d759a2a | 277 | def labels(self): |
switches | 0:0e018d759a2a | 278 | """Get all possible labels for this target""" |
switches | 0:0e018d759a2a | 279 | labels = [self.name] + CORE_LABELS[self.core] + self.extra_labels |
switches | 0:0e018d759a2a | 280 | # Automatically define UVISOR_UNSUPPORTED if the target doesn't |
switches | 0:0e018d759a2a | 281 | # specifically define UVISOR_SUPPORTED |
switches | 0:0e018d759a2a | 282 | if "UVISOR_SUPPORTED" not in labels: |
switches | 0:0e018d759a2a | 283 | labels.append("UVISOR_UNSUPPORTED") |
switches | 0:0e018d759a2a | 284 | return labels |
switches | 0:0e018d759a2a | 285 | |
switches | 0:0e018d759a2a | 286 | def init_hooks(self, hook, toolchain_name): |
switches | 0:0e018d759a2a | 287 | """Initialize the post-build hooks for a toolchain. For now, this |
switches | 0:0e018d759a2a | 288 | function only allows "post binary" hooks (hooks that are executed |
switches | 0:0e018d759a2a | 289 | after the binary image is extracted from the executable file) |
switches | 0:0e018d759a2a | 290 | """ |
switches | 0:0e018d759a2a | 291 | |
switches | 0:0e018d759a2a | 292 | # If there's no hook, simply return |
switches | 0:0e018d759a2a | 293 | try: |
switches | 0:0e018d759a2a | 294 | hook_data = self.post_binary_hook |
switches | 0:0e018d759a2a | 295 | except AttributeError: |
switches | 0:0e018d759a2a | 296 | return |
switches | 0:0e018d759a2a | 297 | # A hook was found. The hook's name is in the format |
switches | 0:0e018d759a2a | 298 | # "classname.functionname" |
switches | 0:0e018d759a2a | 299 | temp = hook_data["function"].split(".") |
switches | 0:0e018d759a2a | 300 | if len(temp) != 2: |
switches | 0:0e018d759a2a | 301 | raise HookError( |
switches | 0:0e018d759a2a | 302 | ("Invalid format for hook '%s' in target '%s'" |
switches | 0:0e018d759a2a | 303 | % (hook_data["function"], self.name)) + |
switches | 0:0e018d759a2a | 304 | " (must be 'class_name.function_name')") |
switches | 0:0e018d759a2a | 305 | class_name, function_name = temp[0], temp[1] |
switches | 0:0e018d759a2a | 306 | # "class_name" must refer to a class in this file, so check if the |
switches | 0:0e018d759a2a | 307 | # class exists |
switches | 0:0e018d759a2a | 308 | mdata = self.get_module_data() |
switches | 0:0e018d759a2a | 309 | if not mdata.has_key(class_name) or \ |
switches | 0:0e018d759a2a | 310 | not inspect.isclass(mdata[class_name]): |
switches | 0:0e018d759a2a | 311 | raise HookError( |
switches | 0:0e018d759a2a | 312 | ("Class '%s' required by '%s' in target '%s'" |
switches | 0:0e018d759a2a | 313 | % (class_name, hook_data["function"], self.name)) + |
switches | 0:0e018d759a2a | 314 | " not found in targets.py") |
switches | 0:0e018d759a2a | 315 | # "function_name" must refer to a static function inside class |
switches | 0:0e018d759a2a | 316 | # "class_name" |
switches | 0:0e018d759a2a | 317 | cls = mdata[class_name] |
switches | 0:0e018d759a2a | 318 | if (not hasattr(cls, function_name)) or \ |
switches | 0:0e018d759a2a | 319 | (not inspect.isfunction(getattr(cls, function_name))): |
switches | 0:0e018d759a2a | 320 | raise HookError( |
switches | 0:0e018d759a2a | 321 | ("Static function '%s' " % function_name) + |
switches | 0:0e018d759a2a | 322 | ("required by '%s' " % hook_data["function"]) + |
switches | 0:0e018d759a2a | 323 | ("in target '%s' " % self.name) + |
switches | 0:0e018d759a2a | 324 | ("not found in class '%s'" % class_name)) |
switches | 0:0e018d759a2a | 325 | # Check if the hook specification also has target restrictions |
switches | 0:0e018d759a2a | 326 | toolchain_restrictions = hook_data.get("toolchains", []) |
switches | 0:0e018d759a2a | 327 | if toolchain_restrictions and \ |
switches | 0:0e018d759a2a | 328 | (toolchain_name not in toolchain_restrictions): |
switches | 0:0e018d759a2a | 329 | return |
switches | 0:0e018d759a2a | 330 | # Finally, hook the requested function |
switches | 0:0e018d759a2a | 331 | hook.hook_add_binary("post", getattr(cls, function_name)) |
switches | 0:0e018d759a2a | 332 | |
switches | 0:0e018d759a2a | 333 | ################################################################################ |
switches | 0:0e018d759a2a | 334 | # Target specific code goes in this section |
switches | 0:0e018d759a2a | 335 | # This code can be invoked from the target description using the |
switches | 0:0e018d759a2a | 336 | # "post_binary_hook" key |
switches | 0:0e018d759a2a | 337 | |
switches | 0:0e018d759a2a | 338 | class LPCTargetCode(object): |
switches | 0:0e018d759a2a | 339 | """General LPC Target patching code""" |
switches | 0:0e018d759a2a | 340 | @staticmethod |
switches | 0:0e018d759a2a | 341 | def lpc_patch(t_self, resources, elf, binf): |
switches | 0:0e018d759a2a | 342 | """Patch an elf file""" |
switches | 0:0e018d759a2a | 343 | t_self.debug("LPC Patch: %s" % os.path.split(binf)[1]) |
switches | 0:0e018d759a2a | 344 | patch(binf) |
switches | 0:0e018d759a2a | 345 | |
switches | 0:0e018d759a2a | 346 | class LPC4088Code(object): |
switches | 0:0e018d759a2a | 347 | """Code specific to the LPC4088""" |
switches | 0:0e018d759a2a | 348 | @staticmethod |
switches | 0:0e018d759a2a | 349 | def binary_hook(t_self, resources, elf, binf): |
switches | 0:0e018d759a2a | 350 | """Hook to be run after an elf file is built""" |
switches | 0:0e018d759a2a | 351 | if not os.path.isdir(binf): |
switches | 0:0e018d759a2a | 352 | # Regular binary file, nothing to do |
switches | 0:0e018d759a2a | 353 | LPCTargetCode.lpc_patch(t_self, resources, elf, binf) |
switches | 0:0e018d759a2a | 354 | return |
switches | 0:0e018d759a2a | 355 | outbin = open(binf + ".temp", "wb") |
switches | 0:0e018d759a2a | 356 | partf = open(os.path.join(binf, "ER_IROM1"), "rb") |
switches | 0:0e018d759a2a | 357 | # Pad the fist part (internal flash) with 0xFF to 512k |
switches | 0:0e018d759a2a | 358 | data = partf.read() |
switches | 0:0e018d759a2a | 359 | outbin.write(data) |
switches | 0:0e018d759a2a | 360 | outbin.write('\xFF' * (512*1024 - len(data))) |
switches | 0:0e018d759a2a | 361 | partf.close() |
switches | 0:0e018d759a2a | 362 | # Read and append the second part (external flash) in chunks of fixed |
switches | 0:0e018d759a2a | 363 | # size |
switches | 0:0e018d759a2a | 364 | chunksize = 128 * 1024 |
switches | 0:0e018d759a2a | 365 | partf = open(os.path.join(binf, "ER_IROM2"), "rb") |
switches | 0:0e018d759a2a | 366 | while True: |
switches | 0:0e018d759a2a | 367 | data = partf.read(chunksize) |
switches | 0:0e018d759a2a | 368 | outbin.write(data) |
switches | 0:0e018d759a2a | 369 | if len(data) < chunksize: |
switches | 0:0e018d759a2a | 370 | break |
switches | 0:0e018d759a2a | 371 | partf.close() |
switches | 0:0e018d759a2a | 372 | outbin.close() |
switches | 0:0e018d759a2a | 373 | # Remove the directory with the binary parts and rename the temporary |
switches | 0:0e018d759a2a | 374 | # file to 'binf' |
switches | 0:0e018d759a2a | 375 | shutil.rmtree(binf, True) |
switches | 0:0e018d759a2a | 376 | os.rename(binf + '.temp', binf) |
switches | 0:0e018d759a2a | 377 | t_self.debug("Generated custom binary file (internal flash + SPIFI)") |
switches | 0:0e018d759a2a | 378 | LPCTargetCode.lpc_patch(t_self, resources, elf, binf) |
switches | 0:0e018d759a2a | 379 | |
switches | 0:0e018d759a2a | 380 | class TEENSY3_1Code(object): |
switches | 0:0e018d759a2a | 381 | """Hooks for the TEENSY3.1""" |
switches | 0:0e018d759a2a | 382 | @staticmethod |
switches | 0:0e018d759a2a | 383 | def binary_hook(t_self, resources, elf, binf): |
switches | 0:0e018d759a2a | 384 | """Hook that is run after elf is generated""" |
switches | 0:0e018d759a2a | 385 | from intelhex import IntelHex |
switches | 0:0e018d759a2a | 386 | binh = IntelHex() |
switches | 0:0e018d759a2a | 387 | binh.loadbin(binf, offset=0) |
switches | 0:0e018d759a2a | 388 | |
switches | 0:0e018d759a2a | 389 | with open(binf.replace(".bin", ".hex"), "w") as file_desc: |
switches | 0:0e018d759a2a | 390 | binh.tofile(file_desc, format='hex') |
switches | 0:0e018d759a2a | 391 | |
switches | 0:0e018d759a2a | 392 | class MTSCode(object): |
switches | 0:0e018d759a2a | 393 | """Generic MTS code""" |
switches | 0:0e018d759a2a | 394 | @staticmethod |
switches | 0:0e018d759a2a | 395 | def _combine_bins_helper(target_name, binf): |
switches | 0:0e018d759a2a | 396 | """combine bins with the bootloader for a particular target""" |
switches | 0:0e018d759a2a | 397 | loader = os.path.join(TOOLS_BOOTLOADERS, target_name, "bootloader.bin") |
switches | 0:0e018d759a2a | 398 | target = binf + ".tmp" |
switches | 0:0e018d759a2a | 399 | if not os.path.exists(loader): |
switches | 0:0e018d759a2a | 400 | print "Can't find bootloader binary: " + loader |
switches | 0:0e018d759a2a | 401 | return |
switches | 0:0e018d759a2a | 402 | outbin = open(target, 'w+b') |
switches | 0:0e018d759a2a | 403 | part = open(loader, 'rb') |
switches | 0:0e018d759a2a | 404 | data = part.read() |
switches | 0:0e018d759a2a | 405 | outbin.write(data) |
switches | 0:0e018d759a2a | 406 | outbin.write('\xFF' * (64*1024 - len(data))) |
switches | 0:0e018d759a2a | 407 | part.close() |
switches | 0:0e018d759a2a | 408 | part = open(binf, 'rb') |
switches | 0:0e018d759a2a | 409 | data = part.read() |
switches | 0:0e018d759a2a | 410 | outbin.write(data) |
switches | 0:0e018d759a2a | 411 | part.close() |
switches | 0:0e018d759a2a | 412 | outbin.seek(0, 0) |
switches | 0:0e018d759a2a | 413 | data = outbin.read() |
switches | 0:0e018d759a2a | 414 | outbin.seek(0, 1) |
switches | 0:0e018d759a2a | 415 | crc = struct.pack('<I', binascii.crc32(data) & 0xFFFFFFFF) |
switches | 0:0e018d759a2a | 416 | outbin.write(crc) |
switches | 0:0e018d759a2a | 417 | outbin.close() |
switches | 0:0e018d759a2a | 418 | os.remove(binf) |
switches | 0:0e018d759a2a | 419 | os.rename(target, binf) |
switches | 0:0e018d759a2a | 420 | |
switches | 0:0e018d759a2a | 421 | @staticmethod |
switches | 0:0e018d759a2a | 422 | def combine_bins_mts_dot(t_self, resources, elf, binf): |
switches | 0:0e018d759a2a | 423 | """A hook for the MTS MDOT""" |
switches | 0:0e018d759a2a | 424 | MTSCode._combine_bins_helper("MTS_MDOT_F411RE", binf) |
switches | 0:0e018d759a2a | 425 | |
switches | 0:0e018d759a2a | 426 | @staticmethod |
switches | 0:0e018d759a2a | 427 | def combine_bins_mts_dragonfly(t_self, resources, elf, binf): |
switches | 0:0e018d759a2a | 428 | """A hoof for the MTS Dragonfly""" |
switches | 0:0e018d759a2a | 429 | MTSCode._combine_bins_helper("MTS_DRAGONFLY_F411RE", binf) |
switches | 0:0e018d759a2a | 430 | |
switches | 0:0e018d759a2a | 431 | class MCU_NRF51Code(object): |
switches | 0:0e018d759a2a | 432 | """NRF51 Hooks""" |
switches | 0:0e018d759a2a | 433 | @staticmethod |
switches | 0:0e018d759a2a | 434 | def binary_hook(t_self, resources, _, binf): |
switches | 0:0e018d759a2a | 435 | """Hook that merges the soft device with the bin file""" |
switches | 0:0e018d759a2a | 436 | # Scan to find the actual paths of soft device |
switches | 0:0e018d759a2a | 437 | sdf = None |
switches | 0:0e018d759a2a | 438 | for softdevice_and_offset_entry\ |
switches | 0:0e018d759a2a | 439 | in t_self.target.EXPECTED_SOFTDEVICES_WITH_OFFSETS: |
switches | 0:0e018d759a2a | 440 | for hexf in resources.hex_files: |
switches | 0:0e018d759a2a | 441 | if hexf.find(softdevice_and_offset_entry['name']) != -1: |
switches | 0:0e018d759a2a | 442 | t_self.debug("SoftDevice file found %s." |
switches | 0:0e018d759a2a | 443 | % softdevice_and_offset_entry['name']) |
switches | 0:0e018d759a2a | 444 | sdf = hexf |
switches | 0:0e018d759a2a | 445 | |
switches | 0:0e018d759a2a | 446 | if sdf is not None: |
switches | 0:0e018d759a2a | 447 | break |
switches | 0:0e018d759a2a | 448 | if sdf is not None: |
switches | 0:0e018d759a2a | 449 | break |
switches | 0:0e018d759a2a | 450 | |
switches | 0:0e018d759a2a | 451 | if sdf is None: |
switches | 0:0e018d759a2a | 452 | t_self.debug("Hex file not found. Aborting.") |
switches | 0:0e018d759a2a | 453 | return |
switches | 0:0e018d759a2a | 454 | |
switches | 0:0e018d759a2a | 455 | # Look for bootloader file that matches this soft device or bootloader |
switches | 0:0e018d759a2a | 456 | # override image |
switches | 0:0e018d759a2a | 457 | blf = None |
switches | 0:0e018d759a2a | 458 | if t_self.target.MERGE_BOOTLOADER is True: |
switches | 0:0e018d759a2a | 459 | for hexf in resources.hex_files: |
switches | 0:0e018d759a2a | 460 | if hexf.find(t_self.target.OVERRIDE_BOOTLOADER_FILENAME) != -1: |
switches | 0:0e018d759a2a | 461 | t_self.debug("Bootloader file found %s." |
switches | 0:0e018d759a2a | 462 | % t_self.target.OVERRIDE_BOOTLOADER_FILENAME) |
switches | 0:0e018d759a2a | 463 | blf = hexf |
switches | 0:0e018d759a2a | 464 | break |
switches | 0:0e018d759a2a | 465 | elif hexf.find(softdevice_and_offset_entry['boot']) != -1: |
switches | 0:0e018d759a2a | 466 | t_self.debug("Bootloader file found %s." |
switches | 0:0e018d759a2a | 467 | % softdevice_and_offset_entry['boot']) |
switches | 0:0e018d759a2a | 468 | blf = hexf |
switches | 0:0e018d759a2a | 469 | break |
switches | 0:0e018d759a2a | 470 | |
switches | 0:0e018d759a2a | 471 | # Merge user code with softdevice |
switches | 0:0e018d759a2a | 472 | from intelhex import IntelHex |
switches | 0:0e018d759a2a | 473 | binh = IntelHex() |
switches | 0:0e018d759a2a | 474 | binh.loadbin(binf, offset=softdevice_and_offset_entry['offset']) |
switches | 0:0e018d759a2a | 475 | |
switches | 0:0e018d759a2a | 476 | if t_self.target.MERGE_SOFT_DEVICE is True: |
switches | 0:0e018d759a2a | 477 | t_self.debug("Merge SoftDevice file %s" |
switches | 0:0e018d759a2a | 478 | % softdevice_and_offset_entry['name']) |
switches | 0:0e018d759a2a | 479 | sdh = IntelHex(sdf) |
switches | 0:0e018d759a2a | 480 | binh.merge(sdh) |
switches | 0:0e018d759a2a | 481 | |
switches | 0:0e018d759a2a | 482 | if t_self.target.MERGE_BOOTLOADER is True and blf is not None: |
switches | 0:0e018d759a2a | 483 | t_self.debug("Merge BootLoader file %s" % blf) |
switches | 0:0e018d759a2a | 484 | blh = IntelHex(blf) |
switches | 0:0e018d759a2a | 485 | binh.merge(blh) |
switches | 0:0e018d759a2a | 486 | |
switches | 0:0e018d759a2a | 487 | with open(binf.replace(".bin", ".hex"), "w") as fileout: |
switches | 0:0e018d759a2a | 488 | binh.tofile(fileout, format='hex') |
switches | 0:0e018d759a2a | 489 | |
switches | 0:0e018d759a2a | 490 | class NCS36510TargetCode: |
switches | 0:0e018d759a2a | 491 | @staticmethod |
switches | 0:0e018d759a2a | 492 | def ncs36510_addfib(t_self, resources, elf, binf): |
switches | 0:0e018d759a2a | 493 | from tools.add_fib import add_fib_at_start |
switches | 0:0e018d759a2a | 494 | print("binf ", binf) |
switches | 0:0e018d759a2a | 495 | add_fib_at_start(binf[:-4]) |
switches | 0:0e018d759a2a | 496 | ################################################################################ |
switches | 0:0e018d759a2a | 497 | |
switches | 0:0e018d759a2a | 498 | # Instantiate all public targets |
switches | 0:0e018d759a2a | 499 | TARGETS = [Target.get_target(name) for name, value |
switches | 0:0e018d759a2a | 500 | in Target.get_json_target_data().items() |
switches | 0:0e018d759a2a | 501 | if value.get("public", True)] |
switches | 0:0e018d759a2a | 502 | |
switches | 0:0e018d759a2a | 503 | # Map each target name to its unique instance |
switches | 0:0e018d759a2a | 504 | TARGET_MAP = dict([(t.name, t) for t in TARGETS]) |
switches | 0:0e018d759a2a | 505 | |
switches | 0:0e018d759a2a | 506 | TARGET_NAMES = TARGET_MAP.keys() |
switches | 0:0e018d759a2a | 507 | |
switches | 0:0e018d759a2a | 508 | # Some targets with different name have the same exporters |
switches | 0:0e018d759a2a | 509 | EXPORT_MAP = {} |
switches | 0:0e018d759a2a | 510 | |
switches | 0:0e018d759a2a | 511 | # Detection APIs |
switches | 0:0e018d759a2a | 512 | def get_target_detect_codes(): |
switches | 0:0e018d759a2a | 513 | """ Returns dictionary mapping detect_code -> platform_name |
switches | 0:0e018d759a2a | 514 | """ |
switches | 0:0e018d759a2a | 515 | result = {} |
switches | 0:0e018d759a2a | 516 | for tgt in TARGETS: |
switches | 0:0e018d759a2a | 517 | for detect_code in tgt.detect_code: |
switches | 0:0e018d759a2a | 518 | result[detect_code] = tgt.name |
switches | 0:0e018d759a2a | 519 | return result |
switches | 0:0e018d759a2a | 520 | |
switches | 0:0e018d759a2a | 521 | def set_targets_json_location(location=None): |
switches | 0:0e018d759a2a | 522 | """Sets the location of the JSON file that contains the targets""" |
switches | 0:0e018d759a2a | 523 | # First instruct Target about the new location |
switches | 0:0e018d759a2a | 524 | Target.set_targets_json_location(location) |
switches | 0:0e018d759a2a | 525 | # Then re-initialize TARGETS, TARGET_MAP and TARGET_NAMES. The |
switches | 0:0e018d759a2a | 526 | # re-initialization does not create new variables, it keeps the old ones |
switches | 0:0e018d759a2a | 527 | # instead. This ensures compatibility with code that does |
switches | 0:0e018d759a2a | 528 | # "from tools.targets import TARGET_NAMES" |
switches | 0:0e018d759a2a | 529 | TARGETS[:] = [Target.get_target(tgt) for tgt, obj |
switches | 0:0e018d759a2a | 530 | in Target.get_json_target_data().items() |
switches | 0:0e018d759a2a | 531 | if obj.get("public", True)] |
switches | 0:0e018d759a2a | 532 | TARGET_MAP.clear() |
switches | 0:0e018d759a2a | 533 | TARGET_MAP.update(dict([(tgt.name, tgt) for tgt in TARGETS])) |
switches | 0:0e018d759a2a | 534 | TARGET_NAMES[:] = TARGET_MAP.keys() |