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