Greg Steiert / maxim_dev

Dependents:   sensomed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers make.py Source File

make.py

00001 #! /usr/bin/env python2
00002 """
00003 mbed SDK
00004 Copyright (c) 2011-2013 ARM Limited
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may not use this file except in compliance with the License.
00008 You may obtain a copy of the License at
00009 
00010     http://www.apache.org/licenses/LICENSE-2.0
00011 
00012 Unless required by applicable law or agreed to in writing, software
00013 distributed under the License is distributed on an "AS IS" BASIS,
00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 See the License for the specific language governing permissions and
00016 limitations under the License.
00017 
00018 
00019 TEST BUILD & RUN
00020 """
00021 import sys
00022 import json
00023 from time import sleep
00024 from shutil import copy
00025 from os.path import join, abspath, dirname
00026 
00027 # Be sure that the tools directory is in the search path
00028 ROOT = abspath(join(dirname(__file__), ".."))
00029 sys.path.insert(0, ROOT)
00030 
00031 from tools.utils import args_error
00032 from tools.utils import NotSupportedException
00033 from tools.paths import BUILD_DIR
00034 from tools.paths import MBED_LIBRARIES
00035 from tools.paths import RTOS_LIBRARIES
00036 from tools.paths import RPC_LIBRARY
00037 from tools.paths import ETH_LIBRARY
00038 from tools.paths import USB_HOST_LIBRARIES, USB_LIBRARIES
00039 from tools.paths import DSP_LIBRARIES
00040 from tools.paths import FS_LIBRARY
00041 from tools.paths import UBLOX_LIBRARY
00042 from tools.tests import TESTS, Test, TEST_MAP
00043 from tools.tests import TEST_MBED_LIB
00044 from tools.tests import test_known, test_name_known
00045 from tools.targets import TARGET_MAP
00046 from tools.options import get_default_options_parser
00047 from tools.options import extract_profile
00048 from tools.build_api import build_project
00049 from tools.build_api import mcu_toolchain_matrix
00050 from utils import argparse_filestring_type
00051 from utils import argparse_many
00052 from utils import argparse_dir_not_parent
00053 from tools.toolchains import mbedToolchain, TOOLCHAIN_CLASSES, TOOLCHAIN_PATHS
00054 from tools.settings import CLI_COLOR_MAP
00055 
00056 if __name__ == '__main__':
00057     # Parse Options
00058     parser = get_default_options_parser(add_app_config=True)
00059     group = parser.add_mutually_exclusive_group(required=False)
00060     group.add_argument("-p",
00061                       type=argparse_many(test_known),
00062                       dest="program",
00063                       help="The index of the desired test program: [0-%d]" % (len(TESTS)-1))
00064 
00065     group.add_argument("-n",
00066                        type=argparse_many(test_name_known),
00067                       dest="program",
00068                       help="The name of the desired test program")
00069 
00070     parser.add_argument("-j", "--jobs",
00071                       type=int,
00072                       dest="jobs",
00073                       default=0,
00074                       help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
00075 
00076     parser.add_argument("-v", "--verbose",
00077                       action="store_true",
00078                       dest="verbose",
00079                       default=False,
00080                       help="Verbose diagnostic output")
00081 
00082     parser.add_argument("--silent",
00083                       action="store_true",
00084                       dest="silent",
00085                       default=False,
00086                       help="Silent diagnostic output (no copy, compile notification)")
00087 
00088     parser.add_argument("-D",
00089                       action="append",
00090                       dest="macros",
00091                       help="Add a macro definition")
00092 
00093     group.add_argument("-S", "--supported-toolchains",
00094                       action="store_true",
00095                       dest="supported_toolchains",
00096                       default=False,
00097                       help="Displays supported matrix of MCUs and toolchains")
00098 
00099     parser.add_argument('-f', '--filter',
00100                       dest='general_filter_regex',
00101                       default=None,
00102                       help='For some commands you can use filter to filter out results')
00103 
00104     # Local run
00105     parser.add_argument("--automated", action="store_true", dest="automated",
00106                       default=False, help="Automated test")
00107     parser.add_argument("--host", dest="host_test",
00108                       default=None, help="Host test")
00109     parser.add_argument("--extra", dest="extra",
00110                       default=None, help="Extra files")
00111     parser.add_argument("--peripherals", dest="peripherals",
00112                       default=None, help="Required peripherals")
00113     parser.add_argument("--dep", dest="dependencies",
00114                       default=None, help="Dependencies")
00115     parser.add_argument("--source", dest="source_dir", type=argparse_filestring_type,
00116                        default=None, help="The source (input) directory", action="append")
00117     parser.add_argument("--duration", type=int, dest="duration",
00118                       default=None, help="Duration of the test")
00119     parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT),
00120                       default=None, help="The build (output) directory")
00121     parser.add_argument("-N", "--artifact-name", dest="artifact_name",
00122                       default=None, help="The built project's name")
00123     parser.add_argument("-d", "--disk", dest="disk",
00124                       default=None, help="The mbed disk")
00125     parser.add_argument("-s", "--serial", dest="serial",
00126                       default=None, help="The mbed serial port")
00127     parser.add_argument("-b", "--baud", type=int, dest="baud",
00128                       default=None, help="The mbed serial baud rate")
00129     group.add_argument("-L", "--list-tests", action="store_true", dest="list_tests",
00130                       default=False, help="List available tests in order and exit")
00131 
00132     # Ideally, all the tests with a single "main" thread can be run with, or
00133     # without the rtos, eth, usb_host, usb, dsp, fat, ublox
00134     parser.add_argument("--rtos",
00135                       action="store_true", dest="rtos",
00136                       default=False, help="Link with RTOS library")
00137 
00138     parser.add_argument("--rpc",
00139                       action="store_true", dest="rpc",
00140                       default=False, help="Link with RPC library")
00141 
00142     parser.add_argument("--eth",
00143                       action="store_true", dest="eth",
00144                       default=False,
00145                       help="Link with Ethernet library")
00146 
00147     parser.add_argument("--usb_host",
00148                       action="store_true",
00149                       dest="usb_host",
00150                       default=False,
00151                       help="Link with USB Host library")
00152 
00153     parser.add_argument("--usb",
00154                       action="store_true",
00155                       dest="usb",
00156                       default=False,
00157                       help="Link with USB Device library")
00158 
00159     parser.add_argument("--dsp",
00160                       action="store_true",
00161                       dest="dsp",
00162                       default=False,
00163                       help="Link with DSP library")
00164 
00165     parser.add_argument("--fat",
00166                       action="store_true",
00167                       dest="fat",
00168                       default=False,
00169                       help="Link with FS ad SD card file system library")
00170 
00171     parser.add_argument("--ublox",
00172                       action="store_true",
00173                       dest="ublox",
00174                       default=False,
00175                       help="Link with U-Blox library")
00176 
00177     parser.add_argument("--testlib",
00178                       action="store_true",
00179                       dest="testlib",
00180                       default=False,
00181                       help="Link with mbed test library")
00182 
00183     # Specify a different linker script
00184     parser.add_argument("-l", "--linker", dest="linker_script",
00185                       type=argparse_filestring_type,
00186                       default=None, help="use the specified linker script")
00187 
00188     options = parser.parse_args()
00189 
00190     # Only prints matrix of supported toolchains
00191     if options.supported_toolchains:
00192         print mcu_toolchain_matrix(platform_filter=options.general_filter_regex)
00193         exit(0)
00194 
00195     # Print available tests in order and exit
00196     if options.list_tests is True:
00197         print '\n'.join(map(str, sorted(TEST_MAP.values())))
00198         sys.exit()
00199 
00200     # force program to "0" if a source dir is specified
00201     if options.source_dir is not None:
00202         p = 0
00203     else:
00204     # Program Number or name
00205         p = options.program
00206 
00207     # If 'p' was set via -n to list of numbers make this a single element integer list
00208     if type(p) != type([]):
00209         p = [p]
00210 
00211     # Target
00212     if options.mcu is None :
00213         args_error(parser, "argument -m/--mcu is required")
00214     mcu = options.mcu[0]
00215 
00216     # Toolchain
00217     if options.tool is None:
00218         args_error(parser, "argument -t/--tool is required")
00219     toolchain = options.tool[0]
00220 
00221     if (options.program is None) and (not options.source_dir):
00222         args_error(parser, "one of -p, -n, or --source is required")
00223 
00224     if options.source_dir and not options.build_dir:
00225         args_error(parser, "argument --build is required when argument --source is provided")
00226 
00227 
00228     if options.color:
00229         # This import happens late to prevent initializing colorization when we don't need it
00230         import colorize
00231         if options.verbose:
00232             notify = mbedToolchain.print_notify_verbose
00233         else:
00234             notify = mbedToolchain.print_notify
00235         notify = colorize.print_in_color_notifier(CLI_COLOR_MAP, notify)
00236     else:
00237         notify = None
00238 
00239     if not TOOLCHAIN_CLASSES[toolchain].check_executable():
00240         search_path = TOOLCHAIN_PATHS[toolchain] or "No path set"
00241         args_error(parser, "Could not find executable for %s.\n"
00242                            "Currently set search path: %s"
00243                            %(toolchain,search_path))
00244 
00245     # Test
00246     for test_no in p:
00247         test = Test(test_no)
00248         if options.automated is not None:    test.automated = options.automated
00249         if options.dependencies is not None: test.dependencies = options.dependencies
00250         if options.host_test is not None:    test.host_test = options.host_test;
00251         if options.peripherals is not None:  test.peripherals = options.peripherals;
00252         if options.duration is not None:     test.duration = options.duration;
00253         if options.extra is not None:        test.extra_files = options.extra
00254 
00255         if not test.is_supported(mcu, toolchain):
00256             print 'The selected test is not supported on target %s with toolchain %s' % (mcu, toolchain)
00257             sys.exit()
00258 
00259         # Linking with extra libraries
00260         if options.rtos:     test.dependencies.append(RTOS_LIBRARIES)
00261         if options.rpc:      test.dependencies.append(RPC_LIBRARY)
00262         if options.eth:      test.dependencies.append(ETH_LIBRARY)
00263         if options.usb_host: test.dependencies.append(USB_HOST_LIBRARIES)
00264         if options.usb:      test.dependencies.append(USB_LIBRARIES)
00265         if options.dsp:      test.dependencies.append(DSP_LIBRARIES)
00266         if options.fat:      test.dependencies.append(FS_LIBRARY)
00267         if options.ublox:    test.dependencies.append(UBLOX_LIBRARY)
00268         if options.testlib:  test.dependencies.append(TEST_MBED_LIB)
00269 
00270         build_dir = join(BUILD_DIR, "test", mcu, toolchain, test.id)
00271         if options.source_dir is not None:
00272             test.source_dir = options.source_dir
00273             build_dir = options.source_dir
00274 
00275         if options.build_dir is not None:
00276             build_dir = options.build_dir
00277 
00278         try:
00279             bin_file = build_project(test.source_dir, build_dir, mcu, toolchain,
00280                                      test.dependencies,
00281                                      linker_script=options.linker_script,
00282                                      clean=options.clean,
00283                                      verbose=options.verbose,
00284                                      notify=notify,
00285                                      silent=options.silent,
00286                                      macros=options.macros,
00287                                      jobs=options.jobs,
00288                                      name=options.artifact_name,
00289                                      app_config=options.app_config,
00290                                      inc_dirs=[dirname(MBED_LIBRARIES)],
00291                                      build_profile=extract_profile(parser,
00292                                                                    options,
00293                                                                    toolchain))
00294             print 'Image: %s'% bin_file
00295 
00296             if options.disk:
00297                 # Simple copy to the mbed disk
00298                 copy(bin_file, options.disk)
00299 
00300             if options.serial:
00301                 # Import pyserial: https://pypi.python.org/pypi/pyserial
00302                 from serial import Serial
00303 
00304                 sleep(TARGET_MAP[mcu].program_cycle_s)
00305 
00306                 serial = Serial(options.serial, timeout = 1)
00307                 if options.baud:
00308                     serial.setBaudrate(options.baud)
00309 
00310                 serial.flushInput()
00311                 serial.flushOutput()
00312 
00313                 try:
00314                     serial.sendBreak()
00315                 except:
00316                     # In linux a termios.error is raised in sendBreak and in setBreak.
00317                     # The following setBreak() is needed to release the reset signal on the target mcu.
00318                     try:
00319                         serial.setBreak(False)
00320                     except:
00321                         pass
00322 
00323                 while True:
00324                     c = serial.read(512)
00325                     sys.stdout.write(c)
00326                     sys.stdout.flush()
00327 
00328         except KeyboardInterrupt, e:
00329             print "\n[CTRL+c] exit"
00330         except NotSupportedException, e:
00331             print "\nNot supported for selected target"
00332         except Exception,e:
00333             if options.verbose:
00334                 import traceback
00335                 traceback.print_exc(file=sys.stdout)
00336             else:
00337                 print "[ERROR] %s" % str(e)
00338             
00339             sys.exit(1)