xref: /OK3568_Linux_fs/yocto/poky/scripts/lib/devtool/runqemu.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# Development tool - runqemu command plugin
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# Copyright (C) 2015 Intel Corporation
4*4882a593Smuzhiyun#
5*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only
6*4882a593Smuzhiyun#
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun"""Devtool runqemu plugin"""
9*4882a593Smuzhiyun
10*4882a593Smuzhiyunimport os
11*4882a593Smuzhiyunimport bb
12*4882a593Smuzhiyunimport logging
13*4882a593Smuzhiyunimport argparse
14*4882a593Smuzhiyunimport glob
15*4882a593Smuzhiyunfrom devtool import exec_build_env_command, setup_tinfoil, DevtoolError
16*4882a593Smuzhiyun
17*4882a593Smuzhiyunlogger = logging.getLogger('devtool')
18*4882a593Smuzhiyun
19*4882a593Smuzhiyundef runqemu(args, config, basepath, workspace):
20*4882a593Smuzhiyun    """Entry point for the devtool 'runqemu' subcommand"""
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun    tinfoil = setup_tinfoil(config_only=True, basepath=basepath)
23*4882a593Smuzhiyun    try:
24*4882a593Smuzhiyun        machine = tinfoil.config_data.getVar('MACHINE')
25*4882a593Smuzhiyun        bindir_native = os.path.join(tinfoil.config_data.getVar('STAGING_DIR'),
26*4882a593Smuzhiyun                                     tinfoil.config_data.getVar('BUILD_ARCH'),
27*4882a593Smuzhiyun                                     tinfoil.config_data.getVar('bindir_native').lstrip(os.path.sep))
28*4882a593Smuzhiyun    finally:
29*4882a593Smuzhiyun        tinfoil.shutdown()
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun    if not glob.glob(os.path.join(bindir_native, 'qemu-system-*')):
32*4882a593Smuzhiyun        raise DevtoolError('QEMU is not available within this SDK')
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun    imagename = args.imagename
35*4882a593Smuzhiyun    if not imagename:
36*4882a593Smuzhiyun        sdk_targets = config.get('SDK', 'sdk_targets', '').split()
37*4882a593Smuzhiyun        if sdk_targets:
38*4882a593Smuzhiyun            imagename = sdk_targets[0]
39*4882a593Smuzhiyun    if not imagename:
40*4882a593Smuzhiyun        raise DevtoolError('Unable to determine image name to run, please specify one')
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun    try:
43*4882a593Smuzhiyun        # FIXME runqemu assumes that if OECORE_NATIVE_SYSROOT is set then it shouldn't
44*4882a593Smuzhiyun        # run bitbake to find out the values of various environment variables, which
45*4882a593Smuzhiyun        # isn't the case for the extensible SDK. Work around it for now.
46*4882a593Smuzhiyun        newenv = dict(os.environ)
47*4882a593Smuzhiyun        newenv.pop('OECORE_NATIVE_SYSROOT', '')
48*4882a593Smuzhiyun        exec_build_env_command(config.init_path, basepath, 'runqemu %s %s %s' % (machine, imagename, " ".join(args.args)), watch=True, env=newenv)
49*4882a593Smuzhiyun    except bb.process.ExecutionError as e:
50*4882a593Smuzhiyun        # We've already seen the output since watch=True, so just ensure we return something to the user
51*4882a593Smuzhiyun        return e.exitcode
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun    return 0
54*4882a593Smuzhiyun
55*4882a593Smuzhiyundef register_commands(subparsers, context):
56*4882a593Smuzhiyun    """Register devtool subcommands from this plugin"""
57*4882a593Smuzhiyun    if context.fixed_setup:
58*4882a593Smuzhiyun        parser_runqemu = subparsers.add_parser('runqemu', help='Run QEMU on the specified image',
59*4882a593Smuzhiyun                                               description='Runs QEMU to boot the specified image',
60*4882a593Smuzhiyun                                               group='testbuild', order=-20)
61*4882a593Smuzhiyun        parser_runqemu.add_argument('imagename', help='Name of built image to boot within QEMU', nargs='?')
62*4882a593Smuzhiyun        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*4882a593Smuzhiyun                                    nargs=argparse.REMAINDER)
64*4882a593Smuzhiyun        parser_runqemu.set_defaults(func=runqemu)
65