
# -------------------------------------------------------------------------------

TARS_PATH    :=/usr/local/tars/cpp

#-------------------------------------------------------------------------------

CC          = gcc
CXX         = g++
CFLAGS      += -std=c++11 -g -O2 -Wno-deprecated -Wall

#-------------------------------------------------------------------------------

TARS2CPP     := ${TARS_PATH}/tools/tars2cpp ${TARS2CPP_FLAG}
PROTO2TARS := ${TARS_PATH}/tools/pb2tarscpp 

#-------------------------------------------------------------------------------

TARS_SRC     := $(wildcard *.tars)
TARS_H       := $(patsubst %.tars,%.h, $(TARS_SRC))

PB_SRC     := $(wildcard *.proto)
PB_H       := $(patsubst %.proto,%.pb.h, $(PB_SRC))
PB_TARS_H  := $(patsubst %.proto,%.tars.h, $(PB_SRC))
PB_CPP     := $(patsubst %.proto,%.pb.cc, $(PB_SRC))
PB_OBJ     := $(patsubst %.proto,%.pb.o, $(PB_SRC))

ifneq ($(PB_SRC),)
	LIB += -lprotobuf
endif

LOCAL_SRC   += $(sort $(wildcard *.cpp *.cc *.c) $(PB_CPP))

LOCAL_OBJ   += $(patsubst %.c,%.o, $(patsubst %.cpp,%.o, $(patsubst %.cc,%.o, $(LOCAL_SRC))))

DEP_FILE    := $(foreach obj, $(LOCAL_OBJ), $(dir $(obj)).$(basename $(notdir $(obj))).d)

#-------------------------------------------------------------------------------

MYSQL_INC       += -I/usr/local/mysql/include/mysql -I/usr/local/mysql/include -I/usr/include/mysql
MYSQL_LIB_DIR   += -L/usr/local/mysql/lib/mysql -L/usr/local/mysql/lib -L/usr/lib/mysql
LIB_DIR         += ${MYSQL_LIB_DIR}
INC_DIR         += ${MYSQL_INC}

#ifneq ($(TARS_OPENTRACKING), 0)
#ifneq ($(TARS_OPENTRACKING), )
#	OPENTRACKING_INC     += -I/usr/local/include
#	OPENTRACKING_LIB_DIR += -L/usr/local/lib 
#	LIB_DIR += ${OPENTRACKING_LIB_DIR}
#	INC_DIR += ${OPENTRACKING_INC}
#endif
#endif



INCLUDE         += -I${TARS_PATH}/include -I./ ${INC_DIR}
LIB             += -L${TARS_PATH}/lib -ltarsservant -ltarsparse -ltarsutil -lpthread ${LIB_DIR}

ifneq ($(TARS_SSL), 0)
ifneq ($(TARS_SSL), )
	#业务编译
	CFLAGS += -DTARS_SSL=1
	LIB += -lssl -lcrypto
endif
endif

ifneq ($(TARS_HTTP2), 0)
ifneq ($(TARS_HTTP2), )
	#业务编译
	CFLAGS += -DTARS_HTTP2=1
	LIB += -lnghttp2_static
endif
endif

#ifneq ($(TARS_OPENTRACKING), 0)
#ifneq ($(TARS_OPENTRACKING), )
	#业务编译
#	CFLAGS += -D_USE_OPENTRACKING=1
#	LIB += -lopentracing -lzipkin_opentracing -lzipkin -lcurl 
#endif
#endif

#-----------------------------------------------------------------------------

TARGET                  := $(strip $(TARGET))
IS_LIB                  := F


ifeq ($(SUFFIX),.a)
    IS_LIB              := T
endif

ifeq ($(SUFFIX),.so)
    IS_LIB              := T
endif

#-------------------------------------------------------------------------------

all : $(PB_H) $(TARS_H) $(LOCAL_OBJ) $(TARGET) FORSTRIP

#-------------------------------------------------------------------------------
ifeq ($(IS_LIB),T)

$(filter %.a,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ)
	ar r $@ $(LOCAL_OBJ)

$(filter %.so,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ)
	$(CC) $(LFLAGS) -shared -o $@ $(LOCAL_OBJ) $(LIB)

$(filter-out %.so %.a,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ)
	$(CXX) $(CFLAGS) -o $@ $^ $(INCLUDE) $(LIB)
    
FORSTRIP:$(TARGET)
ifeq ($(STRIP_FLAG),Y)
	@strip $(TARGET)
endif

else

$(filter %.a,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ)
	ar r $@ $(LOCAL_OBJ)

$(filter %.so,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ)
	$(CC) $(LFLAGS) -shared -o $@ $(LOCAL_OBJ) $(LIB)

$(filter-out %.so %.a,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ)
	$(CXX) $(CFLAGS) -o $@ $^ $(INCLUDE) $(LIB)

FORSTRIP:$(TARGET)
ifeq ($(STRIP_FLAG),Y)
	@strip $(TARGET)
endif

endif

#----------------------------------------------------------------------------------

copyfile = if test -z "$(APP)" || test -z "$(TARGET)"; then \
               echo "['APP' or 'TARGET' option is empty.]"; exit 1; \
           	else \
		       	if test ! -d $(2); then \
              		echo "[No such dir:$(2), now we create it.]";\
    				mkdir -p $(2);\
				fi; \
         		echo "[Copy file $(1) -> $(2)]"; \
         		cp -v $(1) $(2); \
			fi;
#----------------------------------------------------------------------------------

$(PB_H) : $(PB_SRC)
	rm -vf $(PB_H) $(PB_CPP)
	protoc --plugin=protoc-gen-custom=${PROTO2TARS} --custom_out=./ ${PB_SRC} --cpp_out=./

#ifneq ($(TARS_SRC),)
$(TARS_H) : $(TARS_SRC)
	@echo "tars2cpp ${TARS2CPP_FLAG} "
	rm -vf $(TARS_H)
	@echo "tars2cpp ${TARS2CPP_FLAG} "
	$(TARS2CPP) ${TARS2CPP_FLAG} $(TARS_SRC)

#endif
#----------------------------------------------------------------------------------

tar: $(TARGET) $(CONFIG)
	@if [ -d $(TARGET)_tmp_dir ]; then \
		echo "dir has exist:$(TARGET)_tmp_dir, abort."; \
		exit 1; \
	else \
		mkdir $(TARGET)_tmp_dir $(TARGET)_tmp_dir/$(TARGET);\
		cp -rf $(TARGET) $(CONFIG) $(TARGET)_tmp_dir/$(TARGET)/; \
		cd $(TARGET)_tmp_dir; tar --exclude=".svn" --exclude="_svn" -czvf $(TARGET).tgz $(TARGET)/; cd ..; \
		if [ -f "$(TARGET).tgz" ]; then \
			mv -vf $(TARGET).tgz $(TARGET).`date +%Y%m%d%H%M%S`.tgz; \
		fi; \
		mv $(TARGET)_tmp_dir/$(TARGET).tgz ./; \
		rm -rf $(TARGET)_tmp_dir; \
		echo "tar cvfz $(TARGET).tgz ..."; \
	fi
	
HELP += $(HELP_TAR)

ifneq ($(TARS_SRC),)

SERVER_NAME := $(TARGET)
SERVER_DIR  := /home/tarsproto/$(APP)/$(SERVER_NAME)
RELEASE_TARS += $(TARS_SRC) $(TARS_H)

release: $(TARS_H)
	@$(call copyfile, $(RELEASE_TARS) [Mm]akefile , $(SERVER_DIR))
	echo "INCLUDE += -I$(SERVER_DIR)"  >  $(SERVER_DIR)/$(SERVER_NAME).mk;

HELP += $(HELP_RELEASE)
endif
#----------------------------------------------------------------------------------

clean:
	rm -vf $(LOCAL_OBJ) $(TARS_H) $(TARGET) $(TARGETS) ${CLEANFILE} .*.d.tmp gmon.out

cleanall:
	rm -vf $(LOCAL_OBJ) $(PB_TARS_H) $(TARS_H) $(PB_H) $(PB_CPP) $(TARGET) $(TARGETS) $(DEP_FILE) ${CLEANFILE} *.o .*.d.tmp .*.d gmon.out
	rm -vf $(TARGET)*.tgz

HELP += $(HELP_CLEAN)
HELP += $(HELP_CLEANALL)

ifneq ($(DEP_FILE),)
-include $(DEP_FILE)
endif

HELP_RELEASE  = "\n\e[1;33mrelease\e[0m:\t[copy '\e[1;32m$(TARS_SRC) $(TARS_H)\e[0m' -> '/home/tarsproto/$(APP)/$(TARGET)']"
HELP_CLEAN    = "\n\e[1;33mclean\e[0m:\t\t[remove $(LOCAL_OBJ) $(TARGET)]"
HELP_CLEANALL = "\n\e[1;33mcleanall\e[0m:\t[clean & rm .*.d]"
HELP_TAR      = "\n\e[1;33mtar\e[0m:\t\t[will do 'tar $(TARGET).tgz $(RELEASE_FILE)']"

help:
	@echo -e $(HELP)"\n"

#----------------------------------------------------------------------------------

.%.d: %.cc
	@echo "update $@ ..."; \
	echo -n $< | sed s/\.cc/\.o:/ > $@; \
	$(CXX) $(CFLAGS) $(INCLUDE) -MM $< | sed '1s/.*.://' >> $@;

%.o: %.cc
	$(CXX) $(CFLAGS) $(INCLUDE) -o $@ -c $<

.%.d: %.cpp
	@echo "update $@ ..."; \
	echo -n $< | sed s/\.cpp/\.o:/ > $@; \
	$(CXX) $(CFLAGS) $(INCLUDE) -MM $< | sed '1s/.*.://' >> $@;

%.o: %.cpp
	$(CXX) $(CFLAGS) $(INCLUDE) -o $@ -c $<

.%.d: %.c
	@echo "update $@ ..."; \
	echo -n $< | sed s/\.c/\.o:/ > $@; \
	$(CC) $(INCLUDE) -MM $< | sed '1s/.*.://' >> $@;

%.o: %.c
	$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<

#----------------------------------------------------------------------------------

