Clone of official tools

Committer:
theotherjimmy
Date:
Tue Oct 10 16:56:30 2017 -0500
Revision:
40:7d3fa6b99b2b
Parent:
36:96847d42f010
Child:
43:2a7da56ebd24
Update to tools release 5.6.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:66f3b5499f7f 1 """Just a template for subclassing"""
The Other Jimmy 31:8ea194f6145b 2 import os
The Other Jimmy 31:8ea194f6145b 3 from abc import abstractmethod, ABCMeta
The Other Jimmy 31:8ea194f6145b 4 import logging
The Other Jimmy 35:da9c89f8be7d 5 from os.path import join, dirname, relpath, basename, realpath, normpath
The Other Jimmy 31:8ea194f6145b 6 from itertools import groupby
The Other Jimmy 35:da9c89f8be7d 7 from jinja2 import FileSystemLoader, StrictUndefined
screamer 0:66f3b5499f7f 8 from jinja2.environment import Environment
The Other Jimmy 31:8ea194f6145b 9 import copy
screamer 0:66f3b5499f7f 10
screamer 0:66f3b5499f7f 11 from tools.targets import TARGET_MAP
screamer 0:66f3b5499f7f 12
screamer 0:66f3b5499f7f 13
The Other Jimmy 31:8ea194f6145b 14 class TargetNotSupportedException(Exception):
The Other Jimmy 31:8ea194f6145b 15 """Indicates that an IDE does not support a particular MCU"""
The Other Jimmy 31:8ea194f6145b 16 pass
screamer 13:ab47a20b66f0 17
screamer 24:25bff2709c20 18 class ExporterTargetsProperty(object):
The Other Jimmy 31:8ea194f6145b 19 """ Exporter descriptor for TARGETS
The Other Jimmy 31:8ea194f6145b 20 TARGETS as class attribute for backward compatibility
The Other Jimmy 31:8ea194f6145b 21 (allows: if in Exporter.TARGETS)
The Other Jimmy 31:8ea194f6145b 22 """
screamer 24:25bff2709c20 23 def __init__(self, func):
screamer 24:25bff2709c20 24 self.func = func
screamer 24:25bff2709c20 25 def __get__(self, inst, cls):
screamer 24:25bff2709c20 26 return self.func(cls)
screamer 24:25bff2709c20 27
theotherjimmy 40:7d3fa6b99b2b 28 def deprecated_exporter(CLS):
theotherjimmy 40:7d3fa6b99b2b 29 old_init = CLS.__init__
theotherjimmy 40:7d3fa6b99b2b 30 old_name = CLS.NAME
theotherjimmy 40:7d3fa6b99b2b 31 def __init__(*args, **kwargs):
theotherjimmy 40:7d3fa6b99b2b 32 print("==================== DEPRECATION NOTICE ====================")
theotherjimmy 40:7d3fa6b99b2b 33 print("The exporter %s is no longer maintained, and deprecated." % old_name)
theotherjimmy 40:7d3fa6b99b2b 34 print("%s will be removed from mbed OS for the mbed OS 5.6 release." % old_name)
theotherjimmy 40:7d3fa6b99b2b 35 old_init(*args, **kwargs)
theotherjimmy 40:7d3fa6b99b2b 36 CLS.__init__ = __init__
theotherjimmy 40:7d3fa6b99b2b 37 CLS.NAME = "%s (DEPRECATED)" % old_name
theotherjimmy 40:7d3fa6b99b2b 38 return CLS
theotherjimmy 40:7d3fa6b99b2b 39
screamer 0:66f3b5499f7f 40 class Exporter(object):
The Other Jimmy 31:8ea194f6145b 41 """Exporter base class
The Other Jimmy 31:8ea194f6145b 42
The Other Jimmy 31:8ea194f6145b 43 This class is meant to be extended by individual exporters, and provides a
The Other Jimmy 31:8ea194f6145b 44 few helper methods for implementing an exporter with either jinja2 or
The Other Jimmy 31:8ea194f6145b 45 progen.
The Other Jimmy 31:8ea194f6145b 46 """
The Other Jimmy 31:8ea194f6145b 47 __metaclass__ = ABCMeta
screamer 0:66f3b5499f7f 48 TEMPLATE_DIR = dirname(__file__)
screamer 0:66f3b5499f7f 49 DOT_IN_RELATIVE_PATH = False
The Other Jimmy 31:8ea194f6145b 50 NAME = None
theotherjimmy 40:7d3fa6b99b2b 51 TARGETS = set()
The Other Jimmy 31:8ea194f6145b 52 TOOLCHAIN = None
screamer 0:66f3b5499f7f 53
The Other Jimmy 31:8ea194f6145b 54
The Other Jimmy 31:8ea194f6145b 55 def __init__(self, target, export_dir, project_name, toolchain,
The Other Jimmy 31:8ea194f6145b 56 extra_symbols=None, resources=None):
The Other Jimmy 31:8ea194f6145b 57 """Initialize an instance of class exporter
The Other Jimmy 31:8ea194f6145b 58 Positional arguments:
The Other Jimmy 31:8ea194f6145b 59 target - the target mcu/board for this project
The Other Jimmy 31:8ea194f6145b 60 export_dir - the directory of the exported project files
The Other Jimmy 31:8ea194f6145b 61 project_name - the name of the project
The Other Jimmy 31:8ea194f6145b 62 toolchain - an instance of class toolchain
The Other Jimmy 31:8ea194f6145b 63
The Other Jimmy 31:8ea194f6145b 64 Keyword arguments:
The Other Jimmy 31:8ea194f6145b 65 extra_symbols - a list of extra macros for the toolchain
The Other Jimmy 31:8ea194f6145b 66 resources - an instance of class Resources
The Other Jimmy 31:8ea194f6145b 67 """
The Other Jimmy 31:8ea194f6145b 68 self.export_dir = export_dir
screamer 0:66f3b5499f7f 69 self.target = target
The Other Jimmy 31:8ea194f6145b 70 self.project_name = project_name
The Other Jimmy 31:8ea194f6145b 71 self.toolchain = toolchain
screamer 0:66f3b5499f7f 72 jinja_loader = FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))
screamer 0:66f3b5499f7f 73 self.jinja_environment = Environment(loader=jinja_loader)
The Other Jimmy 31:8ea194f6145b 74 self.resources = resources
theotherjimmy 40:7d3fa6b99b2b 75 self.generated_files = []
theotherjimmy 40:7d3fa6b99b2b 76 self.static_files = (
theotherjimmy 40:7d3fa6b99b2b 77 join(self.TEMPLATE_DIR, "GettingStarted.html"),
theotherjimmy 40:7d3fa6b99b2b 78 join(self.TEMPLATE_DIR, ".mbed"),
theotherjimmy 40:7d3fa6b99b2b 79 )
The Other Jimmy 31:8ea194f6145b 80 self.builder_files_dict = {}
The Other Jimmy 31:8ea194f6145b 81 self.add_config()
screamer 0:66f3b5499f7f 82
screamer 0:66f3b5499f7f 83 def get_toolchain(self):
The Other Jimmy 31:8ea194f6145b 84 """A helper getter function that we should probably eliminate"""
screamer 0:66f3b5499f7f 85 return self.TOOLCHAIN
screamer 0:66f3b5499f7f 86
The Other Jimmy 31:8ea194f6145b 87 def add_config(self):
The Other Jimmy 31:8ea194f6145b 88 """Add the containgin directory of mbed_config.h to include dirs"""
The Other Jimmy 31:8ea194f6145b 89 config = self.toolchain.get_config_header()
The Other Jimmy 31:8ea194f6145b 90 if config:
The Other Jimmy 31:8ea194f6145b 91 self.resources.inc_dirs.append(
The Other Jimmy 31:8ea194f6145b 92 dirname(relpath(config,
The Other Jimmy 31:8ea194f6145b 93 self.resources.file_basepath[config])))
The Other Jimmy 31:8ea194f6145b 94
screamer 13:ab47a20b66f0 95 @property
screamer 13:ab47a20b66f0 96 def flags(self):
The Other Jimmy 31:8ea194f6145b 97 """Returns a dictionary of toolchain flags.
The Other Jimmy 31:8ea194f6145b 98 Keys of the dictionary are:
The Other Jimmy 31:8ea194f6145b 99 cxx_flags - c++ flags
The Other Jimmy 31:8ea194f6145b 100 c_flags - c flags
The Other Jimmy 31:8ea194f6145b 101 ld_flags - linker flags
The Other Jimmy 31:8ea194f6145b 102 asm_flags - assembler flags
The Other Jimmy 31:8ea194f6145b 103 common_flags - common options
The Other Jimmy 31:8ea194f6145b 104 """
The Other Jimmy 31:8ea194f6145b 105 config_header = self.toolchain.get_config_header()
The Other Jimmy 31:8ea194f6145b 106 flags = {key + "_flags": copy.deepcopy(value) for key, value
The Other Jimmy 31:8ea194f6145b 107 in self.toolchain.flags.iteritems()}
theotherjimmy 40:7d3fa6b99b2b 108 asm_defines = self.toolchain.get_compile_options(
theotherjimmy 40:7d3fa6b99b2b 109 self.toolchain.get_symbols(for_asm=True),
theotherjimmy 40:7d3fa6b99b2b 110 filter(None, self.resources.inc_dirs),
theotherjimmy 40:7d3fa6b99b2b 111 for_asm=True)
The Other Jimmy 31:8ea194f6145b 112 c_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols()]
The Other Jimmy 31:8ea194f6145b 113 flags['asm_flags'] += asm_defines
The Other Jimmy 31:8ea194f6145b 114 flags['c_flags'] += c_defines
The Other Jimmy 31:8ea194f6145b 115 flags['cxx_flags'] += c_defines
The Other Jimmy 31:8ea194f6145b 116 if config_header:
The Other Jimmy 31:8ea194f6145b 117 config_header = relpath(config_header,
The Other Jimmy 31:8ea194f6145b 118 self.resources.file_basepath[config_header])
The Other Jimmy 31:8ea194f6145b 119 flags['c_flags'] += self.toolchain.get_config_option(config_header)
The Other Jimmy 31:8ea194f6145b 120 flags['cxx_flags'] += self.toolchain.get_config_option(
The Other Jimmy 31:8ea194f6145b 121 config_header)
The Other Jimmy 31:8ea194f6145b 122 return flags
The Other Jimmy 31:8ea194f6145b 123
The Other Jimmy 31:8ea194f6145b 124 def get_source_paths(self):
The Other Jimmy 31:8ea194f6145b 125 """Returns a list of the directories where source files are contained"""
The Other Jimmy 31:8ea194f6145b 126 source_keys = ['s_sources', 'c_sources', 'cpp_sources', 'hex_files',
The Other Jimmy 31:8ea194f6145b 127 'objects', 'libraries']
The Other Jimmy 31:8ea194f6145b 128 source_files = []
The Other Jimmy 31:8ea194f6145b 129 for key in source_keys:
The Other Jimmy 31:8ea194f6145b 130 source_files.extend(getattr(self.resources, key))
The Other Jimmy 31:8ea194f6145b 131 return list(set([os.path.dirname(src) for src in source_files]))
screamer 13:ab47a20b66f0 132
The Other Jimmy 35:da9c89f8be7d 133 def gen_file(self, template_file, data, target_file, **kwargs):
The Other Jimmy 31:8ea194f6145b 134 """Generates a project file from a template using jinja"""
The Other Jimmy 31:8ea194f6145b 135 jinja_loader = FileSystemLoader(
The Other Jimmy 31:8ea194f6145b 136 os.path.dirname(os.path.abspath(__file__)))
The Other Jimmy 35:da9c89f8be7d 137 jinja_environment = Environment(loader=jinja_loader,
The Other Jimmy 35:da9c89f8be7d 138 undefined=StrictUndefined, **kwargs)
The Other Jimmy 31:8ea194f6145b 139
The Other Jimmy 31:8ea194f6145b 140 template = jinja_environment.get_template(template_file)
The Other Jimmy 31:8ea194f6145b 141 target_text = template.render(data)
The Other Jimmy 31:8ea194f6145b 142
The Other Jimmy 31:8ea194f6145b 143 target_path = join(self.export_dir, target_file)
The Other Jimmy 31:8ea194f6145b 144 logging.debug("Generating: %s", target_path)
The Other Jimmy 31:8ea194f6145b 145 open(target_path, "w").write(target_text)
The Other Jimmy 31:8ea194f6145b 146 self.generated_files += [target_path]
screamer 13:ab47a20b66f0 147
The Other Jimmy 31:8ea194f6145b 148 def make_key(self, src):
The Other Jimmy 31:8ea194f6145b 149 """From a source file, extract group name
The Other Jimmy 31:8ea194f6145b 150 Positional Arguments:
The Other Jimmy 31:8ea194f6145b 151 src - the src's location
The Other Jimmy 31:8ea194f6145b 152 """
The Other Jimmy 35:da9c89f8be7d 153 rel_path = relpath(src, self.resources.file_basepath[src])
The Other Jimmy 35:da9c89f8be7d 154 path_list = os.path.normpath(rel_path).split(os.sep)
The Other Jimmy 35:da9c89f8be7d 155 assert len(path_list) >= 1
The Other Jimmy 35:da9c89f8be7d 156 if len(path_list) == 1:
The Other Jimmy 35:da9c89f8be7d 157 key = self.project_name
The Other Jimmy 35:da9c89f8be7d 158 else:
The Other Jimmy 35:da9c89f8be7d 159 key = path_list[0]
The Other Jimmy 31:8ea194f6145b 160 return key
screamer 0:66f3b5499f7f 161
The Other Jimmy 31:8ea194f6145b 162 def group_project_files(self, sources):
The Other Jimmy 31:8ea194f6145b 163 """Group the source files by their encompassing directory
The Other Jimmy 31:8ea194f6145b 164 Positional Arguments:
The Other Jimmy 31:8ea194f6145b 165 sources - array of source locations
The Other Jimmy 31:8ea194f6145b 166
The Other Jimmy 31:8ea194f6145b 167 Returns a dictionary of {group name: list of source locations}
The Other Jimmy 31:8ea194f6145b 168 """
The Other Jimmy 31:8ea194f6145b 169 data = sorted(sources, key=self.make_key)
The Other Jimmy 31:8ea194f6145b 170 return {k: list(g) for k,g in groupby(data, self.make_key)}
screamer 0:66f3b5499f7f 171
screamer 0:66f3b5499f7f 172 @staticmethod
The Other Jimmy 31:8ea194f6145b 173 def build(project_name, log_name='build_log.txt', cleanup=True):
The Other Jimmy 31:8ea194f6145b 174 """Invoke exporters build command within a subprocess.
The Other Jimmy 31:8ea194f6145b 175 This method is assumed to be executed at the same level as exporter
The Other Jimmy 31:8ea194f6145b 176 project files and project source code.
The Other Jimmy 31:8ea194f6145b 177 See uvision/__init__.py, iar/__init__.py, and makefile/__init__.py for
The Other Jimmy 31:8ea194f6145b 178 example implemenation.
screamer 0:66f3b5499f7f 179
The Other Jimmy 31:8ea194f6145b 180 Positional Arguments:
The Other Jimmy 31:8ea194f6145b 181 project_name - the name of the project to build; often required by
The Other Jimmy 31:8ea194f6145b 182 exporter's build command.
screamer 0:66f3b5499f7f 183
The Other Jimmy 31:8ea194f6145b 184 Keyword Args:
The Other Jimmy 31:8ea194f6145b 185 log_name - name of the build log to create. Written and printed out,
The Other Jimmy 31:8ea194f6145b 186 deleted if cleanup = True
The Other Jimmy 31:8ea194f6145b 187 cleanup - a boolean dictating whether exported project files and
The Other Jimmy 31:8ea194f6145b 188 build log are removed after build
screamer 0:66f3b5499f7f 189
The Other Jimmy 31:8ea194f6145b 190 Returns -1 on failure and 0 on success
screamer 0:66f3b5499f7f 191 """
The Other Jimmy 31:8ea194f6145b 192 raise NotImplemented("Implement in derived Exporter class.")
screamer 29:1210849dba19 193
The Other Jimmy 31:8ea194f6145b 194 @abstractmethod
The Other Jimmy 31:8ea194f6145b 195 def generate(self):
The Other Jimmy 31:8ea194f6145b 196 """Generate an IDE/tool specific project file"""
The Other Jimmy 31:8ea194f6145b 197 raise NotImplemented("Implement a generate function in Exporter child class")
The Other Jimmy 36:96847d42f010 198
theotherjimmy 40:7d3fa6b99b2b 199 @classmethod
theotherjimmy 40:7d3fa6b99b2b 200 def is_target_supported(cls, target_name):
theotherjimmy 40:7d3fa6b99b2b 201 """Query support for a particular target
The Other Jimmy 36:96847d42f010 202
theotherjimmy 40:7d3fa6b99b2b 203 NOTE: override this method if your exporter does not provide a static list of targets
theotherjimmy 40:7d3fa6b99b2b 204
theotherjimmy 40:7d3fa6b99b2b 205 Positional Arguments:
theotherjimmy 40:7d3fa6b99b2b 206 target_name - the name of the target.
theotherjimmy 40:7d3fa6b99b2b 207 """
theotherjimmy 40:7d3fa6b99b2b 208 target = TARGET_MAP[target_name]
theotherjimmy 40:7d3fa6b99b2b 209 return bool(set(target.resolution_order_names).intersection(set(cls.TARGETS))) \
theotherjimmy 40:7d3fa6b99b2b 210 and cls.TOOLCHAIN in target.supported_toolchains
theotherjimmy 40:7d3fa6b99b2b 211
theotherjimmy 40:7d3fa6b99b2b 212
theotherjimmy 40:7d3fa6b99b2b 213 @classmethod
theotherjimmy 40:7d3fa6b99b2b 214 def all_supported_targets(cls):
theotherjimmy 40:7d3fa6b99b2b 215 return [t for t in TARGET_MAP.keys() if cls.is_target_supported(t)]
theotherjimmy 40:7d3fa6b99b2b 216
theotherjimmy 40:7d3fa6b99b2b 217
theotherjimmy 40:7d3fa6b99b2b 218 def apply_supported_whitelist(compiler, whitelist, target):
The Other Jimmy 36:96847d42f010 219 """Generate a list of supported targets for a given compiler and post-binary hook
The Other Jimmy 36:96847d42f010 220 white-list."""
theotherjimmy 40:7d3fa6b99b2b 221 if compiler not in target.supported_toolchains:
theotherjimmy 40:7d3fa6b99b2b 222 return False
theotherjimmy 40:7d3fa6b99b2b 223 if not hasattr(target, "post_binary_hook"):
theotherjimmy 40:7d3fa6b99b2b 224 return True
theotherjimmy 40:7d3fa6b99b2b 225 if target.post_binary_hook['function'] in whitelist:
theotherjimmy 40:7d3fa6b99b2b 226 return True
theotherjimmy 40:7d3fa6b99b2b 227 else:
theotherjimmy 40:7d3fa6b99b2b 228 return False