CoolPotOS/build.py

155 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-09-01 14:59:32 +08:00
gcc = '/i686_elf_tools/bin/i686-elf-gcc.exe -w -std=gnu99 -I include/ -std=gnu99 -ffreestanding -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_ = "\\"
src = "src\\"
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-25 18:35:01 +08:00
for file in os.listdir(cd + dir_ + src + 'boot'):
2024-04-09 23:46:56 +08:00
if status and file == 'boot.asm':
2024-05-25 18:35:01 +08:00
cmd = cd + asm + " " + cd + dir_ + src + "boot" + dir_ + file + " -o " + cd + dir_ + "target" + dir_ + \
2024-05-21 21:09:04 +08:00
file.split(".")[0] + ".o"
2024-04-09 23:46:56 +08:00
status = False
else:
2024-05-25 18:35:01 +08:00
cmd = nasm + " " + cd + dir_ + src + "boot" + dir_ + file + " -o " + cd + dir_ + "target" + dir_ + \
2024-05-21 21:09:04 +08:00
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-25 18:35:01 +08:00
status_pci = True
status_ide = True
for file in os.listdir(cd + dir_ + src + 'driver'):
if status_pci and (file == 'pci.c'):
cmd = cd + gcc + "-O0 " + src + "driver" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
status_pci = False
elif status_ide and (file == 'ide.c'):
cmd = cd + gcc + "-O0 " + src + "driver" + dir_ + file + " -o " + "target" + dir_ + file.split(".")[0] + ".o"
status_ide = False
else:
cmd = cd + gcc + "-O0 " + src + "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-25 18:35:01 +08:00
for file in os.listdir(cd + dir_ + src + 'kernel'):
cmd = cd + gcc + "-O0 " + src + "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-25 18:35:01 +08:00
for file in os.listdir(cd + dir_ + src + 'util'):
2024-07-14 00:54:27 +08:00
cmd = cd + gcc + "-O0 " + src + "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-25 18:35:01 +08:00
for file in os.listdir(cd + dir_ + src + 'sysapp'):
2024-07-30 22:50:03 +08:00
cmd = cd + gcc + "-Og " + src + "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-25 18:35:01 +08:00
for file in os.listdir(cd + dir_ + src + 'network'):
cmd = cd + gcc + "-O0 " + src + "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-25 18:35:01 +08:00
for file in os.listdir(cd + dir_ + src + 'fs'):
cmd = cd + gcc + " " + src + "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-07-14 00:54:27 +08:00
cd + "/i686_elf_tools/bin/i686-elf-g++.exe -T linker.ld -o isodir" + dir_ + "sys" + dir_ + "kernel.elf -ffreestanding -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...")
2024-05-25 18:35:01 +08:00
# launch()