]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/workspace_tools/make.py
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / workspace_tools / make.py
1 #! /usr/bin/env python2
2 """
3 mbed SDK
4 Copyright (c) 2011-2013 ARM Limited
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18
19 TEST BUILD & RUN
20 """
21 import sys
22 from time import sleep
23 from shutil import copy
24 from os.path import join, abspath, dirname
25
26 # Be sure that the tools directory is in the search path
27 ROOT = abspath(join(dirname(__file__), ".."))
28 sys.path.insert(0, ROOT)
29
30 from workspace_tools.utils import args_error
31 from workspace_tools.paths import BUILD_DIR
32 from workspace_tools.paths import RTOS_LIBRARIES
33 from workspace_tools.paths import ETH_LIBRARY
34 from workspace_tools.paths import USB_HOST_LIBRARIES, USB_LIBRARIES
35 from workspace_tools.paths import DSP_LIBRARIES
36 from workspace_tools.paths import FS_LIBRARY
37 from workspace_tools.paths import UBLOX_LIBRARY
38 from workspace_tools.tests import TESTS, Test, TEST_MAP
39 from workspace_tools.tests import TEST_MBED_LIB
40 from workspace_tools.targets import TARGET_MAP
41 from workspace_tools.options import get_default_options_parser
42 from workspace_tools.build_api import build_project
43 try:
44 import workspace_tools.private_settings as ps
45 except:
46 ps = object()
47
48
49 if __name__ == '__main__':
50 # Parse Options
51 parser = get_default_options_parser()
52 parser.add_option("-p",
53 type="int",
54 dest="program",
55 help="The index of the desired test program: [0-%d]" % (len(TESTS)-1))
56
57 parser.add_option("-n",
58 dest="program_name",
59 help="The name of the desired test program")
60
61 parser.add_option("-j", "--jobs",
62 type="int",
63 dest="jobs",
64 default=1,
65 help="Number of concurrent jobs (default 1). Use 0 for auto based on host machine's number of CPUs")
66
67 parser.add_option("-v", "--verbose",
68 action="store_true",
69 dest="verbose",
70 default=False,
71 help="Verbose diagnostic output")
72
73 parser.add_option("--silent",
74 action="store_true",
75 dest="silent",
76 default=False,
77 help="Silent diagnostic output (no copy, compile notification)")
78
79 parser.add_option("-D", "",
80 action="append",
81 dest="macros",
82 help="Add a macro definition")
83
84 # Local run
85 parser.add_option("--automated", action="store_true", dest="automated",
86 default=False, help="Automated test")
87 parser.add_option("--host", dest="host_test",
88 default=None, help="Host test")
89 parser.add_option("--extra", dest="extra",
90 default=None, help="Extra files")
91 parser.add_option("--peripherals", dest="peripherals",
92 default=None, help="Required peripherals")
93 parser.add_option("--dep", dest="dependencies",
94 default=None, help="Dependencies")
95 parser.add_option("--source", dest="source_dir",
96 default=None, help="The source (input) directory")
97 parser.add_option("--duration", type="int", dest="duration",
98 default=None, help="Duration of the test")
99 parser.add_option("--build", dest="build_dir",
100 default=None, help="The build (output) directory")
101 parser.add_option("-d", "--disk", dest="disk",
102 default=None, help="The mbed disk")
103 parser.add_option("-s", "--serial", dest="serial",
104 default=None, help="The mbed serial port")
105 parser.add_option("-b", "--baud", type="int", dest="baud",
106 default=None, help="The mbed serial baud rate")
107 parser.add_option("-L", "--list-tests", action="store_true", dest="list_tests",
108 default=False, help="List available tests in order and exit")
109
110 # Ideally, all the tests with a single "main" thread can be run with, or
111 # without the rtos, eth, usb_host, usb, dsp, fat, ublox
112 parser.add_option("--rtos",
113 action="store_true", dest="rtos",
114 default=False, help="Link with RTOS library")
115
116 parser.add_option("--eth",
117 action="store_true", dest="eth",
118 default=False,
119 help="Link with Ethernet library")
120
121 parser.add_option("--usb_host",
122 action="store_true",
123 dest="usb_host",
124 default=False,
125 help="Link with USB Host library")
126
127 parser.add_option("--usb",
128 action="store_true",
129 dest="usb",
130 default=False,
131 help="Link with USB Device library")
132
133 parser.add_option("--dsp",
134 action="store_true",
135 dest="dsp",
136 default=False,
137 help="Link with DSP library")
138
139 parser.add_option("--fat",
140 action="store_true",
141 dest="fat",
142 default=False,
143 help="Link with FS ad SD card file system library")
144
145 parser.add_option("--ublox",
146 action="store_true",
147 dest="ublox",
148 default=False,
149 help="Link with U-Blox library")
150
151 parser.add_option("--testlib",
152 action="store_true",
153 dest="testlib",
154 default=False,
155 help="Link with mbed test library")
156
157 # Specify a different linker script
158 parser.add_option("-l", "--linker", dest="linker_script",
159 default=None, help="use the specified linker script")
160
161 (options, args) = parser.parse_args()
162
163 # Print available tests in order and exit
164 if options.list_tests is True:
165 print '\n'.join(map(str, sorted(TEST_MAP.values())))
166 sys.exit()
167
168 # force program to "0" if a source dir is specified
169 if options.source_dir is not None:
170 p = 0
171 n = None
172 else:
173 # Program Number or name
174 p, n = options.program, options.program_name
175
176 if n is not None and p is not None:
177 args_error(parser, "[ERROR] specify either '-n' or '-p', not both")
178 if n:
179 # We will transform 'n' to list of 'p' (integers which are test numbers)
180 nlist = n.split(',')
181 for test_id in nlist:
182 if test_id not in TEST_MAP.keys():
183 args_error(parser, "[ERROR] Program with name '%s' not found"% test_id)
184
185 p = [TEST_MAP[n].n for n in nlist]
186 elif p is None or (p < 0) or (p > (len(TESTS)-1)):
187 message = "[ERROR] You have to specify one of the following tests:\n"
188 message += '\n'.join(map(str, sorted(TEST_MAP.values())))
189 args_error(parser, message)
190
191 # If 'p' was set via -n to list of numbers make this a single element integer list
192 if type(p) != type([]):
193 p = [p]
194
195 # Target
196 if options.mcu is None :
197 args_error(parser, "[ERROR] You should specify an MCU")
198 mcu = options.mcu
199
200 # Toolchain
201 if options.tool is None:
202 args_error(parser, "[ERROR] You should specify a TOOLCHAIN")
203 toolchain = options.tool
204
205 # Test
206 for test_no in p:
207 test = Test(test_no)
208 if options.automated is not None: test.automated = options.automated
209 if options.dependencies is not None: test.dependencies = options.dependencies
210 if options.host_test is not None: test.host_test = options.host_test;
211 if options.peripherals is not None: test.peripherals = options.peripherals;
212 if options.duration is not None: test.duration = options.duration;
213 if options.extra is not None: test.extra_files = options.extra
214
215 if not test.is_supported(mcu, toolchain):
216 print 'The selected test is not supported on target %s with toolchain %s' % (mcu, toolchain)
217 sys.exit()
218
219 # Linking with extra libraries
220 if options.rtos: test.dependencies.append(RTOS_LIBRARIES)
221 if options.eth: test.dependencies.append(ETH_LIBRARY)
222 if options.usb_host: test.dependencies.append(USB_HOST_LIBRARIES)
223 if options.usb: test.dependencies.append(USB_LIBRARIES)
224 if options.dsp: test.dependencies.append(DSP_LIBRARIES)
225 if options.fat: test.dependencies.append(FS_LIBRARY)
226 if options.ublox: test.dependencies.append(UBLOX_LIBRARY)
227 if options.testlib: test.dependencies.append(TEST_MBED_LIB)
228
229 build_dir = join(BUILD_DIR, "test", mcu, toolchain, test.id)
230 if options.source_dir is not None:
231 test.source_dir = options.source_dir
232 build_dir = options.source_dir
233
234 if options.build_dir is not None:
235 build_dir = options.build_dir
236
237 target = TARGET_MAP[mcu]
238 try:
239 bin_file = build_project(test.source_dir, build_dir, target, toolchain, test.dependencies, options.options,
240 linker_script=options.linker_script,
241 clean=options.clean,
242 verbose=options.verbose,
243 silent=options.silent,
244 macros=options.macros,
245 jobs=options.jobs)
246 print 'Image: %s'% bin_file
247
248 if options.disk:
249 # Simple copy to the mbed disk
250 copy(bin_file, options.disk)
251
252 if options.serial:
253 # Import pyserial: https://pypi.python.org/pypi/pyserial
254 from serial import Serial
255
256 sleep(target.program_cycle_s())
257
258 serial = Serial(options.serial, timeout = 1)
259 if options.baud:
260 serial.setBaudrate(options.baud)
261
262 serial.flushInput()
263 serial.flushOutput()
264
265 try:
266 serial.sendBreak()
267 except:
268 # In linux a termios.error is raised in sendBreak and in setBreak.
269 # The following setBreak() is needed to release the reset signal on the target mcu.
270 try:
271 serial.setBreak(False)
272 except:
273 pass
274
275 while True:
276 c = serial.read(512)
277 sys.stdout.write(c)
278 sys.stdout.flush()
279
280 except KeyboardInterrupt, e:
281 print "\n[CTRL+c] exit"
282 except Exception,e:
283 if options.verbose:
284 import traceback
285 traceback.print_exc(file=sys.stdout)
286 else:
287 print "[ERROR] %s" % str(e)
Imprint / Impressum