file(GLOB_RECURSE CORE_SOURCES CONFIGURE_DEPENDS "src/*.c")

if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
    cmake_minimum_required(VERSION 3.16)
    project(nova64_core VERSION 1.0.0 LANGUAGES C)

    set(CMAKE_C_STANDARD 11)
    set(CMAKE_C_STANDARD_REQUIRED ON)

endif()

if(ESP_PLATFORM)
    # --------------------------------------------------------------------------
    # 1. ESP-IDF BUILD MODE (Target: ESP32-P4)
    # --------------------------------------------------------------------------
    message(STATUS "[Nova64] Building as ESP-IDF Component for ESP32...")

    idf_component_register(
        SRCS
            ${CORE_SOURCES}
        INCLUDE_DIRS
            "include"
        REQUIRES
            freertos
            wamr
    )

else()
    # --------------------------------------------------------------------------
    # 2. DESKTOP BUILD MODE (Target: Windows .dll / Linux .so / macOS .dylib)
    # --------------------------------------------------------------------------

    message(STATUS "[Nova64] Building as Desktop Shared Library...")
    add_compile_definitions(NOVA64_SHUTDOWN_AVAILABLE) # Enable shutdown functionality for desktop builds

    # --------------------------------------------------------------------------
    # FreeRTOS Configuration
    # --------------------------------------------------------------------------
    add_library(freertos_config INTERFACE)

    target_include_directories(freertos_config SYSTEM
        INTERFACE
            ${CMAKE_CURRENT_SOURCE_DIR}/include
    )

    target_compile_definitions(freertos_config
        INTERFACE
            projCOVERAGE_TEST=0
    )

    include(FetchContent)

    # --------------------------------------------------------------------------
    # FreeRTOS Kernel via FetchContent
    # --------------------------------------------------------------------------

    # Download FreeRTOS-Kernel Repository
    FetchContent_Declare(
        FreeRTOS_Kernel
        GIT_REPOSITORY https://github.com/FreeRTOS/FreeRTOS-Kernel.git
        GIT_TAG        V11.3.0
    )
    
    if(WIN32)
        set(FREERTOS_PORT "MSVC_MINGW" CACHE STRING "")
    else()
        set(FREERTOS_PORT "GCC_POSIX" CACHE STRING "")
    endif()
    # Select FreeRTOS heap implementation (provides pvPortMalloc/vPortFree)
    set(FREERTOS_HEAP 4 CACHE STRING "FreeRTOS Heap Implementation")
    # Disable FreeRTOS Demos/Tests
    set(BUILD_TESTING OFF CACHE BOOL "" FORCE)

    # --------------------------------------------------------------------------
    # WAMR (WebAssembly Micro Runtime) via FetchContent
    # --------------------------------------------------------------------------

    FetchContent_Declare(
        wamr_runtime
        GIT_REPOSITORY https://github.com/bytecodealliance/wasm-micro-runtime.git
        GIT_TAG        WAMR-2.4.5
    )

    # WAMR Desktop-Konfiguration
    set(WAMR_BUILD_INTERP 1 CACHE BOOL "" FORCE)
    set(WAMR_BUILD_FAST_INTERP 1 CACHE BOOL "" FORCE)
    # set(WAMR_BUILD_INTERP_TICKS 1 CACHE BOOL "" FORCE)

    set(WAMR_BUILD_JIT 0 CACHE BOOL "" FORCE)
    set(WAMR_BUILD_AOT 0 CACHE BOOL "" FORCE)
    set(WAMR_BUILD_LIBC_BUILTIN 1 CACHE BOOL "" FORCE)
    # Disable WASI
    set(WAMR_BUILD_LIBC_WASI 0 CACHE BOOL "" FORCE)

    if(WIN32)
        set(WAMR_BUILD_PLATFORM "windows" CACHE STRING "" FORCE)
    elseif(APPLE)
        set(WAMR_BUILD_PLATFORM "darwin" CACHE STRING "" FORCE)
    else()
        set(WAMR_BUILD_PLATFORM "linux" CACHE STRING "" FORCE)
    endif()
    
    FetchContent_MakeAvailable(FreeRTOS_Kernel wamr_runtime)

    # --------------------------------------------------------------------------
    # Force -fPIC for FreeRTOS targets (required for linking into .so)
    # --------------------------------------------------------------------------
    if(NOT WIN32)
        if(TARGET freertos_kernel)
            set_target_properties(freertos_kernel PROPERTIES POSITION_INDEPENDENT_CODE ON)
        endif()
        if(TARGET freertos_kernel_port)
            set_target_properties(freertos_kernel_port PROPERTIES POSITION_INDEPENDENT_CODE ON)
        endif()
    endif()

    # --------------------------------------------------------------------------
    # Nova64 Core Target
    # --------------------------------------------------------------------------

    # Shared Library (.dll / .so / .dylib)
    add_library(nova64_core SHARED ${CORE_SOURCES})

    target_include_directories(nova64_core PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    )
    
    # Link FreeRTOS Kernel to the nova64_core library
    target_link_libraries(nova64_core PRIVATE freertos_kernel freertos_config vmlib)

    target_compile_definitions(nova64_core PRIVATE NOVA64_EXPORTS)

    if(MSVC)
        add_compile_definitions(
            COMPILING_WASM_RUNTIME_API
            alignof=__alignof
            _Alignof=__alignof
            )
    endif()

    if (WIN32)
        set_target_properties(nova64_core PROPERTIES
            C_VISIBILITY_PRESET hidden
            VISIBILITY_INLINES_HIDDEN YES
            PREFIX ""
        )
    else()
        set_target_properties(nova64_core PROPERTIES
            C_VISIBILITY_PRESET hidden
            VISIBILITY_INLINES_HIDDEN YES
        )
    endif()

    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

endif()