cmake_minimum_required(VERSION 3.21)

file(READ "${CMAKE_CURRENT_LIST_DIR}/../VERSION" LABELNOVA_VERSION_RAW)
string(STRIP "${LABELNOVA_VERSION_RAW}" LABELNOVA_VERSION)

project(labelnova VERSION "${LABELNOVA_VERSION}" LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets WebEngineWidgets WebChannel PrintSupport)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets WebEngineWidgets WebChannel PrintSupport)

set(APP_SOURCES
  src/main.cpp
  src/bridge.cpp
  src/bridge.h
  src/resources.qrc
)

if(QT_VERSION_MAJOR EQUAL 6)
  qt_add_executable(labelnova MANUAL_FINALIZATION ${APP_SOURCES})
else()
  add_executable(labelnova ${APP_SOURCES})
endif()

target_link_libraries(labelnova
  PRIVATE
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::WebEngineWidgets
    Qt${QT_VERSION_MAJOR}::WebChannel
    Qt${QT_VERSION_MAJOR}::PrintSupport
)

if(QT_VERSION_MAJOR EQUAL 5 AND UNIX)
  # Some UOS/Qt5 WebEngine repositories publish libQt5WebEngineCore without
  # the matching ffmpeg58 runtime libraries in the build root. Relaxing the
  # executable link step keeps the shell buildable while leaving runtime
  # resolution to the target system's Qt stack.
  target_link_options(labelnova PRIVATE "-Wl,--allow-shlib-undefined")
endif()

target_compile_definitions(labelnova PRIVATE
  QT_NO_KEYWORDS
  LABELNOVA_APP_VERSION="${PROJECT_VERSION}"
)

set(FRONTEND_ROOT "${CMAKE_CURRENT_LIST_DIR}/..")

# These install rules are the generic/local install layout; the deepin/UOS store package layout is staged in debian/rules.
install(TARGETS labelnova RUNTIME DESTINATION bin)
install(FILES "${FRONTEND_ROOT}/index.html" DESTINATION frontend)
install(DIRECTORY "${FRONTEND_ROOT}/static" DESTINATION frontend)
install(DIRECTORY "${FRONTEND_ROOT}/icons" DESTINATION frontend)

if(QT_VERSION_MAJOR EQUAL 6)
  qt_finalize_executable(labelnova)
endif()
