{% extends "build.base.j2" %}

{% block variables %}
{{ super() }}
ccflags = $ccflags $
    -I${srcroot}/src/include $
    -I${srcroot}/src/include/x86_64 $
    -fcolor-diagnostics
{% endblock %}

{% block baserules %}
{{ super() }}
rule makerd
    description = Making init ramdisk
    command = $builddir/native/makerd $in $out

rule makeefi
    description = Converting $name
    command = objcopy $
        -j .text $
        -j .sdata $
        -j .data $
        -j .dynamic $
        -j .dynsym $
        -j .rel $
        -j .rela $
        -j .reloc $
        --target=efi-app-x86_64 $
        $in $out

rule makefat
    description = Creating $name
    command = $
        cp $srcroot/assets/diskbase.img $out; $
        mcopy -s -D o -i $out@@1M $builddir/fatroot/* ::/

rule strip
    description = Stripping $name
    command = $
        cp $in $out; $
        objcopy --only-keep-debug $out $out.debug; $
        strip -g $out; $
        objcopy --add-gnu-debuglink=$out.debug $out
{% endblock %}

{% block extra %}
build $builddir/ovmf_vars.fd : cp $srcroot/assets/ovmf/x64/ovmf_vars.fd
    name = ovmf_vars.fd

build $builddir/ovmf_vars_d.fd : cp $srcroot/assets/ovmf/x64/ovmf_vars_d.fd
    name = ovmf_vars_d.fd

build $builddir/jsix.elf | $builddir/jsix.elf.debug : strip $builddir/host/jsix.elf
    name = kernel

build $builddir/jsix.dump : dump $builddir/host/jsix.elf
    name = kernel

build $builddir/jsix.elf-gdb.py : cp ${srcroot}/assets/debugging/jsix.elf-gdb.py
    name = kernel debug python scripts

build $builddir/fatroot/jsix.elf : cp $builddir/jsix.elf
    name = kernel to FAT image

build $builddir/fatroot/efi/boot/bootx64.efi : cp $builddir/boot/boot.efi
    name = bootloader to FAT image

build $builddir/fatroot/initrd.img : makerd ${srcroot}/assets/initrd.toml | $
    ${builddir}/native/makerd $
    ${builddir}/user/nulldrv

build $builddir/jsix.img : makefat | $
    $builddir/fatroot/initrd.img $
    $builddir/fatroot/jsix.elf $
    $builddir/fatroot/efi/boot/bootx64.efi
    name = jsix.img

default $
    $builddir/ovmf_vars.fd $
    $builddir/ovmf_vars_d.fd $
    $builddir/jsix.dump $
    $builddir/jsix.elf-gdb.py $
    $builddir/jsix.img
{% endblock %}

# vim: ft=ninja et ts=4 sts=4 sw=4

