# Project-level configuration.
cmake_minimum_required(VERSION 3.13)
project(runner LANGUAGES CXX)

# The name of the executable created for the application. Change this to change
# the on-disk name of your application.
set(BINARY_NAME "lotti")
# The unique GTK application identifier for this application. See:
# https://wiki.gnome.org/HowDoI/ChooseApplicationID
set(APPLICATION_ID "com.matthiasn.lotti")

# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
# versions of CMake.
cmake_policy(SET CMP0063 NEW)

# Load bundled libraries from the lib/ directory relative to the binary.
set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")

# Root filesystem for cross-building.
if(FLUTTER_TARGET_PLATFORM_SYSROOT)
  set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT})
  set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})
  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
endif()

# Define build configuration options.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE "Debug" CACHE
    STRING "Flutter build mode" FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
    "Debug" "Profile" "Release")
endif()

# Newer clang warns about the `operator"" _x` spelling in third-party plugin
# headers, which -Werror would turn into a build failure. Older clang (14 on
# Ubuntu 22.04, where the AppImage is built) does not know the flag and, with
# -Werror, rejects the flag itself instead, so apply it only where it exists.
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-Wno-deprecated-literal-operator
  CXX_SUPPORTS_WNO_DEPRECATED_LITERAL_OPERATOR)

# Compilation settings that should be applied to most targets.
#
# Be cautious about adding new options here, as plugins use this function by
# default. In most cases, you should add new options to specific targets instead
# of modifying this function.
function(APPLY_STANDARD_SETTINGS TARGET)
  target_compile_features(${TARGET} PUBLIC cxx_std_14)
  target_compile_options(${TARGET} PRIVATE -Wall -Werror)
  target_compile_options(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O3>")
  if(CXX_SUPPORTS_WNO_DEPRECATED_LITERAL_OPERATOR)
    target_compile_options(${TARGET} PRIVATE -Wno-deprecated-literal-operator)
  endif()
  target_compile_definitions(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:NDEBUG>")
  target_compile_definitions(${TARGET} PRIVATE -DAPPLICATION_ID="${APPLICATION_ID}")
endfunction()

# Flutter library and tool build rules.
set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")
add_subdirectory(${FLUTTER_MANAGED_DIR})

# System-level dependencies.
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)

# Application build; see runner/CMakeLists.txt.
add_subdirectory("runner")

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

# Only the install-generated bundle's copy of the executable will launch
# correctly, since the resources must in the right relative locations. To avoid
# people trying to run the unbundled copy, put it in a subdirectory instead of
# the default top-level location.
set_target_properties(${BINARY_NAME}
  PROPERTIES
  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run"
)


# Generated plugin build rules, which manage building the plugins and adding
# them to the application.
# flutter_lame bundles LAME with an upstream CMake 3.0 declaration. Preserve
# the caller's deprecation setting for Lotti itself, but suppress that external
# compatibility warning while configuring generated plugin subdirectories.
get_property(_LOTTI_CMAKE_WARN_DEPRECATED_WAS_SET
  CACHE CMAKE_WARN_DEPRECATED PROPERTY TYPE SET)
if(_LOTTI_CMAKE_WARN_DEPRECATED_WAS_SET)
  get_property(_LOTTI_CMAKE_WARN_DEPRECATED
    CACHE CMAKE_WARN_DEPRECATED PROPERTY VALUE)
endif()
set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "" FORCE)
# Match sherpa_onnx 1.13.4's runtime when flutter_onnxruntime needs headers.
set(ONNXRUNTIME_VERSION "1.27.0" CACHE STRING "Shared speech runtime" FORCE)
# CMake 4 refuses a project that asks for a policy version below 3.5, and
# the lame sources bundled by flutter_lame still ask for 3.0. This floor is
# what the plugins' own cmake_minimum_required reads, so the build no
# longer depends on which CMake the host happens to carry (the hosted
# runner pool mixes 3.x and 4.x images).
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
include(flutter/generated_plugins.cmake)

# Flutter copies plugin files rather than installing their targets. Avoid
# retaining a build-machine runtime path in the shipped TTS plugin.
set_target_properties(flutter_onnxruntime_plugin PROPERTIES
  INSTALL_RPATH "$ORIGIN")
if(_LOTTI_CMAKE_WARN_DEPRECATED_WAS_SET)
  set(CMAKE_WARN_DEPRECATED "${_LOTTI_CMAKE_WARN_DEPRECATED}"
    CACHE BOOL "" FORCE)
else()
  unset(CMAKE_WARN_DEPRECATED CACHE)
endif()


# === Installation ===
# By default, "installing" just makes a relocatable bundle in the build
# directory.
set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle")
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
endif()

# Start with a clean build bundle directory every time.
install(CODE "
  file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\")
  " COMPONENT Runtime)

set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")

install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
  COMPONENT Runtime)

install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
  COMPONENT Runtime)

install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
  COMPONENT Runtime)

# Both speech plugins bundle libonnxruntime.so. Keep sherpa's pinned copy as
# the single runtime, rather than relying on install order to overwrite TTS's
# copy. ONNX Runtime retains the older C API used by flutter_onnxruntime.
foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES})
  if(bundled_library STREQUAL "$<TARGET_FILE:flutter_onnxruntime_plugin>")
    # Installing the target rewrites its runtime search path for this bundle.
    install(TARGETS flutter_onnxruntime_plugin
      LIBRARY DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime)
    continue()
  endif()
  if(bundled_library MATCHES "/libonnxruntime\\.so$" AND
     NOT bundled_library MATCHES "/sherpa_onnx_linux/")
    continue()
  endif()
  install(FILES "${bundled_library}"
    DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
    COMPONENT Runtime)
endforeach(bundled_library)

# The official TTS dependency uses SONAME libonnxruntime.so.1, whereas
# sherpa's prebuilt library uses libonnxruntime.so. Both resolve to one file.
install(CODE "
  file(CREATE_LINK libonnxruntime.so
    \"${INSTALL_BUNDLE_LIB_DIR}/libonnxruntime.so.1\" SYMBOLIC)
" COMPONENT Runtime)

# Copy the native assets provided by the build.dart from all packages.
set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/")
install(DIRECTORY "${NATIVE_ASSETS_DIR}"
   DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
   COMPONENT Runtime)

# Fully re-copy the assets directory on each build to avoid having stale files
# from a previous install.
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
install(CODE "
  file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
  " COMPONENT Runtime)
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
  DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)

# Install the AOT library on non-Debug builds only.
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
  install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
    COMPONENT Runtime)
endif()

# Desktop integration files
set(DESKTOP_FILE "${CMAKE_CURRENT_SOURCE_DIR}/com.matthiasn.lotti.desktop")
set(ICON_DIR "${CMAKE_CURRENT_SOURCE_DIR}/icons")

# Validate required files exist before installation
if(NOT EXISTS "${DESKTOP_FILE}")
  message(WARNING "Desktop file not found: ${DESKTOP_FILE}")
endif()

if(NOT EXISTS "${ICON_DIR}")
  message(WARNING "Icon directory not found: ${ICON_DIR}")
endif()

# Install desktop file for desktop integration
# Note: This installs to the bundle directory for development.
# For system-wide installation, these would go to /usr/share/applications and /usr/share/icons
if(EXISTS "${DESKTOP_FILE}")
  install(FILES "${DESKTOP_FILE}"
    DESTINATION "${CMAKE_INSTALL_PREFIX}/share/applications"
    COMPONENT Runtime)
else()
  message(WARNING "Skipping desktop file installation - file not found")
endif()

# Install icons in various sizes for desktop integration
if(EXISTS "${ICON_DIR}")
  file(GLOB_RECURSE ICON_FILES "${ICON_DIR}/hicolor/*/apps/*.png")
  
  if(ICON_FILES)
    foreach(ICON_FILE ${ICON_FILES})
      # Extract the relative path from the icons directory
      file(RELATIVE_PATH ICON_RELATIVE_PATH "${ICON_DIR}" "${ICON_FILE}")
      get_filename_component(ICON_RELATIVE_DIR "${ICON_RELATIVE_PATH}" DIRECTORY)
      install(FILES "${ICON_FILE}"
        DESTINATION "${CMAKE_INSTALL_PREFIX}/share/icons/${ICON_RELATIVE_DIR}"
        COMPONENT Runtime)
    endforeach()
    
    # Copy icons to assets directory for runtime access
    install(DIRECTORY "${ICON_DIR}/"
      DESTINATION "${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}/assets/icons_linux"
      COMPONENT Runtime)
  else()
    message(WARNING "No icon files found in ${ICON_DIR}")
  endif()
else()
  message(WARNING "Skipping icon installation - directory not found")
endif()
