Clone of official tools
export/exporters.py@36:96847d42f010, 2017-06-22 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Thu Jun 22 11:12:28 2017 -0500
- Revision:
- 36:96847d42f010
- Parent:
- 35:da9c89f8be7d
- Child:
- 40:7d3fa6b99b2b
Tools release 5.5.1
Who changed what in which revision?
User | Revision | Line number | New 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 |
36:96847d42f010 | 63 | self.generated_files = [join(self.TEMPLATE_DIR, "GettingStarted.html"), |
The Other Jimmy |
36:96847d42f010 | 64 | join(self.TEMPLATE_DIR, ".mbed")] |
The Other Jimmy |
31:8ea194f6145b | 65 | self.builder_files_dict = {} |
The Other Jimmy |
31:8ea194f6145b | 66 | self.add_config() |
screamer | 0:66f3b5499f7f | 67 | |
screamer | 0:66f3b5499f7f | 68 | def get_toolchain(self): |
The Other Jimmy |
31:8ea194f6145b | 69 | """A helper getter function that we should probably eliminate""" |
screamer | 0:66f3b5499f7f | 70 | return self.TOOLCHAIN |
screamer | 0:66f3b5499f7f | 71 | |
The Other Jimmy |
31:8ea194f6145b | 72 | def add_config(self): |
The Other Jimmy |
31:8ea194f6145b | 73 | """Add the containgin directory of mbed_config.h to include dirs""" |
The Other Jimmy |
31:8ea194f6145b | 74 | config = self.toolchain.get_config_header() |
The Other Jimmy |
31:8ea194f6145b | 75 | if config: |
The Other Jimmy |
31:8ea194f6145b | 76 | self.resources.inc_dirs.append( |
The Other Jimmy |
31:8ea194f6145b | 77 | dirname(relpath(config, |
The Other Jimmy |
31:8ea194f6145b | 78 | self.resources.file_basepath[config]))) |
The Other Jimmy |
31:8ea194f6145b | 79 | |
screamer | 13:ab47a20b66f0 | 80 | @property |
screamer | 13:ab47a20b66f0 | 81 | def flags(self): |
The Other Jimmy |
31:8ea194f6145b | 82 | """Returns a dictionary of toolchain flags. |
The Other Jimmy |
31:8ea194f6145b | 83 | Keys of the dictionary are: |
The Other Jimmy |
31:8ea194f6145b | 84 | cxx_flags - c++ flags |
The Other Jimmy |
31:8ea194f6145b | 85 | c_flags - c flags |
The Other Jimmy |
31:8ea194f6145b | 86 | ld_flags - linker flags |
The Other Jimmy |
31:8ea194f6145b | 87 | asm_flags - assembler flags |
The Other Jimmy |
31:8ea194f6145b | 88 | common_flags - common options |
The Other Jimmy |
31:8ea194f6145b | 89 | """ |
The Other Jimmy |
31:8ea194f6145b | 90 | config_header = self.toolchain.get_config_header() |
The Other Jimmy |
31:8ea194f6145b | 91 | flags = {key + "_flags": copy.deepcopy(value) for key, value |
The Other Jimmy |
31:8ea194f6145b | 92 | in self.toolchain.flags.iteritems()} |
The Other Jimmy |
31:8ea194f6145b | 93 | asm_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols(True)] |
The Other Jimmy |
31:8ea194f6145b | 94 | c_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols()] |
The Other Jimmy |
31:8ea194f6145b | 95 | flags['asm_flags'] += asm_defines |
The Other Jimmy |
31:8ea194f6145b | 96 | flags['c_flags'] += c_defines |
The Other Jimmy |
31:8ea194f6145b | 97 | flags['cxx_flags'] += c_defines |
The Other Jimmy |
31:8ea194f6145b | 98 | if config_header: |
The Other Jimmy |
31:8ea194f6145b | 99 | config_header = relpath(config_header, |
The Other Jimmy |
31:8ea194f6145b | 100 | self.resources.file_basepath[config_header]) |
The Other Jimmy |
31:8ea194f6145b | 101 | flags['c_flags'] += self.toolchain.get_config_option(config_header) |
The Other Jimmy |
31:8ea194f6145b | 102 | flags['cxx_flags'] += self.toolchain.get_config_option( |
The Other Jimmy |
31:8ea194f6145b | 103 | config_header) |
The Other Jimmy |
31:8ea194f6145b | 104 | return flags |
The Other Jimmy |
31:8ea194f6145b | 105 | |
The Other Jimmy |
31:8ea194f6145b | 106 | def get_source_paths(self): |
The Other Jimmy |
31:8ea194f6145b | 107 | """Returns a list of the directories where source files are contained""" |
The Other Jimmy |
31:8ea194f6145b | 108 | source_keys = ['s_sources', 'c_sources', 'cpp_sources', 'hex_files', |
The Other Jimmy |
31:8ea194f6145b | 109 | 'objects', 'libraries'] |
The Other Jimmy |
31:8ea194f6145b | 110 | source_files = [] |
The Other Jimmy |
31:8ea194f6145b | 111 | for key in source_keys: |
The Other Jimmy |
31:8ea194f6145b | 112 | source_files.extend(getattr(self.resources, key)) |
The Other Jimmy |
31:8ea194f6145b | 113 | return list(set([os.path.dirname(src) for src in source_files])) |
screamer | 13:ab47a20b66f0 | 114 | |
The Other Jimmy |
35:da9c89f8be7d | 115 | def gen_file(self, template_file, data, target_file, **kwargs): |
The Other Jimmy |
31:8ea194f6145b | 116 | """Generates a project file from a template using jinja""" |
The Other Jimmy |
31:8ea194f6145b | 117 | jinja_loader = FileSystemLoader( |
The Other Jimmy |
31:8ea194f6145b | 118 | os.path.dirname(os.path.abspath(__file__))) |
The Other Jimmy |
35:da9c89f8be7d | 119 | jinja_environment = Environment(loader=jinja_loader, |
The Other Jimmy |
35:da9c89f8be7d | 120 | undefined=StrictUndefined, **kwargs) |
The Other Jimmy |
31:8ea194f6145b | 121 | |
The Other Jimmy |
31:8ea194f6145b | 122 | template = jinja_environment.get_template(template_file) |
The Other Jimmy |
31:8ea194f6145b | 123 | target_text = template.render(data) |
The Other Jimmy |
31:8ea194f6145b | 124 | |
The Other Jimmy |
31:8ea194f6145b | 125 | target_path = join(self.export_dir, target_file) |
The Other Jimmy |
31:8ea194f6145b | 126 | logging.debug("Generating: %s", target_path) |
The Other Jimmy |
31:8ea194f6145b | 127 | open(target_path, "w").write(target_text) |
The Other Jimmy |
31:8ea194f6145b | 128 | self.generated_files += [target_path] |
screamer | 13:ab47a20b66f0 | 129 | |
The Other Jimmy |
31:8ea194f6145b | 130 | def make_key(self, src): |
The Other Jimmy |
31:8ea194f6145b | 131 | """From a source file, extract group name |
The Other Jimmy |
31:8ea194f6145b | 132 | Positional Arguments: |
The Other Jimmy |
31:8ea194f6145b | 133 | src - the src's location |
The Other Jimmy |
31:8ea194f6145b | 134 | """ |
The Other Jimmy |
35:da9c89f8be7d | 135 | rel_path = relpath(src, self.resources.file_basepath[src]) |
The Other Jimmy |
35:da9c89f8be7d | 136 | path_list = os.path.normpath(rel_path).split(os.sep) |
The Other Jimmy |
35:da9c89f8be7d | 137 | assert len(path_list) >= 1 |
The Other Jimmy |
35:da9c89f8be7d | 138 | if len(path_list) == 1: |
The Other Jimmy |
35:da9c89f8be7d | 139 | key = self.project_name |
The Other Jimmy |
35:da9c89f8be7d | 140 | else: |
The Other Jimmy |
35:da9c89f8be7d | 141 | key = path_list[0] |
The Other Jimmy |
31:8ea194f6145b | 142 | return key |
screamer | 0:66f3b5499f7f | 143 | |
The Other Jimmy |
31:8ea194f6145b | 144 | def group_project_files(self, sources): |
The Other Jimmy |
31:8ea194f6145b | 145 | """Group the source files by their encompassing directory |
The Other Jimmy |
31:8ea194f6145b | 146 | Positional Arguments: |
The Other Jimmy |
31:8ea194f6145b | 147 | sources - array of source locations |
The Other Jimmy |
31:8ea194f6145b | 148 | |
The Other Jimmy |
31:8ea194f6145b | 149 | Returns a dictionary of {group name: list of source locations} |
The Other Jimmy |
31:8ea194f6145b | 150 | """ |
The Other Jimmy |
31:8ea194f6145b | 151 | data = sorted(sources, key=self.make_key) |
The Other Jimmy |
31:8ea194f6145b | 152 | return {k: list(g) for k,g in groupby(data, self.make_key)} |
screamer | 0:66f3b5499f7f | 153 | |
screamer | 0:66f3b5499f7f | 154 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 155 | def build(project_name, log_name='build_log.txt', cleanup=True): |
The Other Jimmy |
31:8ea194f6145b | 156 | """Invoke exporters build command within a subprocess. |
The Other Jimmy |
31:8ea194f6145b | 157 | This method is assumed to be executed at the same level as exporter |
The Other Jimmy |
31:8ea194f6145b | 158 | project files and project source code. |
The Other Jimmy |
31:8ea194f6145b | 159 | See uvision/__init__.py, iar/__init__.py, and makefile/__init__.py for |
The Other Jimmy |
31:8ea194f6145b | 160 | example implemenation. |
screamer | 0:66f3b5499f7f | 161 | |
The Other Jimmy |
31:8ea194f6145b | 162 | Positional Arguments: |
The Other Jimmy |
31:8ea194f6145b | 163 | project_name - the name of the project to build; often required by |
The Other Jimmy |
31:8ea194f6145b | 164 | exporter's build command. |
screamer | 0:66f3b5499f7f | 165 | |
The Other Jimmy |
31:8ea194f6145b | 166 | Keyword Args: |
The Other Jimmy |
31:8ea194f6145b | 167 | log_name - name of the build log to create. Written and printed out, |
The Other Jimmy |
31:8ea194f6145b | 168 | deleted if cleanup = True |
The Other Jimmy |
31:8ea194f6145b | 169 | cleanup - a boolean dictating whether exported project files and |
The Other Jimmy |
31:8ea194f6145b | 170 | build log are removed after build |
screamer | 0:66f3b5499f7f | 171 | |
The Other Jimmy |
31:8ea194f6145b | 172 | Returns -1 on failure and 0 on success |
screamer | 0:66f3b5499f7f | 173 | """ |
The Other Jimmy |
31:8ea194f6145b | 174 | raise NotImplemented("Implement in derived Exporter class.") |
screamer | 29:1210849dba19 | 175 | |
The Other Jimmy |
31:8ea194f6145b | 176 | @abstractmethod |
The Other Jimmy |
31:8ea194f6145b | 177 | def generate(self): |
The Other Jimmy |
31:8ea194f6145b | 178 | """Generate an IDE/tool specific project file""" |
The Other Jimmy |
31:8ea194f6145b | 179 | raise NotImplemented("Implement a generate function in Exporter child class") |
The Other Jimmy |
36:96847d42f010 | 180 | |
The Other Jimmy |
36:96847d42f010 | 181 | |
The Other Jimmy |
36:96847d42f010 | 182 | def filter_supported(compiler, whitelist): |
The Other Jimmy |
36:96847d42f010 | 183 | """Generate a list of supported targets for a given compiler and post-binary hook |
The Other Jimmy |
36:96847d42f010 | 184 | white-list.""" |
The Other Jimmy |
36:96847d42f010 | 185 | def supported_p(obj): |
The Other Jimmy |
36:96847d42f010 | 186 | """Internal inner function used for filtering""" |
The Other Jimmy |
36:96847d42f010 | 187 | if compiler not in obj.supported_toolchains: |
The Other Jimmy |
36:96847d42f010 | 188 | return False |
The Other Jimmy |
36:96847d42f010 | 189 | if not hasattr(obj, "post_binary_hook"): |
The Other Jimmy |
36:96847d42f010 | 190 | return True |
The Other Jimmy |
36:96847d42f010 | 191 | if obj.post_binary_hook['function'] in whitelist: |
The Other Jimmy |
36:96847d42f010 | 192 | return True |
The Other Jimmy |
36:96847d42f010 | 193 | else: |
The Other Jimmy |
36:96847d42f010 | 194 | return False |
The Other Jimmy |
36:96847d42f010 | 195 | return list(target for target, obj in TARGET_MAP.iteritems() |
The Other Jimmy |
36:96847d42f010 | 196 | if supported_p(obj)) |