CXX      ?= g++
CXXFLAGS ?= -O2 -Wall -Wextra -std=c++17
GTKFLAGS := $(shell pkg-config --cflags gtk+-3.0)
GTKLIBS  := $(shell pkg-config --libs gtk+-3.0)
# X11 + XFrame widgets for the unified canvas (src/xframe_canvas*.cpp):
# the canvas module includes <X11/...> and widgets/common/registry.h.
X11FLAGS := $(shell pkg-config --cflags x11)
X11LIBS  := $(shell pkg-config --libs x11)
WIDGETS_INC := -Iwidgets
# Headers the XFrame canvas core depends on (outside src/).
WIDGET_HDRS := widgets/common/registry.h widgets/common/ico.h widgets/core/xframe_core.h
NPROC    := $(shell nproc 2>/dev/null || echo 4)
# Short git commit, baked into the binary for Help -> About
# (LIGHTGUI_GIT_HASH); "unknown" when built outside a git checkout.
GIT_HASH := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)

# Parallel build, ON BY DEFAULT (standing requirement): the designer is
# compiled one .o per .cpp and linked, so a plain `make` / `make
# lightgui` / `make all` uses all NPROC cores with no -j flag to
# remember. SRCS is a wildcard, so a .cpp added to src/ later is
# compiled as its own .o automatically — no Makefile edit needed, and
# no future build silently falls back to a single core.
MAKEFLAGS += -j$(NPROC)

# LightGUI -- C++/GTK designer (src/) + XFrame widgets (widgets/).
#   make            build the lightgui binary and all widget demos
#   make lightgui   build ./lightgui only
#   make widgets    build widgets/build (all widget demos + palette + layout)
#   make install    install LightGUI system-wide (needs root): the
#                   XFrame widget headers to $(PREFIX)/include/lightgui
#                   so generated apps build, the designer binary to
#                   $(PREFIX)/bin, the UI + palette icons to
#                   $(PREFIX)/share/lightgui, and a desktop entry +
#                   icon so the app menu lists it. The widgets are
#                   header-only, so there are no shared libraries and
#                   no ldconfig step. Run from the designer via
#                   Tools -> Install Libraries…, which prompts for the
#                   root password (pkexec, sudo fallback).
#   make test       run the layout -> LightGUI seam test (no display needed)
#   make clean      remove built artefacts
#   ./ide           start the designer (single process)
.PHONY: all lightgui widgets install test clean

# Install locations. The widgets are header-only (common/ + core/ +
# components/), so `make install` just copies the headers; generated
# apps then build with -I$(PREFIX)/include/lightgui (see
# src/generator.cpp). The designer binary, its Glade UI and the
# palette .ico files go alongside so an installed `lightgui` runs
# anywhere without the checkout.
PREFIX ?= /usr/local
INCLUDEDIR ?= $(PREFIX)/include/lightgui
BINDIR ?= $(PREFIX)/bin
SHAREDIR ?= $(PREFIX)/share/lightgui

SRCS := $(wildcard src/*.cpp)
OBJS := $(SRCS:.cpp=.o)
# Headers the .o files depend on (any header change rebuilds them all;
# cheap, and it keeps the rule simple). The generated embedded headers
# are built by their own rules below when missing.
HDRS := $(wildcard src/*.h) ui/designer.ui
GENHDRS := src/indicators_embedded.h src/drawings_embedded.h

all: lightgui widgets

lightgui: $(OBJS) $(GENHDRS)
	$(CXX) $(CXXFLAGS) -DLIGHTGUI_GIT_HASH='"$(GIT_HASH)"' -o $@ $(OBJS) $(GTKFLAGS) $(GTKLIBS) $(X11LIBS)

# One .o per .cpp (pattern rule — parallelises under -j).
src/%.o: src/%.cpp $(HDRS) $(GENHDRS) $(WIDGET_HDRS)
	$(CXX) $(CXXFLAGS) -DLIGHTGUI_GIT_HASH='"$(GIT_HASH)"' -c $< -o $@ $(GTKFLAGS) $(X11FLAGS) $(WIDGETS_INC)

src/indicators_embedded.h: src/indicators.h tools/embed_indicators.sh
	sh tools/embed_indicators.sh src/indicators.h $@

src/drawings_embedded.h: src/drawings.h tools/embed_drawings.sh
	sh tools/embed_drawings.sh src/drawings.h $@

widgets:
	cmake -S widgets -B widgets/build
	# Clear MAKEFLAGS for the submake: the top-level -j in MAKEFLAGS
	# would otherwise make it warn "-j forced in submake: resetting
	# jobserver mode". cmake still gets -j explicitly, so the widgets
	# still build on all cores.
	MAKEFLAGS= cmake --build widgets/build -j$(NPROC)

install: lightgui
	@echo "Installing LightGUI widget headers to $(INCLUDEDIR)"
	@mkdir -p $(INCLUDEDIR)
	@cd widgets && find common core components -name '*.h' | while read f; do \
		install -D -m 644 "$$f" "$(INCLUDEDIR)/$$f"; \
	done
	@echo "Installed $$(find widgets/common widgets/core widgets/components -name '*.h' | wc -l) headers to $(INCLUDEDIR)"
	@echo "Installing designer binary to $(BINDIR)"
	@mkdir -p $(BINDIR)
	@install -m 755 lightgui $(BINDIR)/lightgui
	@echo "Installing designer UI + palette icons to $(SHAREDIR)"
	@mkdir -p $(SHAREDIR)
	@install -D -m 644 ui/designer.ui $(SHAREDIR)/designer.ui
	@mkdir -p $(SHAREDIR)/icons
	@cd widgets/icons && find . -name '*.ico' | while read f; do \
		install -D -m 644 "$$f" "$(SHAREDIR)/icons/$$f"; \
	done
	@echo "Installed $$(find widgets/icons -name '*.ico' | wc -l) palette icons to $(SHAREDIR)/icons"
	@echo "Installing desktop entry + icon"
	@mkdir -p $(PREFIX)/share/applications $(PREFIX)/share/icons/hicolor/512x512/apps
	@install -D -m 644 lightgui.desktop $(PREFIX)/share/applications/lightgui.desktop
	@install -D -m 644 flatpak/icon.png $(PREFIX)/share/icons/hicolor/512x512/apps/lightgui.png
	@-update-desktop-database $(PREFIX)/share/applications 2>/dev/null; true
	@-gtk-update-icon-cache -q $(PREFIX)/share/icons/hicolor 2>/dev/null; true
	@echo "Installed LightGUI: binary $(BINDIR)/lightgui, headers $(INCLUDEDIR), data $(SHAREDIR)"

test:
	python3 widgets/tests/test_layout_to_lightgui.py

clean:
	rm -f lightgui $(OBJS)
	rm -rf widgets/build

# Visual regression: re-render all 547 widget examples under Xvfb, diff
# each screenshot against tools/visreg/golden/ (ImageMagick RMSE, 0.5%
# threshold), write tools/visreg/report/report.txt. Exit code 0 only
# when every widget PASSes. See tools/visreg/README.md.
visreg:
	tools/visreg/run.sh
