cmake_minimum_required(VERSION 3.21)
project(mister_x64 LANGUAGES CXX C ASM_MASM VERSION 1.30.15)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

option(MISTERX64_DIAGNOSTICO
       "Compila o registro em arquivo. Fica desligado no que vai ao usuario." OFF)

if(MSVC)
    add_compile_options(/utf-8 /W3 /wd4996)
    add_compile_definitions(_CRT_SECURE_NO_WARNINGS NOMINMAX WIN32_LEAN_AND_MEAN)

    
    
    
    add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/GR->)

    
    
    add_link_options(/OPT:REF /OPT:ICF)

    
    
    
    add_link_options(/EMITTOOLVERSIONINFO:NO)
endif()




string(TIMESTAMP MISTER_X64_BUILD_DATE "%Y%m%d" UTC)

execute_process(
    COMMAND git rev-parse --short HEAD
    WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
    OUTPUT_VARIABLE MISTER_X64_COMMIT
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET)
if(NOT MISTER_X64_COMMIT)
    set(MISTER_X64_COMMIT "desconhecido")
endif()

configure_file("${CMAKE_SOURCE_DIR}/src/app/version.h.in"
               "${CMAKE_BINARY_DIR}/generated/app/version.h" @ONLY)



set(SDL3_STATIC_ROOT "${CMAKE_SOURCE_DIR}/third_party/SDL3-static")
if(NOT EXISTS "${SDL3_STATIC_ROOT}/lib/SDL3-static.lib")
    message(FATAL_ERROR
        "SDL3 estatico nao encontrado em ${SDL3_STATIC_ROOT}. "
        "Gere com: cmake -S third_party/sdl3src/SDL3-3.4.14 -B <build> "
        "-DSDL_STATIC=ON -DSDL_SHARED=OFF -DCMAKE_INSTALL_PREFIX=${SDL3_STATIC_ROOT}")
endif()

find_package(SDL3 REQUIRED CONFIG PATHS "${SDL3_STATIC_ROOT}/cmake" NO_DEFAULT_PATH)



set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")



find_package(Python3 COMPONENTS Interpreter REQUIRED)

set(GENERATED_RC "${CMAKE_BINARY_DIR}/resources.rc")




file(GLOB CORE_MANIFESTS "${CMAKE_SOURCE_DIR}/cores/*/core.json")
file(GLOB CORE_LIBRARIES "${CMAKE_SOURCE_DIR}/cores/*/*.dll")





file(GLOB_RECURSE CORE_SUPPORT "${CMAKE_SOURCE_DIR}/cores/*/sys/*")
add_custom_command(
    OUTPUT "${GENERATED_RC}"
    COMMAND ${Python3_EXECUTABLE}
            "${CMAKE_SOURCE_DIR}/tools/make_resources.py"
            "${CMAKE_SOURCE_DIR}/cores"
            "${GENERATED_RC}"
            "${CMAKE_SOURCE_DIR}/assets/mister-x64.ico"
    DEPENDS "${CMAKE_SOURCE_DIR}/tools/make_resources.py"
            "${CMAKE_SOURCE_DIR}/assets/mister-x64.ico"
            ${CORE_MANIFESTS}
            ${CORE_LIBRARIES}
            ${CORE_SUPPORT}
    COMMENT "Preparando cores e manifestos (icone + overlay)"
    VERBATIM)


set(EMBEDDED_OVERLAY "${CMAKE_BINARY_DIR}/embedded/overlay.bin")


add_executable(mister_x64
    src/main.cpp
    src/app/Application.cpp
    src/app/Log.cpp
    src/app/Retrato.cpp
    src/app/Settings.cpp

    src/platform/ChamadaProtegida.asm
    src/platform/Escalonador.cpp
    src/platform/Tombamento.cpp
    src/platform/DynamicLibrary.cpp
    src/platform/Paths.cpp
    src/platform/Embedded.cpp
    src/platform/KeyMaterial.cpp
    src/platform/ContainerSaves.cpp
    src/platform/ImageFile.cpp
    src/platform/Integrity.cpp

    src/video/SdlRenderer.cpp
    src/video/VulkanHost.cpp
    src/video/VulkanDevice.cpp
    src/audio/AudioSystem.cpp
    src/audio/WavRecorder.cpp
    src/input/InputManager.cpp
    src/input/InputMapping.cpp

    src/core/CoreManifest.cpp
    src/core/CoreSettings.cpp
    src/core/CoreManager.cpp
    src/core/backend/CoreLibrary.cpp
    src/core/backend/CoreVfs.cpp

    src/update/Updater.cpp
    src/update/EnvioDeLog.cpp

    src/mister/compat/mister_compat.cpp
    src/mister/osd/OsdTransport.cpp
    src/mister/osd/OsdGeometry.cpp
    src/mister/osd/charrom.cpp        
    src/mister/osd/osd.cpp            
    src/mister/menu/Menu.cpp
    src/mister/menu/FileBrowser.cpp
    src/mister/menu/MenuBackground.cpp

    ${GENERATED_RC}
)

target_include_directories(mister_x64 PRIVATE
    ${CMAKE_BINARY_DIR}/generated
    src
    third_party/coreapi
    third_party/json
    third_party/stb
    ${CMAKE_SOURCE_DIR}/third_party/vulkan)

target_link_libraries(mister_x64 PRIVATE SDL3::SDL3-static avrt winmm bcrypt)

if(MISTERX64_DIAGNOSTICO)
    target_compile_definitions(mister_x64 PRIVATE MISTERX64_DIAGNOSTICO)
    
    
    target_link_libraries(mister_x64 PRIVATE powrprof psapi dbghelp)
endif()

if(WIN32)
    
    
    set_target_properties(mister_x64 PROPERTIES
        WIN32_EXECUTABLE ON
        OUTPUT_NAME "MiSTer-x64")

    
    
    
    
    add_custom_command(TARGET mister_x64 POST_BUILD
        COMMAND ${Python3_EXECUTABLE}
                "${CMAKE_SOURCE_DIR}/tools/append_overlay.py"
                "$<TARGET_FILE:mister_x64>"
                "${EMBEDDED_OVERLAY}"
        COMMENT "Anexando os cores ao fim do executavel"
        VERBATIM)

    
    
    
    add_custom_command(TARGET mister_x64 POST_BUILD
        COMMAND ${Python3_EXECUTABLE}
                "${CMAKE_SOURCE_DIR}/tools/stamp_integrity.py"
                "$<TARGET_FILE:mister_x64>"
        COMMENT "Carimbando o hash de integridade"
        VERBATIM)
endif()

