cmake_minimum_required(VERSION 3.20)

project(qwfassoc LANGUAGES CXX)

# Qt 6 requires C++17 at minimum.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Let CMake auto-process Q_OBJECT, .ui files and .qrc resources.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# Make the bundled Findwfassoc.cmake module visible to find_package().
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# Locate third-party dependencies. The user is responsible for making these
# discoverable through the usual CMake mechanisms (CMAKE_PREFIX_PATH, etc.).
# LinguistTools is required to wire up Qt's translation pipeline
# (lupdate + lrelease) so that .ts files are kept up to date and .qm files
# are embedded as Qt resources.
find_package(Qt6 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(toml11 REQUIRED)
# Findwfassoc.cmake requires the wfassoc_ROOT variable to be set.
find_package(wfassoc REQUIRED)

set(QWFASSOC_SOURCES
    "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/main_window.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/manifest.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/icon_utils.cpp"
)

set(QWFASSOC_HEADERS
    "${CMAKE_CURRENT_SOURCE_DIR}/src/main_window.h"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/manifest.h"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/icon_utils.h"
)

set(QWFASSOC_UI
    "${CMAKE_CURRENT_SOURCE_DIR}/src/main_window.ui"
)

# WIN32 makes this a windowed application on Windows (no console window).
add_executable(qwfassoc WIN32
    ${QWFASSOC_SOURCES}
    ${QWFASSOC_HEADERS}
    ${QWFASSOC_UI}
)

target_include_directories(qwfassoc PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/src"
)

target_link_libraries(qwfassoc PRIVATE
    Qt6::Widgets
    toml11::toml11
    wfassoc::wfassoc
)

# Translation pipeline.
#
# qt6_add_translations() runs lupdate against the target's sources (keeping
# the .ts files below in sync) and then runs lrelease to compile them into
# .qm files. The .qm files are embedded under the ":/i18n" resource prefix,
# where installTranslators() in main.cpp looks them up at runtime.
#
# The .ts files are intentionally shipped empty: translators are expected to
# fill them in. Re-running cmake / building the target refreshes the entries
# found by lupdate without dropping already-translated ones.
set(QWFASSOC_TS_FILES
    "${CMAKE_CURRENT_SOURCE_DIR}/i18n/qwfassoc_zh_CN.ts"
)

qt6_add_translations(qwfassoc
    TS_FILES ${QWFASSOC_TS_FILES}
)
