Files
ViperOS/build.py
T

143 lines
4.7 KiB
Python

import shutil
import os
import sys
import subprocess
import threading
# 解析参数
clean = '--noclean' not in sys.argv
kernel_only = '--kernel-only' in sys.argv
noapp = '--noapp' in sys.argv
# 编译内核
cmd = r'..\TransPyV\output\TransPyV.exe --project ./VKernel/project.vpj --clear-cache'
if clean:
cmd += ' --clean'
os.system(cmd)
shutil.copy("VKernel/.tpv_build/output/kernel.bin", "./disk/sys/kernel/x86_64/kernel.bin")
# --noapp 优先级高于 --kernel-only:只编译内核,不编译所有库和应用
if noapp:
print("[build] --noapp: 跳过所有库与应用编译,复用已有缓存文件")
# 检查必要文件是否存在
required_libs = [
("Libs/SerialLogger/.tpv_build/output/slog.so", "./disk/libs/slog.so"),
("Libs/VPUI64/.tpv_build/output/vpui64.so", "./disk/libs/vpui64.so"),
]
required_apps = [
("Apps/HelloWorld/.tpv_build/output/helloworld.elf", "./disk/apps/hello.elf"),
("Apps/Terminal/.tpv_build/output/terminal.elf", "./disk/apps/terminal.elf"),
("Apps/Gargantua/.tpv_build/output/gargantua.elf", "./disk/apps/gargantua.elf"),
("Apps/Scene3D/.tpv_build/output/scene3d.elf", "./disk/apps/scene3d.elf"),
]
os.makedirs("./disk/apps", exist_ok=True)
os.makedirs("./disk/libs", exist_ok=True)
for src, dst in required_libs + required_apps:
if os.path.exists(src):
shutil.copy(src, dst)
else:
print(f"[build] 警告: {src} 不存在,请先完整构建一次")
elif kernel_only:
# 仅 --kernel-only:只跳过应用,不跳过库(原有逻辑保留)
print("[build] --kernel-only: 跳过其他应用和库的编译")
if not os.path.exists("./disk/apps/hello.elf"):
print("[build] 警告: hello.elf 不存在,请先不带 --kernel-only 完整构建一次")
else:
# 完整编译所有库+应用
so_cmd = r'..\TransPyV\output\TransPyV.exe --project Libs/SerialLogger/project.vpj --clear-cache'
if clean:
so_cmd += ' --clean'
os.system(so_cmd)
vpui_cmd = r'..\TransPyV\output\TransPyV.exe --project Libs/VPUI64/project.vpj --clear-cache'
if clean:
vpui_cmd += ' --clean'
os.system(vpui_cmd)
app_cmd = r'..\TransPyV\output\TransPyV.exe --project Apps/HelloWorld/project.vpj --clear-cache'
if clean:
app_cmd += ' --clean'
os.system(app_cmd)
os.makedirs("./disk/apps", exist_ok=True)
shutil.copy("Apps/HelloWorld/.tpv_build/output/helloworld.elf", "./disk/apps/hello.elf")
term_cmd = r'..\TransPyV\output\TransPyV.exe --project Apps/Terminal/project.vpj --clear-cache'
if clean:
term_cmd += ' --clean'
os.system(term_cmd)
shutil.copy("Apps/Terminal/.tpv_build/output/terminal.elf", "./disk/apps/terminal.elf")
garg_cmd = r'..\TransPyV\output\TransPyV.exe --project Apps/Gargantua/project.vpj --clear-cache'
if clean:
garg_cmd += ' --clean'
os.system(garg_cmd)
shutil.copy("Apps/Gargantua/.tpv_build/output/gargantua.elf", "./disk/apps/gargantua.elf")
scene3d_cmd = r'..\TransPyV\output\TransPyV.exe --project Apps/Scene3D/project.vpj --clear-cache'
if clean:
scene3d_cmd += ' --clean'
os.system(scene3d_cmd)
shutil.copy("Apps/Scene3D/.tpv_build/output/scene3d.elf", "./disk/apps/scene3d.elf")
os.makedirs("./disk/libs", exist_ok=True)
so_path = os.path.abspath("Libs/SerialLogger/.tpv_build/output/slog.so")
vpui_path = os.path.abspath("Libs/VPUI64/.tpv_build/output/vpui64.so")
if os.path.exists(so_path):
shutil.copy(so_path, "./disk/libs/slog.so")
if os.path.exists(vpui_path):
shutil.copy(vpui_path, "./disk/libs/vpui64.so")
os.system('powershell -ExecutionPolicy Bypass -File ./Scripts/disk.ps1')
serial_log = os.path.abspath("serial.log")
if os.path.exists(serial_log):
os.remove(serial_log)
print(f"[build] CWD: {os.getcwd()}")
print(f"[build] serial_log: {serial_log}")
qemu_cmd = [
'qemu-system-x86_64',
'-pflash', r'D:\msys64\mingw64\share\qemu\edk2-x86_64-code.fd',
'-drive', 'file=disk.img,format=raw,if=ide',
'-m', '256m',
'-vga', 'std',
'-usb',
'-device', 'usb-kbd',
'-device', 'usb-mouse',
'-accel', 'tcg,tb-size=128',
'-serial', f'file:{serial_log}',
'-no-reboot'
]
print(f"[build] QEMU cmd: {' '.join(qemu_cmd)}")
proc = subprocess.Popen(qemu_cmd)
proc.wait()
print(f"[build] serial_log path: {serial_log}")
print(f"[build] file exists: {os.path.exists(serial_log)}")
if os.path.exists(serial_log):
with open(serial_log, "r", encoding="utf-8", errors="replace") as f:
content = f.read()
print(f"[build] serial_log size: {len(content)} bytes")
print(content)
else:
print("[build] serial.log not found!")