ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / scripts / Makefile.lib
1 # ===========================================================================
2 # kbuild: Generic definitions
3 # ===========================================================================
4
5 # Standard vars
6
7 comma   := ,
8 empty   :=
9 space   := $(empty) $(empty)
10
11 # Backward compatibility - to be removed...
12 extra-y += $(EXTRA_TARGETS)
13 # Figure out what we need to build from the various variables
14 # ===========================================================================
15
16 # When an object is listed to be built compiled-in and modular,
17 # only build the compiled-in version
18
19 obj-m := $(filter-out $(obj-y),$(obj-m))
20
21 # Libraries are always collected in one lib file.
22 # Filter out objects already built-in
23
24 lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
25
26
27 # Handle objects in subdirs
28 # ---------------------------------------------------------------------------
29 # o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
30 #   and add the directory to the list of dirs to descend into: $(subdir-y)
31 # o if we encounter foo/ in $(obj-m), remove it from $(obj-m) 
32 #   and add the directory to the list of dirs to descend into: $(subdir-m)
33
34 __subdir-y      := $(patsubst %/,%,$(filter %/, $(obj-y)))
35 subdir-y        += $(__subdir-y)
36 __subdir-m      := $(patsubst %/,%,$(filter %/, $(obj-m)))
37 subdir-m        += $(__subdir-m)
38 obj-y           := $(patsubst %/, %/built-in.o, $(obj-y))
39 obj-m           := $(filter-out %/, $(obj-m))
40
41 # Subdirectories we need to descend into
42
43 subdir-ym       := $(sort $(subdir-y) $(subdir-m))
44
45 # if $(foo-objs) exists, foo.o is a composite object 
46 multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
47 multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
48 multi-used   := $(multi-used-y) $(multi-used-m)
49 single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
50
51 # Build list of the parts of our composite objects, our composite
52 # objects depend on those (obviously)
53 multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
54 multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
55 multi-objs   := $(multi-objs-y) $(multi-objs-m)
56
57 # $(subdir-obj-y) is the list of objects in $(obj-y) which do not live
58 # in the local directory
59 subdir-obj-y := $(foreach o,$(obj-y),$(if $(filter-out $(o),$(notdir $(o))),$(o)))
60
61 # $(obj-dirs) is a list of directories that contain object files
62 obj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
63 obj-dirs += $(foreach f,$(host-progs), $(if $(dir $(f)),$(dir $(f))))
64 obj-dirs := $(strip $(sort $(filter-out ./,$(obj-dirs))))
65
66 # Replace multi-part objects by their individual parts, look at local dir only
67 real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y)
68 real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
69
70 # C code
71 # Executables compiled from a single .c file
72 host-csingle    := $(foreach m,$(host-progs),$(if $($(m)-objs),,$(m)))
73
74 # C executables linked based on several .o files
75 host-cmulti     := $(foreach m,$(host-progs),\
76                    $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
77
78 # Object (.o) files compiled from .c files
79 host-cobjs      := $(sort $(foreach m,$(host-progs),$($(m)-objs)))
80
81 # C++ code
82 # C++ executables compiled from at least on .cc file
83 # and zero or more .c files
84 host-cxxmulti   := $(foreach m,$(host-progs),$(if $($(m)-cxxobjs),$(m)))
85
86 # C++ Object (.o) files compiled from .cc files
87 host-cxxobjs    := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
88
89 # Shared libaries (only .c supported)
90 # Shared libraries (.so) - all .so files referenced in "xxx-objs"
91 host-cshlib     := $(sort $(filter %.so, $(host-cobjs)))
92 # Remove .so files from "xxx-objs"
93 host-cobjs      := $(filter-out %.so,$(host-cobjs))
94
95 #Object (.o) files used by the shared libaries
96 host-cshobjs    := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs))))
97
98 # Add subdir path
99
100 extra-y         := $(addprefix $(obj)/,$(extra-y))
101 always          := $(addprefix $(obj)/,$(always))
102 targets         := $(addprefix $(obj)/,$(targets))
103 obj-y           := $(addprefix $(obj)/,$(obj-y))
104 obj-m           := $(addprefix $(obj)/,$(obj-m))
105 lib-y           := $(addprefix $(obj)/,$(lib-y))
106 subdir-obj-y    := $(addprefix $(obj)/,$(subdir-obj-y))
107 real-objs-y     := $(addprefix $(obj)/,$(real-objs-y))
108 real-objs-m     := $(addprefix $(obj)/,$(real-objs-m))
109 single-used-m   := $(addprefix $(obj)/,$(single-used-m))
110 multi-used-y    := $(addprefix $(obj)/,$(multi-used-y))
111 multi-used-m    := $(addprefix $(obj)/,$(multi-used-m))
112 multi-objs-y    := $(addprefix $(obj)/,$(multi-objs-y))
113 multi-objs-m    := $(addprefix $(obj)/,$(multi-objs-m))
114 subdir-ym       := $(addprefix $(obj)/,$(subdir-ym))
115 obj-dirs        := $(addprefix $(obj)/,$(obj-dirs))
116 host-progs      := $(addprefix $(obj)/,$(host-progs))
117 host-csingle    := $(addprefix $(obj)/,$(host-csingle))
118 host-cmulti     := $(addprefix $(obj)/,$(host-cmulti))
119 host-cobjs      := $(addprefix $(obj)/,$(host-cobjs))
120 host-cxxmulti   := $(addprefix $(obj)/,$(host-cxxmulti))
121 host-cxxobjs    := $(addprefix $(obj)/,$(host-cxxobjs))
122 host-cshlib     := $(addprefix $(obj)/,$(host-cshlib))
123 host-cshobjs    := $(addprefix $(obj)/,$(host-cshobjs))
124
125 # The temporary file to save gcc -MD generated dependencies must not
126 # contain a comma
127 depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
128
129 # These flags are needed for modversions and compiling, so we define them here
130 # already
131 # $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will 
132 # end up in (or would, if it gets compiled in)
133 # Note: It's possible that one object gets potentially linked into more
134 #       than one module. In that case KBUILD_MODNAME will be set to foo_bar,
135 #       where foo and bar are the name of the modules.
136 basename_flags = -DKBUILD_BASENAME=$(subst $(comma),_,$(subst -,_,$(*F)))
137 modname_flags  = $(if $(filter 1,$(words $(modname))),-DKBUILD_MODNAME=$(subst $(comma),_,$(subst -,_,$(modname))))
138
139
140 _c_flags       = $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(*F).o)
141 _a_flags       = $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(*F).o)
142 _hostc_flags   = $(HOSTCFLAGS)   $(HOST_EXTRACFLAGS)   $(HOSTCFLAGS_$(*F).o)
143 _hostcxx_flags = $(HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) $(HOSTCXXFLAGS_$(*F).o)
144
145
146 # If building the kernel in a separate objtree expand all occurrences
147 # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
148
149 ifeq ($(KBUILD_SRC),)
150 __c_flags       = $(_c_flags)
151 __a_flags       = $(_a_flags)
152 __hostc_flags   = $(_hostc_flags)
153 __hostcxx_flags = $(_hostcxx_flags)
154 else
155
156 # Prefix -I with $(srctree) if it is not an absolute path
157 addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
158 # Find all -I options and call addtree
159 flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
160
161 # -I$(obj) locates generated .h files
162 # $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
163 #   and locates generated .h files
164 # FIXME: Replace both with specific CFLAGS* statements in the makefiles
165 __c_flags       = $(call addtree,-I$(obj)) $(call flags,_c_flags)
166 __a_flags       =                          $(call flags,_a_flags)
167 __hostc_flags   = -I$(obj)                 $(call flags,_hostc_flags)
168 __hostcxx_flags = -I$(obj)                 $(call flags,_hostcxx_flags)
169 endif
170
171 c_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
172                  $(__c_flags) $(modkern_cflags) \
173                  $(basename_flags) $(modname_flags)
174
175 a_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
176                  $(__a_flags) $(modkern_aflags)
177
178 hostc_flags    = -Wp,-MD,$(depfile) $(__hostc_flags)
179 hostcxx_flags  = -Wp,-MD,$(depfile) $(__hostcxx_flags)
180
181 ld_flags       = $(LDFLAGS) $(EXTRA_LDFLAGS)
182
183 # Finds the multi-part object the current object will be linked into
184 modname-multi = $(sort $(foreach m,$(multi-used),\
185                 $(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
186
187 # Shipped files
188 # ===========================================================================
189
190 quiet_cmd_shipped = SHIPPED $@
191 cmd_shipped = cat $< > $@
192
193 $(obj)/%:: $(src)/%_shipped
194         $(call cmd,shipped)
195
196 # Commands useful for building a boot image
197 # ===========================================================================
198
199 #       Use as following:
200 #
201 #       target: source(s) FORCE
202 #               $(if_changed,ld/objcopy/gzip)
203 #
204 #       and add target to EXTRA_TARGETS so that we know we have to
205 #       read in the saved command line
206
207 # Linking
208 # ---------------------------------------------------------------------------
209
210 quiet_cmd_ld = LD      $@
211 cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
212                $(filter-out FORCE,$^) -o $@ 
213
214 # Objcopy
215 # ---------------------------------------------------------------------------
216
217 quiet_cmd_objcopy = OBJCOPY $@
218 cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
219
220 # Gzip
221 # ---------------------------------------------------------------------------
222
223 quiet_cmd_gzip = GZIP    $@
224 cmd_gzip = gzip -f -9 < $< > $@
225
226 # ===========================================================================
227 # Generic stuff
228 # ===========================================================================
229
230 # function to only execute the passed command if necessary
231 # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
232 # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
233
234 if_changed = $(if $(strip $? \
235                           $(filter-out $(cmd_$(1)),$(cmd_$@))\
236                           $(filter-out $(cmd_$@),$(cmd_$(1)))),\
237         @set -e; \
238         $(if $($(quiet)cmd_$(1)),echo '  $(subst ','\'',$($(quiet)cmd_$(1)))';) \
239         $(cmd_$(1)); \
240         echo 'cmd_$@ := $(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).cmd)
241
242
243 # execute the command and also postprocess generated .d dependencies
244 # file
245
246 if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\
247                           $(filter-out $(cmd_$(1)),$(cmd_$@))\
248                           $(filter-out $(cmd_$@),$(cmd_$(1)))),\
249         @set -e; \
250         $(if $($(quiet)cmd_$(1)),echo '  $(subst ','\'',$($(quiet)cmd_$(1)))';) \
251         $(cmd_$(1)); \
252         scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
253         rm -f $(depfile); \
254         mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
255
256 # Usage: $(call if_changed_rule,foo)
257 # will check if $(cmd_foo) changed, or any of the prequisites changed,
258 # and if so will execute $(rule_foo)
259
260 if_changed_rule = $(if $(strip $? \
261                                $(filter-out $(cmd_$(1)),$(cmd_$@))\
262                                $(filter-out $(cmd_$@),$(cmd_$(1)))),\
263                         @set -e; \
264                         $(rule_$(1)))
265
266 # If quiet is set, only print short version of command
267
268 cmd = @$(if $($(quiet)cmd_$(1)),echo '  $(subst ','\'',$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
269
270 #       $(call descend,<dir>,<target>)
271 #       Recursively call a sub-make in <dir> with target <target> 
272 # Usage is deprecated, because make do not see this as an invocation of make.
273 descend =$(Q)$(MAKE) -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj=$(1) $(2)
274
275 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
276 # Usage:
277 # $(Q)$(MAKE) $(build)=dir
278 build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj