Clone of official tools

Committer:
The Other Jimmy
Date:
Wed Feb 15 13:53:18 2017 -0600
Revision:
35:da9c89f8be7d
Parent:
31:8ea194f6145b
Child:
36:96847d42f010
Update tools to mbed-os 5.3.5

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
screamer 0:66f3b5499f7f 28 class Exporter(object):
The Other Jimmy 31:8ea194f6145b 29 """Exporter base class
The Other Jimmy 31:8ea194f6145b 30
The Other Jimmy 31:8ea194f6145b 31 This class is meant to be extended by individual exporters, and provides a
The Other Jimmy 31:8ea194f6145b 32 few helper methods for implementing an exporter with either jinja2 or
The Other Jimmy 31:8ea194f6145b 33 progen.
The Other Jimmy 31:8ea194f6145b 34 """
The Other Jimmy 31:8ea194f6145b 35 __metaclass__ = ABCMeta
screamer 0:66f3b5499f7f 36 TEMPLATE_DIR = dirname(__file__)
screamer 0:66f3b5499f7f 37 DOT_IN_RELATIVE_PATH = False
The Other Jimmy 31:8ea194f6145b 38 NAME = None
The Other Jimmy 31:8ea194f6145b 39 TARGETS = None
The Other Jimmy 31:8ea194f6145b 40 TOOLCHAIN = None
screamer 0:66f3b5499f7f 41
The Other Jimmy 31:8ea194f6145b 42
The Other Jimmy 31:8ea194f6145b 43 def __init__(self, target, export_dir, project_name, toolchain,
The Other Jimmy 31:8ea194f6145b 44 extra_symbols=None, resources=None):
The Other Jimmy 31:8ea194f6145b 45 """Initialize an instance of class exporter
The Other Jimmy 31:8ea194f6145b 46 Positional arguments:
The Other Jimmy 31:8ea194f6145b 47 target - the target mcu/board for this project
The Other Jimmy 31:8ea194f6145b 48 export_dir - the directory of the exported project files
The Other Jimmy 31:8ea194f6145b 49 project_name - the name of the project
The Other Jimmy 31:8ea194f6145b 50 toolchain - an instance of class toolchain
The Other Jimmy 31:8ea194f6145b 51
The Other Jimmy 31:8ea194f6145b 52 Keyword arguments:
The Other Jimmy 31:8ea194f6145b 53 extra_symbols - a list of extra macros for the toolchain
The Other Jimmy 31:8ea194f6145b 54 resources - an instance of class Resources
The Other Jimmy 31:8ea194f6145b 55 """
The Other Jimmy 31:8ea194f6145b 56 self.export_dir = export_dir
screamer 0:66f3b5499f7f 57 self.target = target
The Other Jimmy 31:8ea194f6145b 58 self.project_name = project_name
The Other Jimmy 31:8ea194f6145b 59 self.toolchain = toolchain
screamer 0:66f3b5499f7f 60 jinja_loader = FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))
screamer 0:66f3b5499f7f 61 self.jinja_environment = Environment(loader=jinja_loader)
The Other Jimmy 31:8ea194f6145b 62 self.resources = resources
The Other Jimmy 31:8ea194f6145b 63 self.generated_files = [join(self.TEMPLATE_DIR,"GettingStarted.html")]
The Other Jimmy 31:8ea194f6145b 64 self.builder_files_dict = {}
The Other Jimmy 31:8ea194f6145b 65 self.add_config()
screamer 0:66f3b5499f7f 66
screamer 0:66f3b5499f7f 67 def get_toolchain(self):
The Other Jimmy 31:8ea194f6145b 68 """A helper getter function that we should probably eliminate"""
screamer 0:66f3b5499f7f 69 return self.TOOLCHAIN
screamer 0:66f3b5499f7f 70
The Other Jimmy 31:8ea194f6145b 71 def add_config(self):
The Other Jimmy 31:8ea194f6145b 72 """Add the containgin directory of mbed_config.h to include dirs"""
The Other Jimmy 31:8ea194f6145b 73 config = self.toolchain.get_config_header()
The Other Jimmy 31:8ea194f6145b 74 if config:
The Other Jimmy 31:8ea194f6145b 75 self.resources.inc_dirs.append(
The Other Jimmy 31:8ea194f6145b 76 dirname(relpath(config,
The Other Jimmy 31:8ea194f6145b 77 self.resources.file_basepath[config])))
The Other Jimmy 31:8ea194f6145b 78
screamer 13:ab47a20b66f0 79 @property
screamer 13:ab47a20b66f0 80 def flags(self):
The Other Jimmy 31:8ea194f6145b 81 """Returns a dictionary of toolchain flags.
The Other Jimmy 31:8ea194f6145b 82 Keys of the dictionary are:
The Other Jimmy 31:8ea194f6145b 83 cxx_flags - c++ flags
The Other Jimmy 31:8ea194f6145b 84 c_flags - c flags
The Other Jimmy 31:8ea194f6145b 85 ld_flags - linker flags
The Other Jimmy 31:8ea194f6145b 86 asm_flags - assembler flags
The Other Jimmy 31:8ea194f6145b 87 common_flags - common options
The Other Jimmy 31:8ea194f6145b 88 """
The Other Jimmy 31:8ea194f6145b 89 config_header = self.toolchain.get_config_header()
The Other Jimmy 31:8ea194f6145b 90 flags = {key + "_flags": copy.deepcopy(value) for key, value
The Other Jimmy 31:8ea194f6145b 91 in self.toolchain.flags.iteritems()}
The Other Jimmy 31:8ea194f6145b 92 asm_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols(True)]
The Other Jimmy 31:8ea194f6145b 93 c_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols()]
The Other Jimmy 31:8ea194f6145b 94 flags['asm_flags'] += asm_defines
The Other Jimmy 31:8ea194f6145b 95 flags['c_flags'] += c_defines
The Other Jimmy 31:8ea194f6145b 96 flags['cxx_flags'] += c_defines
The Other Jimmy 31:8ea194f6145b 97 if config_header:
The Other Jimmy 31:8ea194f6145b 98 config_header = relpath(config_header,
The Other Jimmy 31:8ea194f6145b 99 self.resources.file_basepath[config_header])
The Other Jimmy 31:8ea194f6145b 100 flags['c_flags'] += self.toolchain.get_config_option(config_header)
The Other Jimmy 31:8ea194f6145b 101 flags['cxx_flags'] += self.toolchain.get_config_option(
The Other Jimmy 31:8ea194f6145b 102 config_header)
The Other Jimmy 31:8ea194f6145b 103 return flags
The Other Jimmy 31:8ea194f6145b 104
The Other Jimmy 31:8ea194f6145b 105 def get_source_paths(self):
The Other Jimmy 31:8ea194f6145b 106 """Returns a list of the directories where source files are contained"""
The Other Jimmy 31:8ea194f6145b 107 source_keys = ['s_sources', 'c_sources', 'cpp_sources', 'hex_files',
The Other Jimmy 31:8ea194f6145b 108 'objects', 'libraries']
The Other Jimmy 31:8ea194f6145b 109 source_files = []
The Other Jimmy 31:8ea194f6145b 110 for key in source_keys:
The Other Jimmy 31:8ea194f6145b 111 source_files.extend(getattr(self.resources, key))
The Other Jimmy 31:8ea194f6145b 112 return list(set([os.path.dirname(src) for src in source_files]))
screamer 13:ab47a20b66f0 113
The Other Jimmy 35:da9c89f8be7d 114 def gen_file(self, template_file, data, target_file, **kwargs):
The Other Jimmy 31:8ea194f6145b 115 """Generates a project file from a template using jinja"""
The Other Jimmy 31:8ea194f6145b 116 jinja_loader = FileSystemLoader(
The Other Jimmy 31:8ea194f6145b 117 os.path.dirname(os.path.abspath(__file__)))
The Other Jimmy 35:da9c89f8be7d 118 jinja_environment = Environment(loader=jinja_loader,
The Other Jimmy 35:da9c89f8be7d 119 undefined=StrictUndefined, **kwargs)
The Other Jimmy 31:8ea194f6145b 120
The Other Jimmy 31:8ea194f6145b 121 template = jinja_environment.get_template(template_file)
The Other Jimmy 31:8ea194f6145b 122 target_text = template.render(data)
The Other Jimmy 31:8ea194f6145b 123
The Other Jimmy 31:8ea194f6145b 124 target_path = join(self.export_dir, target_file)
The Other Jimmy 31:8ea194f6145b 125 logging.debug("Generating: %s", target_path)
The Other Jimmy 31:8ea194f6145b 126 open(target_path, "w").write(target_text)
The Other Jimmy 31:8ea194f6145b 127 self.generated_files += [target_path]
screamer 13:ab47a20b66f0 128
The Other Jimmy 31:8ea194f6145b 129 def make_key(self, src):
The Other Jimmy 31:8ea194f6145b 130 """From a source file, extract group name
The Other Jimmy 31:8ea194f6145b 131 Positional Arguments:
The Other Jimmy 31:8ea194f6145b 132 src - the src's location
The Other Jimmy 31:8ea194f6145b 133 """
The Other Jimmy 35:da9c89f8be7d 134 rel_path = relpath(src, self.resources.file_basepath[src])
The Other Jimmy 35:da9c89f8be7d 135 path_list = os.path.normpath(rel_path).split(os.sep)
The Other Jimmy 35:da9c89f8be7d 136 assert len(path_list) >= 1
The Other Jimmy 35:da9c89f8be7d 137 if len(path_list) == 1:
The Other Jimmy 35:da9c89f8be7d 138 key = self.project_name
The Other Jimmy 35:da9c89f8be7d 139 else:
The Other Jimmy 35:da9c89f8be7d 140 key = path_list[0]
The Other Jimmy 31:8ea194f6145b 141 return key
screamer 0:66f3b5499f7f 142
The Other Jimmy 31:8ea194f6145b 143 def group_project_files(self, sources):
The Other Jimmy 31:8ea194f6145b 144 """Group the source files by their encompassing directory
The Other Jimmy 31:8ea194f6145b 145 Positional Arguments:
The Other Jimmy 31:8ea194f6145b 146 sources - array of source locations
The Other Jimmy 31:8ea194f6145b 147
The Other Jimmy 31:8ea194f6145b 148 Returns a dictionary of {group name: list of source locations}
The Other Jimmy 31:8ea194f6145b 149 """
The Other Jimmy 31:8ea194f6145b 150 data = sorted(sources, key=self.make_key)
The Other Jimmy 31:8ea194f6145b 151 return {k: list(g) for k,g in groupby(data, self.make_key)}
screamer 0:66f3b5499f7f 152
screamer 0:66f3b5499f7f 153 @staticmethod
The Other Jimmy 31:8ea194f6145b 154 def build(project_name, log_name='build_log.txt', cleanup=True):
The Other Jimmy 31:8ea194f6145b 155 """Invoke exporters build command within a subprocess.
The Other Jimmy 31:8ea194f6145b 156 This method is assumed to be executed at the same level as exporter
The Other Jimmy 31:8ea194f6145b 157 project files and project source code.
The Other Jimmy 31:8ea194f6145b 158 See uvision/__init__.py, iar/__init__.py, and makefile/__init__.py for
The Other Jimmy 31:8ea194f6145b 159 example implemenation.
screamer 0:66f3b5499f7f 160
The Other Jimmy 31:8ea194f6145b 161 Positional Arguments:
The Other Jimmy 31:8ea194f6145b 162 project_name - the name of the project to build; often required by
The Other Jimmy 31:8ea194f6145b 163 exporter's build command.
screamer 0:66f3b5499f7f 164
The Other Jimmy 31:8ea194f6145b 165 Keyword Args:
The Other Jimmy 31:8ea194f6145b 166 log_name - name of the build log to create. Written and printed out,
The Other Jimmy 31:8ea194f6145b 167 deleted if cleanup = True
The Other Jimmy 31:8ea194f6145b 168 cleanup - a boolean dictating whether exported project files and
The Other Jimmy 31:8ea194f6145b 169 build log are removed after build
screamer 0:66f3b5499f7f 170
The Other Jimmy 31:8ea194f6145b 171 Returns -1 on failure and 0 on success
screamer 0:66f3b5499f7f 172 """
The Other Jimmy 31:8ea194f6145b 173 raise NotImplemented("Implement in derived Exporter class.")
screamer 29:1210849dba19 174
The Other Jimmy 31:8ea194f6145b 175 @abstractmethod
The Other Jimmy 31:8ea194f6145b 176 def generate(self):
The Other Jimmy 31:8ea194f6145b 177 """Generate an IDE/tool specific project file"""
The Other Jimmy 31:8ea194f6145b 178 raise NotImplemented("Implement a generate function in Exporter child class")