[flang][runtime] Make defined formatted I/O process format elementally (#74150)
[llvm-project.git] / libcxx / utils / qemu_baremetal.py
blobaaf5b84489066d797c6bcabd9bb5693fa0dd7f38
1 #!/usr/bin/env python3
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).
14 """
16 import argparse
17 import os
18 import sys
21 def main():
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")
26 parser.add_argument(
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}'")
37 qemu_commandline = [
38 args.qemu,
39 "-chardev",
40 "stdio,mux=on,id=stdio0",
41 "-monitor",
42 "none",
43 "-serial",
44 "none",
45 "-machine",
46 f"{args.machine},accel=tcg",
47 "-device",
48 f"loader,file={args.test_binary},cpu-num=0",
49 "-nographic",
50 *args.qemu_args,
52 if args.cpu:
53 qemu_commandline += ["-cpu", args.cpu]
55 if args.semihosting:
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]
61 elif args.test_args:
62 sys.exit(
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__":
70 exit(main())