Makefile.rules 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #
  2. # $Id$
  3. #
  4. #
  5. # common Makefile rules, included by main Makefile & the modules
  6. #
  7. #
  8. # Uses: NAME, ALLDEP, CC, CFLAGS, C_DEFS, DEFS, C_INCLUDES, INCLUDES, LIBS,
  9. # MKDEP, auto_gen, auto_gen_others, auto_gen_keep,
  10. # depends, objs, extra_objs, static_modules, static_modules_path,
  11. # LD_RPATH
  12. # (all this must be defined previously!, see Makefile.defs & Makefile)
  13. # Optional: SER_LIBS - list of ser libraries that will be automatically
  14. # built if necessary. Format: path/shortname, where shortname is the
  15. # name passed to -l (e.g. for libprint.so the shortname is print)
  16. #
  17. # History:
  18. # --------
  19. # 2007-03-29 set rpath differently for make install
  20. # automatically build listed SER_LIBS if needed (andrei)
  21. # 2008-06-23 automatically rebuild if make time defines or includes
  22. # changed (via makecfg.lst)
  23. # 2009-03-10 support for C_DEFS and C_INCLUDES (DEFS and INCLUDES are now
  24. # used only for "temporary" defines/includes inside modules or
  25. # libs, C_DEFS and C_INCLUDES are used for the common stuff)
  26. # (andrei)
  27. # 2010-03-09 generate dependencies when compiling .o instead of on
  28. # include .d and fix build errors when a .h is moved
  29. # support for using MKDEP="makedepend-f-" (andrei)
  30. # 2010-03-10 support for on the-fly dependency generation (while compiling,
  31. # see CC_MKDEP_OPTS) (andrei)
  32. # check if the saved cfg corresponds with the current one
  33. # (if not rebuild everything)
  34. ifeq (,$(filter $(nodep_targets),$(MAKECMDGOALS)))
  35. -include makecfg.lst
  36. # if trying to build a lib automatically and the lib is already compiled,
  37. # don't rebuild it if the only differences in DEFS or INCLUDES are covered
  38. # by LIB_NOREBUILD_DEFS/LIB_NOREBUILD_INCLUDES
  39. LIB_NOREBUILD_DEFS=
  40. # don't rebuild if the differences are covered by NOREBUILD_DEFS or
  41. # NOREBUILD_INCLUDES
  42. ifneq ($(strip $(filter-out $(NOREBUILD_DEFS),\
  43. $(C_DEFS) $(DEFS))),$(strip $(CFG_DEFS)))
  44. #$(warning different defs: <$(strip $(C_DEFS) $(DEFS))> != )
  45. #$(warning : <$(strip $(CFG_DEFS))>)
  46. $(shell rm -f makecfg.lst)
  47. endif
  48. ifneq ($(strip $(filter-out $(NOREBUILD_INCLUDES),\
  49. $(C_INCLUDES) $(INCLUDES))),$(strip $(CFG_INCLUDES)))
  50. $(shell rm -f makecfg.lst)
  51. endif
  52. endif
  53. ALLDEP+=makecfg.lst
  54. # returns current type: "" core/unknown, "M" module, "L" libray, "U" util
  55. crt_type=$(if $(MOD_NAME),M,$(if $(LIB_NAME),L,$(if $(UTIL_NAME),U)))
  56. cmd_CC=$(CC) $(CFLAGS) $(C_INCLUDES) $(INCLUDES) $(C_DEFS) $(DEFS) -c $< -o $@
  57. cmd_LD=$(LD) $(LDFLAGS) $(objs) $(extra_objs) $(ALL_LIBS) $(SER_RPATH) \
  58. -o $(NAME)
  59. ifeq (,$(CC_MKDEP_OPTS))
  60. # if CCC_MKDEP_OPTS is empty => CC cannot generate dependencies on the fly
  61. cmd_MKDEP=$(MKDEP) $(filter -D% -I%,$(CFLAGS)) $(C_INCLUDES) $(INCLUDES) \
  62. $(C_DEFS) $(DEFS) $< \
  63. | sed -e 's/\#.*//' -e '/:[ ]*$$/d' -e '/^[ ]*$$/d' \
  64. -e 's|.*:|$@: $$(wildcard |' -e 's/\([^\\]\)$$/\1)/'> $*.d
  65. else
  66. # deps can be generated on the fly by cmd_CC
  67. cmd_CC+=$(CC_MKDEP_OPTS)
  68. # no MKDEP command any more
  69. cmd_MKDEP=
  70. endif # CC_MKDEP_OPTS
  71. # what will be displayed if quiet==silent
  72. silent_cmd_CC=CC ($(CC)) [$(strip $(crt_type) $(NAME))] $@
  73. silent_cmd_LD=LD ($(LD)) [$(strip $(crt_type) $(NAME))] $@
  74. ifneq (,$(filter 0 no off verbose noisy, $(Q) $(QUIET)))
  75. override Q:=
  76. quiet=verbose
  77. #shell optional print
  78. oecho=echo $(1)
  79. else
  80. quiet=silent
  81. Q=1
  82. MAKE+= --no-print-directory
  83. #shell optional print
  84. oecho=
  85. endif
  86. module_make= if [ -n "$(1)" -a -r "$(1)/Makefile" ]; then \
  87. $(call oecho, "" ;) \
  88. $(call oecho, "" ;) \
  89. if $$(MAKE) -C $(1) $(2) || [ ${err_fail} != 1 ] ; then \
  90. :; \
  91. else \
  92. exit 1; \
  93. fi ; \
  94. fi ;
  95. quote:= "
  96. escall= $(subst $$,\$$,$(subst $(quote),\$(quote),$1))
  97. exec_cmd= $(if $($(quiet)_cmd_$(1)),\
  98. @echo "$(call escall,$($(quiet)_cmd_$(1)))" ;) $(cmd_$(1))
  99. #implicit rules
  100. %.o:%.c $(ALLDEP)
  101. $(call exec_cmd,CC)
  102. @$(call cmd_MKDEP)
  103. # use RPATH and SER_LIBS if needed (make install and the module depends
  104. # on some ser libs)
  105. ifneq ($(SER_LIBS),)
  106. # abspath & realpath don't work on make <= 3.80
  107. SER_LIBS_DIRS:=$(dir $(SER_LIBS))
  108. ifneq (,$(filter install install% %install, $(MAKECMDGOALS)))
  109. lib_compile_for_install=yes
  110. expected_lib_ipath=$(lib_target)
  111. else
  112. lib_compile_for_install=$(compile_for_install)
  113. # function: expected_lib_ipath ser_lib_dir
  114. expected_lib_ipath=$(1)
  115. endif
  116. ifneq ($(LD_RPATH),)
  117. ifneq (,$(filter install install% %install, $(MAKECMDGOALS)))
  118. SER_RPATH_LST:=$(lib_target)
  119. else
  120. # realpath is not supported in make 3.80 or older
  121. ifeq (,$(filter-out 3.80 3.80.%,$(MAKE_VERSION)))
  122. fullpath=$(shell cd $(1); pwd)
  123. else
  124. fullpath=$(realpath $(1))
  125. endif
  126. SER_RPATH_LST:=$(call fullpath,$(dir $(SER_LIBS)))
  127. endif
  128. ifneq ($(strip $(SER_RPATH_LST)),)
  129. SER_RPATH:=$(addprefix $(LD_RPATH),$(SER_RPATH_LST))
  130. endif
  131. endif
  132. ifeq ($(OS), darwin)
  133. SER_IPATH_LST:=$(addsuffix /libiname.lst,$(SER_LIBS_DIRS))
  134. #$(warning $(NAME) DARWIN, SER_LIBS=$(SER_LIBS), $(SER_LIBS_DIRS), ipath_lst=$(SER_IPATH_LST))
  135. endif
  136. endif
  137. ALL_LIBS=$(LIBS)
  138. ifeq (,$(filter clean %clean clean% proper %proper proper%, $(MAKECMDGOALS)))
  139. ifneq ($(SER_LIBS),)
  140. -include librpath.lst
  141. ifneq ($(SER_RPATH_LST), $(LIB_RPATH_LST))
  142. $(shell rm -f librpath.lst)
  143. endif
  144. endif
  145. SER_LIBS_DEPS:= \
  146. $(foreach l, $(SER_LIBS), $(dir $l)$(LIB_PREFIX)$(notdir $l)$(LIB_SUFFIX))
  147. ALL_LIBS+=$(foreach l, $(SER_LIBS), -L$(dir $l) -l$(notdir $l))
  148. $(NAME): librpath.lst $(SER_LIBS_DEPS)
  149. $(SER_LIBS_DEPS): FORCE
  150. @$(MAKE) -wC $(dir $@) compile_for_install=$(lib_compile_for_install) \
  151. NOREBUILD_DEFS="$(NOREBUILD_DEFS) $(LIB_NOREBUILD_DEFS)" \
  152. NOREBUILD_INCLUDES="$(NOREBUILD_INCLUDES) $(LIB_NOREBUILD_INCLUDES)"
  153. .PHONY: FORCE
  154. FORCE:
  155. ifneq ($(SER_IPATH_LST),)
  156. $(NAME): $(SER_IPATH_LST)
  157. $(SER_IPATH_LST): FORCE
  158. @if grep \
  159. "COMPILED_INAME:=$(call expected_lib_ipath,$(shell cd $(@D); pwd))" \
  160. $(@) 1>/dev/null 2>/dev/null ; \
  161. then :; \
  162. else \
  163. $(call oecho,"re-building $(@D)" ;) \
  164. $(MAKE) -wC $(@D) compile_for_install=$(lib_compile_for_install) ; \
  165. fi
  166. .PHONY: FORCE-BUILD-LIBS
  167. FORCE-BUILD-LIBS:
  168. @for r in $(SER_LIBS_DIRS) ; do \
  169. $(call oecho,building lib $$r ;) \
  170. $(MAKE) -wC $$r compile_for_install=$(lib_compile_for_install) ; \
  171. done
  172. endif
  173. endif
  174. # normal rules
  175. $(NAME): $(objs) $(ALLDEP)
  176. $(call exec_cmd,LD)
  177. librpath.lst: $(ALLDEP)
  178. @echo LIB_RPATH_LST:=$(SER_RPATH_LST) >librpath.lst
  179. makecfg.lst:
  180. @echo "CFG_DEFS:=$(call escall,$(strip \
  181. $(filter-out $(NOREBUILD_DEFS), $(C_DEFS) $(DEFS))))" >>$@
  182. @echo "CFG_INCLUDES:=$(call escall,$(strip \
  183. $(filter-out $(NOREBUILD_INCLUDES),\
  184. $(C_INCLUDES) $(INCLUDES))))" >>$@
  185. .PHONY: all
  186. all: $(NAME) every-module
  187. .PHONY: static
  188. static: $(objs)
  189. # clean only the current directory (no modules or utils)
  190. # (it's usefull to have it separated from clean for speeding up make proper)
  191. .PHONY: local-clean
  192. local-clean:
  193. -@rm -f $(objs) $(NAME) $(objs:.o=.il) librpath.lst 2>/dev/null
  194. .PHONY: clean
  195. clean: local-clean
  196. .PHONY: clean-modules
  197. clean-modules:
  198. -@for r in $(cmodules) $(static_modules_path) "" ; do \
  199. if [ -d "$$r" ]; then \
  200. $(call oecho,"module $$r" ;) \
  201. $(MAKE) -C "$$r" clean ; \
  202. [ -r "$$r"/doc/Makefile ] && $(MAKE) -C "$$r"/doc clean ; \
  203. fi ; \
  204. done
  205. # make proper for the local directory
  206. .PHONY: proper
  207. .PHONY: distclean
  208. .PHONY: realclean
  209. .PHONY: maintainer-clean
  210. proper distclean realclean maintainer-clean: local-clean
  211. -@rm -f $(depends) $(auto_gen) $(auto_gen_others) $(auto_gen_keep) \
  212. makecfg.lst 2>/dev/null
  213. maintainer-clean: clean-tmp
  214. .PHONY: proper-modules
  215. .PHONY: distclean-modules
  216. .PHONY: realclean-modules
  217. .PHONY: maintainer-clean-modules
  218. proper-modules realclean-modules distclean-modules maintainer-clean-modules: \
  219. clean_target=$(patsubst %-modules,%,$@)
  220. proper-modules realclean-modules distclean-modules maintainer-clean-modules:
  221. -@for r in $(cmodules) "" ; do \
  222. if [ -d "$$r" ]; then \
  223. $(MAKE) -C "$$r" $(clean_target); \
  224. [ -r "$$r"/doc/Makefile ] && $(MAKE) -C "$$r"/doc $(clean_target);\
  225. fi ; \
  226. done
  227. .PHONY: clean-tmp
  228. clean-tmp:
  229. -@rm -f TAGS tags *.dbg .*.swp
  230. .PHONY: doxygen
  231. doxygen:
  232. -@mkdir -p $(doxygen_dir)
  233. -@echo "Create Doxygen documentation"
  234. # disable call graphes, because of the DOT dependencies
  235. (cat ./$(COREPATH)/doc/doxygen/ser.doxygen; \
  236. echo "HAVE_DOT=no" ;\
  237. echo "PROJECT_NAME=SIP-ROUTER" ;\
  238. echo "PROJECT_NUMBER=$(NAME)-$(RELEASE)" )| doxygen -
  239. -@echo "Doxygen documentation created"
  240. .PHONY: clean_doxygen
  241. clean_doxygen:
  242. -@rm -rf $(doxygen_dir)/{xml,man,rtf,latex,html}
  243. -@rmdir --ignore-fail-on-non-empty -p $(doxygen_dir) || true
  244. .PHONY: TAGS
  245. TAGS:
  246. $(MKTAGS) --exclude="obsolete/*" -R .
  247. .PHONY: TAGS-ALL
  248. TAGS-ALL:
  249. $(MKTAGS) -R .
  250. ifeq (,$(MAKECMDGOALS))
  251. -include $(depends)
  252. else
  253. ifeq (,$(strip $(nodep_targets)))
  254. include $(COREPATH)/Makefile.targets
  255. endif
  256. ifneq (,$(filter-out $(nodep_targets),$(MAKECMDGOALS)))
  257. -include $(depends)
  258. endif
  259. endif # ifeq (,$(MAKECMDGOALS))