bussy/scripts/build.py
fef 2bcfd294be
add custom post-build script
Rust doesn't support those natively.
2023-05-28 19:45:26 +02:00

53 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python3
# DISCLAIMER: I don't know python. And i don't like python.
# This is only written in python because pretty much everyone already has it
# installed and rust doesn't support post-build scripts for some reason.
import sys
from utils import crc32, run_sync
if len(sys.argv) >= 2:
PROFILE = sys.argv[1]
else:
PROFILE = "debug"
def finalize_stage1(filename: str):
run_sync(
"objcopy", "-O", "binary",
"target/x86-pc-none/" + PROFILE + "/bussy-stage1",
filename
)
with open(filename, "r+b") as file:
data = bytearray(file.read())
size = len(data)
if size > 0x8000:
print("stage1 is too big (greater than 32 K)")
exit(1)
data[2] = size & 0xff
data[3] = size >> 8
csum = crc32(data)
print("CRC32 checksum of stage1 is " + hex(csum))
data[4] = csum & 0xff
data[5] = (csum >> 8) & 0xff
data[6] = (csum >> 16) & 0xff
data[7] = csum >> 24
file.seek(2)
file.write(data[2:8])
print("Running cargo")
if PROFILE == "debug":
run_sync("cargo", "build", "--workspace")
elif PROFILE == "release":
run_sync("cargo", "build", "--workspace", "--release")
else:
run_sync("cargo", "build", "--workspace", "--profile", PROFILE)
print("Finalizing stage1")
finalize_stage1("target/x86-pc-none/" + PROFILE + "/stage1.bin")