]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/workspace_tools/project.py
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / workspace_tools / project.py
1 import sys
2 from os.path import join, abspath, dirname, exists
3 ROOT = abspath(join(dirname(__file__), ".."))
4 sys.path.insert(0, ROOT)
5
6 from shutil import move, rmtree
7 from optparse import OptionParser
8
9 from workspace_tools.paths import EXPORT_DIR, EXPORT_WORKSPACE, EXPORT_TMP
10 from workspace_tools.paths import MBED_BASE, MBED_LIBRARIES
11 from workspace_tools.export import export, setup_user_prj, EXPORTERS, mcu_ide_matrix
12 from workspace_tools.utils import args_error
13 from workspace_tools.tests import TESTS, Test, TEST_MAP
14 from workspace_tools.targets import TARGET_NAMES
15 from workspace_tools.libraries import LIBRARIES
16
17 try:
18     import workspace_tools.private_settings as ps
19 except:
20     ps = object()
21
22
23 if __name__ == '__main__':
24     # Parse Options
25     parser = OptionParser()
26
27     targetnames = TARGET_NAMES
28     targetnames.sort()
29     toolchainlist = EXPORTERS.keys()
30     toolchainlist.sort()
31
32     parser.add_option("-m", "--mcu",
33                       metavar="MCU",
34                       default='LPC1768',
35                       help="generate project for the given MCU (%s)"% ', '.join(targetnames))
36
37     parser.add_option("-i",
38                       dest="ide",
39                       default='uvision',
40                       help="The target IDE: %s"% str(toolchainlist))
41
42     parser.add_option("-c", "--clean",
43                       action="store_true",
44                       default=False,
45                       help="clean the export directory")
46
47     parser.add_option("-p",
48                       type="int",
49                       dest="program",
50                       help="The index of the desired test program: [0-%d]"% (len(TESTS)-1))
51
52     parser.add_option("-n",
53                       dest="program_name",
54                       help="The name of the desired test program")
55
56     parser.add_option("-b",
57                       dest="build",
58                       action="store_true",
59                       default=False,
60                       help="use the mbed library build, instead of the sources")
61
62     parser.add_option("-L", "--list-tests",
63                       action="store_true",
64                       dest="list_tests",
65                       default=False,
66                       help="list available programs in order and exit")
67
68     parser.add_option("-S", "--list-matrix",
69                       action="store_true",
70                       dest="supported_ides",
71                       default=False,
72                       help="displays supported matrix of MCUs and IDEs")
73
74     parser.add_option("-E",
75                       action="store_true",
76                       dest="supported_ides_html",
77                       default=False,
78                       help="writes workspace_tools/export/README.md")
79
80     (options, args) = parser.parse_args()
81
82     # Print available tests in order and exit
83     if options.list_tests is True:
84         print '\n'.join(map(str, sorted(TEST_MAP.values())))
85         sys.exit()
86
87     # Only prints matrix of supported IDEs
88     if options.supported_ides:
89         print mcu_ide_matrix()
90         exit(0)
91
92     # Only prints matrix of supported IDEs
93     if options.supported_ides_html:
94         html = mcu_ide_matrix(verbose_html=True)
95         try:
96             with open("./export/README.md","w") as f:
97                 f.write("Exporter IDE/Platform Support\n")
98                 f.write("-----------------------------------\n")
99                 f.write("\n")
100                 f.write(html)
101         except IOError as e:
102             print "I/O error({0}): {1}".format(e.errno, e.strerror)
103         except:
104             print "Unexpected error:", sys.exc_info()[0]
105             raise
106         exit(0)
107
108     # Clean Export Directory
109     if options.clean:
110         if exists(EXPORT_DIR):
111             rmtree(EXPORT_DIR)
112
113     # Target
114     if options.mcu is None :
115         args_error(parser, "[ERROR] You should specify an MCU")
116     mcus = options.mcu
117
118     # IDE
119     if options.ide is None:
120         args_error(parser, "[ERROR] You should specify an IDE")
121     ide = options.ide
122
123     # Export results
124     successes = []
125     failures = []
126
127     for mcu in mcus.split(','):
128         # Program Number or name
129         p, n = options.program, options.program_name
130
131         if n is not None and p is not None:
132             args_error(parser, "[ERROR] specify either '-n' or '-p', not both")
133         if n:
134             if not n in TEST_MAP.keys():
135                 # Check if there is an alias for this in private_settings.py
136                 if getattr(ps, "test_alias", None) is not None:
137                     alias = ps.test_alias.get(n, "")
138                     if not alias in TEST_MAP.keys():
139                         args_error(parser, "[ERROR] Program with name '%s' not found" % n)
140                     else:
141                         n = alias
142                 else:
143                     args_error(parser, "[ERROR] Program with name '%s' not found" % n)
144             p = TEST_MAP[n].n
145         if p is None or (p < 0) or (p > (len(TESTS)-1)):
146             message = "[ERROR] You have to specify one of the following tests:\n"
147             message += '\n'.join(map(str, sorted(TEST_MAP.values())))
148             args_error(parser, message)
149
150         # Project
151         if p is None or (p < 0) or (p > (len(TESTS)-1)):
152             message = "[ERROR] You have to specify one of the following tests:\n"
153             message += '\n'.join(map(str, sorted(TEST_MAP.values())))
154             args_error(parser, message)
155         test = Test(p)
156
157         # Some libraries have extra macros (called by exporter symbols) to we need to pass
158         # them to maintain compilation macros integrity between compiled library and
159         # header files we might use with it
160         lib_symbols = []
161         for lib in LIBRARIES:
162             if lib['build_dir'] in test.dependencies:
163                 lib_macros = lib.get('macros', None)
164                 if lib_macros is not None:
165                     lib_symbols.extend(lib_macros)
166
167         if not options.build:
168             # Substitute the library builds with the sources
169             # TODO: Substitute also the other library build paths
170             if MBED_LIBRARIES in test.dependencies:
171                 test.dependencies.remove(MBED_LIBRARIES)
172                 test.dependencies.append(MBED_BASE)
173
174         # Build the project with the same directory structure of the mbed online IDE
175         project_dir = join(EXPORT_WORKSPACE, test.id)
176         setup_user_prj(project_dir, test.source_dir, test.dependencies)
177
178         # Export to selected toolchain
179         tmp_path, report = export(project_dir, test.id, ide, mcu, EXPORT_WORKSPACE, EXPORT_TMP, extra_symbols=lib_symbols)
180         if report['success']:
181             zip_path = join(EXPORT_DIR, "%s_%s_%s.zip" % (test.id, ide, mcu))
182             move(tmp_path, zip_path)
183             successes.append("%s::%s\t%s"% (mcu, ide, zip_path))
184         else:
185             failures.append("%s::%s\t%s"% (mcu, ide, report['errormsg']))
186
187     # Prints export results
188     print
189     if len(successes) > 0:
190         print "Successful exports:"
191         for success in successes:
192             print "  * %s"% success
193     if len(failures) > 0:
194         print "Failed exports:"
195         for failure in failures:
196             print "  * %s"% failure