cmake_minimum_required(VERSION 3.16)

project(KirkOS C ASM)

set(VERSION 1)
set(PATCHLEVEL 0)
set(SUBLEVEL 0)
set(EXTRAVERSION "-rc1")

set(KIRKOS_VERSION_STRING
    "${VERSION}.${PATCHLEVEL}.${SUBLEVEL}${EXTRAVERSION}"
)

include(${CMAKE_SOURCE_DIR}/libs/uacpi/uacpi.cmake)

message(STATUS "Building KirkOS version: ${KIRKOS_VERSION_STRING}")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

file(GLOB_RECURSE KIRKOS_SOURCES
    "${CMAKE_SOURCE_DIR}/src/*.c"
    "${CMAKE_SOURCE_DIR}/src/*.S"
)


set(ISO_ROOT "${CMAKE_BINARY_DIR}/iso_root")
set(DISK_IMG "${CMAKE_BINARY_DIR}/disk.img")
set(ISO "${CMAKE_BINARY_DIR}/image.iso")

set(EXT2_ROOT "${CMAKE_SOURCE_DIR}/ext2_root")



add_executable(KirkOS ${KIRKOS_SOURCES})

target_sources(KirkOS PRIVATE
    ${UACPI_SOURCES}
    ${CMAKE_SOURCE_DIR}/libs/uacpi/source/kernel_api.c
)

target_include_directories(KirkOS PRIVATE
    ${UACPI_INCLUDES}
)

target_compile_definitions(KirkOS PRIVATE
    KIRKOS_VERSION="${KIRKOS_VERSION_STRING}"
    KIRKOS_VERSION_MAJOR=${VERSION}
    KIRKOS_VERSION_PATCH=${PATCHLEVEL}
    KIRKOS_VERSION_SUB=${SUBLEVEL}
)



add_custom_command(
    OUTPUT ${ISO_ROOT}/boot/KirkOS
    DEPENDS KirkOS
    COMMAND ${CMAKE_COMMAND} -E copy
            $<TARGET_FILE:KirkOS>
            ${ISO_ROOT}/boot/KirkOS
)

add_custom_target(iso_kernel DEPENDS ${ISO_ROOT}/boot/KirkOS)

set(LIMINE_DIR "${CMAKE_SOURCE_DIR}/limine")

add_custom_target(limine_files
    COMMAND ${CMAKE_COMMAND} -E make_directory ${ISO_ROOT}/boot/limine
    COMMAND ${CMAKE_COMMAND} -E make_directory ${ISO_ROOT}/EFI/BOOT

    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/limine.conf ${ISO_ROOT}/boot/limine/
    COMMAND ${CMAKE_COMMAND} -E copy ${LIMINE_DIR}/limine-bios.sys ${ISO_ROOT}/boot/limine/
    COMMAND ${CMAKE_COMMAND} -E copy ${LIMINE_DIR}/limine-bios-cd.bin ${ISO_ROOT}/boot/limine/
    COMMAND ${CMAKE_COMMAND} -E copy ${LIMINE_DIR}/limine-uefi-cd.bin ${ISO_ROOT}/boot/limine/
    COMMAND ${CMAKE_COMMAND} -E copy ${LIMINE_DIR}/BOOTX64.EFI ${ISO_ROOT}/EFI/BOOT/
    COMMAND ${CMAKE_COMMAND} -E copy ${LIMINE_DIR}/BOOTIA32.EFI ${ISO_ROOT}/EFI/BOOT/
)
add_custom_target(iso
    DEPENDS iso_kernel limine_files
    COMMAND xorriso -as mkisofs -R -r -J
        -b boot/limine/limine-bios-cd.bin
        -no-emul-boot -boot-load-size 4 -boot-info-table
        -hfsplus
        -apm-block-size 2048
        --efi-boot boot/limine/limine-uefi-cd.bin
        -efi-boot-part --efi-boot-image
        --protective-msdos-label
        ${ISO_ROOT}
        -o ${ISO}

    COMMAND ${LIMINE_DIR}/limine bios-install ${ISO}
)

add_custom_target(disk_img
    COMMAND dd if=/dev/zero of=${DISK_IMG} bs=1M count=32
    COMMAND mkfs.ext2 -F -L KIRKOS ${DISK_IMG}

    COMMAND guestfish --rw -a ${DISK_IMG} run
    COMMAND guestfish --rw -a ${DISK_IMG} -m /dev/sda mkdir /root
    COMMAND guestfish --rw -a ${DISK_IMG} -m /dev/sda copy-in ${EXT2_ROOT}/. /

    COMMENT "Creating ext2 disk image via guestfish"
)

add_custom_target(run
    DEPENDS iso disk_img
    COMMAND qemu-system-x86_64
        -cdrom ${ISO}
        -hda ${DISK_IMG}
        -debugcon stdio
        -m 8G
        -smp 2
        -device ich9-intel-hda
        -device hda-duplex
)

add_custom_target(debug
    DEPENDS iso disk_img
    COMMAND qemu-system-x86_64
        -cdrom ${ISO}
        -hda ${DISK_IMG}
        -s -S
        -debugcon stdio
        -d int,cpu_reset
        -m 8G
        -smp 2
        -device ich9-intel-hda,id=hda
        -device hda-duplex,id=codec0,bus=hda.0,cad=0
)

target_include_directories(KirkOS PRIVATE
    "${CMAKE_SOURCE_DIR}/src"
)


# ----------------------------------------
# DEFAULT C FLAGS (cacheable overrides)
# ----------------------------------------
set(KIRKOS_CFLAGS "-g -O2 -pipe -Wall -Wextra -std=gnu11 -ffreestanding -fno-stack-protector -fno-stack-check -fno-lto -fno-PIC -ffunction-sections -fdata-sections -m64 -march=x86-64 -mabi=sysv -mno-80387 -mno-mmx -mno-sse -mno-sse2 -mno-red-zone -mcmodel=kernel"
    CACHE STRING "Default CFLAGS for KirkOS")

set(KIRKOS_LDFLAGS "-nostdlib -static -Wl,--gc-sections -Wl,-T,../linker.lds -Wl,-z,max-page-size=0x1000 -Wl,-m,elf_x86_64"
    CACHE STRING "Default LDFLAGS for KirkOS")

# ----------------------------------------
# Apply flags
# ----------------------------------------
separate_arguments(_cflags UNIX_COMMAND "${KIRKOS_CFLAGS}")
separate_arguments(_ldflags UNIX_COMMAND "${KIRKOS_LDFLAGS}")

target_compile_options(KirkOS PRIVATE ${_cflags})
target_link_options(KirkOS PRIVATE ${_ldflags})