Clone of official tools

Committer:
screamer
Date:
Thu May 19 19:44:41 2016 +0100
Revision:
0:66f3b5499f7f
Child:
13:ab47a20b66f0
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:66f3b5499f7f 1 """
screamer 0:66f3b5499f7f 2 mbed SDK
screamer 0:66f3b5499f7f 3 Copyright (c) 2011-2013 ARM Limited
screamer 0:66f3b5499f7f 4
screamer 0:66f3b5499f7f 5 Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:66f3b5499f7f 6 you may not use this file except in compliance with the License.
screamer 0:66f3b5499f7f 7 You may obtain a copy of the License at
screamer 0:66f3b5499f7f 8
screamer 0:66f3b5499f7f 9 http://www.apache.org/licenses/LICENSE-2.0
screamer 0:66f3b5499f7f 10
screamer 0:66f3b5499f7f 11 Unless required by applicable law or agreed to in writing, software
screamer 0:66f3b5499f7f 12 distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:66f3b5499f7f 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:66f3b5499f7f 14 See the License for the specific language governing permissions and
screamer 0:66f3b5499f7f 15 limitations under the License.
screamer 0:66f3b5499f7f 16 """
screamer 0:66f3b5499f7f 17 import os, tempfile
screamer 0:66f3b5499f7f 18 from os.path import join, exists, basename
screamer 0:66f3b5499f7f 19 from shutil import copytree, rmtree, copy
screamer 0:66f3b5499f7f 20 import yaml
screamer 0:66f3b5499f7f 21
screamer 0:66f3b5499f7f 22 from tools.utils import mkdir
screamer 0:66f3b5499f7f 23 from tools.export import uvision4, codered, gccarm, ds5_5, iar, emblocks, coide, kds, zip, simplicityv3, atmelstudio, sw4stm32
screamer 0:66f3b5499f7f 24 from tools.export.exporters import zip_working_directory_and_clean_up, OldLibrariesException
screamer 0:66f3b5499f7f 25 from tools.targets import TARGET_NAMES, EXPORT_MAP, TARGET_MAP
screamer 0:66f3b5499f7f 26
screamer 0:66f3b5499f7f 27 from project_generator_definitions.definitions import ProGenDef
screamer 0:66f3b5499f7f 28
screamer 0:66f3b5499f7f 29 EXPORTERS = {
screamer 0:66f3b5499f7f 30 'uvision': uvision4.Uvision4,
screamer 0:66f3b5499f7f 31 'lpcxpresso': codered.CodeRed,
screamer 0:66f3b5499f7f 32 'gcc_arm': gccarm.GccArm,
screamer 0:66f3b5499f7f 33 'ds5_5': ds5_5.DS5_5,
screamer 0:66f3b5499f7f 34 'iar': iar.IAREmbeddedWorkbench,
screamer 0:66f3b5499f7f 35 'emblocks' : emblocks.IntermediateFile,
screamer 0:66f3b5499f7f 36 'coide' : coide.CoIDE,
screamer 0:66f3b5499f7f 37 'kds' : kds.KDS,
screamer 0:66f3b5499f7f 38 'simplicityv3' : simplicityv3.SimplicityV3,
screamer 0:66f3b5499f7f 39 'atmelstudio' : atmelstudio.AtmelStudio,
screamer 0:66f3b5499f7f 40 'sw4stm32' : sw4stm32.Sw4STM32,
screamer 0:66f3b5499f7f 41 }
screamer 0:66f3b5499f7f 42
screamer 0:66f3b5499f7f 43 ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN = """
screamer 0:66f3b5499f7f 44 Sorry, the target %s is not currently supported on the %s toolchain.
screamer 0:66f3b5499f7f 45 Please refer to <a href='/handbook/Exporting-to-offline-toolchains' target='_blank'>Exporting to offline toolchains</a> for more information.
screamer 0:66f3b5499f7f 46 """
screamer 0:66f3b5499f7f 47
screamer 0:66f3b5499f7f 48 ERROR_MESSAGE_NOT_EXPORT_LIBS = """
screamer 0:66f3b5499f7f 49 To export this project please <a href='http://mbed.org/compiler/?import=http://mbed.org/users/mbed_official/code/mbed-export/k&mode=lib' target='_blank'>import the export version of the mbed library</a>.
screamer 0:66f3b5499f7f 50 """
screamer 0:66f3b5499f7f 51
screamer 0:66f3b5499f7f 52 def online_build_url_resolver(url):
screamer 0:66f3b5499f7f 53 # TODO: Retrieve the path and name of an online library build URL
screamer 0:66f3b5499f7f 54 return {'path':'', 'name':''}
screamer 0:66f3b5499f7f 55
screamer 0:66f3b5499f7f 56
screamer 0:66f3b5499f7f 57 def export(project_path, project_name, ide, target, destination='/tmp/',
screamer 0:66f3b5499f7f 58 tempdir=None, clean=True, extra_symbols=None, zip=True, build_url_resolver=online_build_url_resolver, relative=False):
screamer 0:66f3b5499f7f 59 # Convention: we are using capitals for toolchain and target names
screamer 0:66f3b5499f7f 60 if target is not None:
screamer 0:66f3b5499f7f 61 target = target.upper()
screamer 0:66f3b5499f7f 62
screamer 0:66f3b5499f7f 63 if tempdir is None:
screamer 0:66f3b5499f7f 64 tempdir = tempfile.mkdtemp()
screamer 0:66f3b5499f7f 65
screamer 0:66f3b5499f7f 66 report = {'success': False, 'errormsg':''}
screamer 0:66f3b5499f7f 67 if ide is None or ide == "zip":
screamer 0:66f3b5499f7f 68 # Simple ZIP exporter
screamer 0:66f3b5499f7f 69 try:
screamer 0:66f3b5499f7f 70 ide = "zip"
screamer 0:66f3b5499f7f 71 exporter = zip.ZIP(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
screamer 0:66f3b5499f7f 72 exporter.scan_and_copy_resources(project_path, tempdir)
screamer 0:66f3b5499f7f 73 exporter.generate()
screamer 0:66f3b5499f7f 74 report['success'] = True
screamer 0:66f3b5499f7f 75 except OldLibrariesException, e:
screamer 0:66f3b5499f7f 76 report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
screamer 0:66f3b5499f7f 77 else:
screamer 0:66f3b5499f7f 78 if ide not in EXPORTERS:
screamer 0:66f3b5499f7f 79 report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
screamer 0:66f3b5499f7f 80 else:
screamer 0:66f3b5499f7f 81 Exporter = EXPORTERS[ide]
screamer 0:66f3b5499f7f 82 target = EXPORT_MAP.get(target, target)
screamer 0:66f3b5499f7f 83 # use progen targets or mbed exporters targets, check progen attribute
screamer 0:66f3b5499f7f 84 use_progen = False
screamer 0:66f3b5499f7f 85 supported = True
screamer 0:66f3b5499f7f 86 try:
screamer 0:66f3b5499f7f 87 if Exporter.PROGEN_ACTIVE:
screamer 0:66f3b5499f7f 88 use_progen = True
screamer 0:66f3b5499f7f 89 except AttributeError:
screamer 0:66f3b5499f7f 90 pass
screamer 0:66f3b5499f7f 91 if use_progen:
screamer 0:66f3b5499f7f 92 if not ProGenDef(ide).is_supported(TARGET_MAP[target].progen['target']):
screamer 0:66f3b5499f7f 93 supported = False
screamer 0:66f3b5499f7f 94 else:
screamer 0:66f3b5499f7f 95 if target not in Exporter.TARGETS:
screamer 0:66f3b5499f7f 96 supported = False
screamer 0:66f3b5499f7f 97
screamer 0:66f3b5499f7f 98 if supported:
screamer 0:66f3b5499f7f 99 # target checked, export
screamer 0:66f3b5499f7f 100 try:
screamer 0:66f3b5499f7f 101 exporter = Exporter(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
screamer 0:66f3b5499f7f 102 exporter.scan_and_copy_resources(project_path, tempdir, relative)
screamer 0:66f3b5499f7f 103 exporter.generate()
screamer 0:66f3b5499f7f 104 report['success'] = True
screamer 0:66f3b5499f7f 105 except OldLibrariesException, e:
screamer 0:66f3b5499f7f 106 report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
screamer 0:66f3b5499f7f 107 else:
screamer 0:66f3b5499f7f 108 report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
screamer 0:66f3b5499f7f 109
screamer 0:66f3b5499f7f 110 zip_path = None
screamer 0:66f3b5499f7f 111 if report['success']:
screamer 0:66f3b5499f7f 112 # readme.txt to contain more exported data
screamer 0:66f3b5499f7f 113 exporter_yaml = {
screamer 0:66f3b5499f7f 114 'project_generator': {
screamer 0:66f3b5499f7f 115 'active' : False,
screamer 0:66f3b5499f7f 116 }
screamer 0:66f3b5499f7f 117 }
screamer 0:66f3b5499f7f 118 if use_progen:
screamer 0:66f3b5499f7f 119 try:
screamer 0:66f3b5499f7f 120 import pkg_resources
screamer 0:66f3b5499f7f 121 version = pkg_resources.get_distribution('project_generator').version
screamer 0:66f3b5499f7f 122 exporter_yaml['project_generator']['version'] = version
screamer 0:66f3b5499f7f 123 exporter_yaml['project_generator']['active'] = True;
screamer 0:66f3b5499f7f 124 exporter_yaml['project_generator_definitions'] = {}
screamer 0:66f3b5499f7f 125 version = pkg_resources.get_distribution('project_generator_definitions').version
screamer 0:66f3b5499f7f 126 exporter_yaml['project_generator_definitions']['version'] = version
screamer 0:66f3b5499f7f 127 except ImportError:
screamer 0:66f3b5499f7f 128 pass
screamer 0:66f3b5499f7f 129 with open(os.path.join(tempdir, 'exporter.yaml'), 'w') as outfile:
screamer 0:66f3b5499f7f 130 yaml.dump(exporter_yaml, outfile, default_flow_style=False)
screamer 0:66f3b5499f7f 131 # add readme file to every offline export.
screamer 0:66f3b5499f7f 132 open(os.path.join(tempdir, 'GettingStarted.htm'),'w').write('<meta http-equiv="refresh" content="0; url=http://mbed.org/handbook/Getting-Started-mbed-Exporters#%s"/>'% (ide))
screamer 0:66f3b5499f7f 133 # copy .hgignore file to exported direcotry as well.
screamer 0:66f3b5499f7f 134 if exists(os.path.join(exporter.TEMPLATE_DIR,'.hgignore')):
screamer 0:66f3b5499f7f 135 copy(os.path.join(exporter.TEMPLATE_DIR,'.hgignore'),tempdir)
screamer 0:66f3b5499f7f 136
screamer 0:66f3b5499f7f 137 if zip:
screamer 0:66f3b5499f7f 138 zip_path = zip_working_directory_and_clean_up(tempdir, destination, project_name, clean)
screamer 0:66f3b5499f7f 139 else:
screamer 0:66f3b5499f7f 140 zip_path = destination
screamer 0:66f3b5499f7f 141
screamer 0:66f3b5499f7f 142 return zip_path, report
screamer 0:66f3b5499f7f 143
screamer 0:66f3b5499f7f 144
screamer 0:66f3b5499f7f 145 ###############################################################################
screamer 0:66f3b5499f7f 146 # Generate project folders following the online conventions
screamer 0:66f3b5499f7f 147 ###############################################################################
screamer 0:66f3b5499f7f 148 def copy_tree(src, dst, clean=True):
screamer 0:66f3b5499f7f 149 if exists(dst):
screamer 0:66f3b5499f7f 150 if clean:
screamer 0:66f3b5499f7f 151 rmtree(dst)
screamer 0:66f3b5499f7f 152 else:
screamer 0:66f3b5499f7f 153 return
screamer 0:66f3b5499f7f 154
screamer 0:66f3b5499f7f 155 copytree(src, dst)
screamer 0:66f3b5499f7f 156
screamer 0:66f3b5499f7f 157
screamer 0:66f3b5499f7f 158 def setup_user_prj(user_dir, prj_path, lib_paths=None):
screamer 0:66f3b5499f7f 159 """
screamer 0:66f3b5499f7f 160 Setup a project with the same directory structure of the mbed online IDE
screamer 0:66f3b5499f7f 161 """
screamer 0:66f3b5499f7f 162 mkdir(user_dir)
screamer 0:66f3b5499f7f 163
screamer 0:66f3b5499f7f 164 # Project Path
screamer 0:66f3b5499f7f 165 copy_tree(prj_path, join(user_dir, "src"))
screamer 0:66f3b5499f7f 166
screamer 0:66f3b5499f7f 167 # Project Libraries
screamer 0:66f3b5499f7f 168 user_lib = join(user_dir, "lib")
screamer 0:66f3b5499f7f 169 mkdir(user_lib)
screamer 0:66f3b5499f7f 170
screamer 0:66f3b5499f7f 171 if lib_paths is not None:
screamer 0:66f3b5499f7f 172 for lib_path in lib_paths:
screamer 0:66f3b5499f7f 173 copy_tree(lib_path, join(user_lib, basename(lib_path)))
screamer 0:66f3b5499f7f 174
screamer 0:66f3b5499f7f 175 def mcu_ide_matrix(verbose_html=False, platform_filter=None):
screamer 0:66f3b5499f7f 176 """ Shows target map using prettytable """
screamer 0:66f3b5499f7f 177 supported_ides = []
screamer 0:66f3b5499f7f 178 for key in EXPORTERS.iterkeys():
screamer 0:66f3b5499f7f 179 supported_ides.append(key)
screamer 0:66f3b5499f7f 180 supported_ides.sort()
screamer 0:66f3b5499f7f 181 from prettytable import PrettyTable, ALL # Only use it in this function so building works without extra modules
screamer 0:66f3b5499f7f 182
screamer 0:66f3b5499f7f 183 # All tests status table print
screamer 0:66f3b5499f7f 184 columns = ["Platform"] + supported_ides
screamer 0:66f3b5499f7f 185 pt = PrettyTable(columns)
screamer 0:66f3b5499f7f 186 # Align table
screamer 0:66f3b5499f7f 187 for col in columns:
screamer 0:66f3b5499f7f 188 pt.align[col] = "c"
screamer 0:66f3b5499f7f 189 pt.align["Platform"] = "l"
screamer 0:66f3b5499f7f 190
screamer 0:66f3b5499f7f 191 perm_counter = 0
screamer 0:66f3b5499f7f 192 target_counter = 0
screamer 0:66f3b5499f7f 193 for target in sorted(TARGET_NAMES):
screamer 0:66f3b5499f7f 194 target_counter += 1
screamer 0:66f3b5499f7f 195
screamer 0:66f3b5499f7f 196 row = [target] # First column is platform name
screamer 0:66f3b5499f7f 197 for ide in supported_ides:
screamer 0:66f3b5499f7f 198 text = "-"
screamer 0:66f3b5499f7f 199 if target in EXPORTERS[ide].TARGETS:
screamer 0:66f3b5499f7f 200 if verbose_html:
screamer 0:66f3b5499f7f 201 text = "&#10003;"
screamer 0:66f3b5499f7f 202 else:
screamer 0:66f3b5499f7f 203 text = "x"
screamer 0:66f3b5499f7f 204 perm_counter += 1
screamer 0:66f3b5499f7f 205 row.append(text)
screamer 0:66f3b5499f7f 206 pt.add_row(row)
screamer 0:66f3b5499f7f 207
screamer 0:66f3b5499f7f 208 pt.border = True
screamer 0:66f3b5499f7f 209 pt.vrules = ALL
screamer 0:66f3b5499f7f 210 pt.hrules = ALL
screamer 0:66f3b5499f7f 211 # creates a html page suitable for a browser
screamer 0:66f3b5499f7f 212 # result = pt.get_html_string(format=True) if verbose_html else pt.get_string()
screamer 0:66f3b5499f7f 213 # creates a html page in a shorter format suitable for readme.md
screamer 0:66f3b5499f7f 214 result = pt.get_html_string() if verbose_html else pt.get_string()
screamer 0:66f3b5499f7f 215 result += "\n"
screamer 0:66f3b5499f7f 216 result += "Total IDEs: %d\n"% (len(supported_ides))
screamer 0:66f3b5499f7f 217 if verbose_html: result += "<br>"
screamer 0:66f3b5499f7f 218 result += "Total platforms: %d\n"% (target_counter)
screamer 0:66f3b5499f7f 219 if verbose_html: result += "<br>"
screamer 0:66f3b5499f7f 220 result += "Total permutations: %d"% (perm_counter)
screamer 0:66f3b5499f7f 221 if verbose_html: result = result.replace("&amp;", "&")
screamer 0:66f3b5499f7f 222 return result