cmake_minimum_required(VERSION 3.14)
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"
  "${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})

# 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")

# 源文件为 UTF-8：MSVC 默认按系统代码页（中文系统 GBK）解析源码，
# main.cpp 的中文宽字符串（窗口标题"资安信插件助手"）会乱码，必须显式声明
target_compile_options(${BINARY_NAME} PRIVATE "/utf-8")

# Add dependency libraries and include directories. Add any application-specific
# dependencies here.
target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app)
target_link_libraries(${BINARY_NAME} PRIVATE "dwmapi.lib")
target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}")

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

# HiTi PavoApi SDK（windows/vendor/）拷到可执行文件旁：
# Dart 侧 DynamicLibrary.open('PavoApiW_x64.dll') 按可执行文件目录查找；
# heatthcd.bin 为 SDK 运行数据，与其余 DLL 一并部署
add_custom_command(TARGET ${BINARY_NAME} POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
    "${CMAKE_CURRENT_SOURCE_DIR}/../vendor/PavoApiW_x64.dll"
    "${CMAKE_CURRENT_SOURCE_DIR}/../vendor/PavoCardSDK_x64.dll"
    "${CMAKE_CURRENT_SOURCE_DIR}/../vendor/HeatTB3.dll"
    "${CMAKE_CURRENT_SOURCE_DIR}/../vendor/heatthcd.bin"
    $<TARGET_FILE_DIR:${BINARY_NAME}>
)

# VC++ 运行库（windows/vendor/vcruntime/）：flutter_windows.dll 与 HiTi SDK 均依赖
# vcruntime140/msvcp140 等——开发机因装 VS 而有，干净电脑没有（安装后启动弹
# "找不到 VCRUNTIME140.dll"）。应用本地部署（exe 旁优先加载）为微软官方允许的分发方式。
# 目录内容从开发机 VS 的 VC\Redist\MSVC\*\x64\Microsoft.VC143.CRT 拷入（见 README）。
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../vendor/vcruntime")
  add_custom_command(TARGET ${BINARY_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
      "${CMAKE_CURRENT_SOURCE_DIR}/../vendor/vcruntime"
      $<TARGET_FILE_DIR:${BINARY_NAME}>
  )
endif()
