2 # ===----------------------------------------------------------------------===##
4 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 # See https://llvm.org/LICENSE.txt for license information.
6 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 # ===----------------------------------------------------------------------===##
10 """qemu_baremetal.py is a utility for running a program with QEMU's system mode.
12 It is able to pass command line arguments to the program and forward input and
13 output (if the underlying baremetal enviroment supports QEMU semihosting).
22 parser
= argparse
.ArgumentParser()
23 parser
.add_argument("--qemu", type=str, required
=True)
24 parser
.add_argument("--cpu", type=str, required
=False)
25 parser
.add_argument("--machine", type=str, default
="virt")
27 "--qemu-arg", dest
="qemu_args", type=str, action
="append", default
=[]
29 parser
.add_argument("--semihosting", action
="store_true", default
=True)
30 parser
.add_argument("--no-semihosting", dest
="semihosting", action
="store_false")
31 parser
.add_argument("--execdir", type=str, required
=True)
32 parser
.add_argument("test_binary")
33 parser
.add_argument("test_args", nargs
=argparse
.ZERO_OR_MORE
, default
=[])
34 args
= parser
.parse_args()
35 if not os
.path
.exists(args
.test_binary
):
36 sys
.exit(f
"Expected argument to be a test executable: '{args.test_binary}'")
40 "stdio,mux=on,id=stdio0",
46 f
"{args.machine},accel=tcg",
48 f
"loader,file={args.test_binary},cpu-num=0",
53 qemu_commandline
+= ["-cpu", args
.cpu
]
56 # Use QEMU's semihosting support to pass argv (supported by picolibc)
57 semihosting_config
= f
"enable=on,chardev=stdio0,arg={args.test_binary}"
58 for arg
in args
.test_args
:
59 semihosting_config
+= f
",arg={arg}"
60 qemu_commandline
+= ["-semihosting-config", semihosting_config
]
63 "Got non-empty test arguments but do no know how to pass them to "
64 "QEMU without semihosting support"
66 os
.execvp(qemu_commandline
[0], qemu_commandline
)
69 if __name__
== "__main__":