Clone of official tools

Committer:
screamer
Date:
Thu Jul 14 20:21:19 2016 +0100
Revision:
13:ab47a20b66f0
Parent:
0:66f3b5499f7f
Child:
23:fbae331171fa
Apply latest tools

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:66f3b5499f7f 1 """Just a template for subclassing"""
screamer 0:66f3b5499f7f 2 import uuid, shutil, os, logging, fnmatch
screamer 0:66f3b5499f7f 3 from os import walk, remove
screamer 0:66f3b5499f7f 4 from os.path import join, dirname, isdir, split
screamer 0:66f3b5499f7f 5 from copy import copy
screamer 0:66f3b5499f7f 6 from jinja2 import Template, FileSystemLoader
screamer 0:66f3b5499f7f 7 from jinja2.environment import Environment
screamer 0:66f3b5499f7f 8 from contextlib import closing
screamer 0:66f3b5499f7f 9 from zipfile import ZipFile, ZIP_DEFLATED
screamer 13:ab47a20b66f0 10 from operator import add
screamer 0:66f3b5499f7f 11
screamer 0:66f3b5499f7f 12 from tools.utils import mkdir
screamer 0:66f3b5499f7f 13 from tools.toolchains import TOOLCHAIN_CLASSES
screamer 0:66f3b5499f7f 14 from tools.targets import TARGET_MAP
screamer 0:66f3b5499f7f 15
screamer 0:66f3b5499f7f 16 from project_generator.generate import Generator
screamer 0:66f3b5499f7f 17 from project_generator.project import Project
screamer 0:66f3b5499f7f 18 from project_generator.settings import ProjectSettings
screamer 0:66f3b5499f7f 19
screamer 13:ab47a20b66f0 20 from tools.config import Config
screamer 13:ab47a20b66f0 21
screamer 0:66f3b5499f7f 22 class OldLibrariesException(Exception): pass
screamer 0:66f3b5499f7f 23
screamer 0:66f3b5499f7f 24 class Exporter(object):
screamer 0:66f3b5499f7f 25 TEMPLATE_DIR = dirname(__file__)
screamer 0:66f3b5499f7f 26 DOT_IN_RELATIVE_PATH = False
screamer 0:66f3b5499f7f 27
screamer 13:ab47a20b66f0 28 def __init__(self, target, inputDir, program_name, build_url_resolver, extra_symbols=None, sources_relative=True):
screamer 0:66f3b5499f7f 29 self.inputDir = inputDir
screamer 0:66f3b5499f7f 30 self.target = target
screamer 0:66f3b5499f7f 31 self.program_name = program_name
screamer 0:66f3b5499f7f 32 self.toolchain = TOOLCHAIN_CLASSES[self.get_toolchain()](TARGET_MAP[target])
screamer 0:66f3b5499f7f 33 self.build_url_resolver = build_url_resolver
screamer 0:66f3b5499f7f 34 jinja_loader = FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))
screamer 0:66f3b5499f7f 35 self.jinja_environment = Environment(loader=jinja_loader)
screamer 0:66f3b5499f7f 36 self.extra_symbols = extra_symbols
screamer 13:ab47a20b66f0 37 self.config_macros = []
screamer 13:ab47a20b66f0 38 self.sources_relative = sources_relative
screamer 13:ab47a20b66f0 39 self.config_header = None
screamer 0:66f3b5499f7f 40
screamer 0:66f3b5499f7f 41 def get_toolchain(self):
screamer 0:66f3b5499f7f 42 return self.TOOLCHAIN
screamer 0:66f3b5499f7f 43
screamer 13:ab47a20b66f0 44 @property
screamer 13:ab47a20b66f0 45 def flags(self):
screamer 13:ab47a20b66f0 46 return self.toolchain.flags
screamer 13:ab47a20b66f0 47
screamer 13:ab47a20b66f0 48 @property
screamer 13:ab47a20b66f0 49 def progen_flags(self):
screamer 13:ab47a20b66f0 50 if not hasattr(self, "_progen_flag_cache") :
screamer 13:ab47a20b66f0 51 self._progen_flag_cache = dict([(key + "_flags", value) for key,value in self.flags.iteritems()])
screamer 13:ab47a20b66f0 52 if self.config_header:
screamer 13:ab47a20b66f0 53 self._progen_flag_cache['c_flags'] += self.toolchain.get_config_option(self.config_header)
screamer 13:ab47a20b66f0 54 self._progen_flag_cache['cxx_flags'] += self.toolchain.get_config_option(self.config_header)
screamer 13:ab47a20b66f0 55 return self._progen_flag_cache
screamer 13:ab47a20b66f0 56
screamer 0:66f3b5499f7f 57 def __scan_and_copy(self, src_path, trg_path):
screamer 0:66f3b5499f7f 58 resources = self.toolchain.scan_resources(src_path)
screamer 0:66f3b5499f7f 59
screamer 0:66f3b5499f7f 60 for r_type in ['headers', 's_sources', 'c_sources', 'cpp_sources',
screamer 0:66f3b5499f7f 61 'objects', 'libraries', 'linker_script',
screamer 13:ab47a20b66f0 62 'lib_builds', 'lib_refs', 'hex_files', 'bin_files']:
screamer 0:66f3b5499f7f 63 r = getattr(resources, r_type)
screamer 0:66f3b5499f7f 64 if r:
screamer 13:ab47a20b66f0 65 self.toolchain.copy_files(r, trg_path, resources=resources)
screamer 0:66f3b5499f7f 66 return resources
screamer 0:66f3b5499f7f 67
screamer 0:66f3b5499f7f 68 @staticmethod
screamer 0:66f3b5499f7f 69 def _get_dir_grouped_files(files):
screamer 0:66f3b5499f7f 70 """ Get grouped files based on the dirname """
screamer 0:66f3b5499f7f 71 files_grouped = {}
screamer 0:66f3b5499f7f 72 for file in files:
screamer 0:66f3b5499f7f 73 rel_path = os.path.relpath(file, os.getcwd())
screamer 0:66f3b5499f7f 74 dir_path = os.path.dirname(rel_path)
screamer 0:66f3b5499f7f 75 if dir_path == '':
screamer 0:66f3b5499f7f 76 # all files within the current dir go into Source_Files
screamer 0:66f3b5499f7f 77 dir_path = 'Source_Files'
screamer 0:66f3b5499f7f 78 if not dir_path in files_grouped.keys():
screamer 0:66f3b5499f7f 79 files_grouped[dir_path] = []
screamer 0:66f3b5499f7f 80 files_grouped[dir_path].append(file)
screamer 0:66f3b5499f7f 81 return files_grouped
screamer 0:66f3b5499f7f 82
screamer 0:66f3b5499f7f 83 def progen_get_project_data(self):
screamer 0:66f3b5499f7f 84 """ Get ProGen project data """
screamer 0:66f3b5499f7f 85 # provide default data, some tools don't require any additional
screamer 0:66f3b5499f7f 86 # tool specific settings
screamer 0:66f3b5499f7f 87 code_files = []
screamer 0:66f3b5499f7f 88 for r_type in ['c_sources', 'cpp_sources', 's_sources']:
screamer 0:66f3b5499f7f 89 for file in getattr(self.resources, r_type):
screamer 0:66f3b5499f7f 90 code_files.append(file)
screamer 0:66f3b5499f7f 91
screamer 0:66f3b5499f7f 92 sources_files = code_files + self.resources.hex_files + self.resources.objects + \
screamer 0:66f3b5499f7f 93 self.resources.libraries
screamer 0:66f3b5499f7f 94 sources_grouped = Exporter._get_dir_grouped_files(sources_files)
screamer 0:66f3b5499f7f 95 headers_grouped = Exporter._get_dir_grouped_files(self.resources.headers)
screamer 0:66f3b5499f7f 96
screamer 0:66f3b5499f7f 97 project_data = {
screamer 0:66f3b5499f7f 98 'common': {
screamer 0:66f3b5499f7f 99 'sources': sources_grouped,
screamer 0:66f3b5499f7f 100 'includes': headers_grouped,
screamer 0:66f3b5499f7f 101 'build_dir':'.build',
screamer 0:66f3b5499f7f 102 'target': [TARGET_MAP[self.target].progen['target']],
screamer 0:66f3b5499f7f 103 'macros': self.get_symbols(),
screamer 0:66f3b5499f7f 104 'export_dir': [self.inputDir],
screamer 0:66f3b5499f7f 105 'linker_file': [self.resources.linker_script],
screamer 0:66f3b5499f7f 106 }
screamer 0:66f3b5499f7f 107 }
screamer 0:66f3b5499f7f 108 return project_data
screamer 0:66f3b5499f7f 109
screamer 0:66f3b5499f7f 110 def progen_gen_file(self, tool_name, project_data):
screamer 13:ab47a20b66f0 111 """ Generate project using ProGen Project API """
screamer 0:66f3b5499f7f 112 settings = ProjectSettings()
screamer 0:66f3b5499f7f 113 project = Project(self.program_name, [project_data], settings)
screamer 0:66f3b5499f7f 114 # TODO: Fix this, the inc_dirs are not valid (our scripts copy files), therefore progen
screamer 0:66f3b5499f7f 115 # thinks it is not dict but a file, and adds them to workspace.
screamer 0:66f3b5499f7f 116 project.project['common']['include_paths'] = self.resources.inc_dirs
screamer 13:ab47a20b66f0 117 project.generate(tool_name, copied=not self.sources_relative)
screamer 0:66f3b5499f7f 118
screamer 0:66f3b5499f7f 119 def __scan_all(self, path):
screamer 0:66f3b5499f7f 120 resources = []
screamer 0:66f3b5499f7f 121
screamer 0:66f3b5499f7f 122 for root, dirs, files in walk(path):
screamer 0:66f3b5499f7f 123 for d in copy(dirs):
screamer 0:66f3b5499f7f 124 if d == '.' or d == '..':
screamer 0:66f3b5499f7f 125 dirs.remove(d)
screamer 0:66f3b5499f7f 126
screamer 0:66f3b5499f7f 127 for file in files:
screamer 0:66f3b5499f7f 128 file_path = join(root, file)
screamer 0:66f3b5499f7f 129 resources.append(file_path)
screamer 0:66f3b5499f7f 130
screamer 0:66f3b5499f7f 131 return resources
screamer 0:66f3b5499f7f 132
screamer 13:ab47a20b66f0 133 def scan_and_copy_resources(self, prj_paths, trg_path, relative=False):
screamer 0:66f3b5499f7f 134 # Copy only the file for the required target and toolchain
screamer 0:66f3b5499f7f 135 lib_builds = []
screamer 13:ab47a20b66f0 136 # Create the configuration object
screamer 13:ab47a20b66f0 137 if isinstance(prj_paths, basestring):
screamer 13:ab47a20b66f0 138 prj_paths = [prj_paths]
screamer 13:ab47a20b66f0 139 config = Config(self.target, prj_paths)
screamer 0:66f3b5499f7f 140 for src in ['lib', 'src']:
screamer 13:ab47a20b66f0 141 resources = self.__scan_and_copy(join(prj_paths[0], src), trg_path)
screamer 13:ab47a20b66f0 142 for path in prj_paths[1:]:
screamer 13:ab47a20b66f0 143 resources.add(self.__scan_and_copy(join(path, src), trg_path))
screamer 13:ab47a20b66f0 144
screamer 0:66f3b5499f7f 145 lib_builds.extend(resources.lib_builds)
screamer 0:66f3b5499f7f 146
screamer 0:66f3b5499f7f 147 # The repository files
screamer 13:ab47a20b66f0 148 #for repo_dir in resources.repo_dirs:
screamer 13:ab47a20b66f0 149 # repo_files = self.__scan_all(repo_dir)
screamer 13:ab47a20b66f0 150 # for path in prj_paths:
screamer 13:ab47a20b66f0 151 # self.toolchain.copy_files(repo_files, trg_path, rel_path=join(path, src))
screamer 0:66f3b5499f7f 152
screamer 0:66f3b5499f7f 153 # The libraries builds
screamer 0:66f3b5499f7f 154 for bld in lib_builds:
screamer 0:66f3b5499f7f 155 build_url = open(bld).read().strip()
screamer 0:66f3b5499f7f 156 lib_data = self.build_url_resolver(build_url)
screamer 0:66f3b5499f7f 157 lib_path = lib_data['path'].rstrip('\\/')
screamer 0:66f3b5499f7f 158 self.__scan_and_copy(lib_path, join(trg_path, lib_data['name']))
screamer 0:66f3b5499f7f 159
screamer 0:66f3b5499f7f 160 # Create .hg dir in mbed build dir so it's ignored when versioning
screamer 0:66f3b5499f7f 161 hgdir = join(trg_path, lib_data['name'], '.hg')
screamer 0:66f3b5499f7f 162 mkdir(hgdir)
screamer 0:66f3b5499f7f 163 fhandle = file(join(hgdir, 'keep.me'), 'a')
screamer 0:66f3b5499f7f 164 fhandle.close()
screamer 0:66f3b5499f7f 165
screamer 0:66f3b5499f7f 166 if not relative:
screamer 0:66f3b5499f7f 167 # Final scan of the actual exported resources
screamer 13:ab47a20b66f0 168 resources = self.toolchain.scan_resources(trg_path)
screamer 13:ab47a20b66f0 169 resources.relative_to(trg_path, self.DOT_IN_RELATIVE_PATH)
screamer 0:66f3b5499f7f 170 else:
screamer 0:66f3b5499f7f 171 # use the prj_dir (source, not destination)
screamer 13:ab47a20b66f0 172 resources = self.toolchain.scan_resources(prj_paths[0])
screamer 13:ab47a20b66f0 173 for path in prj_paths[1:]:
screamer 13:ab47a20b66f0 174 resources.add(toolchain.scan_resources(path))
screamer 13:ab47a20b66f0 175
screamer 13:ab47a20b66f0 176 # Loads the resources into the config system which might expand/modify resources based on config data
screamer 13:ab47a20b66f0 177 self.resources = config.load_resources(resources)
screamer 13:ab47a20b66f0 178
screamer 13:ab47a20b66f0 179 if hasattr(self, "MBED_CONFIG_HEADER_SUPPORTED") and self.MBED_CONFIG_HEADER_SUPPORTED :
screamer 13:ab47a20b66f0 180 # Add the configuration file to the target directory
screamer 13:ab47a20b66f0 181 self.config_header = self.toolchain.MBED_CONFIG_FILE_NAME
screamer 13:ab47a20b66f0 182 config.get_config_data_header(join(trg_path, self.config_header))
screamer 13:ab47a20b66f0 183 self.config_macros = []
screamer 13:ab47a20b66f0 184 else:
screamer 13:ab47a20b66f0 185 # And add the configuration macros to the toolchain
screamer 13:ab47a20b66f0 186 self.config_macros = config.get_config_data_macros()
screamer 0:66f3b5499f7f 187
screamer 0:66f3b5499f7f 188 def gen_file(self, template_file, data, target_file):
screamer 0:66f3b5499f7f 189 template_path = join(Exporter.TEMPLATE_DIR, template_file)
screamer 0:66f3b5499f7f 190 template = self.jinja_environment.get_template(template_file)
screamer 0:66f3b5499f7f 191 target_text = template.render(data)
screamer 0:66f3b5499f7f 192
screamer 0:66f3b5499f7f 193 target_path = join(self.inputDir, target_file)
screamer 0:66f3b5499f7f 194 logging.debug("Generating: %s" % target_path)
screamer 0:66f3b5499f7f 195 open(target_path, "w").write(target_text)
screamer 0:66f3b5499f7f 196
screamer 0:66f3b5499f7f 197 def get_symbols(self, add_extra_symbols=True):
screamer 0:66f3b5499f7f 198 """ This function returns symbols which must be exported.
screamer 0:66f3b5499f7f 199 Please add / overwrite symbols in each exporter separately
screamer 0:66f3b5499f7f 200 """
screamer 13:ab47a20b66f0 201 symbols = self.toolchain.get_symbols() + self.config_macros
screamer 0:66f3b5499f7f 202 # We have extra symbols from e.g. libraries, we want to have them also added to export
screamer 0:66f3b5499f7f 203 if add_extra_symbols:
screamer 0:66f3b5499f7f 204 if self.extra_symbols is not None:
screamer 0:66f3b5499f7f 205 symbols.extend(self.extra_symbols)
screamer 0:66f3b5499f7f 206 return symbols
screamer 0:66f3b5499f7f 207
screamer 0:66f3b5499f7f 208 def zip_working_directory_and_clean_up(tempdirectory=None, destination=None, program_name=None, clean=True):
screamer 0:66f3b5499f7f 209 uid = str(uuid.uuid4())
screamer 0:66f3b5499f7f 210 zipfilename = '%s.zip'%uid
screamer 0:66f3b5499f7f 211
screamer 0:66f3b5499f7f 212 logging.debug("Zipping up %s to %s" % (tempdirectory, join(destination, zipfilename)))
screamer 0:66f3b5499f7f 213 # make zip
screamer 0:66f3b5499f7f 214 def zipdir(basedir, archivename):
screamer 0:66f3b5499f7f 215 assert isdir(basedir)
screamer 0:66f3b5499f7f 216 fakeroot = program_name + '/'
screamer 0:66f3b5499f7f 217 with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z:
screamer 0:66f3b5499f7f 218 for root, _, files in os.walk(basedir):
screamer 0:66f3b5499f7f 219 # NOTE: ignore empty directories
screamer 0:66f3b5499f7f 220 for fn in files:
screamer 0:66f3b5499f7f 221 absfn = join(root, fn)
screamer 0:66f3b5499f7f 222 zfn = fakeroot + '/' + absfn[len(basedir)+len(os.sep):]
screamer 0:66f3b5499f7f 223 z.write(absfn, zfn)
screamer 0:66f3b5499f7f 224
screamer 0:66f3b5499f7f 225 zipdir(tempdirectory, join(destination, zipfilename))
screamer 0:66f3b5499f7f 226
screamer 0:66f3b5499f7f 227 if clean:
screamer 0:66f3b5499f7f 228 shutil.rmtree(tempdirectory)
screamer 0:66f3b5499f7f 229
screamer 0:66f3b5499f7f 230 return join(destination, zipfilename)