#!/usr/bin/env bash
set -euo pipefail

SYSROOT_DIR="/home/kaguya/KirkOS/ext2_root/"

rm -rf headers-build build
rm -f "$SYSROOT_DIR/usr/lib/libc.a" "$SYSROOT_DIR/usr/lib/libm.a" "$SYSROOT_DIR/usr/lib/libdl.a"

# 1. Headers
meson setup \
    --cross-file=x86_64-kirkos-gcc.txt \
    --prefix=/usr \
    -Dheaders_only=true \
    -Dlinux_kernel_headers=/home/kaguya/KirkOS/linux-headers/include \
    headers-build --reconfigure

DESTDIR="$SYSROOT_DIR" ninja -C headers-build install

# 2. Static libraries
meson setup \
    --cross-file=/home/kaguya/KirkOS/user/include/mlibc/x86_64-kirkos-gcc.txt \
    --prefix=/usr \
    -Ddefault_library=static \
    -Dno_headers=true \
    -Dlinux_kernel_headers=/home/kaguya/KirkOS/linux-headers/include \
    build --reconfigure

DESTDIR="$SYSROOT_DIR" ninja -C build install