512f828f62753c6789f8f4a3bab23f46515b26eb
[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 quote:= "
80 escall= $(subst $$,\$$,$(subst $(quote),\$(quote),$1))
81 exec_cmd= $(if $($(quiet)_cmd_$(1)),\
82                         @echo "$(call escall,$($(quiet)_cmd_$(1)))" ;) $(cmd_$(1))
83
84 #implicit rules
85 %.o:%.c  $(ALLDEP)
86         $(call exec_cmd,CC)
87
88 %.d: %.c $(ALLDEP)
89         @set -e; $(MKDEP) $(CFLAGS) $(C_INCLUDES) $(INCLUDES) $(C_DEFS) $(DEFS) $<\
90             |  sed 's#\(\($(*D)/\)\{0,1\}$(*F)\)\.o[ :]*#$*.o $@ : #g' > $@; \
91             test -s $@ || ( rm -f $@; false )
92
93 # use RPATH and SER_LIBS if needed (make install and the module depends
94 # on some ser libs)
95
96 ifneq   ($(SER_LIBS),)
97 # abspath & realpath don't work on make <= 3.80
98 SER_LIBS_DIRS:=$(dir $(SER_LIBS))
99 ifneq   (,$(filter install install% %install, $(MAKECMDGOALS)))
100 lib_compile_for_install=yes
101 expected_lib_ipath=$(lib_target)
102 else
103 lib_compile_for_install=$(compile_for_install)
104 # function: expected_lib_ipath ser_lib_dir
105 expected_lib_ipath=$(1)
106 endif
107 ifneq   ($(LD_RPATH),)
108 ifneq   (,$(filter install install% %install, $(MAKECMDGOALS)))
109 SER_RPATH_LST:=$(lib_target)
110 else
111 # realpath is not supported in make 3.80 or older
112 ifeq (,$(filter-out 3.80 3.80.%,$(MAKE_VERSION)))
113 fullpath=$(shell cd $(1); pwd)
114 else
115 fullpath=$(realpath $(1))
116 endif
117
118 SER_RPATH_LST:=$(call fullpath,$(dir $(SER_LIBS)))
119 endif
120 ifneq   ($(strip $(SER_RPATH_LST)),)
121 SER_RPATH:=$(addprefix $(LD_RPATH),$(SER_RPATH_LST))
122 endif
123 endif
124 ifeq ($(OS), darwin)
125 SER_IPATH_LST:=$(addsuffix /libiname.lst,$(SER_LIBS_DIRS))
126 #$(warning $(NAME) DARWIN, SER_LIBS=$(SER_LIBS), $(SER_LIBS_DIRS), ipath_lst=$(SER_IPATH_LST))
127 endif
128 endif
129
130 ALL_LIBS=$(LIBS)
131
132 ifeq    (,$(filter clean %clean clean% proper %proper proper%, $(MAKECMDGOALS)))
133 ifneq ($(SER_LIBS),)
134 -include librpath.lst
135 ifneq ($(SER_RPATH_LST), $(LIB_RPATH_LST))
136 $(shell rm -f librpath.lst)
137 endif
138 endif
139
140 SER_LIBS_DEPS:= \
141         $(foreach l, $(SER_LIBS), $(dir $l)$(LIB_PREFIX)$(notdir $l)$(LIB_SUFFIX))
142 ALL_LIBS+=$(foreach l, $(SER_LIBS), -L$(dir $l) -l$(notdir $l))
143
144 $(NAME): librpath.lst $(SER_LIBS_DEPS)
145
146 $(SER_LIBS_DEPS): FORCE
147         @$(MAKE) -wC $(dir $@)  compile_for_install=$(lib_compile_for_install) \
148                 NOREBUILD_DEFS="$(NOREBUILD_DEFS) $(LIB_NOREBUILD_DEFS)" \
149                 NOREBUILD_INCLUDES="$(NOREBUILD_INCLUDES) $(LIB_NOREBUILD_INCLUDES)"
150
151 .PHONY: FORCE
152 FORCE:
153
154 ifneq ($(SER_IPATH_LST),)
155
156 $(NAME): $(SER_IPATH_LST) 
157
158 $(SER_IPATH_LST): FORCE
159         @if grep \
160                 "COMPILED_INAME:=$(call expected_lib_ipath,$(shell cd $(@D); pwd))" \
161                 $(@) 1>/dev/null 2>/dev/null ; \
162         then :; \
163         else \
164                 $(call oecho,"re-building $(@D)" ;) \
165                 $(MAKE) -wC $(@D) compile_for_install=$(lib_compile_for_install) ; \
166         fi
167
168 .PHONY: FORCE-BUILD-LIBS
169 FORCE-BUILD-LIBS:
170         @for r in $(SER_LIBS_DIRS) ; do \
171                 $(call oecho,building lib $$r ;) \
172                 $(MAKE) -wC $$r compile_for_install=$(lib_compile_for_install) ; \
173         done
174
175 endif
176
177 endif
178
179 # normal rules
180 $(NAME): $(objs) $(ALLDEP)
181         $(call exec_cmd,LD)
182
183
184 librpath.lst: $(ALLDEP)
185         @echo LIB_RPATH_LST:=$(SER_RPATH_LST) >librpath.lst
186
187 makecfg.lst:
188         @echo "CFG_DEFS:=$(call escall,$(strip \
189                         $(filter-out $(NOREBUILD_DEFS), $(C_DEFS) $(DEFS))))" >>$@
190         @echo "CFG_INCLUDES:=$(call escall,$(strip \
191                         $(filter-out $(NOREBUILD_INCLUDES),\
192                                 $(C_INCLUDES) $(INCLUDES))))" >>$@
193 .PHONY: all
194 all: $(NAME) every-module
195
196 .PHONY: dep
197 dep: $(depends)
198
199 .PHONY: static
200 static: $(objs)
201
202 # clean only the current directory (no modules or utils)
203 # (it's usefull to have it separated from clean for speeding up make proper)
204 .PHONY: local-clean
205 local-clean:
206         -@rm -f $(objs) $(NAME) $(objs:.o=.il) librpath.lst 2>/dev/null
207
208 .PHONY: clean
209 clean: local-clean
210
211 .PHONY: clean-modules
212 clean-modules:
213         -@for r in $(cmodules) $(static_modules_path) "" ; do \
214                 if [ -d "$$r" ]; then \
215                         $(call oecho,"module $$r" ;) \
216                         $(MAKE) -C "$$r" clean ; \
217                         [ -d "$$r"/doc/Makefile ] && $(MAKE) -C "$$r"/doc clean ; \
218                 fi ; \
219         done
220
221 .PHONY: clean-utils
222 clean-utils:
223         @if [ -n "$(cmodules)" ]; then \
224                 for r in $(utils_compile) "" ; do \
225                         if [ -d "$$r" ]; then \
226                                  $(MAKE) -C "$$r" clean ; \
227                         fi ; \
228                 done \
229         fi
230
231 # make proper for the local directory
232 .PHONY: proper
233 .PHONY: distclean
234 .PHONY: realclean
235 .PHONY: maintainer-clean
236 proper distclean realclean maintainer-clean: local-clean
237         -@rm -f $(depends) $(auto_gen) $(auto_gen_others) $(auto_gen_keep) \
238                         makecfg.lst 2>/dev/null
239
240 maintainer-clean: clean-tmp
241
242 .PHONY: proper-modules
243 .PHONY: distclean-modules
244 .PHONY: realclean-modules
245 .PHONY: maintainer-clean-modules
246 proper-modules realclean-modules distclean-modules maintainer-clean-modules: \
247  clean_target=$(patsubst %-modules,%,$@)
248 proper-modules realclean-modules distclean-modules maintainer-clean-modules:
249         -@for r in $(cmodules) "" ; do \
250                 if [ -d "$$r" ]; then \
251                         $(MAKE) -C "$$r" $(clean_target); \
252                         [ -r "$$r"/doc/Makefile ] && $(MAKE) -C "$$r"/doc $(clean_target);\
253                 fi ; \
254         done
255
256 .PHONY: proper-utils
257 .PHONY: distclean-utils
258 .PHONY: realclean-utils
259 .PHONY: maintainer-clean-utils
260 proper-utils realclean-utils distclean-utils maintainer-clean-utils: \
261  clean_target=$(patsubst %-utils,%,$@)
262 proper-utils realclean-utils distclean-utils maintainer-clean-utils:
263         @if [ -n "$(cmodules)" ]; then \
264                 for r in $(utils_compile) "" ; do \
265                         if [ -d "$$r" ]; then \
266                                  $(MAKE) -C "$$r" $(clean_target); \
267                         fi ; \
268                 done \
269         fi
270
271 .PHONY: clean-tmp
272 clean-tmp:
273         -@rm -f TAGS tags *.dbg .*.swp
274
275
276 .PHONY: doxygen
277 doxygen:
278         -@mkdir -p $(doxygen_dir)
279         doxygen ./$(COREPATH)/doc/doxygen/ser.doxygen
280
281
282 .PHONY: clean_doxygen
283 clean_doxygen:
284         -@rm -rf $(doxygen_dir)/{xml,man,rtf,latex,html}
285         -@rmdir --ignore-fail-on-non-empty -p $(doxygen_dir) || true
286
287
288 .PHONY: TAGS
289 TAGS:
290         $(MKTAGS) 
291         
292         
293         
294 ifeq (,$(MAKECMDGOALS))
295 -include $(depends)
296 else
297 ifeq (,$(strip $(nodep_targets)))
298 include $(COREPATH)/Makefile.targets
299 endif
300 ifneq (,$(filter-out $(nodep_targets),$(MAKECMDGOALS)))
301 -include $(depends)
302 endif
303 endif # ifeq (,$(MAKECMDGOALS))