cmake_minimum_required(VERSION 3.16)
project(CebolaRecompiled C)
set(SDL3_DIR "${CMAKE_CURRENT_SOURCE_DIR}/work/deps/sdl-build" CACHE PATH "SDL3 CMake package")
find_package(SDL3 CONFIG REQUIRED)
set(CMAKE_C_STANDARD 11)
set(ENGINE "${CMAKE_CURRENT_LIST_DIR}/work/deps/smsggrecomp" CACHE PATH "Runtime experimental")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/runtime/glue.c")
  set(CEBOLA_RUNTIME "${CMAKE_CURRENT_SOURCE_DIR}/runtime")
else()
  set(CEBOLA_RUNTIME "${ENGINE}/runner")
endif()
file(GLOB BANK_SOURCES CONFIGURE_DEPENDS generated/cebola_bank*.c)
# Large conservative switches compile much faster at O1. Keep the scheduler,
# device runtime and UI at the normal Release optimization level.
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
  set_source_files_properties(${BANK_SOURCES} PROPERTIES COMPILE_OPTIONS "-O1")
endif()
add_library(cebola-core STATIC ${CEBOLA_RUNTIME}/glue.c src/replay.c
  ${ENGINE}/runner/video/sms_vdp.c ${ENGINE}/runner/audio/sn76489.c
  ${ENGINE}/runner/external/superzazu/z80.c
  generated/cebola_full.c generated/cebola_dispatch.c generated/cebola_layout.c
  generated/cebola_step.c ${BANK_SOURCES})
target_include_directories(cebola-core PUBLIC ${CEBOLA_RUNTIME} src ${ENGINE}/runner ${ENGINE}/runner/include generated)
if(NOT MSVC)
  target_link_libraries(cebola-core PUBLIC m)
endif()
add_executable(cebola-headless ${ENGINE}/runner/main.c)
target_link_libraries(cebola-headless PRIVATE cebola-core)
add_executable(cebola-decoder-verify tests/decoder_halt.c ${ENGINE}/recompiler/src/z80_decoder.c ${ENGINE}/runner/external/superzazu/z80.c)
target_include_directories(cebola-decoder-verify PRIVATE ${ENGINE}/recompiler/src ${ENGINE}/runner/external/superzazu)
# These assertions are the regression contract, including in Release builds.
if(NOT MSVC)
  target_compile_options(cebola-decoder-verify PRIVATE -UNDEBUG)
else()
  target_compile_options(cebola-decoder-verify PRIVATE /UNDEBUG)
endif()
add_executable(cebola-verify src/verify.c)
target_link_libraries(cebola-verify PRIVATE cebola-core)
add_executable(CebolaRecompiled WIN32 src/app.c src/theme.c src/input.c src/preferences.c src/choice.c)
set_target_properties(CebolaRecompiled PROPERTIES OUTPUT_NAME "CebolaRecompiled-v0.10")
add_executable(cebola-theme-preview src/theme_preview.c src/theme.c)
target_include_directories(cebola-theme-preview PRIVATE ${ENGINE}/runner)
target_link_libraries(cebola-theme-preview PRIVATE user32 gdi32 ole32 windowscodecs uuid m msimg32)
target_compile_definitions(CebolaRecompiled PRIVATE UNICODE _UNICODE WINVER=0x0A00 _WIN32_WINNT=0x0A00)
target_link_libraries(CebolaRecompiled PRIVATE cebola-core SDL3::SDL3 user32 gdi32 comdlg32 shell32 winmm ole32 windowscodecs uuid comctl32 uxtheme msimg32)
add_executable(cebola-system-verify src/system_verify.c src/input.c src/preferences.c src/choice.c)
add_executable(cebola-interface-preview src/interface_preview.c src/input.c src/preferences.c src/choice.c)
target_include_directories(cebola-interface-preview PRIVATE ${ENGINE}/runner)
target_compile_definitions(cebola-interface-preview PRIVATE UNICODE _UNICODE)
target_link_libraries(cebola-interface-preview PRIVATE SDL3::SDL3 user32 gdi32 shell32 winmm ole32 windowscodecs uuid comctl32 uxtheme m)
target_compile_definitions(cebola-system-verify PRIVATE UNICODE _UNICODE)
target_link_libraries(cebola-system-verify PRIVATE SDL3::SDL3 user32 gdi32 shell32 winmm ole32 windowscodecs uuid comctl32 uxtheme msimg32)
add_custom_command(TARGET CebolaRecompiled POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:CebolaRecompiled>/assets"
  COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/assets" "$<TARGET_FILE_DIR:CebolaRecompiled>/assets"
  COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/LICENSES" "$<TARGET_FILE_DIR:CebolaRecompiled>/LICENSES"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE" "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE_SCOPE.md" "${CMAKE_CURRENT_SOURCE_DIR}/THIRD_PARTY.md" "$<TARGET_FILE_DIR:CebolaRecompiled>"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:SDL3::SDL3-shared>" "$<TARGET_FILE_DIR:CebolaRecompiled>")
if(MINGW)
  target_link_options(cebola-verify PRIVATE -municode)
  target_link_options(CebolaRecompiled PRIVATE -municode)
  target_link_options(cebola-theme-preview PRIVATE -municode)
  target_link_options(cebola-system-verify PRIVATE -municode)
  target_link_options(cebola-interface-preview PRIVATE -municode)
endif()
