cmake_minimum_required(VERSION 3.16)
project(HailToTheKing C)
set(CMAKE_C_STANDARD 11)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()
file(GLOB GEN_SRC ${CMAKE_SOURCE_DIR}/build/gen/*.c)
add_executable(hail-to-the-king runtime/runtime.c runtime/hw.c runtime/bios_hle.c runtime/disc.c runtime/pvr.c runtime/dma.c runtime/maple.c runtime/aica.c runtime/aica_synth.c runtime/arm7.c runtime/coro.c runtime/interp.c runtime/render_gl.c runtime/upscale.c runtime/png.c runtime/textpatch.c runtime/iso9660.c runtime/sfd.c runtime/models.c ${GEN_SRC})
target_include_directories(hail-to-the-king PRIVATE runtime build/gen)
# the SH-4 code relies on wrapping 32-bit arithmetic and type punning
target_compile_options(hail-to-the-king PRIVATE -fwrapv -fno-strict-aliasing -Wno-unused-variable -Wno-unused-label -Wno-tautological-compare)
if(NOT MSVC)
  target_link_libraries(hail-to-the-king PRIVATE m)
endif()

# PLATFORM=headless (EGL surfaceless, PNG screenshots) or sdl (window + gamepad)
set(PLATFORM "headless" CACHE STRING "headless or sdl")
set(EXT_INC "" CACHE PATH "extra include dir (e.g. the folder holding SDL2/SDL.h)")
target_include_directories(hail-to-the-king PRIVATE ${CMAKE_SOURCE_DIR}/third_party/khronos)
if(EXT_INC)
  target_include_directories(hail-to-the-king PRIVATE ${EXT_INC})
endif()
if(PLATFORM STREQUAL "sdl")
  target_sources(hail-to-the-king PRIVATE runtime/platform_sdl.c)
  set(SDL2_LIB "SDL2" CACHE STRING "SDL2 library to link")
  target_link_libraries(hail-to-the-king PRIVATE ${SDL2_LIB})
else()
  target_sources(hail-to-the-king PRIVATE runtime/platform_headless.c)
  target_link_libraries(hail-to-the-king PRIVATE -l:libEGL.so.1)
endif()
if(WIN32)
  target_link_libraries(hail-to-the-king PRIVATE winpthread)
endif()
