CoolPotOS/build.py

165 lines
5.1 KiB
Python
Raw Normal View History

2024-04-09 23:46:56 +08:00
import os
2024-05-03 20:32:10 +08:00
import sys
2024-04-09 23:46:56 +08:00
2024-05-21 21:09:04 +08:00
def check_os():
if sys.platform.startswith('darwin'):
return "Mac OS X"
elif sys.platform.startswith('win'):
return "Windows"
elif sys.platform.startswith('linux'):
return "Linux"
else:
return "Unknown"
if check_os() == "Windows":
gcc = '/i686_elf_tools/bin/i686-elf-gcc.exe -w -std=gnu99 -I include/ -std=gnu99 -ffreestanding -O2 -c -Wincompatible-pointer-types'
asm = '/i686_elf_tools/bin/i686-elf-as.exe'
nasm = "nasm -f elf32"
ld = '/i686_elf_tools/bin/i686-elf-g++.exe'
cd = os.getcwd() # 获取当前执行目录 'D:\CrashPowerDOS-main\'
out = "target"
dir_ = "\\"
elif check_os() == "Linux":
gcc = 'gcc.exe -w -std=gnu99 -I include/ -std=gnu99 -ffreestanding -O0 -c -Wincompatible-pointer-types'
asm = 'gcc.exe'
nasm = "nasm -f elf32"
ld = 'g++.exe'
cd = os.getcwd() # 获取当前执行目录 '\mnt\d\CrashPowerDOS-main\'
out = "target"
dir_ = "/"
2024-05-03 20:32:10 +08:00
2024-04-09 23:46:56 +08:00
def clean():
2024-05-02 11:06:52 +08:00
print("Clean target folder")
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + "target"): # 遍历指定文件夹下所有文件
os.remove(cd + dir_ + "target" + dir_ + file)
2024-04-09 23:46:56 +08:00
return 0
2024-05-03 20:32:10 +08:00
2024-04-09 23:46:56 +08:00
def build_boot(): # 构建引导程序
print("Building boot source code...")
status = True
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + 'boot'):
2024-04-09 23:46:56 +08:00
if status and file == 'boot.asm':
2024-05-21 21:09:04 +08:00
cmd = cd + asm + " " + cd + dir_ + "boot" + dir_ + file + " -o " + cd + dir_ + "target" + dir_ + \
file.split(".")[0] + ".o"
2024-04-09 23:46:56 +08:00
status = False
else:
2024-05-21 21:09:04 +08:00
cmd = nasm + " " + cd + dir_ + "boot" + dir_ + file + " -o " + cd + dir_ + "target" + dir_ + \
file.split(".")[0] + ".o"
2024-04-09 23:46:56 +08:00
e = os.system(cmd) # os.system 执行命令 e为返回值(非0即不正常退出,可做判断终止构建流程)
if e != 0:
return -1
return 0
def build_driver(): # 构建内置驱动程序
print("Building driver source code...")
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + 'driver'):
cmd = cd + gcc + " " + "driver" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
2024-04-09 23:46:56 +08:00
e = os.system(cmd)
if e != 0:
return -1
return 0
def build_kernel(): # 构建内核本体
print("Building kernel source code...")
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + 'kernel'):
cmd = cd + gcc + " " + "kernel" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
2024-04-09 23:46:56 +08:00
e = os.system(cmd)
if e != 0:
return -1
return 0
def build_data(): # 构建数据结构实现
2024-04-11 23:32:28 +08:00
print("Building util source code...")
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + 'util'):
cmd = cd + gcc + " " + "util" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
2024-04-09 23:46:56 +08:00
e = os.system(cmd)
if e != 0:
return -1
return 0
def build_sysapp(): # 构建内置系统应用
print("Building sysapp source code...")
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + 'sysapp'):
cmd = cd + gcc + " " + "sysapp" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
2024-04-09 23:46:56 +08:00
e = os.system(cmd)
if e != 0:
return -1
return 0
2024-05-12 00:17:47 +08:00
def build_network(): # 构建网络系统
print("Building network source code...")
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + 'network'):
cmd = cd + gcc + " " + "network" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
2024-05-12 00:17:47 +08:00
e = os.system(cmd)
if e != 0:
return -1
return 0
2024-05-21 21:09:04 +08:00
2024-05-12 14:34:03 +08:00
def build_fs(): # 构建文件系统
print("Building fs source code...")
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + 'fs'):
cmd = cd + gcc + " " + "fs" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
2024-05-12 14:34:03 +08:00
e = os.system(cmd)
if e != 0:
return -1
return 0
2024-05-21 21:09:04 +08:00
2024-04-09 23:46:56 +08:00
def linker(): # 交叉编译链接
print("Linking object files...")
source_file = ""
2024-05-21 21:09:04 +08:00
for file in os.listdir(cd + dir_ + 'target'):
source_file = source_file + " target" + dir_ + file
2024-04-09 23:46:56 +08:00
return os.system(
2024-05-21 21:09:04 +08:00
cd + "/i686_elf_tools/bin/i686-elf-g++.exe -T linker.ld -o isodir" + dir_ + "sys" + dir_ + "kernel.elf -ffreestanding -O2 -nostdlib " + source_file + " -lgcc")
2024-04-09 23:46:56 +08:00
2024-05-21 21:09:04 +08:00
def launch():
if check_os() == "Windows":
if len(sys.argv) == 0 or sys.argv[1] == 'vga':
print("Graphics MODE [VGA]")
os.system("qemu-system-i386 -net nic,model=pcnet -net user -kernel isodir\\sys\\kernel.elf -hda diskx.img")
elif sys.argv[1] == 'vbe':
print("Graphics MODE [VBE]")
os.system("qemu-system-i386 -vga std -net nic,model=pcnet -net user -kernel isodir\\sys\\kernel.elf -hda diskx.img")
elif check_os() == "Linux":
os.system("grub-mkrescue -o cpos.iso isodir")
os.system("qemu-system-i386 -vga std -cdrom cpos.iso")
2024-04-09 23:46:56 +08:00
clean()
a = build_boot()
if a != 0:
exit(-1)
a = build_driver()
if a != 0:
exit(-1)
a = build_kernel()
if a != 0:
exit(-1)
a = build_data()
if a != 0:
exit(-1)
a = build_sysapp()
2024-05-12 00:17:47 +08:00
if a != 0:
exit(-1)
a = build_network()
2024-05-12 14:34:03 +08:00
if a != 0:
exit(-1)
a = build_fs()
2024-04-09 23:46:56 +08:00
if a != 0:
exit(-1)
a = linker()
if a != 0:
exit(-1)
print("Launching i386 vm...")