.PHONY: all clean build zip

API := x11

CFLAGS := -MD -Wall -g -lm -lpthread -ldl -I ./inc/

ifeq ($(API), arm)	
	CC := arm-cortexa9_neon-linux-gnueabihf-gcc
	CFLAGS := $(CFLAGS) -D LV_TARGET_FB
else
	CC := gcc
	CFLAGS := $(CFLAGS) -lX11
endif

TARGETS := pm3-lvgl cvend audio_test
EXES := $(patsubst %,bin/%,$(TARGETS))
SRCS := $(shell find src inc -type f -iname '*.c')
OBJS := $(patsubst src/%.c,obj/%.o,$(patsubst inc/%.c,obj/lib/%.o,$(SRCS)))
NO_BUILD := $(patsubst %,obj/%.o,$(TARGETS))

all: build

build: $(EXES)

$(EXES): bin/%: obj/%.o $(filter-out $(NO_BUILD), $(OBJS))
	@mkdir -p $(dir $@)
	@echo "Building $@"
	@$(CC) $(CFLAGS) -o $@ $^

obj/%.o: src/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -o $@ -c $<

obj/lib/%.o: inc/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -o $@ -c $<

clean:
	rm -rf bin
	rm -rf obj
	rm -rf .artifacts
	rm -rf $(PROJ).zip

-include $(OBJS:.o=.d)
