# Makefile for Tokyo Tyrant



#================================================================
# Setting Variables
#================================================================


# Generic settings
SHELL = @SHELL@

# Package information
PACKAGE = @PACKAGE_NAME@
VERSION = @PACKAGE_VERSION@
PACKAGEDIR = $(PACKAGE)-$(VERSION)
PACKAGETGZ = $(PACKAGE)-$(VERSION).tar.gz
LIBVER = @MYLIBVER@
LIBREV = @MYLIBREV@
PROTVER = @MYPROTVER@

# Targets
HEADERFILES = @MYHEADERFILES@
LIBRARYFILES = @MYLIBRARYFILES@
LIBOBJFILES = @MYLIBOBJFILES@
COMMANDFILES = @MYCOMMANDFILES@
INITFILES = @MYINITFILES@
MAN1FILES = @MYMAN1FILES@
MAN3FILES = @MYMAN3FILES@
MAN8FILES = @MYMAN8FILES@
SCREXTFILES = @MYSCREXTFILES@
DOCUMENTFILES = @MYDOCUMENTFILES@
PCFILES = @MYPCFILES@

# Install destinations
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
INCLUDEDIR = @includedir@
LIBDIR = @libdir@
BINDIR = @bindir@
SBINDIR = @sbindir@
LIBEXECDIR = @libexecdir@
DATADIR = @datadir@/$(PACKAGE)
MAN1DIR = @mandir@/man1
MAN3DIR = @mandir@/man3
MAN8DIR = @mandir@/man8
PCDIR = @libdir@/pkgconfig
DESTDIR =

# Building configuration
CC = @CC@
CPPFLAGS = @MYCPPFLAGS@ \
  -D_TT_PREFIX="\"$(prefix)\"" -D_TT_INCLUDEDIR="\"$(INCLUDEDIR)\"" \
  -D_TT_LIBDIR="\"$(LIBDIR)\"" -D_TT_BINDIR="\"$(BINDIR)\"" -D_TT_LIBEXECDIR="\"$(LIBEXECDIR)\"" \
  -D_TT_APPINC="\"-I$(INCLUDEDIR)\"" -D_TT_APPLIBS="\"-L$(LIBDIR) -ltokyotyrant @LIBS@\""
CFLAGS = @MYCFLAGS@
LDFLAGS = @MYLDFLAGS@
CMDLDFLAGS = @MYCMDLDFLAGS@
LIBS = @LIBS@
LDENV = LD_RUN_PATH=/lib:/usr/lib:$(LIBDIR):$(HOME)/lib:/usr/local/lib:@MYRUNPATH@:.
RUNENV = @MYLDLIBPATHENV@=.:/lib:/usr/lib:$(LIBDIR):$(HOME)/lib:/usr/local/lib:@MYRUNPATH@
POSTCMD = @MYPOSTCMD@



#================================================================
# Suffix rules
#================================================================


.SUFFIXES :
.SUFFIXES : .c .o

.c.o :
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $<



#================================================================
# Actions
#================================================================


all : $(LIBRARYFILES) $(COMMANDFILES)
	@$(POSTCMD)
	@printf '\n'
	@printf '#================================================================\n'
	@printf '# Ready to install.\n'
	@printf '#================================================================\n'


clean :
	rm -rf $(LIBRARYFILES) $(LIBOBJFILES) $(COMMANDFILES) \
	  *.o a.out check.in check.out gmon.out *.vlog words.tsv \
	  casket casket-* casket.* *.tch *.tcb *.tcf *.tct *.idx.* \
	  *.ulog ulog 1978* 1979* *.rts *.pid *~ hoge moge tako ika


version :
	vernum=`expr $(LIBVER)00 + $(LIBREV)` ; \
	  sed -e 's/_TT_VERSION.*/_TT_VERSION    "$(VERSION)"/' \
	    -e "s/_TT_LIBVER.*/_TT_LIBVER     $$vernum/" \
	    -e 's/_TT_PROTVER.*/_TT_PROTVER    "$(PROTVER)"/' ttutil.h > ttutil.h~
	[ -f ttutil.h~ ] && mv -f ttutil.h~ ttutil.h


untabify :
	ls *.c *.h | while read name ; \
	  do \
	    sed -e 's/\t/        /g' -e 's/ *$$//' $$name > $$name~; \
	    [ -f $$name~ ] && mv -f $$name~ $$name ; \
	  done


install :
	mkdir -p $(DESTDIR)$(INCLUDEDIR)
	cp -Rf $(HEADERFILES) $(DESTDIR)$(INCLUDEDIR)
	mkdir -p $(DESTDIR)$(LIBDIR)
	cp -Rf $(LIBRARYFILES) $(DESTDIR)$(LIBDIR)
	mkdir -p $(DESTDIR)$(BINDIR)
	cp -Rf $(COMMANDFILES) $(DESTDIR)$(BINDIR)
	mkdir -p $(DESTDIR)$(SBINDIR)
	cp -Rf $(INITFILES) $(DESTDIR)$(SBINDIR)
	mkdir -p $(DESTDIR)$(DATADIR)
	cp -Rf $(SCREXTFILES) $(DOCUMENTFILES) $(DESTDIR)$(DATADIR)
	mkdir -p $(DESTDIR)$(MAN1DIR)
	cd man && cp -Rf $(MAN1FILES) $(DESTDIR)$(MAN1DIR)
	mkdir -p $(DESTDIR)$(MAN3DIR)
	cd man && cp -Rf $(MAN3FILES) $(DESTDIR)$(MAN3DIR)
	mkdir -p $(DESTDIR)$(MAN8DIR)
	cd man && cp -Rf $(MAN8FILES) $(DESTDIR)$(MAN8DIR)
	mkdir -p $(DESTDIR)$(PCDIR)
	cp -Rf $(PCFILES) $(DESTDIR)$(PCDIR)
	@printf '\n'
	@printf '#================================================================\n'
	@printf '# Thanks for using Tokyo Tyrant.\n'
	@printf '#================================================================\n'


install-strip :
	make DESTDIR=$(DESTDIR) install
	cd $(DESTDIR)$(BINDIR) && strip $(MYCOMMANDS)


uninstall :
	cd $(DESTDIR)$(INCLUDEDIR) && rm -f $(HEADERFILES)
	cd $(DESTDIR)$(LIBDIR) && rm -f $(LIBRARYFILES)
	cd $(DESTDIR)$(BINDIR) && rm -f $(COMMANDFILES)
	cd $(DESTDIR)$(SBINDIR) && rm -f $(INITFILES)
	cd $(DESTDIR)$(MAN1DIR) && rm -f $(MAN1FILES)
	cd $(DESTDIR)$(MAN3DIR) && rm -f $(MAN3FILES)
	cd $(DESTDIR)$(MAN8DIR) && rm -f $(MAN8FILES)
	rm -rf $(DESTDIR)$(DATADIR)
	cd $(DESTDIR)$(PCDIR) && rm -f $(PCFILES)


dist :
	make version
	make untabify
	make distclean
	cd .. && tar cvf - $(PACKAGEDIR) | gzip -c > $(PACKAGETGZ)
	sync ; sync


distclean : clean
	cd example && make clean
	rm -rf Makefile tokyotyrant.pc config.cache config.log config.status autom4te.cache


check :
	$(RUNENV) $(RUNCMD) ./tcrmgr version
	$(RUNENV) $(RUNCMD) ./tcrtest write -cnum 5 -tout 5 -rnd 127.0.0.1 50000
	$(RUNENV) $(RUNCMD) ./tcrtest write -cnum 5 -tout 5 -nr -rnd 127.0.0.1 50000
	$(RUNENV) $(RUNCMD) ./tcrtest write -cnum 5 -tout 5 127.0.0.1 50000
	$(RUNENV) $(RUNCMD) ./tcrtest read -cnum 5 -tout 5 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrtest read -cnum 5 -tout 5 -mul 5 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrtest remove -cnum 5 -tout 5 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrtest rcat -cnum 5 -tout 5 127.0.0.1 50000
	$(RUNENV) $(RUNCMD) ./tcrtest rcat -cnum 5 -tout 5 -shl 50 127.0.0.1 50000
	$(RUNENV) $(RUNCMD) ./tcrmgr vanish 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrtest rcat -cnum 5 -tout 5 -dad 127.0.0.1 50000
	$(RUNENV) $(RUNCMD) ./tcrtest rcat -cnum 5 -tout 5 -ext putcat -xlr 127.0.0.1 50000
	$(RUNENV) $(RUNCMD) ./tcrtest misc -cnum 5 -tout 5 127.0.0.1 5000
	$(RUNENV) $(RUNCMD) ./tcrtest wicked -cnum 5 -tout 5 127.0.0.1 5000
	$(RUNENV) $(RUNCMD) ./tcrmgr inform 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrmgr vanish 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrmttest write -tnum 5 127.0.0.1 5000
	$(RUNENV) $(RUNCMD) ./tcrmttest read -tnum 5 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrmttest remove -tnum 5 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrmttest write -tnum 5 -ext putcat -rnd 127.0.0.1 5000
	$(RUNENV) $(RUNCMD) ./tcrmttest typical -tnum 5 127.0.0.1 5000
	$(RUNENV) $(RUNCMD) ./tcrmgr vanish 127.0.0.1
	$(RUNENV) $(RUNCMD) ./tcrmgr put 127.0.0.1 one first
	$(RUNENV) $(RUNCMD) ./tcrmgr put 127.0.0.1 two second
	$(RUNENV) $(RUNCMD) ./tcrmgr put -dk 127.0.0.1 three third
	$(RUNENV) $(RUNCMD) ./tcrmgr put -dc 127.0.0.1 three third
	$(RUNENV) $(RUNCMD) ./tcrmgr put -dc 127.0.0.1 three third
	$(RUNENV) $(RUNCMD) ./tcrmgr put -dc 127.0.0.1 three third
	$(RUNENV) $(RUNCMD) ./tcrmgr put 127.0.0.1 four fourth
	$(RUNENV) $(RUNCMD) ./tcrmgr put -dk 127.0.0.1 five fifth
	$(RUNENV) $(RUNCMD) ./tcrmgr out 127.0.0.1 one
	$(RUNENV) $(RUNCMD) ./tcrmgr out 127.0.0.1 two
	$(RUNENV) $(RUNCMD) ./tcrmgr get 127.0.0.1 three > check.out
	$(RUNENV) $(RUNCMD) ./tcrmgr get 127.0.0.1 four > check.out
	$(RUNENV) $(RUNCMD) ./tcrmgr get 127.0.0.1 five > check.out
	$(RUNENV) $(RUNCMD) ./tcrmgr mget 127.0.0.1 one two three four five > check.out
	$(RUNENV) $(RUNCMD) ./tcrmgr misc 127.0.0.1 putlist six sixth seven seventh
	$(RUNENV) $(RUNCMD) ./tcrmgr misc 127.0.0.1 outlist six
	$(RUNENV) $(RUNCMD) ./tcrmgr misc 127.0.0.1 getlist three four five six > check.out
	$(RUNENV) $(RUNCMD) ./tcrmgr list -pv 127.0.0.1 > check.out
	$(RUNENV) $(RUNCMD) ./tcrmgr list -pv -fm f 127.0.0.1 > check.out
	$(RUNENV) $(RUNCMD) ./tcrmgr http -ih http://127.0.0.1:1978/five > check.out
	rm -rf ulog ; mkdir -p ulog
	$(RUNENV) $(RUNCMD) ./ttultest write -lim 10000 ulog 5000
	$(RUNENV) $(RUNCMD) ./ttultest write -lim 10000 -as ulog 5000
	$(RUNENV) $(RUNCMD) ./ttultest read ulog
	rm -rf ulog ; mkdir -p ulog
	$(RUNENV) $(RUNCMD) ./ttultest thread -lim 10000 ulog 5 5000
	$(RUNENV) $(RUNCMD) ./ttultest thread -lim 10000 -as ulog 5 5000
	@printf '\n'
	@printf '#================================================================\n'
	@printf '# Checking completed.\n'
	@printf '#================================================================\n'


check-valgrind :
	make RUNCMD="valgrind --tool=memcheck --log-file=%p.vlog" check
	grep ERROR *.vlog | grep -v ' 0 errors' ; true
	grep 'at exit' *.vlog | grep -v ' 0 bytes' ; true


check-forever :
	while true ; \
	  do \
	    make check || break ; \
	  done


words :
	cat -n /usr/share/dict/words | \
	  sed -e 's/^ *//' -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' \
	    -e 's/^\([0-9]*\)\t\(.*\)/\2\t\1/' > words.tsv
	./tcrmgr importtsv localhost words.tsv


.PHONY : all clean install check



#================================================================
# Building binaries
#================================================================


libtokyotyrant.a : $(LIBOBJFILES)
	$(AR) $(ARFLAGS) $@ $(LIBOBJFILES)


libtokyotyrant.so.$(LIBVER).$(LIBREV).0 : $(LIBOBJFILES)
	if uname -a | egrep -i 'SunOS' > /dev/null ; \
	  then \
	    $(CC) $(CFLAGS) -shared -Wl,-G,-h,libtokyotyrant.so.$(LIBVER) -o $@ \
	      $(LIBOBJFILES) $(LDFLAGS) $(LIBS) ; \
	  else \
	    $(CC) $(CFLAGS) -shared -Wl,-soname,libtokyotyrant.so.$(LIBVER) -o $@ \
	      $(LIBOBJFILES) $(LDFLAGS) $(LIBS) ; \
	  fi


libtokyotyrant.so.$(LIBVER) : libtokyotyrant.so.$(LIBVER).$(LIBREV).0
	ln -f -s libtokyotyrant.so.$(LIBVER).$(LIBREV).0 $@


libtokyotyrant.so : libtokyotyrant.so.$(LIBVER).$(LIBREV).0
	ln -f -s libtokyotyrant.so.$(LIBVER).$(LIBREV).0 $@


libtokyotyrant.$(LIBVER).$(LIBREV).0.dylib : $(LIBOBJFILES)
	$(CC) $(CFLAGS) -dynamiclib -o $@ \
	  -install_name $(LIBDIR)/libtokyotyrant.$(LIBVER).dylib \
	  -current_version $(LIBVER).$(LIBREV).0 -compatibility_version $(LIBVER) \
	  $(LIBOBJFILES) $(LDFLAGS) $(LIBS)


libtokyotyrant.$(LIBVER).dylib : libtokyotyrant.$(LIBVER).$(LIBREV).0.dylib
	ln -f -s libtokyotyrant.$(LIBVER).$(LIBREV).0.dylib $@


libtokyotyrant.dylib : libtokyotyrant.$(LIBVER).$(LIBREV).0.dylib
	ln -f -s libtokyotyrant.$(LIBVER).$(LIBREV).0.dylib $@


ttskelmock.so : ttskelmock.o
	$(CC) $(CFLAGS) -shared -o $@ $< $(LDFLAGS) $(LIBS)


ttskeldir.so : ttskeldir.o
	$(CC) $(CFLAGS) -shared -o $@ $< $(LDFLAGS) $(LIBS)


ttskelproxy.so : ttskelproxy.o
	$(CC) $(CFLAGS) -shared -o $@ $< $(LDFLAGS) $(LIBS)


ttskelnull.so : ttskelnull.o
	$(CC) $(CFLAGS) -shared -o $@ $< $(LDFLAGS) $(LIBS)


ttskelmock.bundle : ttskelmock.o
	$(CC) $(CFLAGS) -bundle -flat_namespace -undefined suppress -o $@ $< $(LDFLAGS) $(LIBS)


ttskeldir.bundle : ttskeldir.o
	$(CC) $(CFLAGS) -bundle -flat_namespace -undefined suppress -o $@ $< $(LDFLAGS) $(LIBS)


ttskelproxy.bundle : ttskelproxy.o
	$(CC) $(CFLAGS) -bundle -flat_namespace -undefined suppress -o $@ $< $(LDFLAGS) $(LIBS)


ttskelnull.bundle : ttskelnull.o
	$(CC) $(CFLAGS) -bundle -flat_namespace -undefined suppress -o $@ $< $(LDFLAGS) $(LIBS)


ttserver : ttserver.o scrext.o $(LIBRARYFILES)
	$(LDENV) $(CC) $(CFLAGS) -o $@ $< scrext.o $(LDFLAGS) $(CMDLDFLAGS) -ltokyotyrant $(LIBS)


ttulmgr : ttulmgr.o $(LIBRARYFILES)
	$(LDENV) $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CMDLDFLAGS) -ltokyotyrant $(LIBS)


ttultest : ttultest.o $(LIBRARYFILES)
	$(LDENV) $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CMDLDFLAGS) -ltokyotyrant $(LIBS)


tcrtest : tcrtest.o $(LIBRARYFILES)
	$(LDENV) $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CMDLDFLAGS) -ltokyotyrant $(LIBS)


tcrmttest : tcrmttest.o $(LIBRARYFILES)
	$(LDENV) $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CMDLDFLAGS) -ltokyotyrant $(LIBS)


tcrmgr : tcrmgr.o $(LIBRARYFILES)
	$(LDENV) $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CMDLDFLAGS) -ltokyotyrant $(LIBS)


myconf.o scrext.o : myconf.h

ttutil.o : myconf.h ttutil.h

tculog.o : myconf.h ttutil.h tculog.h

tcrdb.o : myconf.h ttutil.h tcrdb.h

ttskelproxy.o : myconf.h ttutil.h tcrdb.h

ttserver.o ttulmgr.o ttultest.o tcrtest.o tcrmgr.o : myconf.h ttutil.h tculog.h tcrdb.h

ttserver.o : scrext.h



# END OF FILE
