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