[libcxx-commits] [libcxx] [runtimes] Add a qemu-system executor script (PR #68643)

Alexander Richardson via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 9 11:30:30 PST 2023


https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/68643

>From ed214aba679677f81a41295717abd1761f22fcc3 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Mon, 9 Oct 2023 16:27:15 -0700
Subject: [PATCH 1/3] [runtimes] Add a qemu-system executor script

This is useful when trying to test libc++/libc++abi/libunwind against a
baremetal enviroment (e.g. picolibc).

See also https://reviews.llvm.org/D155521
---
 libcxx/utils/qemu_baremetal.py | 73 ++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100755 libcxx/utils/qemu_baremetal.py

diff --git a/libcxx/utils/qemu_baremetal.py b/libcxx/utils/qemu_baremetal.py
new file mode 100755
index 000000000000000..63139cb3638e2d3
--- /dev/null
+++ b/libcxx/utils/qemu_baremetal.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+# ===----------------------------------------------------------------------===##
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ===----------------------------------------------------------------------===##
+
+"""qemu_baremetal.py is a utility for running a program with QEMU's system mode.
+
+It is able to pass command line arguments to the program and forward input and
+output (if the underlying baremetal enviroment supports QEMU semihosting).
+"""
+
+import argparse
+import os
+import platform
+import subprocess
+import sys
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("--qemu", type=str, required=True)
+    parser.add_argument("--cpu", type=str, required=False)
+    parser.add_argument("--machine", type=str, default="virt")
+    parser.add_argument(
+        "--qemu-arg", dest="qemu_args", type=str, action="append", default=[]
+    )
+    parser.add_argument(
+        "--semihosting", type=argparse.BooleanOptionalAction, default=True
+    )
+    parser.add_argument("--execdir", type=str, required=True)
+    parser.add_argument("test_binary")
+    parser.add_argument("test_args", nargs=argparse.ZERO_OR_MORE, default=[])
+    args = parser.parse_args()
+    if not os.path.exists(args.test_binary):
+        sys.exit(f"Expected argument to be a test executable: '{args.test_binary}'")
+    qemu_commandline = [
+        args.qemu,
+        "-chardev",
+        "stdio,mux=on,id=stdio0",
+        "-monitor",
+        "none",
+        "-serial",
+        "none",
+        "-machine",
+        f"{args.machine},accel=tcg",
+        "-device",
+        f"loader,file={args.test_binary},cpu-num=0",
+        "-nographic",
+        *args.qemu_args,
+    ]
+    if args.cpu:
+        qemu_commandline += ["-cpu", args.cpu]
+
+    if args.semihosting:
+        # Use QEMU's semihosting support to pass argv (supported by picolibc)
+        semihosting_config = f"enable=on,chardev=stdio0,arg={args.test_binary}"
+        for arg in args.test_args:
+            semihosting_config += f",arg={arg}"
+        qemu_commandline += ["-semihosting-config", semihosting_config]
+    elif args.test_args:
+        sys.exit(
+            "Got non-empty test arguments but do no know how to pass them to "
+            "QEMU without semihosting support"
+        )
+    os.execvp(qemu_commandline[0], qemu_commandline)
+
+
+if __name__ == "__main__":
+    exit(main())

>From e22e675a1d10f792160f4aeb1a23c97468bbeb7e Mon Sep 17 00:00:00 2001
From: Alexander Richardson <mail at alexrichardson.me>
Date: Mon, 9 Oct 2023 21:45:07 -0700
Subject: [PATCH 2/3] Fix python interpreter

---
 libcxx/utils/qemu_baremetal.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/utils/qemu_baremetal.py b/libcxx/utils/qemu_baremetal.py
index 63139cb3638e2d3..9695ddb586ab8a4 100755
--- a/libcxx/utils/qemu_baremetal.py
+++ b/libcxx/utils/qemu_baremetal.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # ===----------------------------------------------------------------------===##
 #
 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.

>From 395798ff4341d882525b81cc7b5bce75a9ad03d0 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Thu, 9 Nov 2023 11:28:27 -0800
Subject: [PATCH 3/3] Avoid depending on python 3.8

---
 libcxx/utils/qemu_baremetal.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/utils/qemu_baremetal.py b/libcxx/utils/qemu_baremetal.py
index 9695ddb586ab8a4..1508059470d8299 100755
--- a/libcxx/utils/qemu_baremetal.py
+++ b/libcxx/utils/qemu_baremetal.py
@@ -28,8 +28,9 @@ def main():
     parser.add_argument(
         "--qemu-arg", dest="qemu_args", type=str, action="append", default=[]
     )
+    parser.add_argument("--semihosting", action="store_true", default=True)
     parser.add_argument(
-        "--semihosting", type=argparse.BooleanOptionalAction, default=True
+        "--no-semihosting", dest="semihosting", action="store_false", default=True
     )
     parser.add_argument("--execdir", type=str, required=True)
     parser.add_argument("test_binary")



More information about the libcxx-commits mailing list