cmake_minimum_required(VERSION 3.27)

# PLUGIN_VERSION lives in .env (single source of truth - also read by
# flake.nix) so .gitea/workflows/version-bump.yml only ever has to touch one
# file, rather than a "VERSION X.Y" grep/sed that can't tell this project's
# own version apart from cmake_minimum_required's version above.
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/.env" HYPR_CHROME_ENV_LINES)
foreach(HYPR_CHROME_ENV_LINE IN LISTS HYPR_CHROME_ENV_LINES)
    if(HYPR_CHROME_ENV_LINE MATCHES "^PLUGIN_VERSION=(.*)$")
        set(HYPR_CHROME_PLUGIN_VERSION "${CMAKE_MATCH_1}")
    endif()
endforeach()

project(hypr-chrome
    DESCRIPTION "Draws a chamfered HUD-style border frame with a title bar around each window."
    VERSION ${HYPR_CHROME_PLUGIN_VERSION}
)

# Match the Makefile's prior default (CXXFLAGS ?= -O2) so a bare `cmake -B
# build` still produces an optimized build; pass -DCMAKE_BUILD_TYPE=Debug to
# override.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Lets clangd discover build/compile_commands.json automatically (it checks
# common build-dir names on its own) - no hand-rolled compile_flags.txt
# needed.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(PkgConfig REQUIRED)
pkg_check_modules(deps REQUIRED IMPORTED_TARGET
    hyprland
    libdrm
    libinput
    libudev
    pixman-1
    wayland-server
    xkbcommon
)

add_library(hypr-chrome SHARED
    src/Main.cpp
    src/ChromeDecoration.cpp
    src/ChromePassElement.cpp
)

target_link_libraries(hypr-chrome PRIVATE rt PkgConfig::deps)

set_target_properties(hypr-chrome PROPERTIES
    PREFIX ""
    POSITION_INDEPENDENT_CODE ON
    # hyprctl plugin load/buildAndLoad.sh both expect the built .so at the
    # repo root, not buried in the build tree.
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}
)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    # GCC emits STB_GNU_UNIQUE bindings for vague-linkage symbols (header
    # inline/static globals) by default; those break repeated dlopen/dlclose
    # of the same symbol names, which is exactly what reloading this plugin
    # does. Clang doesn't emit them and doesn't support this flag.
    target_compile_options(hypr-chrome PRIVATE --no-gnu-unique)
endif()

install(TARGETS hypr-chrome)
