include ../platform.mk

ifndef BUILD_DIR
BUILD_DIR = build/$(BUILD_PLATFORM)
endif

ifeq ($(CC),cc)
CC = gcc
LD = $(CC)
endif

CFLAGS = -Wall -O2 \
	-I../library \
	-I../library/libtenki/src \
	-DENABLE_ALL_CHIPS \

LDFLAGS = -lm

ifeq ($(BUILD_TARGET_OS), Windows)
ifndef LIBUSB_DIR
LIBUSB_DIR = ../../libusb
endif
CFLAGS += -DWIN32_LEAN_AND_MEAN -I$(LIBUSB_DIR)/include
LDFLAGS += -L$(LIBUSB_DIR)/$(BUILD_PLATFORM) -lusb-1.0 -lws2_32 -liphlpapi
EXE = .exe
else
CFLAGS += `pkg-config libusb-1.0 --cflags`
LDFLAGS += `pkg-config libusb-1.0 --libs`
endif

APPS = dracal-usb-get dracal-usb-set dracal-usb-cal usbtenkiget usbtenkiset usbtenkical \
	dracal-sensgate-get dracal-sensgate-log dracal-sensgate-logtool dracal-sensgate-scan

# Objects and targets

VPATH = ../library:../library/libtenki/src

OBJDIR = $(BUILD_DIR)/obj
BINDIR = bin

APP_OBJS = $(APPS:%=%.o)
OBJS = $(filter-out $(APP_OBJS), $(patsubst %.c, %.o, $(wildcard *.c)))
OBJS += $(patsubst ../library/%.c, %.o, $(wildcard ../library/*.c))
OBJS += $(patsubst ../library/libtenki/src/%.c, %.o, $(wildcard ../library/libtenki/src/*.c))

.PHONY: all clean $(APPS) $(APP_OBJS) $(OBJS)

all: $(APPS)

clean:
	rm -rf $(BUILD_DIR)
	rm -rf $(BINDIR)

dracal-usb-get: dracal-usb-get.o calpoint.o calcurve.o chip.o chip_conv.o device.o list.o pow.o quantity.o unit.o \
	usbtenki.o usbtenki_provider.o source.o timestamp.o virtual.o
dracal-usb-get: | $(BINDIR)
	$(LD) $(^:%=$(OBJDIR)/%) -o $(@:%=$(BINDIR)/%)$(EXE) $(LDFLAGS)

dracal-usb-set: dracal-usb-set.o calpoint.o calcurve.o chip.o chip_conv.o device.o list.o pow.o quantity.o unit.o \
	usbtenki.o usbtenki_provider.o source.o virtual.o
dracal-usb-set: | $(BINDIR)
	$(LD) $(^:%=$(OBJDIR)/%) -o $(@:%=$(BINDIR)/%)$(EXE) $(LDFLAGS)

dracal-usb-cal: dracal-usb-cal.o calpoint.o calcurve.o chip.o chip_conv.o device.o list.o pow.o quantity.o unit.o  \
	usbtenki.o usbtenki_provider.o source.o timestamp.o virtual.o
dracal-usb-cal: | $(BINDIR)
	$(LD) $(^:%=$(OBJDIR)/%) -o $(@:%=$(BINDIR)/%)$(EXE) $(LDFLAGS)

usbtenkiget: dracal-usb-get
	ln -sf dracal-usb-get$(EXE) $(BINDIR)/usbtenkiget$(EXE)

usbtenkiset: dracal-usb-set
	ln -sf dracal-usb-set$(EXE) $(BINDIR)/usbtenkiset$(EXE)

usbtenkical: dracal-usb-cal
	ln -sf dracal-usb-cal$(EXE) $(BINDIR)/usbtenkical$(EXE)

dracal-sensgate-get: dracal-sensgate-get.o discovery.o buffer.o calpoint.o calcurve.o chip.o comparison.o csv.o \
	datalog.o device.o heap.o list.o pow.o quantity.o select_helper.o socket.o source.o stream.o \
	tenkinet.o timestamp.o unit.o version.o virtual.o
dracal-sensgate-get: | $(BINDIR) $(OUTDIR)
	$(LD) $(^:%=$(OBJDIR)/%) -o $(@:%=$(BINDIR)/%)$(EXE) $(LDFLAGS)

dracal-sensgate-log: dracal-sensgate-log.o buffer.o calpoint.o calcurve.o chip.o comparison.o csv.o \
	datalog.o device.o heap.o list.o pow.o quantity.o select_helper.o socket.o source.o stream.o \
	tenkinet.o timestamp.o unit.o version.o virtual.o
dracal-sensgate-log: | $(BINDIR) $(OUTDIR)
	$(LD) $(^:%=$(OBJDIR)/%) -o $(@:%=$(BINDIR)/%)$(EXE) $(LDFLAGS)

dracal-sensgate-logtool: dracal-sensgate-logtool.o buffer.o chip.o csv.o datalog.o device.o \
	list.o quantity.o unit.o version.o
dracal-sensgate-logtool: | $(BINDIR) $(OUTDIR)
	$(LD) $(^:%=$(OBJDIR)/%) -o $(@:%=$(BINDIR)/%)$(EXE) $(LDFLAGS)

dracal-sensgate-scan: dracal-sensgate-scan.o discovery.o list.o select_helper.o socket.o timestamp.o
dracal-sensgate-scan: | $(BINDIR) $(OUTDIR)
	$(LD) $(^:%=$(OBJDIR)/%) -o $(@:%=$(BINDIR)/%)$(EXE) $(LDFLAGS)

$(APP_OBJS) $(OBJS): %.o: $(OBJDIR)/%.o

$(APP_OBJS:%=$(OBJDIR)/%): $(OBJDIR)/%.o : %.c | $(OBJDIR)
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJS:%=$(OBJDIR)/%): $(OBJDIR)/%.o : %.c %.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR) $(BINDIR) $(OUTDIR):
	mkdir -p $@
