# Default value so one can compile i3-input standalone
TOPDIR=..

include $(TOPDIR)/common.mk

# Depend on the object files of all source-files in src/*.c and on all header files
AUTOGENERATED:=cfgparse.tab.c cfgparse.yy.c
FILES:=$(patsubst %.c,%.o,$(filter-out $(AUTOGENERATED),$(wildcard *.c)))
HEADERS:=$(wildcard *.h)

CPPFLAGS += -I$(TOPDIR)/include

# Depend on the specific file (.c for each .o) and on all headers
%.o: %.c ${HEADERS}
	echo "[i3-config-wizard] CC $<"
	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

all: i3-config-wizard

i3-config-wizard: $(TOPDIR)/libi3/libi3.a cfgparse.y.o cfgparse.yy.o ${FILES}
	echo "[i3-config-wizard] LINK i3-config-wizard"
	$(CC) $(LDFLAGS) -o $@ $(filter-out libi3/libi3.a,$^) $(LIBS)

$(TOPDIR)/libi3/%.a: $(TOPDIR)/libi3/*.c
	$(MAKE) -C $(TOPDIR)/libi3

cfgparse.yy.o: cfgparse.l cfgparse.y.o ${HEADERS}
	echo "[i3-config-wizard] LEX $<"
	flex -i -o$(@:.o=.c) $<
	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(@:.o=.c)

cfgparse.y.o: cfgparse.y ${HEADERS}
	echo "[i3-config-wizard] YACC $<"
	bison --debug --verbose -b $(basename $< .y) -d $<
	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(<:.y=.tab.c)


install: all
	echo "[i3-config-wizard] INSTALL"
	$(INSTALL) -d -m 0755 $(DESTDIR)$(PREFIX)/bin
	$(INSTALL) -m 0755 i3-config-wizard $(DESTDIR)$(PREFIX)/bin/

clean:
	rm -f *.o cfgparse.tab.{c,h} cfgparse.output cfgparse.yy.c

distclean: clean
	rm -f i3-config-wizard
