cmake_minimum_required(VERSION 3.18)
project(pokedaisy_windows C)
enable_language(RC) # pokedaisy.rc: the exe's icon
# PokeDaisy on Windows: the same SDL2 libretro frontend as linux/ (its host is shared, unlocked: input, sound, pacing,
# the battery save and its backups, states, fast-forward, rewind, the host menu), loading the cores' .dlls from beside
# the executable: pokedaisy_gbc_libretro.dll (GB / GBC, mGBA), pokedaisy_gba_libretro.dll (GBA, armGBA),
# pokedaisy_ds_libretro.dll (DS, the app's melonDS engine) and dolphin_libretro.dll (GameCube, libretro's Dolphin core,
# with its dolphin-emu/Sys folder; windows/fetch-dolphin.sh brings both). Build the cores first into the same folder.
#
# With MinGW-w64 from MSYS2 (C:/msys64/mingw64/bin on PATH), from the repo root:
#   cmake -G "MinGW Makefiles" -S libretro         -B build/win-gbc
#   cmake --build build/win-gbc --target pokedaisy_gbc_libretro -j 8
#   cmake -G "MinGW Makefiles" -S libretro/armgba  -B build/win-gba -DARMGBA_JIT=OFF
#   cmake --build build/win-gba --target pokedaisy_gba_libretro -j 8
#   cmake -G "MinGW Makefiles" -S windows/ds-core  -B build/win-ds
#   cmake --build build/win-ds -j 8
#   cmake -G "MinGW Makefiles" -S windows          -B build/win-host
#   cmake --build build/win-host -j 8
# ARMGBA_JIT=OFF is required: armGBA's x64 recompiler emits code for the SysV calling convention, so on Win64 it
# crashes as soon as a game runs and the core uses its exact-CPU interpreter instead.
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

find_path(SDL2_INCLUDE_DIR SDL.h PATH_SUFFIXES SDL2 REQUIRED)
find_library(SDL2_LIBRARY NAMES SDL2 REQUIRED)

file(GLOB PD_CORE CONFIGURE_DEPENDS ../3ds/core/*.c)
# The host's new game library: the app's own list with covers (linux/library.c), and zlib's puff for its PNGs.
# PD_HW_RENDER: the GameCube / Wii core (dolphin_libretro.dll) draws with OpenGL, so the host gives it a context of its
# own and reads its framebuffer back (linux/main.c). No OpenGL import library is needed: every GL call is loaded by
# SDL_GL_GetProcAddress.
add_library(pd_puff STATIC ../third_party/zlib/contrib/puff/puff.c)
target_include_directories(pd_puff PUBLIC ../third_party/zlib/contrib/puff)
# Zipped ROMs: zlib's inflate (linux/rom_zip.c).
add_library(pd_inflate STATIC ../third_party/zlib/adler32.c ../third_party/zlib/crc32.c ../third_party/zlib/inffast.c
  ../third_party/zlib/inflate.c ../third_party/zlib/inftrees.c ../third_party/zlib/zutil.c)
# zconf.h: a clean zlib checkout has it; once zlib's own CMake has run in the tree (another core's build) it's renamed
# zconf.h.included. Either is copied into the build tree, so this build never depends on which state the submodule is in.
set(PD_ZLIB_GEN ${CMAKE_CURRENT_BINARY_DIR}/zlib)
file(MAKE_DIRECTORY ${PD_ZLIB_GEN})
set(PD_ZCONF ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/zlib/zconf.h)
if(NOT EXISTS ${PD_ZCONF})
  set(PD_ZCONF ${PD_ZCONF}.included)
endif()
configure_file(${PD_ZCONF} ${PD_ZLIB_GEN}/zconf.h COPYONLY)
target_include_directories(pd_inflate PUBLIC ${PD_ZLIB_GEN} ../third_party/zlib)
# SETTINGS > VERSION: the app's versionName.
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/../app/build.gradle.kts POCKET_VERSION_LINE REGEX "versionName = ")
string(REGEX MATCH "\"[^\"]+\"" POCKET_VERSION "${POCKET_VERSION_LINE}")
# The exe's version resource (pokedaisy.rc's VERSIONINFO): an unsigned exe with no name or version reads as suspicious
# to Defender's machine-learning check (1.7.0 beta 1 was flagged Trojan:Win32/Sprisky.U!cl).
string(REPLACE "\"" "" PD_VERSION_TEXT "${POCKET_VERSION}")
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(\\.([0-9]+))?" PD_VERSION_NUMBERS "${PD_VERSION_TEXT}")
foreach(i 1 2 3)
  set(PD_V${i} "${CMAKE_MATCH_${i}}")
  if(PD_V${i} STREQUAL "")
    set(PD_V${i} 0)
  endif()
endforeach()
set(PD_V4 "${CMAKE_MATCH_5}")
if(PD_V4 STREQUAL "")
  set(PD_V4 0)
endif()
configure_file(pokedaisy_version.h.in ${CMAKE_CURRENT_BINARY_DIR}/pokedaisy_version.h @ONLY)
# The resource script embeds the manifest (per-monitor DPI awareness): rebuild it when the manifest changes.
set_source_files_properties(pokedaisy.rc PROPERTIES OBJECT_DEPENDS
  "${CMAKE_CURRENT_SOURCE_DIR}/pokedaisy.manifest;${CMAKE_CURRENT_BINARY_DIR}/pokedaisy_version.h")
add_executable(PokeDaisy ../linux/main.c ../linux/library.c ../linux/cover_png.c ../linux/rom_zip.c ../linux/shared_save.c ../linux/web.c ../3ds/desktop/pd_png.c ${PD_CORE} pokedaisy.rc)
target_include_directories(PokeDaisy PRIVATE
  ${CMAKE_CURRENT_BINARY_DIR} # pokedaisy_version.h, for pokedaisy.rc
  ../3ds/core
  ../third_party/mgba/src/platform/libretro # libretro.h
  ${SDL2_INCLUDE_DIR})
# The host keeps its own main(): SDL's WinMain shim isn't needed, and none of SDL_main applies to it.
target_compile_definitions(PokeDaisy PRIVATE _CRT_SECURE_NO_WARNINGS NOMINMAX SDL_MAIN_HANDLED PD_HW_RENDER=1)
if(POCKET_VERSION)
  target_compile_definitions(PokeDaisy PRIVATE POCKET_VERSION=${POCKET_VERSION})
endif()
target_compile_options(PokeDaisy PRIVATE -Wall -Wextra)
if(MINGW)
  # The host's library uses the same POSIX odds and ends as the cores (mkdir's mode, localtime_r, ...).
  target_include_directories(PokeDaisy PRIVATE ../windows/include)
  target_compile_options(PokeDaisy PRIVATE -include ${CMAKE_CURRENT_SOURCE_DIR}/../windows/include/pd_win_compat.h)
endif()
target_link_libraries(PokeDaisy PRIVATE pd_puff pd_inflate ${SDL2_LIBRARY} ws2_32)
set_target_properties(PokeDaisy PROPERTIES WIN32_EXECUTABLE OFF)

# SDL2.dll has to sit beside the game's exe.
get_filename_component(SDL2_LIB_DIR ${SDL2_LIBRARY} DIRECTORY)
find_file(SDL2_DLL NAMES SDL2.dll PATHS ${SDL2_LIB_DIR}/../bin ${SDL2_LIB_DIR} NO_DEFAULT_PATH)
if(SDL2_DLL)
  add_custom_command(TARGET PokeDaisy POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SDL2_DLL}
                     $<TARGET_FILE_DIR:PokeDaisy>/SDL2.dll)
endif()
