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