xref: /OK3568_Linux_fs/yocto/poky/scripts/lib/devtool/runqemu.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1# Development tool - runqemu command plugin
2#
3# Copyright (C) 2015 Intel Corporation
4#
5# SPDX-License-Identifier: GPL-2.0-only
6#
7
8"""Devtool runqemu plugin"""
9
10import os
11import bb
12import logging
13import argparse
14import glob
15from devtool import exec_build_env_command, setup_tinfoil, DevtoolError
16
17logger = logging.getLogger('devtool')
18
19def runqemu(args, config, basepath, workspace):
20    """Entry point for the devtool 'runqemu' subcommand"""
21
22    tinfoil = setup_tinfoil(config_only=True, basepath=basepath)
23    try:
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))
28    finally:
29        tinfoil.shutdown()
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:
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)
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
55def 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',
59                                               description='Runs QEMU to boot the specified image',
60                                               group='testbuild', order=-20)
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)
65