cmake_minimum_required(VERSION 3.20)
project(touchsensor VERSION 2.0.0 LANGUAGES CXX)

option(BUILD_EXAMPLE "Build the cpstream demo executable" ON)
set(QT_VERSION Qt6 CACHE STRING "Qt major version to use")

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE "Release")

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

add_compile_options(-Os -O3)

list(APPEND CMAKE_PREFIX_PATH
  "C:/msys64/mingw64/include"
)

find_package(${QT_VERSION} REQUIRED COMPONENTS Widgets Network PrintSupport)
find_package(Eigen3 REQUIRED)

qt_standard_project_setup()

file(
  GLOB_RECURSE MODERN_QT_SOURCES
  CONFIGURE_DEPENDS
  "modern-qt/*.cc"
)
set(MODERN_QT_HEADERS
  modern-qt/widget/select.hh
  modern-qt/widget/select.impl.hh
  modern-qt/widget/sliders.hh
)
add_library(modern-qt SHARED ${MODERN_QT_SOURCES} ${MODERN_QT_HEADERS})
target_include_directories(modern-qt PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(modern-qt
  PUBLIC
    ${QT_VERSION}::Widgets
    ${QT_VERSION}::Network
    Eigen3::Eigen
)

file(
  GLOB_RECURSE QCUSTOMPLOT_SOURCES
  CONFIGURE_DEPENDS
  "qcustomplot/*.cpp"
  "qcustomplot/*.h"
)
add_library(qcustomplot SHARED ${QCUSTOMPLOT_SOURCES})
target_include_directories(qcustomplot PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(qcustomplot
  PUBLIC
    ${QT_VERSION}::Core
    ${QT_VERSION}::Gui
    ${QT_VERSION}::PrintSupport
)

file(
  GLOB_RECURSE COMPONENT_SOURCES
  CONFIGURE_DEPENDS
  "components/*.cc"
)
file(
  GLOB_RECURSE UTILITY_SOURCES
  CONFIGURE_DEPENDS
  "dlog/*.cc"
)

set(FFMSEP_SOURCES
  components/ffmsep/cpdecoder.cc
  components/ffmsep/cpstream_core.cc
  components/ffmsep/tactile/tacdec.cc
)
set(FFMSEP_HEADERS
  components/ffmsep/cpdecoder.hh
  components/ffmsep/cpstream_core.hh
  components/ffmsep/tactile/tacdec.hh
)
set(FFMSEP_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/components/ffmsep")

set(TOUCHSENSOR_HEADERS
  component.hh
  components/charts/heatmap.hh
  components/charts/heatmap.impl.hh
  dlog/dlog.hh
  ${FFMSEP_HEADERS}
)

qt6_add_resources(APP_RESOURCES resources.qrc)

add_executable(${PROJECT_NAME}
  ${COMPONENT_SOURCES}
  ${UTILITY_SOURCES}
  ${TOUCHSENSOR_HEADERS}
  main.cc
)
target_sources(${PROJECT_NAME} PRIVATE ${APP_RESOURCES})
target_include_directories(${PROJECT_NAME}
  PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${FFMSEP_INCLUDE_DIR}
)
target_link_libraries(${PROJECT_NAME}
  PRIVATE
    ${QT_VERSION}::Widgets
    ${QT_VERSION}::Network
    modern-qt
    qcustomplot
    serial
    setupapi
    spdlog
)

if(BUILD_EXAMPLE)
  add_executable(cpstream_demo
    examples/cpstream_demo.cc
    ${FFMSEP_SOURCES}
  )
  target_include_directories(cpstream_demo
    PRIVATE
      ${CMAKE_CURRENT_SOURCE_DIR}
      ${FFMSEP_INCLUDE_DIR}
  )
  target_link_libraries(cpstream_demo PRIVATE serial)
  target_link_libraries(cpstream_demo PRIVATE setupapi)
endif()
