tcp: minor optimization
[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, 
11 # auto_gen_others, depends, objs, extra_objs, static_modules, 
12 # static_modules_path, 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 ifneq ($(strip $(C_DEFS) $(DEFS)), $(strip $(CFG_DEFS)))
35 #$(warning different defs: <$(strip $(C_DEFS) $(DEFS))> != )
36 #$(warning               : <$(strip $(CFG_DEFS))>)
37 $(shell rm -f makecfg.lst)
38 endif
39 ifneq ($(strip $(C_INCLUDES) $(INCLUDES)), $(strip $(CFG_INCLUDES)))
40 $(shell rm -f makecfg.lst)
41 endif
42 endif
43
44 ALLDEP+=makecfg.lst
45
46 #implicit rules
47 %.o:%.c  $(ALLDEP)
48         $(CC) $(CFLAGS) $(C_INCLUDES) $(INCLUDES) $(C_DEFS) $(DEFS) -c $< -o $@
49
50 %.d: %.c $(ALLDEP)
51         @set -e; $(MKDEP) $(CFLAGS) $(C_INCLUDES) $(INCLUDES) $(C_DEFS) $(DEFS) $<\
52             |  sed 's#\(\($(*D)/\)\{0,1\}$(*F)\)\.o[ :]*#$*.o $@ : #g' > $@; \
53             test -s $@ || ( rm -f $@; false )
54
55 # use RPATH and SER_LIBS if needed (make install and the module depends
56 # on some ser libs)
57
58 ifneq   ($(SER_LIBS),)
59 # abspath & realpath don't seem to work on darwin
60 SER_LIBS_DIRS:=$(dir $(SER_LIBS))
61 ifneq   (,$(filter install install% %install, $(MAKECMDGOALS)))
62 lib_compile_for_install=yes
63 expected_lib_ipath=$(lib_target)
64 else
65 lib_compile_for_install=$(compile_for_install)
66 # function: expected_lib_ipath ser_lib_dir
67 expected_lib_ipath=$(1)
68 endif
69 ifneq   ($(LD_RPATH),)
70 ifneq   (,$(filter install install% %install, $(MAKECMDGOALS)))
71 SER_RPATH_LST:=$(lib_target)
72 else
73 SER_RPATH_LST:=$(realpath $(dir $(SER_LIBS)))
74 endif
75 ifneq   ($(strip $(SER_RPATH_LST)),)
76 SER_RPATH:=$(addprefix $(LD_RPATH),$(SER_RPATH_LST))
77 endif
78 endif
79 ifeq ($(OS), darwin)
80 SER_IPATH_LST:=$(addsuffix /libiname.lst,$(SER_LIBS_DIRS))
81 #$(warning $(NAME) DARWIN, SER_LIBS=$(SER_LIBS), $(SER_LIBS_DIRS), ipath_lst=$(SER_IPATH_LST))
82 endif
83 endif
84
85 ALL_LIBS=$(LIBS)
86
87 ifeq    (,$(filter clean %clean clean% proper %proper proper%, $(MAKECMDGOALS)))
88 ifneq ($(SER_LIBS),)
89 -include librpath.lst
90 ifneq ($(SER_RPATH_LST), $(LIB_RPATH_LST))
91 $(shell rm -f librpath.lst)
92 endif
93 endif
94
95 SER_LIBS_DEPS:= \
96         $(foreach l, $(SER_LIBS), $(dir $l)$(LIB_PREFIX)$(notdir $l)$(LIB_SUFFIX))
97 ALL_LIBS+=$(foreach l, $(SER_LIBS), -L$(dir $l) -l$(notdir $l))
98
99 $(NAME): librpath.lst $(SER_LIBS_DEPS)
100
101 $(SER_LIBS_DEPS): FORCE
102         $(MAKE) -wC $(dir $@)  compile_for_install=$(lib_compile_for_install)
103
104 .PHONY: FORCE
105 FORCE:
106
107 ifneq ($(SER_IPATH_LST),)
108
109 $(NAME): $(SER_IPATH_LST) 
110
111 $(SER_IPATH_LST): FORCE
112         @if grep \
113                 "COMPILED_INAME:=$(call expected_lib_ipath,$(shell cd $(@D); pwd))" \
114                 $(@) 1>/dev/null 2>/dev/null ; \
115         then :; \
116         else \
117                 echo "re-building $(@D)" ; \
118                 $(MAKE) -wC $(@D) compile_for_install=$(lib_compile_for_install) ; \
119         fi
120
121 .PHONY: FORCE-BUILD-LIBS
122 FORCE-BUILD-LIBS:
123         @for r in $(SER_LIBS_DIRS) ; do \
124                 echo building lib $$r; \
125                 $(MAKE) -wC $$r compile_for_install=$(lib_compile_for_install) ; \
126         done
127
128 endif
129
130 endif
131
132 # normal rules
133 $(NAME): $(objs) $(ALLDEP)
134         $(LD) $(LDFLAGS) $(objs) $(extra_objs) $(ALL_LIBS) $(SER_RPATH) -o $(NAME)
135
136
137 librpath.lst: $(ALLDEP)
138         @echo LIB_RPATH_LST:=$(SER_RPATH_LST) >librpath.lst
139
140 makecfg.lst:
141         @echo CFG_DEFS:=\
142                 $(subst ',\', $(subst ",\", $(strip $(C_DEFS) $(DEFS)))) >>$@
143         @echo CFG_INCLUDES:=\
144                 $(subst ',\', $(subst ",\", $(strip $(C_INCLUDES) $(INCLUDES)))) >>$@
145 .PHONY: all
146 all: $(NAME) modules
147
148 .PHONY: dep
149 dep: $(depends)
150
151 .PHONY: static
152 static: $(objs)
153
154 .PHONY: clean
155 clean: 
156         -@rm -f $(objs) $(NAME) $(objs:.o=.il) librpath.lst 2>/dev/null
157         -@for r in $(modules) $(static_modules_path) "" ; do \
158                 if [ -d "$$r" ]; then \
159                         echo "module $$r" ; \
160                         $(MAKE) -C $$r clean ; \
161                         $(MAKE) -C $$r/doc clean ; \
162                 fi ; \
163         done 
164         @if [ -n "$(modules)" ]; then \
165                 for r in $(utils_compile) "" ; do \
166                         if [ -d "$$r" ]; then \
167                                  $(MAKE) -C $$r clean ; \
168                         fi ; \
169                 done \
170         fi
171
172 .PHONY: proper
173 .PHONY: distclean
174 .PHONY: realclean
175 proper realclean distclean: mrproper
176
177 mrproper: clean 
178         -@rm -f $(depends) $(auto_gen) $(auto_gen_others) \
179                         makecfg.lst 2>/dev/null
180         -@for r in $(modules) "" ; do \
181                 if [ -d "$$r" ]; then \
182                         $(MAKE) -C $$r proper ; \
183                         $(MAKE) -C $$r/doc proper ; \
184                 fi ; \
185         done 
186         @if [ -n "$(modules)" ]; then \
187                 for r in $(utils_compile) "" ; do \
188                         if [ -d "$$r" ]; then \
189                                  $(MAKE) -C $$r proper ; \
190                         fi ; \
191                 done \
192         fi
193
194 .PHONY: mantainer-cleaan
195 mantainer-clean: distclean
196         -rm -f TAGS tags *.dbg .*.swp
197         -@for r in $(modules) "" ; do \
198                 if [ -d "$$r" ]; then \
199                         $(MAKE) -C $$r mantainer-clean; \
200                 fi ; \
201          done 
202
203
204 .PHONY: doxygen
205 doxygen:
206         -@mkdir -p $(doxygen_dir)
207         doxygen ./$(COREPATH)/doc/doxygen/ser.doxygen
208
209
210 .PHONY: clean_doxygen
211 clean_doxygen:
212         -@rm -rf $(doxygen_dir)/{xml,man,rtf,latex,html}
213         -@rmdir --ignore-fail-on-non-empty -p $(doxygen_dir) || true
214
215
216 .PHONY: TAGS
217 TAGS:
218         $(MKTAGS) 
219         
220         
221         
222 ifeq (,$(MAKECMDGOALS))
223 -include $(depends)
224 else
225 ifeq (,$(strip $(nodep_targets)))
226 include Makefile.targets
227 endif
228 ifneq (,$(filter-out $(nodep_targets),$(MAKECMDGOALS)))
229 -include $(depends)
230 endif
231 endif # ifeq (,$(MAKECMDGOALS))