CC=gcc
AR=ar
STRIP=strip
#CFLAGS += -O3 -Wall -static -lsqlite3 -lm -lpthread -ldl -I. -L. 
CFLAGS += -O3 -Wall -static -lm -lpthread -ldl -I. -L. 
TIMEOUT=5

ifeq ($(DEBUG),y)
	CFLAGS +=-g -D_DEBUG
endif

OBJ = main-sqlite.o sqlite3.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),linux64)
#	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
ifeq ($(MACHINE),aarch64)
  ARCH = aarch64
  PREFIX = aarch64-none-linux-gnu
  CC = $(AARCH64DIR)/bin/$(PREFIX)-gcc
  AR = $(AARCH64DIR)/bin/$(PREFIX)-ar
  STRIP = $(AARCH64DIR)/bin/$(PREFIX)-strip
endif
ifeq ($(MACHINE),riscv64)
  ARCH = riscv64
  PREFIX = riscv64-linux-gnu
  CC = $(PREFIX)-gcc
  AR = $(PREFIX)-ar
  STRIP = $(PREFIX)-strip
endif




PROGNAME=http-okay-$(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)
else
	$(RM) -f $(PROGNAME).aarch64.elf $(PROGNAME).com.dbg
endif 

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

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


