# The GTK layer, the mode-metrics-gtk package. Added after the Mode C core,
#   add_subdirectory(path/to/mode-metrics-c)
#   add_subdirectory(path/to/mode-metrics-gtk)
# One source, a library for each GTK found. mode_gtk4 for GTK 4, mode_gtk3 for GTK 3.
# The Linux layer both toolkits share, inside this package, or next to it in the repository.
if(NOT TARGET mode_linux)
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/linux)
    add_subdirectory(linux)
  else()
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../linux linux)
  endif()
endif()
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK4 IMPORTED_TARGET gtk4)
pkg_check_modules(GTK3 IMPORTED_TARGET gtk+-3.0)
if(NOT GTK4_FOUND AND NOT GTK3_FOUND)
  message(FATAL_ERROR "The GTK layer needs GTK 4 or GTK 3 (pkg-config gtk4 or gtk+-3.0)")
endif()

set(MODE_GTK_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/../tests/linux)
foreach(v 4 3)
  if(GTK${v}_FOUND)
    add_library(mode_gtk${v} STATIC mode_gtk.c)
    target_include_directories(mode_gtk${v} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
    target_link_libraries(mode_gtk${v} PUBLIC mode_linux PkgConfig::GTK${v})
    target_compile_options(mode_gtk${v} PRIVATE -Wall -Wextra -Werror -Wno-unused-parameter -Wno-deprecated-declarations)
    if(MODE_BUILD_TESTS AND EXISTS ${MODE_GTK_TESTS})
      add_executable(gtk${v}_unit ${MODE_GTK_TESTS}/gtk_unit.c)
      target_include_directories(gtk${v}_unit PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
      target_link_libraries(gtk${v}_unit PRIVATE mode_linux PkgConfig::GTK${v})
      target_compile_options(gtk${v}_unit PRIVATE -Wno-deprecated-declarations)
      add_executable(gtk${v}_app ${MODE_GTK_TESTS}/gtk_app.c)
      target_link_libraries(gtk${v}_app PRIVATE mode_gtk${v})
      target_compile_options(gtk${v}_app PRIVATE -Wno-deprecated-declarations)
      # Needs a display, run under Xvfb by tests/linux/run.sh.
      add_test(NAME gtk${v}_unit COMMAND gtk${v}_unit)
      set_tests_properties(gtk${v}_unit PROPERTIES LABELS display)
    endif()
  endif()
endforeach()
