Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
tools/export/exporters.py@3:1198227e6421, 2016-12-16 (annotated)
- Committer:
- switches
- Date:
- Fri Dec 16 16:27:57 2016 +0000
- Revision:
- 3:1198227e6421
- Parent:
- 0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | """Just a template for subclassing""" |
switches | 0:5c4d7b2438d3 | 2 | import os |
switches | 0:5c4d7b2438d3 | 3 | from abc import abstractmethod, ABCMeta |
switches | 0:5c4d7b2438d3 | 4 | import logging |
switches | 0:5c4d7b2438d3 | 5 | from os.path import join, dirname, relpath, basename, realpath |
switches | 0:5c4d7b2438d3 | 6 | from itertools import groupby |
switches | 0:5c4d7b2438d3 | 7 | from jinja2 import FileSystemLoader |
switches | 0:5c4d7b2438d3 | 8 | from jinja2.environment import Environment |
switches | 0:5c4d7b2438d3 | 9 | import copy |
switches | 0:5c4d7b2438d3 | 10 | |
switches | 0:5c4d7b2438d3 | 11 | from tools.targets import TARGET_MAP |
switches | 0:5c4d7b2438d3 | 12 | |
switches | 0:5c4d7b2438d3 | 13 | |
switches | 0:5c4d7b2438d3 | 14 | class TargetNotSupportedException(Exception): |
switches | 0:5c4d7b2438d3 | 15 | """Indicates that an IDE does not support a particular MCU""" |
switches | 0:5c4d7b2438d3 | 16 | pass |
switches | 0:5c4d7b2438d3 | 17 | |
switches | 0:5c4d7b2438d3 | 18 | class ExporterTargetsProperty(object): |
switches | 0:5c4d7b2438d3 | 19 | """ Exporter descriptor for TARGETS |
switches | 0:5c4d7b2438d3 | 20 | TARGETS as class attribute for backward compatibility |
switches | 0:5c4d7b2438d3 | 21 | (allows: if in Exporter.TARGETS) |
switches | 0:5c4d7b2438d3 | 22 | """ |
switches | 0:5c4d7b2438d3 | 23 | def __init__(self, func): |
switches | 0:5c4d7b2438d3 | 24 | self.func = func |
switches | 0:5c4d7b2438d3 | 25 | def __get__(self, inst, cls): |
switches | 0:5c4d7b2438d3 | 26 | return self.func(cls) |
switches | 0:5c4d7b2438d3 | 27 | |
switches | 0:5c4d7b2438d3 | 28 | class Exporter(object): |
switches | 0:5c4d7b2438d3 | 29 | """Exporter base class |
switches | 0:5c4d7b2438d3 | 30 | |
switches | 0:5c4d7b2438d3 | 31 | This class is meant to be extended by individual exporters, and provides a |
switches | 0:5c4d7b2438d3 | 32 | few helper methods for implementing an exporter with either jinja2 or |
switches | 0:5c4d7b2438d3 | 33 | progen. |
switches | 0:5c4d7b2438d3 | 34 | """ |
switches | 0:5c4d7b2438d3 | 35 | __metaclass__ = ABCMeta |
switches | 0:5c4d7b2438d3 | 36 | TEMPLATE_DIR = dirname(__file__) |
switches | 0:5c4d7b2438d3 | 37 | DOT_IN_RELATIVE_PATH = False |
switches | 0:5c4d7b2438d3 | 38 | NAME = None |
switches | 0:5c4d7b2438d3 | 39 | TARGETS = None |
switches | 0:5c4d7b2438d3 | 40 | TOOLCHAIN = None |
switches | 0:5c4d7b2438d3 | 41 | |
switches | 0:5c4d7b2438d3 | 42 | |
switches | 0:5c4d7b2438d3 | 43 | def __init__(self, target, export_dir, project_name, toolchain, |
switches | 0:5c4d7b2438d3 | 44 | extra_symbols=None, resources=None): |
switches | 0:5c4d7b2438d3 | 45 | """Initialize an instance of class exporter |
switches | 0:5c4d7b2438d3 | 46 | Positional arguments: |
switches | 0:5c4d7b2438d3 | 47 | target - the target mcu/board for this project |
switches | 0:5c4d7b2438d3 | 48 | export_dir - the directory of the exported project files |
switches | 0:5c4d7b2438d3 | 49 | project_name - the name of the project |
switches | 0:5c4d7b2438d3 | 50 | toolchain - an instance of class toolchain |
switches | 0:5c4d7b2438d3 | 51 | |
switches | 0:5c4d7b2438d3 | 52 | Keyword arguments: |
switches | 0:5c4d7b2438d3 | 53 | extra_symbols - a list of extra macros for the toolchain |
switches | 0:5c4d7b2438d3 | 54 | resources - an instance of class Resources |
switches | 0:5c4d7b2438d3 | 55 | """ |
switches | 0:5c4d7b2438d3 | 56 | self.export_dir = export_dir |
switches | 0:5c4d7b2438d3 | 57 | self.target = target |
switches | 0:5c4d7b2438d3 | 58 | self.project_name = project_name |
switches | 0:5c4d7b2438d3 | 59 | self.toolchain = toolchain |
switches | 0:5c4d7b2438d3 | 60 | jinja_loader = FileSystemLoader(os.path.dirname(os.path.abspath(__file__))) |
switches | 0:5c4d7b2438d3 | 61 | self.jinja_environment = Environment(loader=jinja_loader) |
switches | 0:5c4d7b2438d3 | 62 | self.resources = resources |
switches | 0:5c4d7b2438d3 | 63 | self.generated_files = [join(self.TEMPLATE_DIR,"GettingStarted.html")] |
switches | 0:5c4d7b2438d3 | 64 | self.builder_files_dict = {} |
switches | 0:5c4d7b2438d3 | 65 | self.add_config() |
switches | 0:5c4d7b2438d3 | 66 | |
switches | 0:5c4d7b2438d3 | 67 | def get_toolchain(self): |
switches | 0:5c4d7b2438d3 | 68 | """A helper getter function that we should probably eliminate""" |
switches | 0:5c4d7b2438d3 | 69 | return self.TOOLCHAIN |
switches | 0:5c4d7b2438d3 | 70 | |
switches | 0:5c4d7b2438d3 | 71 | def add_config(self): |
switches | 0:5c4d7b2438d3 | 72 | """Add the containgin directory of mbed_config.h to include dirs""" |
switches | 0:5c4d7b2438d3 | 73 | config = self.toolchain.get_config_header() |
switches | 0:5c4d7b2438d3 | 74 | if config: |
switches | 0:5c4d7b2438d3 | 75 | self.resources.inc_dirs.append( |
switches | 0:5c4d7b2438d3 | 76 | dirname(relpath(config, |
switches | 0:5c4d7b2438d3 | 77 | self.resources.file_basepath[config]))) |
switches | 0:5c4d7b2438d3 | 78 | |
switches | 0:5c4d7b2438d3 | 79 | @property |
switches | 0:5c4d7b2438d3 | 80 | def flags(self): |
switches | 0:5c4d7b2438d3 | 81 | """Returns a dictionary of toolchain flags. |
switches | 0:5c4d7b2438d3 | 82 | Keys of the dictionary are: |
switches | 0:5c4d7b2438d3 | 83 | cxx_flags - c++ flags |
switches | 0:5c4d7b2438d3 | 84 | c_flags - c flags |
switches | 0:5c4d7b2438d3 | 85 | ld_flags - linker flags |
switches | 0:5c4d7b2438d3 | 86 | asm_flags - assembler flags |
switches | 0:5c4d7b2438d3 | 87 | common_flags - common options |
switches | 0:5c4d7b2438d3 | 88 | """ |
switches | 0:5c4d7b2438d3 | 89 | config_header = self.toolchain.get_config_header() |
switches | 0:5c4d7b2438d3 | 90 | flags = {key + "_flags": copy.deepcopy(value) for key, value |
switches | 0:5c4d7b2438d3 | 91 | in self.toolchain.flags.iteritems()} |
switches | 0:5c4d7b2438d3 | 92 | asm_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols(True)] |
switches | 0:5c4d7b2438d3 | 93 | c_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols()] |
switches | 0:5c4d7b2438d3 | 94 | flags['asm_flags'] += asm_defines |
switches | 0:5c4d7b2438d3 | 95 | flags['c_flags'] += c_defines |
switches | 0:5c4d7b2438d3 | 96 | flags['cxx_flags'] += c_defines |
switches | 0:5c4d7b2438d3 | 97 | if config_header: |
switches | 0:5c4d7b2438d3 | 98 | config_header = relpath(config_header, |
switches | 0:5c4d7b2438d3 | 99 | self.resources.file_basepath[config_header]) |
switches | 0:5c4d7b2438d3 | 100 | flags['c_flags'] += self.toolchain.get_config_option(config_header) |
switches | 0:5c4d7b2438d3 | 101 | flags['cxx_flags'] += self.toolchain.get_config_option( |
switches | 0:5c4d7b2438d3 | 102 | config_header) |
switches | 0:5c4d7b2438d3 | 103 | return flags |
switches | 0:5c4d7b2438d3 | 104 | |
switches | 0:5c4d7b2438d3 | 105 | def get_source_paths(self): |
switches | 0:5c4d7b2438d3 | 106 | """Returns a list of the directories where source files are contained""" |
switches | 0:5c4d7b2438d3 | 107 | source_keys = ['s_sources', 'c_sources', 'cpp_sources', 'hex_files', |
switches | 0:5c4d7b2438d3 | 108 | 'objects', 'libraries'] |
switches | 0:5c4d7b2438d3 | 109 | source_files = [] |
switches | 0:5c4d7b2438d3 | 110 | for key in source_keys: |
switches | 0:5c4d7b2438d3 | 111 | source_files.extend(getattr(self.resources, key)) |
switches | 0:5c4d7b2438d3 | 112 | return list(set([os.path.dirname(src) for src in source_files])) |
switches | 0:5c4d7b2438d3 | 113 | |
switches | 0:5c4d7b2438d3 | 114 | def gen_file(self, template_file, data, target_file): |
switches | 0:5c4d7b2438d3 | 115 | """Generates a project file from a template using jinja""" |
switches | 0:5c4d7b2438d3 | 116 | jinja_loader = FileSystemLoader( |
switches | 0:5c4d7b2438d3 | 117 | os.path.dirname(os.path.abspath(__file__))) |
switches | 0:5c4d7b2438d3 | 118 | jinja_environment = Environment(loader=jinja_loader) |
switches | 0:5c4d7b2438d3 | 119 | |
switches | 0:5c4d7b2438d3 | 120 | template = jinja_environment.get_template(template_file) |
switches | 0:5c4d7b2438d3 | 121 | target_text = template.render(data) |
switches | 0:5c4d7b2438d3 | 122 | |
switches | 0:5c4d7b2438d3 | 123 | target_path = join(self.export_dir, target_file) |
switches | 0:5c4d7b2438d3 | 124 | logging.debug("Generating: %s", target_path) |
switches | 0:5c4d7b2438d3 | 125 | open(target_path, "w").write(target_text) |
switches | 0:5c4d7b2438d3 | 126 | self.generated_files += [target_path] |
switches | 0:5c4d7b2438d3 | 127 | |
switches | 0:5c4d7b2438d3 | 128 | def make_key(self, src): |
switches | 0:5c4d7b2438d3 | 129 | """From a source file, extract group name |
switches | 0:5c4d7b2438d3 | 130 | Positional Arguments: |
switches | 0:5c4d7b2438d3 | 131 | src - the src's location |
switches | 0:5c4d7b2438d3 | 132 | """ |
switches | 0:5c4d7b2438d3 | 133 | key = basename(dirname(src)) |
switches | 0:5c4d7b2438d3 | 134 | if key == ".": |
switches | 0:5c4d7b2438d3 | 135 | key = basename(realpath(self.export_dir)) |
switches | 0:5c4d7b2438d3 | 136 | return key |
switches | 0:5c4d7b2438d3 | 137 | |
switches | 0:5c4d7b2438d3 | 138 | def group_project_files(self, sources): |
switches | 0:5c4d7b2438d3 | 139 | """Group the source files by their encompassing directory |
switches | 0:5c4d7b2438d3 | 140 | Positional Arguments: |
switches | 0:5c4d7b2438d3 | 141 | sources - array of source locations |
switches | 0:5c4d7b2438d3 | 142 | |
switches | 0:5c4d7b2438d3 | 143 | Returns a dictionary of {group name: list of source locations} |
switches | 0:5c4d7b2438d3 | 144 | """ |
switches | 0:5c4d7b2438d3 | 145 | data = sorted(sources, key=self.make_key) |
switches | 0:5c4d7b2438d3 | 146 | return {k: list(g) for k,g in groupby(data, self.make_key)} |
switches | 0:5c4d7b2438d3 | 147 | |
switches | 0:5c4d7b2438d3 | 148 | @staticmethod |
switches | 0:5c4d7b2438d3 | 149 | def build(project_name, log_name='build_log.txt', cleanup=True): |
switches | 0:5c4d7b2438d3 | 150 | """Invoke exporters build command within a subprocess. |
switches | 0:5c4d7b2438d3 | 151 | This method is assumed to be executed at the same level as exporter |
switches | 0:5c4d7b2438d3 | 152 | project files and project source code. |
switches | 0:5c4d7b2438d3 | 153 | See uvision/__init__.py, iar/__init__.py, and makefile/__init__.py for |
switches | 0:5c4d7b2438d3 | 154 | example implemenation. |
switches | 0:5c4d7b2438d3 | 155 | |
switches | 0:5c4d7b2438d3 | 156 | Positional Arguments: |
switches | 0:5c4d7b2438d3 | 157 | project_name - the name of the project to build; often required by |
switches | 0:5c4d7b2438d3 | 158 | exporter's build command. |
switches | 0:5c4d7b2438d3 | 159 | |
switches | 0:5c4d7b2438d3 | 160 | Keyword Args: |
switches | 0:5c4d7b2438d3 | 161 | log_name - name of the build log to create. Written and printed out, |
switches | 0:5c4d7b2438d3 | 162 | deleted if cleanup = True |
switches | 0:5c4d7b2438d3 | 163 | cleanup - a boolean dictating whether exported project files and |
switches | 0:5c4d7b2438d3 | 164 | build log are removed after build |
switches | 0:5c4d7b2438d3 | 165 | |
switches | 0:5c4d7b2438d3 | 166 | Returns -1 on failure and 0 on success |
switches | 0:5c4d7b2438d3 | 167 | """ |
switches | 0:5c4d7b2438d3 | 168 | raise NotImplemented("Implement in derived Exporter class.") |
switches | 0:5c4d7b2438d3 | 169 | |
switches | 0:5c4d7b2438d3 | 170 | @abstractmethod |
switches | 0:5c4d7b2438d3 | 171 | def generate(self): |
switches | 0:5c4d7b2438d3 | 172 | """Generate an IDE/tool specific project file""" |
switches | 0:5c4d7b2438d3 | 173 | raise NotImplemented("Implement a generate function in Exporter child class") |