TARGET  = mgos_i2c
CC      = gcc
# CFLAGS  = -g -O -Wall -Wextra -std=c89 -pedantic -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
CFLAGS  = -g -O2 -pedantic -Werror -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
LINKER  = gcc
LFLAGS  = -O -Wall -I. -lm

.PHONY: default all clean

default: autogen $(TARGET)
all: default

SRCDIR  = src
INCDIR  = include
OBJDIR  = build
LIBDIR  = libs
TESTDIR = tests
BINDIR  = .

SRCS     := $(shell (echo src/tests_autogen.c; find -L $(SRCDIR) $(LIBDIR) $(TESTDIR) -type f -name '*.c' | egrep -v 'deps/|build/') | sort | uniq )
INCS     := $(shell find -L $(SRCDIR) $(LIBDIR) $(TESTDIR) $(INCDIR) -type d -name 'include'| egrep -v 'deps/|build/' )
INCFLAGS := $(patsubst %,-I %, $(INCS))
OBJS     := $(patsubst %.c, build/%.o, $(SRCS))
RM       = rm -f
RMDIR    = rm -r -f

$(BINDIR)/$(TARGET): $(OBJS)
	$(LINKER) $(OBJS) $(LFLAGS) -o $@

$(OBJS): $(OBJDIR)/%.o : %.c
	@mkdir -p $(shell dirname $(OBJS))
	$(CC) $(CFLAGS) $(INCFLAGS) -c $< -o $@

.PHONY: clean
clean:
	$(RMDIR) $(OBJDIR)
	$(RM) $(BINDIR)/$(TARGET)
	$(RM) $(INCDIR)/tests_autogen.h
	$(RM) $(SRCDIR)/tests_autogen.c

.PHONY: autogen
autogen:
	@./autogen.sh
