blob: ac1ac97353ae6c118311ed6cccf6b693a534e369 [file] [log] [blame]
Denis Vlasenko7d219aa2006-10-05 10:17:08 +00001# Backward compatibility - to be removed...
2extra-y += $(EXTRA_TARGETS)
3# Figure out what we need to build from the various variables
4# ===========================================================================
5
6# When an object is listed to be built compiled-in and modular,
7# only build the compiled-in version
8
9obj-m := $(filter-out $(obj-y),$(obj-m))
10
11# Libraries are always collected in one lib file.
12# Filter out objects already built-in
13
14lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
15
16
17# Handle objects in subdirs
18# ---------------------------------------------------------------------------
19# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
20# and add the directory to the list of dirs to descend into: $(subdir-y)
Denis Vlasenkof7996f32007-01-11 17:20:00 +000021# o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000022# and add the directory to the list of dirs to descend into: $(subdir-m)
23
24__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
25subdir-y += $(__subdir-y)
26__subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
27subdir-m += $(__subdir-m)
28obj-y := $(patsubst %/, %/built-in.o, $(obj-y))
29obj-m := $(filter-out %/, $(obj-m))
30
31# Subdirectories we need to descend into
32
33subdir-ym := $(sort $(subdir-y) $(subdir-m))
34
Denis Vlasenkof7996f32007-01-11 17:20:00 +000035# if $(foo-objs) exists, foo.o is a composite object
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000036multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
37multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
38multi-used := $(multi-used-y) $(multi-used-m)
39single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
40
41# Build list of the parts of our composite objects, our composite
42# objects depend on those (obviously)
43multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
44multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
45multi-objs := $(multi-objs-y) $(multi-objs-m)
46
47# $(subdir-obj-y) is the list of objects in $(obj-y) which do not live
48# in the local directory
49subdir-obj-y := $(foreach o,$(obj-y),$(if $(filter-out $(o),$(notdir $(o))),$(o)))
50
51# $(obj-dirs) is a list of directories that contain object files
52obj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
53
54# Replace multi-part objects by their individual parts, look at local dir only
55real-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)
56real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
57
58# Add subdir path
59
60extra-y := $(addprefix $(obj)/,$(extra-y))
61always := $(addprefix $(obj)/,$(always))
62targets := $(addprefix $(obj)/,$(targets))
63obj-y := $(addprefix $(obj)/,$(obj-y))
64obj-m := $(addprefix $(obj)/,$(obj-m))
65lib-y := $(addprefix $(obj)/,$(lib-y))
66subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y))
67real-objs-y := $(addprefix $(obj)/,$(real-objs-y))
68real-objs-m := $(addprefix $(obj)/,$(real-objs-m))
69single-used-m := $(addprefix $(obj)/,$(single-used-m))
70multi-used-y := $(addprefix $(obj)/,$(multi-used-y))
71multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
72multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))
73multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))
74subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
75obj-dirs := $(addprefix $(obj)/,$(obj-dirs))
76
77# These flags are needed for modversions and compiling, so we define them here
78# already
Denis Vlasenkof7996f32007-01-11 17:20:00 +000079# $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000080# end up in (or would, if it gets compiled in)
81# Note: It's possible that one object gets potentially linked into more
82# than one module. In that case KBUILD_MODNAME will be set to foo_bar,
83# where foo and bar are the name of the modules.
Jean-Philippe Bruckered8af512020-03-12 17:19:45 +010084name-fix = $(squote)$(quote)$(subst $(comma),_,$(subst -,_,$1))$(quote)$(squote)
85basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(*F))
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000086modname_flags = $(if $(filter 1,$(words $(modname))),\
Jean-Philippe Bruckered8af512020-03-12 17:19:45 +010087 -DKBUILD_MODNAME=$(call name-fix,$(modname)))
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000088
89_c_flags = $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(*F).o)
90_a_flags = $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(*F).o)
91_cpp_flags = $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS_$(@F))
92
93# If building the kernel in a separate objtree expand all occurrences
94# of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
95
96ifeq ($(KBUILD_SRC),)
97__c_flags = $(_c_flags)
98__a_flags = $(_a_flags)
99__cpp_flags = $(_cpp_flags)
100else
101
102# -I$(obj) locates generated .h files
103# $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
104# and locates generated .h files
105# FIXME: Replace both with specific CFLAGS* statements in the makefiles
106__c_flags = $(call addtree,-I$(obj)) $(call flags,_c_flags)
107__a_flags = $(call flags,_a_flags)
108__cpp_flags = $(call flags,_cpp_flags)
109endif
110
111c_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
112 $(__c_flags) $(modkern_cflags) \
Jean-Philippe Bruckered8af512020-03-12 17:19:45 +0100113 $(basename_flags) $(modname_flags)
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000114
115a_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
Bernhard Reutner-Fischer831c7542022-04-21 13:37:10 +0200116 $(__c_flags) \
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000117 $(__a_flags) $(modkern_aflags)
118
119cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
120
Denis Vlasenko2a8329e2008-11-08 20:36:32 +0000121# Seems to be a wrong thing to do. LDFLAGS contains gcc's flags,
122# yet ld_flags is fed to ld.
123#ld_flags = $(LDFLAGS) $(EXTRA_LDFLAGS)
124# Remove the -Wl, prefix from linker options normally passed through gcc
125ld_flags = $(filter-out -Wl$(comma)%,$(LDFLAGS) $(EXTRA_LDFLAGS))
126
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000127
128# Finds the multi-part object the current object will be linked into
129modname-multi = $(sort $(foreach m,$(multi-used),\
130 $(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
131
132# Shipped files
133# ===========================================================================
134
135quiet_cmd_shipped = SHIPPED $@
136cmd_shipped = cat $< > $@
137
138$(obj)/%:: $(src)/%_shipped
139 $(call cmd,shipped)
140
141# Commands useful for building a boot image
142# ===========================================================================
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000143#
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000144# Use as following:
145#
146# target: source(s) FORCE
147# $(if_changed,ld/objcopy/gzip)
148#
149# and add target to EXTRA_TARGETS so that we know we have to
150# read in the saved command line
151
152# Linking
153# ---------------------------------------------------------------------------
154
Denis Vlasenko401de642008-06-06 16:11:12 +0000155# TODO: LDFLAGS usually is supposed to contain gcc's flags, not ld's.
156# but here we feed them to ld!
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000157quiet_cmd_ld = LD $@
158cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000159 $(filter-out FORCE,$^) -o $@
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000160
161# Objcopy
162# ---------------------------------------------------------------------------
163
164quiet_cmd_objcopy = OBJCOPY $@
165cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
166
167# Gzip
168# ---------------------------------------------------------------------------
169
170quiet_cmd_gzip = GZIP $@
171cmd_gzip = gzip -f -9 < $< > $@