| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 1 | # Development tool - runqemu command plugin | 
|  | 2 | # | 
|  | 3 | # Copyright (C) 2015 Intel Corporation | 
|  | 4 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 6 | # | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 7 |  | 
|  | 8 | """Devtool runqemu plugin""" | 
|  | 9 |  | 
|  | 10 | import os | 
|  | 11 | import bb | 
|  | 12 | import logging | 
|  | 13 | import argparse | 
|  | 14 | import glob | 
|  | 15 | from devtool import exec_build_env_command, setup_tinfoil, DevtoolError | 
|  | 16 |  | 
|  | 17 | logger = logging.getLogger('devtool') | 
|  | 18 |  | 
|  | 19 | def runqemu(args, config, basepath, workspace): | 
|  | 20 | """Entry point for the devtool 'runqemu' subcommand""" | 
|  | 21 |  | 
|  | 22 | tinfoil = setup_tinfoil(config_only=True, basepath=basepath) | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 23 | try: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | machine = tinfoil.config_data.getVar('MACHINE') | 
|  | 25 | bindir_native = os.path.join(tinfoil.config_data.getVar('STAGING_DIR'), | 
|  | 26 | tinfoil.config_data.getVar('BUILD_ARCH'), | 
|  | 27 | tinfoil.config_data.getVar('bindir_native').lstrip(os.path.sep)) | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | finally: | 
|  | 29 | tinfoil.shutdown() | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 30 |  | 
|  | 31 | if not glob.glob(os.path.join(bindir_native, 'qemu-system-*')): | 
|  | 32 | raise DevtoolError('QEMU is not available within this SDK') | 
|  | 33 |  | 
|  | 34 | imagename = args.imagename | 
|  | 35 | if not imagename: | 
|  | 36 | sdk_targets = config.get('SDK', 'sdk_targets', '').split() | 
|  | 37 | if sdk_targets: | 
|  | 38 | imagename = sdk_targets[0] | 
|  | 39 | if not imagename: | 
|  | 40 | raise DevtoolError('Unable to determine image name to run, please specify one') | 
|  | 41 |  | 
|  | 42 | try: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 43 | # FIXME runqemu assumes that if OECORE_NATIVE_SYSROOT is set then it shouldn't | 
|  | 44 | # run bitbake to find out the values of various environment variables, which | 
|  | 45 | # isn't the case for the extensible SDK. Work around it for now. | 
|  | 46 | newenv = dict(os.environ) | 
|  | 47 | newenv.pop('OECORE_NATIVE_SYSROOT', '') | 
|  | 48 | exec_build_env_command(config.init_path, basepath, 'runqemu %s %s %s' % (machine, imagename, " ".join(args.args)), watch=True, env=newenv) | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 49 | except bb.process.ExecutionError as e: | 
|  | 50 | # We've already seen the output since watch=True, so just ensure we return something to the user | 
|  | 51 | return e.exitcode | 
|  | 52 |  | 
|  | 53 | return 0 | 
|  | 54 |  | 
|  | 55 | def register_commands(subparsers, context): | 
|  | 56 | """Register devtool subcommands from this plugin""" | 
|  | 57 | if context.fixed_setup: | 
|  | 58 | parser_runqemu = subparsers.add_parser('runqemu', help='Run QEMU on the specified image', | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 59 | description='Runs QEMU to boot the specified image', | 
|  | 60 | group='testbuild', order=-20) | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 61 | parser_runqemu.add_argument('imagename', help='Name of built image to boot within QEMU', nargs='?') | 
|  | 62 | parser_runqemu.add_argument('args', help='Any remaining arguments are passed to the runqemu script (pass --help after imagename to see what these are)', | 
|  | 63 | nargs=argparse.REMAINDER) | 
|  | 64 | parser_runqemu.set_defaults(func=runqemu) |