blob: 7cf75cc4f849dfe8a75a05a604809c2ec5a19bed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001# ===========================================================================
2# kbuild: Generic definitions
3# ===========================================================================
4
5# Standard vars
6
7comma := ,
8empty :=
9space := $(empty) $(empty)
10
11# Backward compatibility - to be removed...
12extra-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
19obj-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
24lib-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)))
35subdir-y += $(__subdir-y)
36__subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
37subdir-m += $(__subdir-m)
38obj-y := $(patsubst %/, %/built-in.o, $(obj-y))
39obj-m := $(filter-out %/, $(obj-m))
40
41# Subdirectories we need to descend into
42
43subdir-ym := $(sort $(subdir-y) $(subdir-m))
44
45# if $(foo-objs) exists, foo.o is a composite object
46multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
47multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
48multi-used := $(multi-used-y) $(multi-used-m)
49single-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)
53multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
54multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
55multi-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
59subdir-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
62obj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
63
64# Replace multi-part objects by their individual parts, look at local dir only
65real-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)
66real-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
70extra-y := $(addprefix $(obj)/,$(extra-y))
71always := $(addprefix $(obj)/,$(always))
72targets := $(addprefix $(obj)/,$(targets))
73obj-y := $(addprefix $(obj)/,$(obj-y))
74obj-m := $(addprefix $(obj)/,$(obj-m))
75lib-y := $(addprefix $(obj)/,$(lib-y))
76subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y))
77real-objs-y := $(addprefix $(obj)/,$(real-objs-y))
78real-objs-m := $(addprefix $(obj)/,$(real-objs-m))
79single-used-m := $(addprefix $(obj)/,$(single-used-m))
80multi-used-y := $(addprefix $(obj)/,$(multi-used-y))
81multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
82multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))
83multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))
84subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
85obj-dirs := $(addprefix $(obj)/,$(obj-dirs))
86
87# The temporary file to save gcc -MD generated dependencies must not
88# contain a comma
89depfile = $(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.
98basename_flags = -DKBUILD_BASENAME=$(subst $(comma),_,$(subst -,_,$(*F)))
99modname_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
108ifeq ($(KBUILD_SRC),)
109__c_flags = $(_c_flags)
110__a_flags = $(_a_flags)
111__cpp_flags = $(_cpp_flags)
112else
113
114# Prefix -I with $(srctree) if it is not an absolute path
115addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
116# Find all -I options and call addtree
117flags = $(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)
126endif
127
128c_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
129 $(__c_flags) $(modkern_cflags) \
130 $(basename_flags) $(modname_flags)
131
132a_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
133 $(__a_flags) $(modkern_aflags)
134
135cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
136
137ld_flags = $(LDFLAGS) $(EXTRA_LDFLAGS)
138
139# Finds the multi-part object the current object will be linked into
140modname-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
146quiet_cmd_shipped = SHIPPED $@
147cmd_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
166quiet_cmd_ld = LD $@
167cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
168 $(filter-out FORCE,$^) -o $@
169
170# Objcopy
171# ---------------------------------------------------------------------------
172
173quiet_cmd_objcopy = OBJCOPY $@
174cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
175
176# Gzip
177# ---------------------------------------------------------------------------
178
179quiet_cmd_gzip = GZIP $@
180cmd_gzip = gzip -f -9 < $< > $@
181
182# ===========================================================================
183# Generic stuff
184# ===========================================================================
185
186ifneq ($(KBUILD_NOCMDDEP),1)
187# Check if both arguments has same arguments. Result in empty string if equal
188# User may override this check using make KBUILD_NOCMDDEP=1
189arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
190
191endif
192
193# echo command. Short version is $(quiet) equals quiet, otherwise full command
194echo-cmd = $(if $($(quiet)cmd_$(1)), \
195 echo ' $(subst ','\'',$($(quiet)cmd_$(1)))';)
196
197# function to only execute the passed command if necessary
198# >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
199# note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
200#
201if_changed = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
202 @set -e; \
203 $(echo-cmd) \
204 $(cmd_$(1)); \
205 echo 'cmd_$@ := $(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).cmd)
206
207
208# execute the command and also postprocess generated .d dependencies
209# file
210
211if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\
212 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
213 @set -e; \
214 $(echo-cmd) \
215 $(cmd_$(1)); \
216 scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
217 rm -f $(depfile); \
218 mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
219
220# Usage: $(call if_changed_rule,foo)
221# will check if $(cmd_foo) changed, or any of the prequisites changed,
222# and if so will execute $(rule_foo)
223
224if_changed_rule = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
225 @set -e; \
226 $(rule_$(1)))
227
228# If quiet is set, only print short version of command
229
230cmd = @$(if $($(quiet)cmd_$(1)),echo ' $(subst ','\'',$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
231
232# $(call descend,<dir>,<target>)
233# Recursively call a sub-make in <dir> with target <target>
234# Usage is deprecated, because make do not see this as an invocation of make.
235descend =$(Q)$(MAKE) -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj=$(1) $(2)
236
237# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
238# Usage:
239# $(Q)$(MAKE) $(build)=dir
240build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
241
242# filechk is used to check if the content of a generated file is updated.
243# Sample usage:
244# define filechk_sample
245# echo $KERNELRELEASE
246# endef
247# version.h : Makefile
248# $(call filechk,sample)
249# The rule defined shall write to stdout the content of the new file.
250# The existing file will be compared with the new one.
251# - If no file exist it is created
252# - If the content differ the new file is used
253# - If they are equal no change, and no timestamp update
254
255define filechk
256 $(Q)set -e; \
257 echo ' CHK $@'; \
258 mkdir -p $(dir $@); \
259 $(filechk_$(1)) $(2) > $@.tmp; \
260 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
261 rm -f $@.tmp; \
262 else \
263 echo ' UPD $@'; \
264 mv -f $@.tmp $@; \
265 fi
266endef
267