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