mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
tools/export/exporters.py@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | """Just a template for subclassing""" |
elessair | 0:f269e3021894 | 2 | import os |
elessair | 0:f269e3021894 | 3 | import sys |
elessair | 0:f269e3021894 | 4 | import logging |
elessair | 0:f269e3021894 | 5 | from os.path import join, dirname, relpath, basename, realpath |
elessair | 0:f269e3021894 | 6 | from itertools import groupby |
elessair | 0:f269e3021894 | 7 | from jinja2 import FileSystemLoader |
elessair | 0:f269e3021894 | 8 | from jinja2.environment import Environment |
elessair | 0:f269e3021894 | 9 | import copy |
elessair | 0:f269e3021894 | 10 | |
elessair | 0:f269e3021894 | 11 | from tools.targets import TARGET_MAP |
elessair | 0:f269e3021894 | 12 | |
elessair | 0:f269e3021894 | 13 | |
elessair | 0:f269e3021894 | 14 | class OldLibrariesException(Exception): |
elessair | 0:f269e3021894 | 15 | """Exception that indicates an export can not complete due to an out of date |
elessair | 0:f269e3021894 | 16 | library version. |
elessair | 0:f269e3021894 | 17 | """ |
elessair | 0:f269e3021894 | 18 | pass |
elessair | 0:f269e3021894 | 19 | |
elessair | 0:f269e3021894 | 20 | class FailedBuildException(Exception): |
elessair | 0:f269e3021894 | 21 | """Exception that indicates that a build failed""" |
elessair | 0:f269e3021894 | 22 | pass |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | class TargetNotSupportedException(Exception): |
elessair | 0:f269e3021894 | 25 | """Indicates that an IDE does not support a particular MCU""" |
elessair | 0:f269e3021894 | 26 | pass |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | class ExporterTargetsProperty(object): |
elessair | 0:f269e3021894 | 29 | """ Exporter descriptor for TARGETS |
elessair | 0:f269e3021894 | 30 | TARGETS as class attribute for backward compatibility |
elessair | 0:f269e3021894 | 31 | (allows: if in Exporter.TARGETS) |
elessair | 0:f269e3021894 | 32 | """ |
elessair | 0:f269e3021894 | 33 | def __init__(self, func): |
elessair | 0:f269e3021894 | 34 | self.func = func |
elessair | 0:f269e3021894 | 35 | def __get__(self, inst, cls): |
elessair | 0:f269e3021894 | 36 | return self.func(cls) |
elessair | 0:f269e3021894 | 37 | |
elessair | 0:f269e3021894 | 38 | class Exporter(object): |
elessair | 0:f269e3021894 | 39 | """Exporter base class |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | This class is meant to be extended by individual exporters, and provides a |
elessair | 0:f269e3021894 | 42 | few helper methods for implementing an exporter with either jinja2 or |
elessair | 0:f269e3021894 | 43 | progen. |
elessair | 0:f269e3021894 | 44 | """ |
elessair | 0:f269e3021894 | 45 | TEMPLATE_DIR = dirname(__file__) |
elessair | 0:f269e3021894 | 46 | DOT_IN_RELATIVE_PATH = False |
elessair | 0:f269e3021894 | 47 | NAME = None |
elessair | 0:f269e3021894 | 48 | TARGETS = None |
elessair | 0:f269e3021894 | 49 | TOOLCHAIN = None |
elessair | 0:f269e3021894 | 50 | |
elessair | 0:f269e3021894 | 51 | def __init__(self, target, export_dir, project_name, toolchain, |
elessair | 0:f269e3021894 | 52 | extra_symbols=None, resources=None): |
elessair | 0:f269e3021894 | 53 | """Initialize an instance of class exporter |
elessair | 0:f269e3021894 | 54 | Positional arguments: |
elessair | 0:f269e3021894 | 55 | target - the target mcu/board for this project |
elessair | 0:f269e3021894 | 56 | export_dir - the directory of the exported project files |
elessair | 0:f269e3021894 | 57 | project_name - the name of the project |
elessair | 0:f269e3021894 | 58 | toolchain - an instance of class toolchain |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | Keyword arguments: |
elessair | 0:f269e3021894 | 61 | extra_symbols - a list of extra macros for the toolchain |
elessair | 0:f269e3021894 | 62 | resources - an instance of class Resources |
elessair | 0:f269e3021894 | 63 | """ |
elessair | 0:f269e3021894 | 64 | self.export_dir = export_dir |
elessair | 0:f269e3021894 | 65 | self.target = target |
elessair | 0:f269e3021894 | 66 | self.project_name = project_name |
elessair | 0:f269e3021894 | 67 | self.toolchain = toolchain |
elessair | 0:f269e3021894 | 68 | jinja_loader = FileSystemLoader(os.path.dirname(os.path.abspath(__file__))) |
elessair | 0:f269e3021894 | 69 | self.jinja_environment = Environment(loader=jinja_loader) |
elessair | 0:f269e3021894 | 70 | self.resources = resources |
elessair | 0:f269e3021894 | 71 | self.generated_files = [join(self.TEMPLATE_DIR,"GettingStarted.html")] |
elessair | 0:f269e3021894 | 72 | self.builder_files_dict = {} |
elessair | 0:f269e3021894 | 73 | self.add_config() |
elessair | 0:f269e3021894 | 74 | |
elessair | 0:f269e3021894 | 75 | def get_toolchain(self): |
elessair | 0:f269e3021894 | 76 | """A helper getter function that we should probably eliminate""" |
elessair | 0:f269e3021894 | 77 | return self.TOOLCHAIN |
elessair | 0:f269e3021894 | 78 | |
elessair | 0:f269e3021894 | 79 | def add_config(self): |
elessair | 0:f269e3021894 | 80 | """Add the containgin directory of mbed_config.h to include dirs""" |
elessair | 0:f269e3021894 | 81 | config = self.toolchain.get_config_header() |
elessair | 0:f269e3021894 | 82 | if config: |
elessair | 0:f269e3021894 | 83 | self.resources.inc_dirs.append( |
elessair | 0:f269e3021894 | 84 | dirname(relpath(config, |
elessair | 0:f269e3021894 | 85 | self.resources.file_basepath[config]))) |
elessair | 0:f269e3021894 | 86 | |
elessair | 0:f269e3021894 | 87 | @property |
elessair | 0:f269e3021894 | 88 | def flags(self): |
elessair | 0:f269e3021894 | 89 | """Returns a dictionary of toolchain flags. |
elessair | 0:f269e3021894 | 90 | Keys of the dictionary are: |
elessair | 0:f269e3021894 | 91 | cxx_flags - c++ flags |
elessair | 0:f269e3021894 | 92 | c_flags - c flags |
elessair | 0:f269e3021894 | 93 | ld_flags - linker flags |
elessair | 0:f269e3021894 | 94 | asm_flags - assembler flags |
elessair | 0:f269e3021894 | 95 | common_flags - common options |
elessair | 0:f269e3021894 | 96 | """ |
elessair | 0:f269e3021894 | 97 | config_header = self.toolchain.get_config_header() |
elessair | 0:f269e3021894 | 98 | flags = {key + "_flags": copy.deepcopy(value) for key, value |
elessair | 0:f269e3021894 | 99 | in self.toolchain.flags.iteritems()} |
elessair | 0:f269e3021894 | 100 | asm_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols(True)] |
elessair | 0:f269e3021894 | 101 | c_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols()] |
elessair | 0:f269e3021894 | 102 | flags['asm_flags'] += asm_defines |
elessair | 0:f269e3021894 | 103 | flags['c_flags'] += c_defines |
elessair | 0:f269e3021894 | 104 | flags['cxx_flags'] += c_defines |
elessair | 0:f269e3021894 | 105 | if config_header: |
elessair | 0:f269e3021894 | 106 | config_header = relpath(config_header, |
elessair | 0:f269e3021894 | 107 | self.resources.file_basepath[config_header]) |
elessair | 0:f269e3021894 | 108 | flags['c_flags'] += self.toolchain.get_config_option(config_header) |
elessair | 0:f269e3021894 | 109 | flags['cxx_flags'] += self.toolchain.get_config_option( |
elessair | 0:f269e3021894 | 110 | config_header) |
elessair | 0:f269e3021894 | 111 | return flags |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | def get_source_paths(self): |
elessair | 0:f269e3021894 | 114 | """Returns a list of the directories where source files are contained""" |
elessair | 0:f269e3021894 | 115 | source_keys = ['s_sources', 'c_sources', 'cpp_sources', 'hex_files', |
elessair | 0:f269e3021894 | 116 | 'objects', 'libraries'] |
elessair | 0:f269e3021894 | 117 | source_files = [] |
elessair | 0:f269e3021894 | 118 | for key in source_keys: |
elessair | 0:f269e3021894 | 119 | source_files.extend(getattr(self.resources, key)) |
elessair | 0:f269e3021894 | 120 | return list(set([os.path.dirname(src) for src in source_files])) |
elessair | 0:f269e3021894 | 121 | |
elessair | 0:f269e3021894 | 122 | def check_supported(self): |
elessair | 0:f269e3021894 | 123 | """Indicated if this combination of IDE and MCU is supported""" |
elessair | 0:f269e3021894 | 124 | if self.target not in self.TARGETS or \ |
elessair | 0:f269e3021894 | 125 | self.TOOLCHAIN not in TARGET_MAP[self.target].supported_toolchains: |
elessair | 0:f269e3021894 | 126 | raise TargetNotSupportedException() |
elessair | 0:f269e3021894 | 127 | return True |
elessair | 0:f269e3021894 | 128 | |
elessair | 0:f269e3021894 | 129 | def gen_file(self, template_file, data, target_file): |
elessair | 0:f269e3021894 | 130 | """Generates a project file from a template using jinja""" |
elessair | 0:f269e3021894 | 131 | jinja_loader = FileSystemLoader( |
elessair | 0:f269e3021894 | 132 | os.path.dirname(os.path.abspath(__file__))) |
elessair | 0:f269e3021894 | 133 | jinja_environment = Environment(loader=jinja_loader) |
elessair | 0:f269e3021894 | 134 | |
elessair | 0:f269e3021894 | 135 | template = jinja_environment.get_template(template_file) |
elessair | 0:f269e3021894 | 136 | target_text = template.render(data) |
elessair | 0:f269e3021894 | 137 | |
elessair | 0:f269e3021894 | 138 | target_path = join(self.export_dir, target_file) |
elessair | 0:f269e3021894 | 139 | logging.debug("Generating: %s", target_path) |
elessair | 0:f269e3021894 | 140 | open(target_path, "w").write(target_text) |
elessair | 0:f269e3021894 | 141 | self.generated_files += [target_path] |
elessair | 0:f269e3021894 | 142 | |
elessair | 0:f269e3021894 | 143 | def make_key(self, src): |
elessair | 0:f269e3021894 | 144 | """From a source file, extract group name |
elessair | 0:f269e3021894 | 145 | Positional Arguments: |
elessair | 0:f269e3021894 | 146 | src - the src's location |
elessair | 0:f269e3021894 | 147 | """ |
elessair | 0:f269e3021894 | 148 | key = basename(dirname(src)) |
elessair | 0:f269e3021894 | 149 | if key == ".": |
elessair | 0:f269e3021894 | 150 | key = basename(realpath(self.export_dir)) |
elessair | 0:f269e3021894 | 151 | return key |
elessair | 0:f269e3021894 | 152 | |
elessair | 0:f269e3021894 | 153 | def group_project_files(self, sources): |
elessair | 0:f269e3021894 | 154 | """Group the source files by their encompassing directory |
elessair | 0:f269e3021894 | 155 | Positional Arguments: |
elessair | 0:f269e3021894 | 156 | sources - array of sourc locations |
elessair | 0:f269e3021894 | 157 | |
elessair | 0:f269e3021894 | 158 | Returns a dictionary of {group name: list of source locations} |
elessair | 0:f269e3021894 | 159 | """ |
elessair | 0:f269e3021894 | 160 | data = sorted(sources, key=self.make_key) |
elessair | 0:f269e3021894 | 161 | return {k: list(g) for k,g in groupby(data, self.make_key)} |