feat(tool): Add tools to test asm and ir
This commit is contained in:
+16
-3
@@ -84,6 +84,11 @@ def parse_args() -> argparse.Namespace:
|
||||
action="store_true",
|
||||
help="do not compile tests/std.c with the assembly file",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--stdin-file",
|
||||
type=Path,
|
||||
help="file used as stdin when running the compiled program",
|
||||
)
|
||||
args = parser.parse_args(tool_args)
|
||||
args.program_args = program_args
|
||||
return args
|
||||
@@ -95,9 +100,13 @@ def require_command(command: str) -> None:
|
||||
sys.exit(127)
|
||||
|
||||
|
||||
def run_command(command: list[str]) -> subprocess.CompletedProcess[bytes]:
|
||||
def run_command(
|
||||
command: list[str],
|
||||
*,
|
||||
stdin: bytes | None = None,
|
||||
) -> subprocess.CompletedProcess[bytes]:
|
||||
try:
|
||||
return subprocess.run(command, capture_output=True, check=False)
|
||||
return subprocess.run(command, input=stdin, capture_output=True, check=False)
|
||||
except OSError as err:
|
||||
print(f"error: failed to run {command[0]}: {err}", file=sys.stderr)
|
||||
sys.exit(127)
|
||||
@@ -125,6 +134,9 @@ def main() -> int:
|
||||
if not args.no_std_c and not std_c.is_file():
|
||||
print(f"error: std C file not found: {std_c}", file=sys.stderr)
|
||||
return 2
|
||||
if args.stdin_file is not None and not args.stdin_file.is_file():
|
||||
print(f"error: stdin file not found: {args.stdin_file}", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
require_command(args.cc)
|
||||
require_command(args.qemu)
|
||||
@@ -164,7 +176,8 @@ def main() -> int:
|
||||
print("gdb: use 'si' or 'c'; do not use 'run'", file=sys.stderr, flush=True)
|
||||
qemu_cmd.extend([str(output), *args.program_args])
|
||||
|
||||
run_result = run_command(qemu_cmd)
|
||||
stdin = args.stdin_file.read_bytes() if args.stdin_file is not None else None
|
||||
run_result = run_command(qemu_cmd, stdin=stdin)
|
||||
sys.stdout.buffer.write(run_result.stdout)
|
||||
sys.stderr.buffer.write(run_result.stderr)
|
||||
if run_result.stdout and not run_result.stdout.endswith(b"\n"):
|
||||
|
||||
Reference in New Issue
Block a user