add custom post-build script

Rust doesn't support those natively.
main
anna 12 months ago
parent 43d454e9fe
commit 2bcfd294be
Signed by: fef
GPG Key ID: 2585C2DC6D79B485

@ -0,0 +1,53 @@
#!/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")

@ -0,0 +1,49 @@
# Miscellaneous utilities, meant to be included from build.py
import os
# Do a fork/execl because that way all children get the same stdout,
# i.e. if it's a tty the executed program should behave appropriately.
# Python probably has an idiomatic way to do this, but frankly idc.
def run_sync(program: str, *args):
path = os.popen("which " + program).read().strip()
child = os.fork()
if child == 0:
os.execl(path, program, *args)
print("execl() failed")
exit(1)
elif child == -1:
print("fork() failed")
exit(1)
status = os.waitpid(child, 0)
if status[1] != 0:
print(program + " exited with status code " + str(status[1]))
exit(1)
def generate_crc_tab(poly_reverse: int):
tab = []
for crc in range(256):
for _ in range(8):
lsb = crc % 2
crc >>= 1
if lsb == 1:
crc ^= poly_reverse
tab.append(crc)
return tab
def do_crc(data, table):
crc = 0xffffffff
for byte in data:
crc = (crc >> 8) ^ table[(crc ^ byte) & 0xff]
return ~crc & 0xffffffff
CCITT32_TAB = generate_crc_tab(0xedb88320)
def crc32(data: bytes):
global CCITT32_TAB
return do_crc(data, CCITT32_TAB)
Loading…
Cancel
Save