Makefile.rules
247c2b65
 #
 # $Id$
 #
 #
 # common Makefile rules, included by main Makefile & the  modules
 #
 
 #
f072585d
 # Uses: NAME, ALLDEP, CC, CFLAGS, C_DEFS, DEFS, C_INCLUDES, INCLUDES, LIBS, 
1849815b
 #       MKDEP, auto_gen, auto_gen_others, auto_gen_keep, 
 #       depends, objs, extra_objs, static_modules, static_modules_path,
 #       LD_RPATH
247c2b65
 # (all this must  be defined previously!,  see Makefile.defs & Makefile)
3922c022
 # 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)
5cace4ed
 #  2008-06-23  automatically rebuild if make time defines or includes
 #              changed (via makecfg.lst)
f072585d
 #  2009-03-10  support for C_DEFS and C_INCLUDES (DEFS and INCLUDES are now
 #              used only for "temporary" defines/includes inside modules or
 #              libs, C_DEFS and C_INCLUDES are used for the common stuff)
 #              (andrei)
5a9014ee
 #  2010-03-09  generate dependencies when compiling .o instead of on
 #               include .d and fix build errors when a .h is moved
 #              support for using MKDEP="makedepend-f-"      (andrei)
 #  2010-03-10  support for on the-fly dependency generation (while compiling,
 #               see CC_MKDEP_OPTS)  (andrei)
247c2b65
 
 
5cace4ed
 # check if the saved cfg corresponds with the current one
 # (if not rebuild everything)
7742b1d2
 ifeq (,$(filter $(nodep_targets),$(MAKECMDGOALS)))
5cace4ed
 -include makecfg.lst
19c54611
 # if trying  to build a lib automatically and the lib is already compiled,
 # don't rebuild it if the only differences in DEFS or INCLUDES are covered
 # by LIB_NOREBUILD_DEFS/LIB_NOREBUILD_INCLUDES
d240dd18
 LIB_NOREBUILD_DEFS=
19c54611
 
 # don't rebuild if the differences are covered by NOREBUILD_DEFS or 
 # NOREBUILD_INCLUDES
d240dd18
 ifneq ($(strip $(filter-out $(NOREBUILD_DEFS),\
 		$(C_DEFS) $(DEFS))),$(strip $(CFG_DEFS)))
d74483f8
 #$(warning different defs: <$(strip $(C_DEFS) $(DEFS))> != )
5cace4ed
 #$(warning               : <$(strip $(CFG_DEFS))>)
 $(shell rm -f makecfg.lst)
 endif
d240dd18
 ifneq ($(strip $(filter-out $(NOREBUILD_INCLUDES),\
 			$(C_INCLUDES) $(INCLUDES))),$(strip $(CFG_INCLUDES)))
5cace4ed
 $(shell rm -f makecfg.lst)
 endif
 endif
 
 ALLDEP+=makecfg.lst
247c2b65
 
e37e8d6a
 # returns current type: "" core/unknown, "M" module, "L" libray, "U" util
 crt_type=$(if $(MOD_NAME),M,$(if $(LIB_NAME),L,$(if $(UTIL_NAME),U)))
 
 cmd_CC=$(CC) $(CFLAGS) $(C_INCLUDES) $(INCLUDES) $(C_DEFS) $(DEFS) -c $< -o $@
 cmd_LD=$(LD) $(LDFLAGS) $(objs) $(extra_objs) $(ALL_LIBS) $(SER_RPATH) \
 	-o $(NAME)
 
5a9014ee
 ifeq (,$(CC_MKDEP_OPTS))
 # if CCC_MKDEP_OPTS is empty => CC cannot generate dependencies on the fly
 cmd_MKDEP=$(MKDEP) $(filter -D% -I%,$(CFLAGS)) $(C_INCLUDES) $(INCLUDES) \
 			$(C_DEFS) $(DEFS) $< \
 		|  sed	-e 's/\#.*//' -e '/:[ 	]*$$/d' -e '/^[ 	]*$$/d' \
 				-e 's|.*:|$@: $$(wildcard |' -e 's/\([^\\]\)$$/\1)/'> $*.d
 else
 # deps can be generated on the fly by cmd_CC
 cmd_CC+=$(CC_MKDEP_OPTS)
 # no MKDEP command any more
 cmd_MKDEP=
 endif # CC_MKDEP_OPTS
 
e37e8d6a
 # what will be displayed if quiet==silent
 silent_cmd_CC=CC ($(CC)) [$(strip $(crt_type) $(NAME))]		$@
 silent_cmd_LD=LD ($(LD)) [$(strip $(crt_type) $(NAME))]		$@
 
9f264d53
 ifneq (,$(filter 0 no off verbose noisy, $(Q) $(QUIET)))
 override Q:=
 quiet=verbose
 #shell optional print
 oecho=echo $(1)
 else
e37e8d6a
 quiet=silent
 Q=1
a91ba01f
 MAKE+= --no-print-directory
 #shell optional print
 oecho=
e37e8d6a
 endif
 
329e20e2
 module_make=	if [ -n "$(1)" -a -r "$(1)/Makefile" ]; then \
 			$(call oecho, "" ;) \
 			$(call oecho, "" ;) \
 			if  $$(MAKE) -C $(1) $(2) || [ ${err_fail} != 1 ] ; then \
 				:; \
 			else \
 				exit 1; \
 			fi ; \
 		fi ;
 
e37e8d6a
 quote:= "
d74483f8
 escall= $(subst $$,\$$,$(subst $(quote),\$(quote),$1))
e37e8d6a
 exec_cmd= $(if $($(quiet)_cmd_$(1)),\
 			@echo "$(call escall,$($(quiet)_cmd_$(1)))" ;) $(cmd_$(1))
 
247c2b65
 #implicit rules
04bb7a49
 %.o:%.c  $(ALLDEP)
e37e8d6a
 	$(call exec_cmd,CC)
5a9014ee
 	@$(call cmd_MKDEP)
247c2b65
 
3922c022
 # use RPATH and SER_LIBS if needed (make install and the module depends
 # on some ser libs)
 
c853f68a
 ifneq	($(SER_LIBS),)
a52cbf0a
 # abspath & realpath don't work on make <= 3.80
08067fd5
 SER_LIBS_DIRS:=$(dir $(SER_LIBS))
da33ba73
 ifneq	(,$(filter install install% %install, $(MAKECMDGOALS)))
 lib_compile_for_install=yes
 expected_lib_ipath=$(lib_target)
 else
b5b92e04
 lib_compile_for_install=$(compile_for_install)
da33ba73
 # function: expected_lib_ipath ser_lib_dir
 expected_lib_ipath=$(1)
 endif
c853f68a
 ifneq	($(LD_RPATH),)
da33ba73
 ifneq	(,$(filter install install% %install, $(MAKECMDGOALS)))
28b28d2c
 SER_RPATH_LST:=$(lib_target)
3922c022
 else
a52cbf0a
 # realpath is not supported in make 3.80 or older
 ifeq (,$(filter-out 3.80 3.80.%,$(MAKE_VERSION)))
 fullpath=$(shell cd $(1); pwd)
 else
 fullpath=$(realpath $(1))
 endif
 
 SER_RPATH_LST:=$(call fullpath,$(dir $(SER_LIBS)))
3922c022
 endif
 ifneq	($(strip $(SER_RPATH_LST)),)
 SER_RPATH:=$(addprefix $(LD_RPATH),$(SER_RPATH_LST))
c853f68a
 endif
 endif
da33ba73
 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
3922c022
 endif
 
 ALL_LIBS=$(LIBS)
 
da33ba73
 ifeq	(,$(filter clean %clean clean% proper %proper proper%, $(MAKECMDGOALS)))
3922c022
 ifneq ($(SER_LIBS),)
 -include librpath.lst
 ifneq ($(SER_RPATH_LST), $(LIB_RPATH_LST))
 $(shell rm -f librpath.lst)
 endif
da33ba73
 endif
3922c022
 
 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))
 
7742b1d2
 $(NAME): librpath.lst $(SER_LIBS_DEPS)
c853f68a
 
7742b1d2
 $(SER_LIBS_DEPS): FORCE
19c54611
 	@$(MAKE) -wC $(dir $@)  compile_for_install=$(lib_compile_for_install) \
 		NOREBUILD_DEFS="$(NOREBUILD_DEFS) $(LIB_NOREBUILD_DEFS)" \
 		NOREBUILD_INCLUDES="$(NOREBUILD_INCLUDES) $(LIB_NOREBUILD_INCLUDES)"
da33ba73
 
7742b1d2
 .PHONY: FORCE
 FORCE:
da33ba73
 
 ifneq ($(SER_IPATH_LST),)
 
 $(NAME): $(SER_IPATH_LST) 
 
 $(SER_IPATH_LST): FORCE
08067fd5
 	@if grep \
 		"COMPILED_INAME:=$(call expected_lib_ipath,$(shell cd $(@D); pwd))" \
 		$(@) 1>/dev/null 2>/dev/null ; \
da33ba73
 	then :; \
 	else \
a91ba01f
 		$(call oecho,"re-building $(@D)" ;) \
08067fd5
 		$(MAKE) -wC $(@D) compile_for_install=$(lib_compile_for_install) ; \
da33ba73
 	fi
 
 .PHONY: FORCE-BUILD-LIBS
 FORCE-BUILD-LIBS:
 	@for r in $(SER_LIBS_DIRS) ; do \
a91ba01f
 		$(call oecho,building lib $$r ;) \
08067fd5
 		$(MAKE) -wC $$r compile_for_install=$(lib_compile_for_install) ; \
da33ba73
 	done
 
 endif
3922c022
 
 endif
247c2b65
 
 # normal rules
dcb5f364
 $(NAME): $(objs) $(ALLDEP)
e37e8d6a
 	$(call exec_cmd,LD)
247c2b65
 
 
3922c022
 librpath.lst: $(ALLDEP)
 	@echo LIB_RPATH_LST:=$(SER_RPATH_LST) >librpath.lst
 
5cace4ed
 makecfg.lst:
d74483f8
 	@echo "CFG_DEFS:=$(call escall,$(strip \
 			$(filter-out $(NOREBUILD_DEFS), $(C_DEFS) $(DEFS))))" >>$@
 	@echo "CFG_INCLUDES:=$(call escall,$(strip \
d240dd18
 			$(filter-out $(NOREBUILD_INCLUDES),\
d74483f8
 				$(C_INCLUDES) $(INCLUDES))))" >>$@
247c2b65
 .PHONY: all
27cdfd2c
 all: $(NAME) every-module
247c2b65
 
404073d3
 .PHONY: static
 static: $(objs)
 
8ca5d8dd
 # clean only the current directory (no modules or utils)
 # (it's usefull to have it separated from clean for speeding up make proper)
 .PHONY: local-clean
 local-clean:
7742b1d2
 	-@rm -f $(objs) $(NAME) $(objs:.o=.il) librpath.lst 2>/dev/null
8ca5d8dd
 
 .PHONY: clean
 clean: local-clean
 
 .PHONY: clean-modules
 clean-modules:
27cdfd2c
 	-@for r in $(cmodules) $(static_modules_path) "" ; do \
a65ed385
 		if [ -d "$$r" ]; then \
a91ba01f
 			$(call oecho,"module $$r" ;) \
8ca5d8dd
 			$(MAKE) -C "$$r" clean ; \
413c76aa
 			[ -r "$$r"/doc/Makefile ] && $(MAKE) -C "$$r"/doc clean ; \
e7a9bd30
 		fi ; \
8ca5d8dd
 	done
 
 # make proper for the local directory
247c2b65
 .PHONY: proper
 .PHONY: distclean
 .PHONY: realclean
8ca5d8dd
 .PHONY: maintainer-clean
 proper distclean realclean maintainer-clean: local-clean
1849815b
 	-@rm -f $(depends) $(auto_gen) $(auto_gen_others) $(auto_gen_keep) \
7742b1d2
 			makecfg.lst 2>/dev/null
8ca5d8dd
 
 maintainer-clean: clean-tmp
 
 .PHONY: proper-modules
 .PHONY: distclean-modules
 .PHONY: realclean-modules
 .PHONY: maintainer-clean-modules
 proper-modules realclean-modules distclean-modules maintainer-clean-modules: \
  clean_target=$(patsubst %-modules,%,$@)
 proper-modules realclean-modules distclean-modules maintainer-clean-modules:
27cdfd2c
 	-@for r in $(cmodules) "" ; do \
a65ed385
 		if [ -d "$$r" ]; then \
8ca5d8dd
 			$(MAKE) -C "$$r" $(clean_target); \
 			[ -r "$$r"/doc/Makefile ] && $(MAKE) -C "$$r"/doc $(clean_target);\
e7a9bd30
 		fi ; \
8ca5d8dd
 	done
 
 .PHONY: clean-tmp
 clean-tmp:
 	-@rm -f TAGS tags *.dbg .*.swp
e7a9bd30
 
247c2b65
 
a51f9e36
 .PHONY: doxygen
 doxygen:
 	-@mkdir -p $(doxygen_dir)
0134586c
 	-@echo "Create Doxygen documentation"
 	# disable call graphes, because of the DOT dependencies
 	(cat ./$(COREPATH)/doc/doxygen/ser.doxygen; \
 	echo "HAVE_DOT=no" ;\
5a03489e
 	echo "PROJECT_NAME=SIP-ROUTER" ;\
0134586c
 	echo "PROJECT_NUMBER=$(NAME)-$(RELEASE)" )| doxygen -
 	-@echo "Doxygen documentation created"
a51f9e36
 
 .PHONY: clean_doxygen
 clean_doxygen:
 	-@rm -rf $(doxygen_dir)/{xml,man,rtf,latex,html}
 	-@rmdir --ignore-fail-on-non-empty -p $(doxygen_dir) || true
 
 
247c2b65
 .PHONY: TAGS
 TAGS:
8d5f8bbc
 	$(MKTAGS) --exclude="obsolete/*" -R .
 
 .PHONY: TAGS-ALL
 TAGS-ALL:
 	$(MKTAGS) -R .
 
 
 
04bb7a49
 ifeq (,$(MAKECMDGOALS))
a2dd69c1
 -include $(depends)
6567f6c7
 else
 ifeq (,$(strip $(nodep_targets)))
7d07255c
 include $(COREPATH)/Makefile.targets
04bb7a49
 endif
6567f6c7
 ifneq (,$(filter-out $(nodep_targets),$(MAKECMDGOALS)))
a2dd69c1
 -include $(depends)
247c2b65
 endif
6567f6c7
 endif # ifeq (,$(MAKECMDGOALS))