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