CC=gcc
AR=ar
STRIP=strip
CFLAGS += -O3 -Wall -Werror -static
TIMEOUT=5

ifeq ($(DEBUG),y)
	CFLAGS +=-g -D_DEBUG
endif
CFLAGS += -DTIMEOUT=${TIMEOUT}

OBJ = tinydns.o

TARGET = linux
ARCH = $(shell uname -m)

ifeq ($(MACHINE),arm6)
  ARCH=arm6
  PREFIX=arm-linux-gnueabi
  CC=$(PREFIX)-gcc
  AR=$(PREFIX)-ar
  STRIP=$(PREFIX)-strip
endif
ifeq ($(MACHINE),arm7)
  ARCH=arm7
  PREFIX=arm-linux-gnueabihf
  CC=$(PREFIX)-gcc
  AR=$(PREFIX)-ar
  STRIP=$(PREFIX)-strip
endif
ifeq ($(MACHINE),win64)
  ARCH=win64
  TARGET=windows.exe
  PREFIX=x86_64-w64-mingw32
  CC=$(PREFIX)-gcc
  AR=$(PREFIX)-ar
  STRIP=$(PREFIX)-strip
	CFLAGS+=-D_WINDOWS_
endif
ifeq ($(MACHINE),win32)
  ARCH=win32
  TARGET=windows.exe
  PREFIX=i686-w64-mingw32
  CC=$(PREFIX)-gcc
  AR=$(PREFIX)-ar
  STRIP=$(PREFIX)-strip
	CFLAGS+=-D_WINDOWS_
endif
ifeq ($(MACHINE),cosmocc)
  ARCH=multi
  TARGET=cosmocc
  PREFIX=cosmo
  CC=$(COSMOCCDIR)/bin/$(PREFIX)cc
  AR=$(COSMOCCDIR)/bin/$(PREFIX)ar
endif



PROGNAME=tinydns-$(ARCH)-$(TARGET)

.PHONY: all clean
all: clean $(PROGNAME)

$(PROGNAME): $(OBJ)
	$(warning )
	$(warning Compiling : $(PROGNAME) )
	$(warning $(OBJ))
	$(warning With CFLAGS : $(CFLAGS) )
	$(warning With LDFLAGS: $(LDFLAGS) )
	$(warning With ARCH   : $(ARCH) )
	$(warning With TARGET : $(TARGET) )
	$(warning )
	$(CC) -o $@ $^ $(CFLAGS)
ifneq ($(MACHINE),cosmocc)
	$(STRIP) $(PROGNAME)
endif 

clean:
	rm -f *.o *~ core 
	$(RM) -vf $(PROGNAME)

ifeq ($(MACHINE),cosmocc)
	$(RM) -vf $(PROGNAME).aarch64.elf $(PROGNAME).com.dbg
endif 


