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