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