vserver 1.9.5.x5
[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
64 # Replace multi-part objects by their individual parts, look at local dir only
65 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)
66 real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
67
68 # Add subdir path
69
70 extra-y         := $(addprefix $(obj)/,$(extra-y))
71 always          := $(addprefix $(obj)/,$(always))
72 targets         := $(addprefix $(obj)/,$(targets))
73 obj-y           := $(addprefix $(obj)/,$(obj-y))
74 obj-m           := $(addprefix $(obj)/,$(obj-m))
75 lib-y           := $(addprefix $(obj)/,$(lib-y))
76 subdir-obj-y    := $(addprefix $(obj)/,$(subdir-obj-y))
77 real-objs-y     := $(addprefix $(obj)/,$(real-objs-y))
78 real-objs-m     := $(addprefix $(obj)/,$(real-objs-m))
79 single-used-m   := $(addprefix $(obj)/,$(single-used-m))
80 multi-used-y    := $(addprefix $(obj)/,$(multi-used-y))
81 multi-used-m    := $(addprefix $(obj)/,$(multi-used-m))
82 multi-objs-y    := $(addprefix $(obj)/,$(multi-objs-y))
83 multi-objs-m    := $(addprefix $(obj)/,$(multi-objs-m))
84 subdir-ym       := $(addprefix $(obj)/,$(subdir-ym))
85 obj-dirs        := $(addprefix $(obj)/,$(obj-dirs))
86
87 # The temporary file to save gcc -MD generated dependencies must not
88 # contain a comma
89 depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
90
91 # These flags are needed for modversions and compiling, so we define them here
92 # already
93 # $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will 
94 # end up in (or would, if it gets compiled in)
95 # Note: It's possible that one object gets potentially linked into more
96 #       than one module. In that case KBUILD_MODNAME will be set to foo_bar,
97 #       where foo and bar are the name of the modules.
98 basename_flags = -DKBUILD_BASENAME=$(subst $(comma),_,$(subst -,_,$(*F)))
99 modname_flags  = $(if $(filter 1,$(words $(modname))),-DKBUILD_MODNAME=$(subst $(comma),_,$(subst -,_,$(modname))))
100
101 _c_flags       = $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(*F).o)
102 _a_flags       = $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(*F).o)
103 _cpp_flags     = $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS_$(@F))
104
105 # If building the kernel in a separate objtree expand all occurrences
106 # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
107
108 ifeq ($(KBUILD_SRC),)
109 __c_flags       = $(_c_flags)
110 __a_flags       = $(_a_flags)
111 __cpp_flags     = $(_cpp_flags)
112 else
113
114 # Prefix -I with $(srctree) if it is not an absolute path
115 addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
116 # Find all -I options and call addtree
117 flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
118
119 # -I$(obj) locates generated .h files
120 # $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
121 #   and locates generated .h files
122 # FIXME: Replace both with specific CFLAGS* statements in the makefiles
123 __c_flags       = $(call addtree,-I$(obj)) $(call flags,_c_flags)
124 __a_flags       =                          $(call flags,_a_flags)
125 __cpp_flags     =                          $(call flags,_cpp_flags)
126 endif
127
128 c_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
129                  $(__c_flags) $(modkern_cflags) \
130                  $(basename_flags) $(modname_flags)
131
132 a_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
133                  $(__a_flags) $(modkern_aflags)
134
135 cpp_flags      = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
136
137 ld_flags       = $(LDFLAGS) $(EXTRA_LDFLAGS)
138
139 # Finds the multi-part object the current object will be linked into
140 modname-multi = $(sort $(foreach m,$(multi-used),\
141                 $(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
142
143 # Shipped files
144 # ===========================================================================
145
146 quiet_cmd_shipped = SHIPPED $@
147 cmd_shipped = cat $< > $@
148
149 $(obj)/%:: $(src)/%_shipped
150         $(call cmd,shipped)
151
152 # Commands useful for building a boot image
153 # ===========================================================================
154
155 #       Use as following:
156 #
157 #       target: source(s) FORCE
158 #               $(if_changed,ld/objcopy/gzip)
159 #
160 #       and add target to EXTRA_TARGETS so that we know we have to
161 #       read in the saved command line
162
163 # Linking
164 # ---------------------------------------------------------------------------
165
166 quiet_cmd_ld = LD      $@
167 cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
168                $(filter-out FORCE,$^) -o $@ 
169
170 # Objcopy
171 # ---------------------------------------------------------------------------
172
173 quiet_cmd_objcopy = OBJCOPY $@
174 cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
175
176 # Gzip
177 # ---------------------------------------------------------------------------
178
179 quiet_cmd_gzip = GZIP    $@
180 cmd_gzip = gzip -f -9 < $< > $@
181
182 # ===========================================================================
183 # Generic stuff
184 # ===========================================================================
185
186 # function to only execute the passed command if necessary
187 # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
188 # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
189
190 if_changed = $(if $(strip $? \
191                           $(filter-out $(cmd_$(1)),$(cmd_$@))\
192                           $(filter-out $(cmd_$@),$(cmd_$(1)))),\
193         @set -e; \
194         $(if $($(quiet)cmd_$(1)),echo '  $(subst ','\'',$($(quiet)cmd_$(1)))';) \
195         $(cmd_$(1)); \
196         echo 'cmd_$@ := $(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).cmd)
197
198
199 # execute the command and also postprocess generated .d dependencies
200 # file
201
202 if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\
203                           $(filter-out $(cmd_$(1)),$(cmd_$@))\
204                           $(filter-out $(cmd_$@),$(cmd_$(1)))),\
205         @set -e; \
206         $(if $($(quiet)cmd_$(1)),echo '  $(subst ','\'',$($(quiet)cmd_$(1)))';) \
207         $(cmd_$(1)); \
208         scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
209         rm -f $(depfile); \
210         mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
211
212 # Usage: $(call if_changed_rule,foo)
213 # will check if $(cmd_foo) changed, or any of the prequisites changed,
214 # and if so will execute $(rule_foo)
215
216 if_changed_rule = $(if $(strip $? \
217                                $(filter-out $(cmd_$(1)),$(cmd_$@))\
218                                $(filter-out $(cmd_$@),$(cmd_$(1)))),\
219                         @set -e; \
220                         $(rule_$(1)))
221
222 # If quiet is set, only print short version of command
223
224 cmd = @$(if $($(quiet)cmd_$(1)),echo '  $(subst ','\'',$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
225
226 #       $(call descend,<dir>,<target>)
227 #       Recursively call a sub-make in <dir> with target <target> 
228 # Usage is deprecated, because make do not see this as an invocation of make.
229 descend =$(Q)$(MAKE) -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj=$(1) $(2)
230
231 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
232 # Usage:
233 # $(Q)$(MAKE) $(build)=dir
234 build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
235
236 # filechk is used to check if the content of a generated file is updated.
237 # Sample usage:
238 # define filechk_sample
239 #       echo $KERNELRELEASE
240 # endef
241 # version.h : Makefile
242 #       $(call filechk,sample)
243 # The rule defined shall write to stdout the content of the new file.
244 # The existing file will be compared with the new one.
245 # - If no file exist it is created
246 # - If the content differ the new file is used
247 # - If they are equal no change, and no timestamp update
248
249 define filechk
250         $(Q)set -e;                             \
251         echo '  CHK     $@';                    \
252         mkdir -p $(dir $@);                     \
253         $(filechk_$(1)) $(2) > $@.tmp;          \
254         if [ -r $@ ] && cmp -s $@ $@.tmp; then  \
255                 rm -f $@.tmp;                   \
256         else                                    \
257                 echo '  UPD     $@';            \
258                 mv -f $@.tmp $@;                \
259         fi
260 endef
261