cmake_minimum_required(VERSION 3.16)
project(runner LANGUAGES CXX)

# Define the application target. To change its name, change BINARY_NAME in the
# top-level CMakeLists.txt, not the value here, or `flutter run` will no longer
# work.
#
# Any new source files that you add to the application should be added here.
add_executable(${BINARY_NAME} WIN32
  "flutter_window.cpp"
  "main.cpp"
  "utils.cpp"
  "win32_window.cpp"
  "mpv/display_mode_manager.cpp"
  "mpv/mpv_player.cpp"
  "mpv/mpv_plugin.cpp"
  "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
  "Runner.rc"
  "runner.exe.manifest"
)

# Apply the standard set of build settings. This can be removed for applications
# that need different build settings.
apply_standard_settings(${BINARY_NAME})

# Setup precompiled header
target_precompile_headers(${BINARY_NAME} PRIVATE "pch.h")

# Add preprocessor definitions for the build version.
target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION=\"${FLUTTER_VERSION}\"")
target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_MAJOR=${FLUTTER_VERSION_MAJOR}")
target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_MINOR=${FLUTTER_VERSION_MINOR}")
target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_PATCH=${FLUTTER_VERSION_PATCH}")
target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_BUILD=${FLUTTER_VERSION_BUILD}")

# Disable Windows macros that collide with C++ standard library functions.
target_compile_definitions(${BINARY_NAME} PRIVATE "NOMINMAX")

# Build simdutf as a static library from the single-header amalgamation.
add_library(simdutf STATIC "${simdutf_SOURCE_DIR}/simdutf.cpp")
target_include_directories(simdutf PUBLIC "${simdutf_SOURCE_DIR}")
target_compile_features(simdutf PUBLIC cxx_std_17)
# Suppress warnings in third-party code
target_compile_options(simdutf PRIVATE /W0)

# Add dependency libraries and include directories. Add any application-specific
# dependencies here.
target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app flutter_wrapper_plugin)
target_link_libraries(${BINARY_NAME} PRIVATE "dwmapi.lib" "comctl32.lib")
target_link_libraries(${BINARY_NAME} PRIVATE "${MPV_LIB_DIR}/libmpv.dll.a")
target_link_libraries(${BINARY_NAME} PRIVATE simdutf)
target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}")
target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(${BINARY_NAME} PRIVATE "${MPV_INCLUDE_DIR}")
target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/../shared/cpp")

# Run the Flutter tool portions of the build. This must not be removed.
add_dependencies(${BINARY_NAME} flutter_assemble)

option(PLEZY_BUILD_MPV_PROPERTY_CONTRACT_TESTS
  "Build the focused Windows mpv property-result contract tests" OFF)
if(PLEZY_BUILD_MPV_PROPERTY_CONTRACT_TESTS)
  enable_testing()

  add_executable(mpv_property_result_contract_test
    "../../shared/mpv/mpv_player_common_test.cpp"
  )
  apply_standard_settings(mpv_property_result_contract_test)
  target_link_libraries(mpv_property_result_contract_test PRIVATE "${MPV_LIB_DIR}/libmpv.dll.a")
  target_include_directories(mpv_property_result_contract_test PRIVATE "${MPV_INCLUDE_DIR}")

  add_executable(mpv_player_property_contract_test
    "mpv/mpv_player.cpp"
    "mpv/mpv_player_property_contract_test.cpp"
  )
  apply_standard_settings(mpv_player_property_contract_test)
  target_compile_definitions(mpv_player_property_contract_test PRIVATE "NOMINMAX")
  target_link_libraries(
    mpv_player_property_contract_test
    PRIVATE flutter "${MPV_LIB_DIR}/libmpv.dll.a" simdutf "user32.lib"
  )
  target_include_directories(
    mpv_player_property_contract_test
    PRIVATE
      "${CMAKE_SOURCE_DIR}"
      "${CMAKE_CURRENT_SOURCE_DIR}"
      "${MPV_INCLUDE_DIR}"
      "${CMAKE_SOURCE_DIR}/../shared/cpp"
  )

  foreach(test_target IN ITEMS mpv_property_result_contract_test mpv_player_property_contract_test)
    add_custom_command(
      TARGET ${test_target}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${MPV_LIB_DIR}/libmpv-2.dll"
        "$<TARGET_FILE_DIR:${test_target}>"
    )
  endforeach()

  add_test(NAME mpv_property_result_contract_test COMMAND mpv_property_result_contract_test)
  add_test(NAME mpv_player_property_contract_test COMMAND mpv_player_property_contract_test)
endif()
