cmake_minimum_required(VERSION 3.10)
set(CMAKE_C_STANDARD 11)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Generate compile_commands.json for clangd" FORCE)
project(wlgol
    VERSION 0.0.1
    DESCRIPTION "Wayland Game Of Life"
    LANGUAGES C)

find_package(PkgConfig REQUIRED)
pkg_check_modules(XKBCOMMON REQUIRED xkbcommon)

# Download xdg-shell.xml
# file(DOWNLOAD "https://cgit.freedesktop.org/wayland/wayland-protocols/plain/stable/xdg-shell/xdg-shell.xml" "${CMAKE_CURRENT_BINARY_DIR}/xdg-shell.xml")

# Generation of xdg-shell.h and xdg-shell.c
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/include/xdg-shell.h
    COMMAND wayland-scanner client-header ${CMAKE_SOURCE_DIR}/xdg-shell.xml ${CMAKE_CURRENT_BINARY_DIR}/include/xdg-shell.h
    DEPENDS ${CMAKE_SOURCE_DIR}/xdg-shell.xml
)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell.c
    COMMAND wayland-scanner private-code ${CMAKE_SOURCE_DIR}/xdg-shell.xml ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell.c
    DEPENDS ${CMAKE_SOURCE_DIR}/xdg-shell.xml
)

# viewporter
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/include/viewporter.h
    COMMAND wayland-scanner client-header ${CMAKE_SOURCE_DIR}/viewporter.xml ${CMAKE_CURRENT_BINARY_DIR}/include/viewporter.h
    DEPENDS ${CMAKE_SOURCE_DIR}/viewporter.xml
)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/viewporter.c
    COMMAND wayland-scanner private-code ${CMAKE_SOURCE_DIR}/viewporter.xml ${CMAKE_CURRENT_BINARY_DIR}/viewporter.c
    DEPENDS ${CMAKE_SOURCE_DIR}/viewporter.xml
)

# fractional-scale-v1
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/include/fractional-scale-v1.h
    COMMAND wayland-scanner client-header ${CMAKE_SOURCE_DIR}/fractional-scale-v1.xml ${CMAKE_CURRENT_BINARY_DIR}/include/fractional-scale-v1.h
    DEPENDS ${CMAKE_SOURCE_DIR}/fractional-scale-v1.xml
)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/fractional-scale-v1.c
    COMMAND wayland-scanner private-code ${CMAKE_SOURCE_DIR}/fractional-scale-v1.xml ${CMAKE_CURRENT_BINARY_DIR}/fractional-scale-v1.c
    DEPENDS ${CMAKE_SOURCE_DIR}/fractional-scale-v1.xml
)

set(SOURCES
    tkeyboard.c
    tpointer.c
    toml.c
    config.c
    grid.c
    pattern.c
    ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell.c
    ${CMAKE_CURRENT_BINARY_DIR}/viewporter.c
    ${CMAKE_CURRENT_BINARY_DIR}/fractional-scale-v1.c
)

set(GENHEADERS
    ${CMAKE_CURRENT_BINARY_DIR}/include/xdg-shell.h
    ${CMAKE_CURRENT_BINARY_DIR}/include/viewporter.h
    ${CMAKE_CURRENT_BINARY_DIR}/include/fractional-scale-v1.h
)

add_executable(wlgol wlgol.c ${SOURCES} ${GENHEADERS})

target_include_directories(wlgol
    PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
    PUBLIC ${XKBCOMMON_INCLUDE_DIRS}
)

target_link_libraries(wlgol
    wayland-client
    wayland-cursor
    m
    ${XKBCOMMON_LIBRARIES}
)

target_compile_options(wlgol PRIVATE
    ${XKBCOMMON_CFLAGS_OTHER}
)



enable_testing()

add_executable(test_config tests/test_config.c
    ${SOURCES}
)
target_link_libraries(test_config
    wayland-client
    wayland-cursor
    ${XKBCOMMON_LIBRARIES}
)
target_include_directories(test_config
    PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
    PUBLIC ${XKBCOMMON_INCLUDE_DIRS}
)
target_compile_options(test_config PRIVATE
    ${XKBCOMMON_CFLAGS_OTHER}
)
add_test(NAME Config COMMAND test_config)

add_executable(test_grid tests/test_grid.c
    ${SOURCES}
)
target_link_libraries(test_grid
    wayland-client
    wayland-cursor
    m
    ${XKBCOMMON_LIBRARIES}
)
target_include_directories(test_grid
    PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
    PUBLIC ${XKBCOMMON_INCLUDE_DIRS}
)
target_compile_options(test_grid PRIVATE
    ${XKBCOMMON_CFLAGS_OTHER}
)
add_test(NAME Grid COMMAND test_grid)

add_executable(test_pattern tests/test_pattern.c
    ${SOURCES}
)
target_link_libraries(test_pattern
    wayland-client
    wayland-cursor
    m
    ${XKBCOMMON_LIBRARIES}
)
target_include_directories(test_pattern
    PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
    PUBLIC ${XKBCOMMON_INCLUDE_DIRS}
)
target_compile_options(test_pattern PRIVATE
    ${XKBCOMMON_CFLAGS_OTHER}
)
add_test(NAME Pattern COMMAND test_pattern)
