Clone of official tools

Committer:
screamer
Date:
Sat Jul 16 22:51:17 2016 +0100
Revision:
23:fbae331171fa
Parent:
13:ab47a20b66f0
Child:
31:8ea194f6145b
Ported fixes from mbedmicro/mbed

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 23:fbae331171fa 24 from tools.export.exporters import zip_working_directory_and_clean_up, OldLibrariesException, FailedBuildException
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 23:fbae331171fa 61 tempdir=None, pgen_build = False, clean=True, extra_symbols=None, make_zip=True, sources_relative=False,
screamer 23:fbae331171fa 62 build_url_resolver=online_build_url_resolver, progen_build=False):
screamer 0:66f3b5499f7f 63 # Convention: we are using capitals for toolchain and target names
screamer 0:66f3b5499f7f 64 if target is not None:
screamer 0:66f3b5499f7f 65 target = target.upper()
screamer 0:66f3b5499f7f 66
screamer 0:66f3b5499f7f 67 if tempdir is None:
screamer 0:66f3b5499f7f 68 tempdir = tempfile.mkdtemp()
screamer 0:66f3b5499f7f 69
screamer 13:ab47a20b66f0 70 use_progen = False
screamer 13:ab47a20b66f0 71 supported = True
screamer 23:fbae331171fa 72 report = {'success': False, 'errormsg':'', 'skip': False}
screamer 23:fbae331171fa 73
screamer 0:66f3b5499f7f 74 if ide is None or ide == "zip":
screamer 0:66f3b5499f7f 75 # Simple ZIP exporter
screamer 0:66f3b5499f7f 76 try:
screamer 0:66f3b5499f7f 77 ide = "zip"
screamer 0:66f3b5499f7f 78 exporter = zip.ZIP(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
screamer 13:ab47a20b66f0 79 exporter.scan_and_copy_resources(project_path, tempdir, sources_relative)
screamer 0:66f3b5499f7f 80 exporter.generate()
screamer 0:66f3b5499f7f 81 report['success'] = True
screamer 0:66f3b5499f7f 82 except OldLibrariesException, e:
screamer 0:66f3b5499f7f 83 report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
screamer 0:66f3b5499f7f 84 else:
screamer 0:66f3b5499f7f 85 if ide not in EXPORTERS:
screamer 0:66f3b5499f7f 86 report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
screamer 23:fbae331171fa 87 report['skip'] = True
screamer 0:66f3b5499f7f 88 else:
screamer 0:66f3b5499f7f 89 Exporter = EXPORTERS[ide]
screamer 0:66f3b5499f7f 90 target = EXPORT_MAP.get(target, target)
screamer 0:66f3b5499f7f 91 try:
screamer 0:66f3b5499f7f 92 if Exporter.PROGEN_ACTIVE:
screamer 0:66f3b5499f7f 93 use_progen = True
screamer 0:66f3b5499f7f 94 except AttributeError:
screamer 0:66f3b5499f7f 95 pass
screamer 23:fbae331171fa 96
screamer 23:fbae331171fa 97 if target not in Exporter.TARGETS or Exporter.TOOLCHAIN not in TARGET_MAP[target].supported_toolchains:
screamer 23:fbae331171fa 98 supported = False
screamer 23:fbae331171fa 99
screamer 0:66f3b5499f7f 100 if use_progen:
screamer 0:66f3b5499f7f 101 if not ProGenDef(ide).is_supported(TARGET_MAP[target].progen['target']):
screamer 0:66f3b5499f7f 102 supported = False
screamer 0:66f3b5499f7f 103
screamer 0:66f3b5499f7f 104 if supported:
screamer 0:66f3b5499f7f 105 # target checked, export
screamer 0:66f3b5499f7f 106 try:
screamer 13:ab47a20b66f0 107 exporter = Exporter(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols, sources_relative=sources_relative)
screamer 13:ab47a20b66f0 108 exporter.scan_and_copy_resources(project_path, tempdir, sources_relative)
screamer 23:fbae331171fa 109 if progen_build:
screamer 23:fbae331171fa 110 #try to build with pgen ide builders
screamer 23:fbae331171fa 111 try:
screamer 23:fbae331171fa 112 exporter.generate(progen_build=True)
screamer 23:fbae331171fa 113 report['success'] = True
screamer 23:fbae331171fa 114 except FailedBuildException, f:
screamer 23:fbae331171fa 115 report['errormsg'] = "Build Failed"
screamer 23:fbae331171fa 116 else:
screamer 23:fbae331171fa 117 exporter.generate()
screamer 23:fbae331171fa 118 report['success'] = True
screamer 0:66f3b5499f7f 119 except OldLibrariesException, e:
screamer 0:66f3b5499f7f 120 report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
screamer 23:fbae331171fa 121
screamer 0:66f3b5499f7f 122 else:
screamer 0:66f3b5499f7f 123 report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
screamer 23:fbae331171fa 124 report['skip'] = True
screamer 0:66f3b5499f7f 125
screamer 0:66f3b5499f7f 126 zip_path = None
screamer 0:66f3b5499f7f 127 if report['success']:
screamer 0:66f3b5499f7f 128 # readme.txt to contain more exported data
screamer 0:66f3b5499f7f 129 exporter_yaml = {
screamer 0:66f3b5499f7f 130 'project_generator': {
screamer 0:66f3b5499f7f 131 'active' : False,
screamer 0:66f3b5499f7f 132 }
screamer 0:66f3b5499f7f 133 }
screamer 0:66f3b5499f7f 134 if use_progen:
screamer 0:66f3b5499f7f 135 try:
screamer 0:66f3b5499f7f 136 import pkg_resources
screamer 0:66f3b5499f7f 137 version = pkg_resources.get_distribution('project_generator').version
screamer 0:66f3b5499f7f 138 exporter_yaml['project_generator']['version'] = version
screamer 0:66f3b5499f7f 139 exporter_yaml['project_generator']['active'] = True;
screamer 0:66f3b5499f7f 140 exporter_yaml['project_generator_definitions'] = {}
screamer 0:66f3b5499f7f 141 version = pkg_resources.get_distribution('project_generator_definitions').version
screamer 0:66f3b5499f7f 142 exporter_yaml['project_generator_definitions']['version'] = version
screamer 0:66f3b5499f7f 143 except ImportError:
screamer 0:66f3b5499f7f 144 pass
screamer 0:66f3b5499f7f 145 with open(os.path.join(tempdir, 'exporter.yaml'), 'w') as outfile:
screamer 0:66f3b5499f7f 146 yaml.dump(exporter_yaml, outfile, default_flow_style=False)
screamer 0:66f3b5499f7f 147 # add readme file to every offline export.
screamer 0:66f3b5499f7f 148 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 149 # copy .hgignore file to exported direcotry as well.
screamer 0:66f3b5499f7f 150 if exists(os.path.join(exporter.TEMPLATE_DIR,'.hgignore')):
screamer 13:ab47a20b66f0 151 copy(os.path.join(exporter.TEMPLATE_DIR,'.hgignore'), tempdir)
screamer 13:ab47a20b66f0 152 if make_zip:
screamer 0:66f3b5499f7f 153 zip_path = zip_working_directory_and_clean_up(tempdir, destination, project_name, clean)
screamer 0:66f3b5499f7f 154 else:
screamer 0:66f3b5499f7f 155 zip_path = destination
screamer 0:66f3b5499f7f 156
screamer 0:66f3b5499f7f 157 return zip_path, report
screamer 0:66f3b5499f7f 158
screamer 0:66f3b5499f7f 159
screamer 0:66f3b5499f7f 160 ###############################################################################
screamer 0:66f3b5499f7f 161 # Generate project folders following the online conventions
screamer 0:66f3b5499f7f 162 ###############################################################################
screamer 0:66f3b5499f7f 163 def copy_tree(src, dst, clean=True):
screamer 0:66f3b5499f7f 164 if exists(dst):
screamer 0:66f3b5499f7f 165 if clean:
screamer 0:66f3b5499f7f 166 rmtree(dst)
screamer 0:66f3b5499f7f 167 else:
screamer 0:66f3b5499f7f 168 return
screamer 0:66f3b5499f7f 169
screamer 0:66f3b5499f7f 170 copytree(src, dst)
screamer 0:66f3b5499f7f 171
screamer 0:66f3b5499f7f 172
screamer 0:66f3b5499f7f 173 def setup_user_prj(user_dir, prj_path, lib_paths=None):
screamer 0:66f3b5499f7f 174 """
screamer 0:66f3b5499f7f 175 Setup a project with the same directory structure of the mbed online IDE
screamer 0:66f3b5499f7f 176 """
screamer 0:66f3b5499f7f 177 mkdir(user_dir)
screamer 0:66f3b5499f7f 178
screamer 0:66f3b5499f7f 179 # Project Path
screamer 0:66f3b5499f7f 180 copy_tree(prj_path, join(user_dir, "src"))
screamer 0:66f3b5499f7f 181
screamer 0:66f3b5499f7f 182 # Project Libraries
screamer 0:66f3b5499f7f 183 user_lib = join(user_dir, "lib")
screamer 0:66f3b5499f7f 184 mkdir(user_lib)
screamer 0:66f3b5499f7f 185
screamer 0:66f3b5499f7f 186 if lib_paths is not None:
screamer 0:66f3b5499f7f 187 for lib_path in lib_paths:
screamer 0:66f3b5499f7f 188 copy_tree(lib_path, join(user_lib, basename(lib_path)))
screamer 0:66f3b5499f7f 189
screamer 0:66f3b5499f7f 190 def mcu_ide_matrix(verbose_html=False, platform_filter=None):
screamer 0:66f3b5499f7f 191 """ Shows target map using prettytable """
screamer 0:66f3b5499f7f 192 supported_ides = []
screamer 0:66f3b5499f7f 193 for key in EXPORTERS.iterkeys():
screamer 0:66f3b5499f7f 194 supported_ides.append(key)
screamer 0:66f3b5499f7f 195 supported_ides.sort()
screamer 0:66f3b5499f7f 196 from prettytable import PrettyTable, ALL # Only use it in this function so building works without extra modules
screamer 0:66f3b5499f7f 197
screamer 0:66f3b5499f7f 198 # All tests status table print
screamer 0:66f3b5499f7f 199 columns = ["Platform"] + supported_ides
screamer 0:66f3b5499f7f 200 pt = PrettyTable(columns)
screamer 0:66f3b5499f7f 201 # Align table
screamer 0:66f3b5499f7f 202 for col in columns:
screamer 0:66f3b5499f7f 203 pt.align[col] = "c"
screamer 0:66f3b5499f7f 204 pt.align["Platform"] = "l"
screamer 0:66f3b5499f7f 205
screamer 0:66f3b5499f7f 206 perm_counter = 0
screamer 0:66f3b5499f7f 207 target_counter = 0
screamer 0:66f3b5499f7f 208 for target in sorted(TARGET_NAMES):
screamer 0:66f3b5499f7f 209 target_counter += 1
screamer 0:66f3b5499f7f 210
screamer 0:66f3b5499f7f 211 row = [target] # First column is platform name
screamer 0:66f3b5499f7f 212 for ide in supported_ides:
screamer 0:66f3b5499f7f 213 text = "-"
screamer 0:66f3b5499f7f 214 if target in EXPORTERS[ide].TARGETS:
screamer 0:66f3b5499f7f 215 if verbose_html:
screamer 0:66f3b5499f7f 216 text = "&#10003;"
screamer 0:66f3b5499f7f 217 else:
screamer 0:66f3b5499f7f 218 text = "x"
screamer 0:66f3b5499f7f 219 perm_counter += 1
screamer 0:66f3b5499f7f 220 row.append(text)
screamer 0:66f3b5499f7f 221 pt.add_row(row)
screamer 0:66f3b5499f7f 222
screamer 0:66f3b5499f7f 223 pt.border = True
screamer 0:66f3b5499f7f 224 pt.vrules = ALL
screamer 0:66f3b5499f7f 225 pt.hrules = ALL
screamer 0:66f3b5499f7f 226 # creates a html page suitable for a browser
screamer 0:66f3b5499f7f 227 # result = pt.get_html_string(format=True) if verbose_html else pt.get_string()
screamer 0:66f3b5499f7f 228 # creates a html page in a shorter format suitable for readme.md
screamer 0:66f3b5499f7f 229 result = pt.get_html_string() if verbose_html else pt.get_string()
screamer 0:66f3b5499f7f 230 result += "\n"
screamer 0:66f3b5499f7f 231 result += "Total IDEs: %d\n"% (len(supported_ides))
screamer 0:66f3b5499f7f 232 if verbose_html: result += "<br>"
screamer 0:66f3b5499f7f 233 result += "Total platforms: %d\n"% (target_counter)
screamer 0:66f3b5499f7f 234 if verbose_html: result += "<br>"
screamer 0:66f3b5499f7f 235 result += "Total permutations: %d"% (perm_counter)
screamer 0:66f3b5499f7f 236 if verbose_html: result = result.replace("&amp;", "&")
screamer 0:66f3b5499f7f 237 return result