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