]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/workspace_tools/export/__init__.py
xt_usb: Fix XT soft reset
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / workspace_tools / export / __init__.py
1 """
2 mbed SDK
3 Copyright (c) 2011-2013 ARM Limited
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 """
17 import os, tempfile
18 from os.path import join, exists, basename
19 from shutil import copytree, rmtree, copy
20
21 from workspace_tools.utils import mkdir
22 from workspace_tools.export import uvision4, codesourcery, codered, gccarm, ds5_5, iar, emblocks, coide, kds, zip
23 from workspace_tools.export.exporters import zip_working_directory_and_clean_up, OldLibrariesException
24 from workspace_tools.targets import TARGET_NAMES, EXPORT_MAP
25
26 EXPORTERS = {
27     'uvision': uvision4.Uvision4,
28     'lpcxpresso': codered.CodeRed,
29     'codesourcery': codesourcery.CodeSourcery,
30     'gcc_arm': gccarm.GccArm,
31     'ds5_5': ds5_5.DS5_5,
32     'iar': iar.IAREmbeddedWorkbench,
33     'emblocks' : emblocks.IntermediateFile,
34     'coide' : coide.CoIDE,
35     'kds' : kds.KDS,
36 }
37
38 ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN = """
39 Sorry, the target %s is not currently supported on the %s toolchain.
40 Please refer to <a href='/handbook/Exporting-to-offline-toolchains' target='_blank'>Exporting to offline toolchains</a> for more information.
41 """
42
43 ERROR_MESSAGE_NOT_EXPORT_LIBS = """
44 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>.
45 """
46
47 def online_build_url_resolver(url):
48     # TODO: Retrieve the path and name of an online library build URL
49     return {'path':'', 'name':''}
50
51
52 def export(project_path, project_name, ide, target, destination='/tmp/',
53            tempdir=None, clean=True, extra_symbols=None, build_url_resolver=online_build_url_resolver):
54     # Convention: we are using capitals for toolchain and target names
55     if target is not None:
56         target = target.upper()
57
58     if tempdir is None:
59         tempdir = tempfile.mkdtemp()
60
61     report = {'success': False}
62     if ide is None or ide == "zip":
63         # Simple ZIP exporter
64         try:
65             ide = "zip"
66             exporter = zip.ZIP(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
67             exporter.scan_and_copy_resources(project_path, tempdir)
68             exporter.generate()
69             report['success'] = True
70         except OldLibrariesException, e:
71             report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
72     else:
73         if ide not in EXPORTERS:
74             report['errormsg'] = "Unsupported toolchain"
75         else:
76             Exporter = EXPORTERS[ide]
77             target = EXPORT_MAP.get(target, target)
78             if target not in Exporter.TARGETS:
79                 report['errormsg'] = ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN % (target, ide)
80             else:
81                 try:
82                     exporter = Exporter(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
83                     exporter.scan_and_copy_resources(project_path, tempdir)
84                     exporter.generate()
85                     report['success'] = True
86                 except OldLibrariesException, e:
87                     report['errormsg'] = ERROR_MESSAGE_NOT_EXPORT_LIBS
88
89     zip_path = None
90     if report['success']:
91         # add readme file to every offline export.
92         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))
93         # copy .hgignore file to exported direcotry as well.
94         copy(os.path.join(exporter.TEMPLATE_DIR,'.hgignore'),tempdir)
95         zip_path = zip_working_directory_and_clean_up(tempdir, destination, project_name, clean)
96
97     return zip_path, report
98
99
100 ###############################################################################
101 # Generate project folders following the online conventions
102 ###############################################################################
103 def copy_tree(src, dst, clean=True):
104     if exists(dst):
105         if clean:
106             rmtree(dst)
107         else:
108             return
109
110     copytree(src, dst)
111
112
113 def setup_user_prj(user_dir, prj_path, lib_paths=None):
114     """
115     Setup a project with the same directory structure of the mbed online IDE
116     """
117     mkdir(user_dir)
118
119     # Project Path
120     copy_tree(prj_path, join(user_dir, "src"))
121
122     # Project Libraries
123     user_lib = join(user_dir, "lib")
124     mkdir(user_lib)
125
126     if lib_paths is not None:
127         for lib_path in lib_paths:
128             copy_tree(lib_path, join(user_lib, basename(lib_path)))
129
130 def mcu_ide_matrix(verbose_html=False, platform_filter=None):
131     """  Shows target map using prettytable """
132     supported_ides = []
133     for key in EXPORTERS.iterkeys():
134         supported_ides.append(key)
135     supported_ides.sort()
136     from prettytable import PrettyTable, ALL # Only use it in this function so building works without extra modules
137
138     # All tests status table print
139     columns = ["Platform"] + supported_ides
140     pt = PrettyTable(columns)
141     # Align table
142     for col in columns:
143         pt.align[col] = "c"
144     pt.align["Platform"] = "l"
145
146     perm_counter = 0
147     target_counter = 0
148     for target in sorted(TARGET_NAMES):
149         target_counter += 1
150
151         row = [target]  # First column is platform name
152         for ide in supported_ides:
153             text = "-"
154             if target in EXPORTERS[ide].TARGETS:
155                 if verbose_html: 
156                     text = "&#10003;" 
157                 else: 
158                     text = "x"
159                 perm_counter += 1
160             row.append(text)
161         pt.add_row(row)
162
163     pt.border = True
164     pt.vrules = ALL
165     pt.hrules = ALL
166     # creates a html page suitable for a browser
167     # result = pt.get_html_string(format=True) if verbose_html else pt.get_string()
168     # creates a html page in a shorter format suitable for readme.md
169     result = pt.get_html_string() if verbose_html else pt.get_string()
170     result += "\n"
171     result += "Total IDEs: %d\n"% (len(supported_ides))
172     if verbose_html: result += "<br>"
173     result += "Total platforms: %d\n"% (target_counter)
174     if verbose_html: result += "<br>"
175     result += "Total permutations: %d"% (perm_counter)
176     if verbose_html: result = result.replace("&amp;", "&")
177     return result