cmake_minimum_required(VERSION 3.14)
project(runner LANGUAGES CXX)

# Native Ninja builds reuse Flutter's generated configuration. Read the release
# identity from pubspec on every configure so PackageInfo cannot stay on an old version.
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/../../pubspec.yaml" HAO_VERSION_LINE REGEX "^version: ")
if(NOT HAO_VERSION_LINE MATCHES "^version: ([0-9]+)\\.([0-9]+)\\.([0-9]+)\\+([0-9]+)$")
  message(FATAL_ERROR "Hao release version must include a numeric build number")
endif()
set(FLUTTER_VERSION_MAJOR "${CMAKE_MATCH_1}")
set(FLUTTER_VERSION_MINOR "${CMAKE_MATCH_2}")
set(FLUTTER_VERSION_PATCH "${CMAKE_MATCH_3}")
set(FLUTTER_VERSION_BUILD "${CMAKE_MATCH_4}")
set(FLUTTER_VERSION "${FLUTTER_VERSION_MAJOR}.${FLUTTER_VERSION_MINOR}.${FLUTTER_VERSION_PATCH}+${FLUTTER_VERSION_BUILD}")

# Ninja/MSVC does not track ICON file includes inside RC scripts reliably.
# Recompile the embedded resources when the approved app icon changes.
set_source_files_properties("Runner.rc" PROPERTIES
  OBJECT_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/resources/app_icon.ico"
)

# 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"
  "hao_float.cpp"
  "hao_thumbnail.cpp"
  "main.cpp"
  "utils.cpp"
  "win32_window.cpp"
  "winrt_ext.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})

# winrt_ext.cpp uses C++/WinRT, which needs C++20 for the standard <coroutine>
# header; under C++17 it falls back to <experimental/coroutine>, which recent
# MSVC STLs reject with error STL1011.
target_compile_features(${BINARY_NAME} PRIVATE cxx_std_20)

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

# 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_link_libraries(${BINARY_NAME} PRIVATE "gdiplus.lib" "shell32.lib" "ole32.lib")
# C++/WinRT (winrt_ext.cpp): newer Windows SDKs require linking the WinRT
# activation functions (RoGetActivationFactory etc.) explicitly.
target_link_libraries(${BINARY_NAME} PRIVATE "runtimeobject.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)
