# Makefile
# ex.  > make PLATFORM_BITS=64 INST_PREFIX=/usr
#

ifeq ($(OS),Windows_NT)
    UNAME := Windows
else
    UNAME := $(shell uname -s)
endif


NAME = azoniguruma
DATE_TAG:=`date +%Y%m%d`

INST_PREFIX = /usr/local

ifeq ($(UNAME),Darwin)
	CCOPT = -I.   -I/usr/local/include -fno-strict-aliasing
	SOEXT := dylib
	OPT_SOLINK := -dynamiclib -Wl,-undefined -Wl,dynamic_lookup
else
	CCOPT = -I.   -I/usr/local/include
	SOEXT := so
	OPT_SOLINK := -shared -dynamiclib
endif

LDOPT =  -L$(INST_PREFIX)/lib

CFLAGS = -Wall -O2 $(CCOPT)

AZ_CCOPT = -Wall -O2 $(CCOPT) -I$(INST_PREFIX)/include
AZPC_LIBS = onig
AZ_LIB_EXT = $(INST_PREFIX)/lib/azprolog/ext
AZ_LIB_OBJ = $(INST_PREFIX)/lib/azprolog/obj

LDLIBS=$(LDOPT) -lonig

EXEC_TARGETS = oniguruma.$(SOEXT) lib_oniguruma.a oniguruma azoniguruma check

default: oniguruma.$(SOEXT) lib_oniguruma.a

oniguruma: oniguruma_userfile.c
	$(CC) $(CFLAGS) -c $<
	$(CC) -o $@ oniguruma_userfile.o $(AZ_LIB_OBJ)/azpi.o $(AZ_LIB_OBJ)/curses.o -L$(AZ_LIB_EXT) -lazp -l_oniguruma -lonig -lcurses -lm -ldl

azoniguruma: oniguruma.c
	azpc -p $< /i /e $@ /curses  /ccopt "$(AZ_CCOPT)" /lib $(AZPC_LIBS)
	rm -f *.o

oniguruma.$(SOEXT): oniguruma.c
	$(CC) $(CFLAGS) -fPIC -o oniguruma.o -c $<
	$(CC) $(OPT_SOLINK) -o $@ oniguruma.o $(LDLIBS)
	chmod 644 $@
	rm -f oniguruma.o

lib_oniguruma.a: oniguruma.c
	$(CC) $(CFLAGS) -o oniguruma.o -c $<
	ar rv $@ oniguruma.o
	ranlib $@

oniguruma.o: oniguruma.c

install:
	cp -p oniguruma.$(SOEXT) lib_oniguruma.a $(AZ_LIB_EXT)

clean:
	rm -f *~ .*~ *.o mkaz userfile.c $(EXEC_TARGETS)

tar:
	cd ..; tar -cvf $(NAME)-$(DATE_TAG).tar $(NAME)
	gzip ../$(NAME)-$(DATE_TAG).tar
