1cur_makefile := $(lastword $(MAKEFILE_LIST))
2
3$(cur_makefile): ;
4
5# Backward compatibility
6asflags-y  += $(EXTRA_AFLAGS)
7ccflags-y  += $(EXTRA_CFLAGS)
8cppflags-y += $(EXTRA_CPPFLAGS)
9ldflags-y  += $(EXTRA_LDFLAGS)
10
11#
12# flags that take effect in sub directories
13export KBUILD_SUBDIR_ASFLAGS := $(KBUILD_SUBDIR_ASFLAGS) $(subdir-asflags-y)
14export KBUILD_SUBDIR_CCFLAGS := $(KBUILD_SUBDIR_CCFLAGS) $(subdir-ccflags-y)
15
16# Figure out what we need to build from the various variables
17# ===========================================================================
18
19# Handle archives
20# ---------------------------------------------------------------------------
21
22# Extract archives from $(obj-y)
23archive-y				:= $(filter %.a, $(obj-y))
24archive-custom-valid	:= $(filter $(archive-custom), $(archive-y))
25
26ifeq ($(archive-y),)
27archive-src-y	:=
28archive-bin-y	:=
29else # archive-y
30ifeq ($(FORCE_TO_USE_LIB),1)
31archive-bin-y	:= $(archive-y)
32else
33# Filter out archive-custom-valid
34archive-y	:= $(filter-out $(archive-custom-valid), $(archive-y))
35
36# Extract archives that have source files available
37archive-src-y	:= $(foreach m, $(archive-y), $(if $(wildcard $(addprefix $(srctree)/$(src)/,$($(m:.a=-y):.o=.*))), $(m)))
38
39# Extract archives that do NOT have source files
40archive-bin-y	:= $(filter-out $(archive-src-y), $(archive-y))
41endif
42
43# If the source files of the archive (including archive-custom-valid) exist, build the source files directly
44obj-y			:= $(filter-out $(archive-src-y) $(archive-custom-valid), $(obj-y)) $(foreach m, $(archive-src-y) $(archive-custom-valid), $($(m:.a=-y)))
45endif # archive-y
46
47# Handle composite objects
48# ---------------------------------------------------------------------------
49
50# if $(foo-objs) exists, foo.o is a composite object
51multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
52
53# Build list of the parts of our composite objects, our composite
54# objects depend on those (obviously)
55multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
56multi-objs-subdir 	:= $(patsubst %/,%,$(filter %/, $(multi-objs-y)))
57multi-objs-archive	:= $(filter %.a, $(multi-objs-y))
58multi-objs-y := $(patsubst %/, %/built-in$(built_in_suffix), $(multi-objs-y))
59multi-objs   := $(multi-objs-y)
60
61define multi_subdir_built_in
62$(foreach m, $(1), $(if $($(m:.o=$(strip $2))), $(eval $(m:.o=$(strip $2)) := $(patsubst %/, %/built-in$(built_in_suffix), $($(m:.o=$(strip $2)))))))
63endef
64
65ifneq ($(multi-objs-subdir),)
66$(call multi_subdir_built_in,$(multi-used-y),-objs)
67$(call multi_subdir_built_in,$(multi-used-y),-y)
68endif
69
70ifneq ($(multi-objs-archive),)
71ifneq ($(filter $(archive-custom), $(multi-objs-archive)),)
72$(error Composite objects cannot be built from customized archives)
73endif
74mobjs-archive-src-y	:=
75mobjs-archive-bin-y	:=
76mobjs-archive-y		:= $(multi-objs-archive)
77
78ifeq ($(FORCE_TO_USE_LIB),1)
79mobjs-archive-bin-y	+= $(mobjs-archive-y)
80else
81# Extract archives that have source files available
82mobjs-archive-src-y	+= $(foreach m, $(mobjs-archive-y), $(if $(wildcard $(addprefix $(srctree)/$(src)/,$($(m:.a=-y):.o=.*))), $(m)))
83
84# Extract archives that do NOT have source files
85mobjs-archive-bin-y	:= $(filter-out $(mobjs-archive-src-y), $(mobjs-archive-y))
86endif
87
88# If the source files of the archive (including archive-custom-valid) exist, build the source files directly
89mobjs-obj-y			:= $(foreach m, $(mobjs-archive-src-y), $($(m:.a=-y)))
90
91archive-y			+= $(mobjs-archive-y)
92archive-bin-y		+= $(mobjs-archive-bin-y)
93archive-src-y		+= $(mobjs-archive-src-y)
94obj-y				+= $(mobjs-obj-y)
95endif
96
97# Handle objects in subdirs
98# ---------------------------------------------------------------------------
99# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o or .a
100#   and add the directory to the list of dirs to descend into: $(subdir-y)
101
102__subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y)))
103subdir-y	+= $(__subdir-y)
104obj-y		:= $(patsubst %/, %/built-in$(built_in_suffix), $(obj-y))
105
106# Subdirectories we need to descend into
107subdir-y	:= $(sort $(subdir-y) $(multi-objs-subdir))
108
109# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
110# tell kbuild to descend
111subdir-obj-y := $(filter %/built-in$(built_in_suffix), $(obj-y) $(multi-objs-y))
112
113# $(obj-dirs) is a list of directories that contain object files
114obj-dirs := $(dir $(multi-objs) $(obj-y))
115
116# Replace multi-part objects by their individual parts, look at local dir only
117real-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)
118
119# Add subdir path
120
121extra-y		:= $(addprefix $(obj)/,$(extra-y))
122always		:= $(addprefix $(obj)/,$(always))
123targets		:= $(addprefix $(obj)/,$(targets))
124obj-y		:= $(addprefix $(obj)/,$(obj-y))
125lib-y		:= $(addprefix $(obj)/,$(lib-y))
126subdir-obj-y	:= $(addprefix $(obj)/,$(subdir-obj-y))
127real-objs-y	:= $(addprefix $(obj)/,$(real-objs-y))
128multi-used-y	:= $(addprefix $(obj)/,$(multi-used-y))
129multi-objs-y	:= $(addprefix $(obj)/,$(multi-objs-y))
130subdir-y	:= $(addprefix $(obj)/,$(subdir-y))
131obj-dirs	:= $(addprefix $(obj)/,$(obj-dirs))
132archive-y	:= $(addprefix $(obj)/,$(archive-y))
133archive-src-y	:= $(addprefix $(obj)/,$(archive-src-y))
134archive-bin-y	:= $(addprefix $(obj)/,$(archive-bin-y))
135archive-custom-valid	:= $(addprefix $(obj)/,$(archive-custom-valid))
136
137orig_c_flags   = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(KBUILD_SUBDIR_CCFLAGS) \
138                 $(ccflags-y) $(CFLAGS_$(basetarget).o)
139_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(orig_c_flags) $(C_ONLY_FLAGS))
140_c++_flags     = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(orig_c_flags) $(C++_ONLY_FLAGS))
141_a_flags       = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(KBUILD_SUBDIR_ASFLAGS) \
142                 $(asflags-y) $(AFLAGS_$(basetarget).o)
143_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
144
145# If building the kernel in a separate objtree expand all occurrences
146# of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
147
148ifeq ($(KBUILD_SRC),)
149__c_flags	= $(_c_flags)
150__c++_flags	= $(_c++_flags)
151__a_flags	= $(_a_flags)
152__cpp_flags     = $(_cpp_flags)
153else
154
155# -I$(obj) locates generated .h files
156# $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
157#   and locates generated .h files
158# FIXME: Replace both with specific CFLAGS* statements in the makefiles
159__c_flags	= $(call addtree,-I$(obj)) $(call flags,_c_flags)
160__c++_flags	= $(call addtree,-I$(obj)) $(call flags,_c++_flags)
161__a_flags	=                          $(call flags,_a_flags)
162__cpp_flags	=                          $(call flags,_cpp_flags)
163endif
164
165ifeq ($(WIN_PLAT),y)
166# Not to include system header files in dependency files,
167# for there are space characters in system path by default.
168depfile_flags  = -MMD -MP -MF $(depfile)
169else
170depfile_flags  = -MD -MP -MF $(depfile)
171endif
172
173c_flags        = $(depfile_flags) $(NOSTDINC_FLAGS) $(__c_flags)
174
175c++_flags      = $(depfile_flags) $(NOSTDINC_FLAGS) $(__c++_flags)
176
177a_flags        = $(depfile_flags) $(NOSTDINC_FLAGS) $(__a_flags)
178
179a_cpp_flags    = $(a_flags)
180
181cpp_flags      = $(depfile_flags) $(NOSTDINC_FLAGS) $(__cpp_flags)
182
183ld_flags       = $(LDFLAGS) $(ldflags-y)
184
185# Useful for describing the dependency of composite objects
186# Usage:
187#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
188define multi_depend
189$(foreach m, $(notdir $1), \
190	$(eval $(obj)/$m: \
191	$(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
192endef
193
194# Commands useful for building a boot image
195# ===========================================================================
196#
197#	Use as following:
198#
199#	target: source(s) FORCE
200#		$(if_changed,ld/objcopy/gzip)
201#
202#	and add target to extra-y so that we know we have to
203#	read in the saved command line
204
205# Linking
206# ---------------------------------------------------------------------------
207
208quiet_cmd_ld = LD      $@
209cmd_ld = $(LD) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \
210	       $(filter-out FORCE,$^) -o $@
211
212# Objcopy
213# ---------------------------------------------------------------------------
214
215quiet_cmd_objcopy = OBJCOPY $@
216cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
217
218