1 # ===========================================================================
2 # kbuild: Generic definitions
3 # ===========================================================================
9 space := $(empty) $(empty)
11 # Backward compatibility - to be removed...
12 extra-y += $(EXTRA_TARGETS)
13 # Figure out what we need to build from the various variables
14 # ===========================================================================
16 # When an object is listed to be built compiled-in and modular,
17 # only build the compiled-in version
19 obj-m := $(filter-out $(obj-y),$(obj-m))
21 # Libraries are always collected in one lib file.
22 # Filter out objects already built-in
24 lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
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)
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))
41 # Subdirectories we need to descend into
43 subdir-ym := $(sort $(subdir-y) $(subdir-m))
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)))
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)
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)))
61 # $(obj-dirs) is a list of directories that contain object files
62 obj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
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)))
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))
87 # The temporary file to save gcc -MD generated dependencies must not
89 depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
91 # These flags are needed for modversions and compiling, so we define them here
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))))
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))
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 '/').
108 ifeq ($(KBUILD_SRC),)
109 __c_flags = $(_c_flags)
110 __a_flags = $(_a_flags)
111 __cpp_flags = $(_cpp_flags)
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)))
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)
128 c_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
129 $(__c_flags) $(modkern_cflags) \
130 $(basename_flags) $(modname_flags)
132 a_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
133 $(__a_flags) $(modkern_aflags)
135 cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
137 ld_flags = $(LDFLAGS) $(EXTRA_LDFLAGS)
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=))))
144 # ===========================================================================
146 quiet_cmd_shipped = SHIPPED $@
147 cmd_shipped = cat $< > $@
149 $(obj)/%:: $(src)/%_shipped
152 # Commands useful for building a boot image
153 # ===========================================================================
157 # target: source(s) FORCE
158 # $(if_changed,ld/objcopy/gzip)
160 # and add target to EXTRA_TARGETS so that we know we have to
161 # read in the saved command line
164 # ---------------------------------------------------------------------------
167 cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
168 $(filter-out FORCE,$^) -o $@
171 # ---------------------------------------------------------------------------
173 quiet_cmd_objcopy = OBJCOPY $@
174 cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
177 # ---------------------------------------------------------------------------
179 quiet_cmd_gzip = GZIP $@
180 cmd_gzip = gzip -f -9 < $< > $@
182 # ===========================================================================
184 # ===========================================================================
186 ifneq ($(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
189 arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
193 # echo command. Short version is $(quiet) equals quiet, otherwise full command
194 echo-cmd = $(if $($(quiet)cmd_$(1)), \
195 echo ' $(subst ','\'',$($(quiet)cmd_$(1)))';)
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
201 if_changed = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
205 echo 'cmd_$@ := $(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).cmd)
208 # execute the command and also postprocess generated .d dependencies
211 if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\
212 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
216 scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
218 mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
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)
224 if_changed_rule = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
228 # If quiet is set, only print short version of command
230 cmd = @$(if $($(quiet)cmd_$(1)),echo ' $(subst ','\'',$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
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.
235 descend =$(Q)$(MAKE) -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj=$(1) $(2)
237 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
239 # $(Q)$(MAKE) $(build)=dir
240 build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
242 # filechk is used to check if the content of a generated file is updated.
244 # define filechk_sample
245 # echo $KERNELRELEASE
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
258 mkdir -p $(dir $@); \
259 $(filechk_$(1)) $(2) > $@.tmp; \
260 if [ -r $@ ] && cmp -s $@ $@.tmp; then \