Implement basic build script in python

This commit is contained in:
Gvidas Juknevičius 2025-11-20 22:28:50 +02:00
parent 6fd5770acc
commit 8733755888
Signed by: MCorange
GPG Key ID: 5BE6B533CB76FE86

57
x.py
View File

@ -13,6 +13,10 @@ TS_VRB = 2
CWD = os.path.dirname(os.path.abspath(__file__)) CWD = os.path.dirname(os.path.abspath(__file__))
BUILD_D = f"{CWD}/build" BUILD_D = f"{CWD}/build"
INITRAMFS_DIR = f"{BUILD_D}/linux/initramfs"
INITRAMFS_FILE_TMP = f"{BUILD_D}/linux/initramfs.cpio"
INITRAMFS_FILE = f"{BUILD_D}/linux/initramfs.cpio.gz"
KERNEL_FILE = F"{BUILD_D}/linux/KernelImage"
def shift(arr): def shift(arr):
@ -25,10 +29,10 @@ def timestamp(dt=datetime.now(), fmt="hours"):
return f"{dt.hour:0>2}:{dt.minute:0>2}:{dt.second:0>2}" return f"{dt.hour:0>2}:{dt.minute:0>2}:{dt.second:0>2}"
def run(*cmds, parallel=False, tag=None, interactive=False): def run(*cmds, parallel=False, tag=None, interactive=False, chdir=None):
threads = [] threads = []
def run_single(cmd, tag): def run_single(cmd, tag, chdir):
env = os.environ.copy() env = os.environ.copy()
env["MAKE_TERMOUT"] = "true" env["MAKE_TERMOUT"] = "true"
env["MAKE_TERMERR"] = "true" env["MAKE_TERMERR"] = "true"
@ -38,6 +42,7 @@ def run(*cmds, parallel=False, tag=None, interactive=False):
cmd, shell=True, cmd, shell=True,
stdout=None if interactive else subprocess.PIPE, stdout=None if interactive else subprocess.PIPE,
stderr=None if interactive else subprocess.STDOUT, stderr=None if interactive else subprocess.STDOUT,
cwd=chdir if chdir else None,
# text=None if interactive else True, # text=None if interactive else True,
) as proc: ) as proc:
if not interactive: if not interactive:
@ -62,11 +67,11 @@ def run(*cmds, parallel=False, tag=None, interactive=False):
f"{tag}-{i}" if tag else None) f"{tag}-{i}" if tag else None)
if parallel: if parallel:
t = threading.Thread( t = threading.Thread(
target=run_single, args=(command, current_tag)) target=run_single, args=(command, current_tag, chdir))
t.start() t.start()
threads.append(t) threads.append(t)
else: else:
run_single(command, current_tag) run_single(command, current_tag, chdir)
for t in threads: for t in threads:
t.join() t.join()
@ -112,6 +117,7 @@ def cmd_build(args):
print(" linux - Everything bellow") print(" linux - Everything bellow")
print(" kernel - The linux kernel") print(" kernel - The linux kernel")
print(" busybox - Busybox") print(" busybox - Busybox")
print(" initramfs - Busybox")
print(" rust - Builds all of the rust components") print(" rust - Builds all of the rust components")
print(" img - Rebuilds the image") print(" img - Rebuilds the image")
@ -125,6 +131,8 @@ def cmd_build(args):
def build_kernel(args): def build_kernel(args):
run(f"make -C {CWD}/linux/kernel O={BUILD_D}/linux/kernel KCONFIG_CONFIG='{ run(f"make -C {CWD}/linux/kernel O={BUILD_D}/linux/kernel KCONFIG_CONFIG='{
CWD}/linux/.kernel-config' -j{MAX_JOBS}", tag="kernel") CWD}/linux/.kernel-config' -j{MAX_JOBS}", tag="kernel")
run(f"cp {
BUILD_D}/linux/kernel/arch/x86_64/boot/bzImage {KERNEL_IMAGE}")
def build_busybox(args): def build_busybox(args):
run( run(
@ -138,6 +146,38 @@ def cmd_build(args):
def build_rust(args): def build_rust(args):
todo("build_rust") todo("build_rust")
def build_initramfs(args):
run(f"rm -rf {INITRAMFS_DIR}")
run(f"mkdir -p {INITRAMFS_DIR}/{{etc,mnt,proc,run,srv,tmp,var,boot,dev,home,media,opt,root,sbin,sys,usr/bin,usr/lib}}")
run("ln -s usr/bin bin",
"ln -s usr/lib lib",
chdir=INITRAMFS_DIR)
init_script =\
"#!/bin/sh\\n" +\
"mount -t proc none /proc\\n" +\
"mount -t sysfs none /sys\\n" +\
"echo 'Successfully booted'\\n" +\
"exec /bin/sh\\n"
run(f"echo {init_script} > {INITRAMFS_DIR}/init")
# run(
# f"chmod +x {INITRAMFS_DIR}/init",
# f"chmod +x {INITRAMFS_DIR}/sbin/init"
# )
run(
f"make -C {CWD}/linux/busybox O={BUILD_D}/linux/busybox KCONFIG_CONFIG='{
CWD}/linux/.busybox-config' -j{MAX_JOBS} CONFIG_PREFIX={INITRAMFS_DIR} install",
)
run(
f"find {INITRAMFS_DIR} | cpio -o -H newc > {INITRAMFS_FILE_TMP}",
f"gzip -f {INITRAMFS_FILE_TMP}"
)
def build_image(args): def build_image(args):
# for the later bootable.img or whatever file # for the later bootable.img or whatever file
todo("build_image") todo("build_image")
@ -169,16 +209,22 @@ def cmd_build(args):
elif cmd == "img": elif cmd == "img":
build_init() build_init()
build_image(args) build_image(args)
elif cmd == "initramfs":
build_initramfs(args)
else: else:
print(f"ERROR: Unknown component '{cmd}'") print(f"ERROR: Unknown component '{cmd}'")
cmd_help() cmd_help()
exit(1) exit(1)
def cmd_qemu(args):
run(f"qemu-system-x86_64 -kernel {KERNEL_FILE} -initrd {INITRAMFS_FILE} -append \"rdinit=/usr/sbin/init console=ttyS0\" -serial mon:stdio")
AVAILABLE_SUBCOMMANDS = { AVAILABLE_SUBCOMMANDS = {
"help": help, "help": help,
"configure": cmd_configure, "configure": cmd_configure,
"build": cmd_build "build": cmd_build,
"qemu": cmd_qemu,
} }
@ -188,6 +234,7 @@ def help():
print(" help - Show this help") print(" help - Show this help")
print(" configure - Configure components") print(" configure - Configure components")
print(" build - Compile/build components") print(" build - Compile/build components")
print(" qemu - Run a vm with the current build")
if __name__ == "__main__": if __name__ == "__main__":