cmake_minimum_required(VERSION 3.20)
project(BombermanRecomp C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

if(NOT DEFINED SNESRECOMP_ROOT)
  set(SNESRECOMP_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/snesrecomp")
endif()
include("${SNESRECOMP_ROOT}/runner/runner.cmake")
list(REMOVE_ITEM SNESRECOMP_RUNNER_SOURCES
  "${SNESRECOMP_ROOT}/runner/src/keybinds.c"
  "${SNESRECOMP_ROOT}/runner/src/host_report.c")
file(GLOB GENERATED_SOURCES CONFIGURE_DEPENDS
  "${CMAKE_CURRENT_SOURCE_DIR}/generated/*.c")
if(NOT GENERATED_SOURCES)
  message(FATAL_ERROR "Run scripts/generate.py before building")
endif()

set(BOMBERMAN_COMMON_SOURCES
  ${SNESRECOMP_RUNNER_SOURCES}
  ${GENERATED_SOURCES}
  runner/bomberman_game.c
  runner/host_support.c
  runner/verified_rom.c)

add_executable(bomberman_headless
  ${BOMBERMAN_COMMON_SOURCES}
  runner/headless_main.c)
add_executable(BombermanRecomp
  ${BOMBERMAN_COMMON_SOURCES}
  runner/win32_main.c)
foreach(target bomberman_headless BombermanRecomp)
  target_include_directories(${target} PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/config"
    "${CMAKE_CURRENT_SOURCE_DIR}/runner"
    ${SNESRECOMP_RUNNER_INCLUDE_DIRS})
  target_compile_definitions(${target} PRIVATE
    SNESRECOMP_TRACE=0
    SNESRECOMP_REVERSE_DEBUG=0
    SNESRECOMP_EXTERNAL_RAM_ROUTINE_GUARDS=1
    SYSTEM_VOLUME_MIXER_AVAILABLE=0)
  if(MSVC)
    target_compile_options(${target} PRIVATE /W0)
    target_compile_definitions(${target} PRIVATE _CRT_SECURE_NO_WARNINGS)
    set_property(TARGET ${target} PROPERTY
      MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  endif()
  target_link_libraries(${target} PRIVATE ${SNESRECOMP_RUNNER_LIBRARIES})
endforeach()
if(WIN32)
  target_link_libraries(bomberman_headless PRIVATE user32 ws2_32)
  target_link_libraries(BombermanRecomp PRIVATE
    user32 ws2_32 gdi32 winmm comdlg32)
endif()
