cmake_minimum_required(VERSION 3.16)
project(einhander C CXX)
if(WIN32)
  enable_language(RC)
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()
file(GLOB GEN_SRC CONFIGURE_DEPENDS gen/*.cpp)
file(GLOB RT_SRC CONFIGURE_DEPENDS runtime/*.cpp)
add_executable(einhander ${RT_SRC} ${GEN_SRC})
target_include_directories(einhander PRIVATE runtime gen third_party/miniz)
# miniz (deflate) for the packed disc file
target_sources(einhander PRIVATE third_party/miniz/miniz.c)
# libchdr (reads .chd disc images for the import)
file(GLOB CHDR_SRC third_party/libchdr/src/*.c)
list(APPEND CHDR_SRC third_party/libchdr/deps/lzma-26.02/src/LzmaDec.c third_party/libchdr/deps/zstd-1.5.7/zstddeclib.c)
target_sources(einhander PRIVATE ${CHDR_SRC})
target_include_directories(einhander PRIVATE third_party/libchdr/include)
set_source_files_properties(${CHDR_SRC} PROPERTIES COMPILE_OPTIONS "-w;-O2")
set_source_files_properties(third_party/miniz/miniz.c PROPERTIES COMPILE_DEFINITIONS "MINIZ_NO_ARCHIVE_APIS;MINIZ_NO_STDIO;MINIZ_NO_TIME" COMPILE_OPTIONS "-w")
# The generated code is large and simple: -O1 compiles much faster with little speed loss.
set_source_files_properties(${GEN_SRC} PROPERTIES COMPILE_OPTIONS "-O1;-fno-strict-aliasing;-w;-g1")
if(WIN32)
  set(SDL2_DIR "" CACHE PATH "SDL2 mingw development package (x86_64-w64-mingw32 folder)")
  target_include_directories(einhander PRIVATE runtime/gl ${SDL2_DIR}/include)
  target_compile_definitions(einhander PRIVATE SDL_MAIN_HANDLED)
  target_compile_options(einhander PRIVATE -O2 -fno-strict-aliasing -g0)
  target_link_directories(einhander PRIVATE ${SDL2_DIR}/lib)
  if(EXISTS ${CMAKE_SOURCE_DIR}/runtime/app.rc)
    target_sources(einhander PRIVATE runtime/app.rc)
  endif()
  target_link_libraries(einhander PRIVATE SDL2 winmm -mwindows -static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive,-Bdynamic)
elseif(USE_SDL)
  target_compile_definitions(einhander PRIVATE USE_SDL)
  target_compile_options(einhander PRIVATE -O2 -fno-strict-aliasing -g)
  target_link_libraries(einhander PRIVATE SDL2 GL pthread)
else()
  target_compile_options(einhander PRIVATE -O2 -fno-strict-aliasing -g)
  target_link_libraries(einhander PRIVATE EGL GL pthread)
endif()
