#
# $Id$
#
#
# common Makefile rules, included by main Makefile & the  modules
#

#
# Uses: NAME, ALLDEP, CC, CFLAGS, DEFS, INCLUDES, LIBS, MKDEP, auto_gen, 
# auto_gen_others, depends, objs, extra_objs, static_modules, 
# static_modules_path, LD_RPATH
# (all this must  be defined previously!,  see Makefile.defs & Makefile)
# Optional: SER_LIBS - list of ser libraries that will be automatically
#  built if necessary. Format: path/shortname, where shortname is the 
#  name passed to -l (e.g. for libprint.so the shortname is print)
#
# History:
# --------
#  2007-03-29  set rpath differently for make install
#              automatically build listed SER_LIBS if needed (andrei)
#



#implicit rules
%.o:%.c  $(ALLDEP)
	$(CC) $(CFLAGS) $(INCLUDES) $(DEFS) -c $< -o $@

%.d: %.c $(ALLDEP)
	@set -e; $(MKDEP) $(CFLAGS) $(INCLUDES) $(DEFS) $< \
	|  sed 's#\(\($*D)\)\?$(*F)\)\.o[ :]*#$*.o $@ : #g' > $@; \
	[ -s $@ ] || rm -f $@

# use RPATH and SER_LIBS if needed (make install and the module depends
# on some ser libs)

ifneq	($(SER_LIBS),)
# abspath & realpath don't seem to work on darwin
SER_LIBS_DIRS:=$(dir $(SER_LIBS))
ifneq	(,$(filter install install% %install, $(MAKECMDGOALS)))
lib_compile_for_install=yes
expected_lib_ipath=$(lib_target)
else
lib_compile_for_install=no
# function: expected_lib_ipath ser_lib_dir
expected_lib_ipath=$(1)
endif
ifneq	($(LD_RPATH),)
ifneq	(,$(filter install install% %install, $(MAKECMDGOALS)))
SER_RPATH_LST:=$(lib_target)
else
SER_RPATH_LST:=$(realpath $(dir $(SER_LIBS)))
endif
ifneq	($(strip $(SER_RPATH_LST)),)
SER_RPATH:=$(addprefix $(LD_RPATH),$(SER_RPATH_LST))
endif
endif
ifeq ($(OS), darwin)
SER_IPATH_LST:=$(addsuffix /libiname.lst,$(SER_LIBS_DIRS))
#$(warning $(NAME) DARWIN, SER_LIBS=$(SER_LIBS), $(SER_LIBS_DIRS), ipath_lst=$(SER_IPATH_LST))
endif
endif

ALL_LIBS=$(LIBS)

ifeq	(,$(filter clean %clean clean% proper %proper proper%, $(MAKECMDGOALS)))
ifneq ($(SER_LIBS),)
-include librpath.lst
ifneq ($(SER_RPATH_LST), $(LIB_RPATH_LST))
$(shell rm -f librpath.lst)
endif
endif

SER_LIBS_DEPS:= \
	$(foreach l, $(SER_LIBS), $(dir $l)$(LIB_PREFIX)$(notdir $l)$(LIB_SUFFIX))
ALL_LIBS+=$(foreach l, $(SER_LIBS), -L$(dir $l) -l$(notdir $l))

$(NAME): $(SER_LIBS_DEPS) librpath.lst

$(SER_LIBS_DEPS):
	$(MAKE) -wC $(dir $@)  compile_for_install=$(lib_compile_for_install)


ifneq ($(SER_IPATH_LST),)

$(NAME): $(SER_IPATH_LST) 

$(SER_IPATH_LST): FORCE
	@if grep \
		"COMPILED_INAME:=$(call expected_lib_ipath,$(shell cd $(@D); pwd))" \
		$(@) 1>/dev/null 2>/dev/null ; \
	then :; \
	else \
		echo "re-building $(@D)" ; \
		$(MAKE) -wC $(@D) compile_for_install=$(lib_compile_for_install) ; \
	fi

.PHONY: FORCE
FORCE:

.PHONY: FORCE-BUILD-LIBS
FORCE-BUILD-LIBS:
	@for r in $(SER_LIBS_DIRS) ; do \
		echo building lib $$r; \
		$(MAKE) -wC $$r compile_for_install=$(lib_compile_for_install) ; \
	done

endif

endif

# normal rules
$(NAME): $(objs) $(ALLDEP)
	$(LD) $(LDFLAGS) $(objs) $(extra_objs) $(ALL_LIBS) $(SER_RPATH) -o $(NAME)


librpath.lst: $(ALLDEP)
	@echo LIB_RPATH_LST:=$(SER_RPATH_LST) >librpath.lst

.PHONY: all
all: $(NAME) modules

.PHONY: dep
dep: $(depends)

.PHONY: static
static: $(objs)

.PHONY: clean
clean: 
	-@rm -f $(objs) $(NAME) $(objs:.o=.il) librpath.lst 2>/dev/null
	-@for r in $(modules) $(static_modules_path) "" ; do \
		if [ -d "$$r" ]; then \
			echo "module $$r" ; \
			$(MAKE) -C $$r clean ; \
			$(MAKE) -C $$r/doc clean ; \
		fi ; \
	done 
	@if [ -n "$(modules)" ]; then \
		for r in $(utils_compile) "" ; do \
			if [ -d "$$r" ]; then \
				 $(MAKE) -C $$r clean ; \
			fi ; \
		done \
	fi

.PHONY: proper
.PHONY: distclean
.PHONY: realclean
proper realclean distclean: clean 
	-@rm -f $(depends) $(auto_gen) $(auto_gen_others) 2>/dev/null
	-@for r in $(modules) "" ; do \
		if [ -d "$$r" ]; then \
			 $(MAKE) -C $$r proper ; \
		        $(MAKE) -C $$r/doc proper ; \
		fi ; \
	done 
	@if [ -n "$(modules)" ]; then \
		for r in $(utils_compile) "" ; do \
			if [ -d "$$r" ]; then \
				 $(MAKE) -C $$r proper ; \
			fi ; \
		done \
	fi

.PHONY: mantainer-cleaan
mantainer-clean: distclean
	-rm -f TAGS tags *.dbg .*.swp
	-@for r in $(modules) "" ; do \
		if [ -d "$$r" ]; then \
			$(MAKE) -C $$r mantainer-clean; \
		fi ; \
	 done 


.PHONY: TAGS
TAGS:
	$(MKTAGS) 
	
ifeq (,$(MAKECMDGOALS))
-include $(depends)
endif
ifneq (,$(filter-out clean proper distclean realclean mantainer-clean TAGS \
		tar modules, $(MAKECMDGOALS)))
-include $(depends)
endif