
##----------------------------------------------------------##
##                                                          ##
##  Universal Makefile Rules                                ##
##                                                          ##
##  Created : Wei Liu 07/03/07                              ##
##  Revision: [Last]Wei Liu 07/07/07                        ##
##                                                          ##
##----------------------------------------------------------##


UMAKE_VERSION := V2.0

##-------------------------------------
##
## Work Directory : /usr/local/include
## Default Target : all
##
##-------------------------------------
default: all
.PHONY: all clean rebuild test indent splint doc          \
        dir config check bk lsbk rmbk unzip umakever usage\
        FORCE
.SUFFIXES:

umakever:
	@echo "Universal Makefile (UMake) Version: $(UMAKE_VERSION)"
 
##--------------------------------------
##
##  Makefile CFG defination check
##
##--------------------------------------

ifeq "$(MODULE)" ""
$(error Please input the module name (MODULE = )in makefile. )
endif

ifeq "$(CFG)" ""
CFG=debug
$(warnning No configuration specified for CFG. Defaulting to $(MODULE) - debug. )
endif

ifeq "$(BUILD)" ""
BUILD=lib
$(warnning No configuration specified for BUILD. Defaulting to create lib$(MODULE).a. )
endif

ifeq "$(SRC_PATH)" ""
SRC_PATH=.
$(warnning No configuration specified for SRC_PATH. Defaulting to ./. )
endif

COVER_NEED   ?= no
PLT_PATH     ?= ../../plat
APP_PATH     ?= ../../app
TYPE         ?= plat

##--------------------------------------
##
##  Gcc Flag for debug or release
##
##-------------------------------------- 
CC                  := gcc
GCC_CFLAGS          := -Wall -MM
AR_LINK             := ar -r

RELS_FLAGS_ADD		+= -DNDEBUG	
RELEASE_CFLAGS      += -g -Wall -I. $(RELS_FLAGS_ADD) -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient
RELEASE_LINK_CFLAGS =  -g $(RELS_FLAGS_ADD) -o
DEBUG_CFLAGS        += -g -Wall -rdynamic -DDEBUG -I. $(DBUG_FLAGS_ADD) -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -I/usr/include/openssl
DEBUG_LINK_CFLAGS   =  -g -rdynamic -DDEBUG -o  

GLIB_CFLAGS         = -I/usr/include/glib-2.0
GLIB_CFLAGS1        = -I/usr/lib/x86_64-linux-gnu/glib-2.0/include

ifeq "$(COVER_NEED)" "yes"
DEBUG_CFLAGS        += -fprofile-arcs -ftest-coverage -pg
endif

GCC_CFLAGS=$(DEBUG_CFLAGS) $(GLIB_CFLAGS) $(GLIB_CFLAGS1)
GCC_LINK_CFLAGS=$(DEBUG_LINK_CFLAGS)

##--------------------------------------
##
##  Project setting
##
##-------------------------------------- 
OBJDIR:=./obj
LIBDIR:=./lib
UTDIR :=./ut
DOCDIR:=./doc
DIRBUILD=$(OBJDIR)

ifeq "$(BUILD)" "lib"
BINDIR:=./bin
OUTFILE=$(LIBDIR)/lib$(MODULE).a
DIRNEED=$(UTDIR) $(DOCDIR)
DIRBUILD+=$(LIBDIR)
else
BINDIR:=.
OUTFILE=$(BINDIR)/$(MODULE)
DIRNEED=
DIRBUILD+=$(BINDIR)
endif

##--------------------------------------
##
##  source , object and dependencies files
##
##-------------------------------------- 

SRC_SUBDIR := $(shell find $(SRC_PATH) -type d)

vpath %.c $(SRC_SUBDIR)
vpath %.o $(OBJDIR)
vpath %.d $(OBJDIR)

SRC_FULL_PATH = $(foreach dir,$(SRC_SUBDIR),$(wildcard $(dir)/*.c))
SRC_FILES     = $(foreach file, $(notdir $(SRC_FULL_PATH)) ,$(OBJDIR)/$(file)) 
COMMON_OBJ    = $(SRC_FILES:%.c=%.o)

TEST_OBJ_PATH ?= ../../obj

TEST_OBJ      = $(foreach dir,$(TEST_OBJ_PATH),$(wildcard $(dir)/*.o))

OBJ=$(COMMON_OBJ) $(OBJ_ADD)
ALL_OBJ := $(OBJ) $(TEST_OBJ)

##--------------------------------------
##
##  Lib setting
##
##--------------------------------------

ifeq "$(COVER_NEED)" "yes"
LIBCOVER=-lgcov
endif

MODULE_PLT_LIB=$(foreach lib,$(PLT_LIB), -L$(PLT_PATH)/$(lib)/lib/ -l$(lib) ) 
MODULE_PLT_LIB+=-lm


MODULE_APP_LIB=$(foreach lib,$(APP_LIB),-L$(APP_PATH)/$(lib)/lib -l$(lib))



##--------------------------------------
##
## Rules
##
##--------------------------------------
CCFLAG_SWITCH ?= off
CC_COMPILE =$(CC) $(GCC_CFLAGS) -c $< -o $@ 
CC_PRG_LINK=$(CC) $(GCC_LINK_CFLAGS) $(OUTFILE) $(ALL_OBJ) $(LIBCOVER) $(MODULE_APP_LIB) $(MODULE_PLT_LIB) $(LIB_ADD) 
CC_LIB_LINK=$(AR_LINK) $(OUTFILE) $(ALL_OBJ) 

COMPILE=$(CC_COMPILE)
PRG_LINK=$(CC_PRG_LINK)
LIB_LINK=$(CC_LIB_LINK)

ifeq "$(BUILD)" "exef"
LINK=$(PRG_LINK)
else
LINK=$(LIB_LINK)
endif

# Build rules

all: preproc start dir $(ALL_OBJ) #prtdebug
	@echo Linking :$(OUTFILE)  
ifeq "$(CCFLAG_SWITCH)" "off"
	@$(LINK)
else
	$(LINK)
endif
	@$(POSTPROC_CMD)	  
	@echo -e "\n================================================================================\n"   

sinclude $(DEPENDS)	

release : CC_COMPILE =$(CC) $(RELEASE_CFLAGS) -c $< -o $@
release : CC_PRG_LINK=$(CC) $(RELEASE_LINK_CFLAGS) $(OUTFILE) $(ALL_OBJ) $(MODULE_APP_LIB) $(MODULE_PLT_LIB) $(LIB_ADD)
release : all 

##--------------------------------------
##
##  Make command to use for dependencies
##
##--------------------------------------

MAKE   :=make
RM     :=rm
MKDIR  :=mkdir

preproc:
	@$(PREPROC_CMD)	

start:
	@echo -e "\n================================================================================\n"
	@echo "[Building Project]: $(notdir $(MODULE))"
ifeq "$(CCFLAG_SWITCH)" "off"
	@echo "Show Gcc Flags switch = OFF"
endif	

prtdebug:
	@echo "$(MODULE)-$(BUILD)[$(CFG)] build source file:"  "$(SRC_FULL_PATH)"
	@echo   SRC_SUBDIR: $(SRC_SUBDIR) 
	@echo   SRC_FULL_PATH : $(SRC_FULL_PATH) 
	@echo   SRC_FILES : $(SRC_FILES)
	@echo   ALL_OBJ : $(ALL_OBJ)  
	@echo   LIB:$(MODULE_PLT_LIB)
	@echo   PLT_LIB: $(PLT_LIB)
	@echo   CCFLAG_SWITCH :$(CCFLAG_SWITCH)

config: dir    

dir: 
	@$(foreach dir,$(DIRNEED),$(MKDIR) -p $(DIRNEED) --mode=0777; ) 
	@$(foreach dir,$(DIRBUILD),$(MKDIR) -p $(dir) --mode=0777; )     
	        
##--------------------------------------
##
##  Make Rebuild and clean
##
##--------------------------------------

ifneq "$(PROJ)" ""
FRIEND_PROJ    := $(shell )
endif

jumprebuild:
ifneq "$(PROJ)" ""
	@cd $(FRIEND_PROJ); mak rebuild ; cd -
endif	

# Rebuild this project
rebuild: jumprebuild cleanall all

# Clean this project and all dependencies
cleanall: clean

# Clean this project
clean:
	@echo -e "||--------------------------------------------------------------- "
	@echo -e "|| Umake clean gcc , lcov, doxygen generated and temporary files. "
	@echo -e "||--------------------------------------------------------------- "
	@$(RM) -rf $(OBJDIR) $(OUTFILE) $(COVER_REPORT_PATH) ./doc/doxygen.conf ./doc/html  ./doc/latex ./doc/rtf $(foreach dir,$(SRC_SUBDIR),$(dir)/*~) 


##--------------------------------------
##
##  indent Makefile.indent
##
##--------------------------------------
include Makefile.indent

indent:
	@chmod 777 $(SRC_FULL_PATH)
	@echo ===================================Indent START=================================
	@echo
	@echo "[Indent source file ]: $(SRC_FULL_PATH)"
	$(call MAKE_INDENT , $(SRC_FULL_PATH))	
	
##--------------------------------------
##
##  splint makefile.splint
##
##--------------------------------------
include Makefile.splint

SPLINT_FLAG_SWITCH ?= off

splint:
	@chmod 777 $(SRC_FULL_PATH)
	@echo =================================Splint START==================================
	@echo
ifeq "$(SPLINT_FLAG_SWITCH)" "on"
	@echo "[Splint flags       ]: $(SPLINT_FLAGS)"
endif
	@echo "[Lint Clean Project]: $(notdir $(MODULE))"
	$(call MAKE_SPLINT, $(SRC_FULL_PATH))

##--------------------------------------
##
##  doc Makefile.doxygen
##
##--------------------------------------
include Makefile.doxygen

doc:
	@chmod 777 $(SRC_FULL_PATH)
	@echo ==================================Doxygen START=================================
	@echo
	$(call MAKE_DOC, $(SRC_FULL_PATH))
	
	
##--------------------------------------
##
##  backup Makefile.backup
##
##--------------------------------------
include Makefile.backup

##--------------------------------------
##
##  cov Makefile.cov
##
##--------------------------------------
include Makefile.cov

##--------------------------------------
##
##  usage Makefile.usage
##
##--------------------------------------
include Makefile.usage

##--------------------------------------
##
##  Make dependencies
##
##--------------------------------------

$(OBJDIR)/%.d:%.c
	@$(CC) $< -MM -MD -o $@			
		
$(OBJDIR)/%.o:%.c 
	
ifeq "$(CCFLAG_SWITCH)" "off"
	@echo -e "building: $(notdir $@) \t\t\t\t please wait ..."
	@$(COMPILE)
else
	$(COMPILE)
endif	
		
DEPENDS=$(COMMON_OBJ:.o=.d)

##--------------------------------------
##
##  Make force
##
##--------------------------------------
FORCE:	

