
NAME           = nkk
MAJOR          = 0
MINOR          = 1
PREFIX        ?= /usr
PROTOCOLS      = layer-shell xdg-shell
MODULES        = nkk_display nkk_egl_window nkk_layer_window nkk_printf nkk_sub_window nkk_window nkk_xdg_window
DEPS           = egl pangocairo wayland-client wayland-cursor xkbcommon
CFLAGS        += -fPIC -g -Wall -Werror
CFLAGS        += $(shell pkgconf --cflags $(DEPS))
LDLIBS        += $(shell pkgconf --libs $(DEPS))
WSCANNER       = $(shell pkgconf --variable=wayland_scanner wayland-scanner)
VPATH          = src:protocols

all: lib$(NAME).so.$(MAJOR) nkk.pc

lib$(NAME).so.$(MAJOR): $(PROTOCOLS:%=%.o) $(MODULES:%=%.o)
	$(CC) -shared -Wl,-soname,lib$(NAME).so.$(MAJOR) -o $@ $^ $(LDFLAGS) $(LDLIBS)

layer-shell.c: wlr-layer-shell-unstable-v1.xml
	$(WSCANNER) private-code $^ $@
	$(WSCANNER) client-header $^ layer-shell.h

xdg-shell.c: protocols/xdg-shell.xml
	$(WSCANNER) private-code $^ $@
	$(WSCANNER) client-header $^ xdg-shell.h

nkk.pc: nkk.pc.in
	sed 's|PREFIX|$(PREFIX)|' $^ > $@

install:
	install -Dm755 lib$(NAME).so.$(MAJOR) $(DESTDIR)$(PREFIX)/lib/lib$(NAME).so.$(MAJOR)
	ln -sf lib$(NAME).so.$(MAJOR) $(DESTDIR)$(PREFIX)/lib/lib$(NAME).so
	install -Dm644 LICENSE $(DESTDIR)$(PREFIX)/share/licenses/$(NAME)/LICENSE
	install -Dm644 src/nkk.h $(DESTDIR)$(PREFIX)/include/nkk.h
	install -Dm644 nkk.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig/nkk.pc
	install -Dm644 man/nkk_display.3 $(DESTDIR)$(PREFIX)/share/man/man3/nkk_display.3
	install -Dm644 man/nkk_window.3 $(DESTDIR)$(PREFIX)/share/man/man3/nkk_window.3
	install -Dm644 man/nkk_printf.3 $(DESTDIR)$(PREFIX)/share/man/man3/nkk_printf.3

clean:
	rm -f *.o layer-shell.* xdg-shell.*
	$(MAKE) -C examples clean

distclean: clean
	rm -f lib$(NAME).so* nkk.pc

.PHONY: clean distclean install
