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 """
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 13:ab47a20b66f0 23 from tools.export import uvision4, uvision5, codered, gccarm, ds5_5, iar, emblocks, coide, kds, zip, simplicityv3, atmelstudio, sw4stm32, e2studio
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 13:ab47a20b66f0 31 'uvision4': uvision4.Uvision4,
screamer 13:ab47a20b66f0 32 'uvision5': uvision5.Uvision5,
screamer 0:66f3b5499f7f 33 'lpcxpresso': codered.CodeRed,
screamer 0:66f3b5499f7f 34 'gcc_arm': gccarm.GccArm,
screamer 0:66f3b5499f7f 35 'ds5_5': ds5_5.DS5_5,
screamer 0:66f3b5499f7f 36 'iar': iar.IAREmbeddedWorkbench,
screamer 0:66f3b5499f7f 37 'emblocks' : emblocks.IntermediateFile,
screamer 0:66f3b5499f7f 38 'coide' : coide.CoIDE,
screamer 0:66f3b5499f7f 39 'kds' : kds.KDS,
screamer 0:66f3b5499f7f 40 'simplicityv3' : simplicityv3.SimplicityV3,
screamer 0:66f3b5499f7f 41 'atmelstudio' : atmelstudio.AtmelStudio,
screamer 0:66f3b5499f7f 42 'sw4stm32' : sw4stm32.Sw4STM32,
screamer 13:ab47a20b66f0 43 'e2studio' : e2studio.E2Studio,
screamer 0:66f3b5499f7f 44 }
screamer 0:66f3b5499f7f 45
screamer 0:66f3b5499f7f 46 ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN = """
screamer 0:66f3b5499f7f 47 Sorry, the target %s is not currently supported on the %s toolchain.
screamer 0:66f3b5499f7f 48 Please refer to <a href='/handbook/Exporting-to-offline-toolchains' target='_blank'>Exporting to offline toolchains</a> for more information.
screamer 0:66f3b5499f7f 49 """
screamer 0:66f3b5499f7f 50
screamer 0:66f3b5499f7f 51 ERROR_MESSAGE_NOT_EXPORT_LIBS = """
screamer 0:66f3b5499f7f 52 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 53 """
screamer 0:66f3b5499f7f 54
screamer 0:66f3b5499f7f 55 def online_build_url_resolver(url):
screamer 0:66f3b5499f7f 56 # TODO: Retrieve the path and name of an online library build URL
screamer 0:66f3b5499f7f 57 return {'path':'', 'name':''}
screamer 0:66f3b5499f7f 58
screamer 0:66f3b5499f7f 59
screamer 0:66f3b5499f7f 60 def export(project_path, project_name, ide, target, destination='/tmp/',
screamer 13:ab47a20b66f0 61 tempdir=None, clean=True, extra_symbols=None, make_zip=True, sources_relative=False, build_url_resolver=online_build_url_resolver):
screamer 0:66f3b5499f7f 62 # Convention: we are using capitals for toolchain and target names
screamer 0:66f3b5499f7f 63 if target is not None:
screamer 0:66f3b5499f7f 64 target = target.upper()
screamer 0:66f3b5499f7f 65
screamer 0:66f3b5499f7f 66 if tempdir is None:
screamer 0:66f3b5499f7f 67 tempdir = tempfile.mkdtemp()
screamer 0:66f3b5499f7f 68
screamer 13:ab47a20b66f0 69 use_progen = False
screamer 13:ab47a20b66f0 70 supported = True
screamer 0:66f3b5499f7f 71 report = {'success': False, 'errormsg':''}
screamer 13:ab47a20b66f0 72
screamer 0:66f3b5499f7f 73 if ide is None or ide == "zip":
screamer 0:66f3b5499f7f 74 # Simple ZIP exporter
screamer 0:66f3b5499f7f 75 try:
screamer 0:66f3b5499f7f 76 ide = "zip"
screamer 0:66f3b5499f7f 77 exporter = zip.ZIP(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
screamer 13:ab47a20b66f0 78 exporter.scan_and_copy_resources(project_path, tempdir, sources_relative)
screamer 0:66f3b5499f7f 79 exporter.generate()
screamer 0:66f3b5499f7f 80 report['success'] = True
screamer 0:66f3b5499f7f 81 except OldLibrariesException, e:
screamer 0:66f3b5499f7f 82 report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
screamer 0:66f3b5499f7f 83 else:
screamer 0:66f3b5499f7f 84 if ide not in EXPORTERS:
screamer 0:66f3b5499f7f 85 report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
screamer 0:66f3b5499f7f 86 else:
screamer 0:66f3b5499f7f 87 Exporter = EXPORTERS[ide]
screamer 0:66f3b5499f7f 88 target = EXPORT_MAP.get(target, target)
screamer 0:66f3b5499f7f 89 try:
screamer 0:66f3b5499f7f 90 if Exporter.PROGEN_ACTIVE:
screamer 0:66f3b5499f7f 91 use_progen = True
screamer 0:66f3b5499f7f 92 except AttributeError:
screamer 0:66f3b5499f7f 93 pass
screamer 0:66f3b5499f7f 94 if use_progen:
screamer 0:66f3b5499f7f 95 if not ProGenDef(ide).is_supported(TARGET_MAP[target].progen['target']):
screamer 0:66f3b5499f7f 96 supported = False
screamer 0:66f3b5499f7f 97 else:
screamer 0:66f3b5499f7f 98 if target not in Exporter.TARGETS:
screamer 0:66f3b5499f7f 99 supported = False
screamer 0:66f3b5499f7f 100
screamer 0:66f3b5499f7f 101 if supported:
screamer 0:66f3b5499f7f 102 # target checked, export
screamer 0:66f3b5499f7f 103 try:
screamer 13:ab47a20b66f0 104 exporter = Exporter(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols, sources_relative=sources_relative)
screamer 13:ab47a20b66f0 105 exporter.scan_and_copy_resources(project_path, tempdir, sources_relative)
screamer 0:66f3b5499f7f 106 exporter.generate()
screamer 0:66f3b5499f7f 107 report['success'] = True
screamer 0:66f3b5499f7f 108 except OldLibrariesException, e:
screamer 0:66f3b5499f7f 109 report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
screamer 0:66f3b5499f7f 110 else:
screamer 0:66f3b5499f7f 111 report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
screamer 0:66f3b5499f7f 112
screamer 0:66f3b5499f7f 113 zip_path = None
screamer 0:66f3b5499f7f 114 if report['success']:
screamer 0:66f3b5499f7f 115 # readme.txt to contain more exported data
screamer 0:66f3b5499f7f 116 exporter_yaml = {
screamer 0:66f3b5499f7f 117 'project_generator': {
screamer 0:66f3b5499f7f 118 'active' : False,
screamer 0:66f3b5499f7f 119 }
screamer 0:66f3b5499f7f 120 }
screamer 0:66f3b5499f7f 121 if use_progen:
screamer 0:66f3b5499f7f 122 try:
screamer 0:66f3b5499f7f 123 import pkg_resources
screamer 0:66f3b5499f7f 124 version = pkg_resources.get_distribution('project_generator').version
screamer 0:66f3b5499f7f 125 exporter_yaml['project_generator']['version'] = version
screamer 0:66f3b5499f7f 126 exporter_yaml['project_generator']['active'] = True;
screamer 0:66f3b5499f7f 127 exporter_yaml['project_generator_definitions'] = {}
screamer 0:66f3b5499f7f 128 version = pkg_resources.get_distribution('project_generator_definitions').version
screamer 0:66f3b5499f7f 129 exporter_yaml['project_generator_definitions']['version'] = version
screamer 0:66f3b5499f7f 130 except ImportError:
screamer 0:66f3b5499f7f 131 pass
screamer 0:66f3b5499f7f 132 with open(os.path.join(tempdir, 'exporter.yaml'), 'w') as outfile:
screamer 0:66f3b5499f7f 133 yaml.dump(exporter_yaml, outfile, default_flow_style=False)
screamer 0:66f3b5499f7f 134 # add readme file to every offline export.
screamer 0:66f3b5499f7f 135 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 136 # copy .hgignore file to exported direcotry as well.
screamer 0:66f3b5499f7f 137 if exists(os.path.join(exporter.TEMPLATE_DIR,'.hgignore')):
screamer 13:ab47a20b66f0 138 copy(os.path.join(exporter.TEMPLATE_DIR,'.hgignore'), tempdir)
screamer 13:ab47a20b66f0 139 if make_zip:
screamer 0:66f3b5499f7f 140 zip_path = zip_working_directory_and_clean_up(tempdir, destination, project_name, clean)
screamer 0:66f3b5499f7f 141 else:
screamer 0:66f3b5499f7f 142 zip_path = destination
screamer 0:66f3b5499f7f 143
screamer 0:66f3b5499f7f 144 return zip_path, report
screamer 0:66f3b5499f7f 145
screamer 0:66f3b5499f7f 146
screamer 0:66f3b5499f7f 147 ###############################################################################
screamer 0:66f3b5499f7f 148 # Generate project folders following the online conventions
screamer 0:66f3b5499f7f 149 ###############################################################################
screamer 0:66f3b5499f7f 150 def copy_tree(src, dst, clean=True):
screamer 0:66f3b5499f7f 151 if exists(dst):
screamer 0:66f3b5499f7f 152 if clean:
screamer 0:66f3b5499f7f 153 rmtree(dst)
screamer 0:66f3b5499f7f 154 else:
screamer 0:66f3b5499f7f 155 return
screamer 0:66f3b5499f7f 156
screamer 0:66f3b5499f7f 157 copytree(src, dst)
screamer 0:66f3b5499f7f 158
screamer 0:66f3b5499f7f 159
screamer 0:66f3b5499f7f 160 def setup_user_prj(user_dir, prj_path, lib_paths=None):
screamer 0:66f3b5499f7f 161 """
screamer 0:66f3b5499f7f 162 Setup a project with the same directory structure of the mbed online IDE
screamer 0:66f3b5499f7f 163 """
screamer 0:66f3b5499f7f 164 mkdir(user_dir)
screamer 0:66f3b5499f7f 165
screamer 0:66f3b5499f7f 166 # Project Path
screamer 0:66f3b5499f7f 167 copy_tree(prj_path, join(user_dir, "src"))
screamer 0:66f3b5499f7f 168
screamer 0:66f3b5499f7f 169 # Project Libraries
screamer 0:66f3b5499f7f 170 user_lib = join(user_dir, "lib")
screamer 0:66f3b5499f7f 171 mkdir(user_lib)
screamer 0:66f3b5499f7f 172
screamer 0:66f3b5499f7f 173 if lib_paths is not None:
screamer 0:66f3b5499f7f 174 for lib_path in lib_paths:
screamer 0:66f3b5499f7f 175 copy_tree(lib_path, join(user_lib, basename(lib_path)))
screamer 0:66f3b5499f7f 176
screamer 0:66f3b5499f7f 177 def mcu_ide_matrix(verbose_html=False, platform_filter=None):
screamer 0:66f3b5499f7f 178 """ Shows target map using prettytable """
screamer 0:66f3b5499f7f 179 supported_ides = []
screamer 0:66f3b5499f7f 180 for key in EXPORTERS.iterkeys():
screamer 0:66f3b5499f7f 181 supported_ides.append(key)
screamer 0:66f3b5499f7f 182 supported_ides.sort()
screamer 0:66f3b5499f7f 183 from prettytable import PrettyTable, ALL # Only use it in this function so building works without extra modules
screamer 0:66f3b5499f7f 184
screamer 0:66f3b5499f7f 185 # All tests status table print
screamer 0:66f3b5499f7f 186 columns = ["Platform"] + supported_ides
screamer 0:66f3b5499f7f 187 pt = PrettyTable(columns)
screamer 0:66f3b5499f7f 188 # Align table
screamer 0:66f3b5499f7f 189 for col in columns:
screamer 0:66f3b5499f7f 190 pt.align[col] = "c"
screamer 0:66f3b5499f7f 191 pt.align["Platform"] = "l"
screamer 0:66f3b5499f7f 192
screamer 0:66f3b5499f7f 193 perm_counter = 0
screamer 0:66f3b5499f7f 194 target_counter = 0
screamer 0:66f3b5499f7f 195 for target in sorted(TARGET_NAMES):
screamer 0:66f3b5499f7f 196 target_counter += 1
screamer 0:66f3b5499f7f 197
screamer 0:66f3b5499f7f 198 row = [target] # First column is platform name
screamer 0:66f3b5499f7f 199 for ide in supported_ides:
screamer 0:66f3b5499f7f 200 text = "-"
screamer 0:66f3b5499f7f 201 if target in EXPORTERS[ide].TARGETS:
screamer 0:66f3b5499f7f 202 if verbose_html:
screamer 0:66f3b5499f7f 203 text = "&#10003;"
screamer 0:66f3b5499f7f 204 else:
screamer 0:66f3b5499f7f 205 text = "x"
screamer 0:66f3b5499f7f 206 perm_counter += 1
screamer 0:66f3b5499f7f 207 row.append(text)
screamer 0:66f3b5499f7f 208 pt.add_row(row)
screamer 0:66f3b5499f7f 209
screamer 0:66f3b5499f7f 210 pt.border = True
screamer 0:66f3b5499f7f 211 pt.vrules = ALL
screamer 0:66f3b5499f7f 212 pt.hrules = ALL
screamer 0:66f3b5499f7f 213 # creates a html page suitable for a browser
screamer 0:66f3b5499f7f 214 # result = pt.get_html_string(format=True) if verbose_html else pt.get_string()
screamer 0:66f3b5499f7f 215 # creates a html page in a shorter format suitable for readme.md
screamer 0:66f3b5499f7f 216 result = pt.get_html_string() if verbose_html else pt.get_string()
screamer 0:66f3b5499f7f 217 result += "\n"
screamer 0:66f3b5499f7f 218 result += "Total IDEs: %d\n"% (len(supported_ides))
screamer 0:66f3b5499f7f 219 if verbose_html: result += "<br>"
screamer 0:66f3b5499f7f 220 result += "Total platforms: %d\n"% (target_counter)
screamer 0:66f3b5499f7f 221 if verbose_html: result += "<br>"
screamer 0:66f3b5499f7f 222 result += "Total permutations: %d"% (perm_counter)
screamer 0:66f3b5499f7f 223 if verbose_html: result = result.replace("&amp;", "&")
screamer 0:66f3b5499f7f 224 return result