kbuild: enable 'make CPPFLAGS=...' to add additional options to CPP
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / scripts / Makefile.lib
CommitLineData
1da177e4
LT
1# Figure out what we need to build from the various variables
2# ===========================================================================
3
4# When an object is listed to be built compiled-in and modular,
5# only build the compiled-in version
6
7obj-m := $(filter-out $(obj-y),$(obj-m))
8
9# Libraries are always collected in one lib file.
10# Filter out objects already built-in
11
12lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
13
14
15# Handle objects in subdirs
16# ---------------------------------------------------------------------------
17# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
18# and add the directory to the list of dirs to descend into: $(subdir-y)
19# o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
20# and add the directory to the list of dirs to descend into: $(subdir-m)
21
22__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
23subdir-y += $(__subdir-y)
24__subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
25subdir-m += $(__subdir-m)
26obj-y := $(patsubst %/, %/built-in.o, $(obj-y))
27obj-m := $(filter-out %/, $(obj-m))
28
29# Subdirectories we need to descend into
30
31subdir-ym := $(sort $(subdir-y) $(subdir-m))
32
33# if $(foo-objs) exists, foo.o is a composite object
34multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
35multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
36multi-used := $(multi-used-y) $(multi-used-m)
37single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
38
39# Build list of the parts of our composite objects, our composite
40# objects depend on those (obviously)
41multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
42multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
43multi-objs := $(multi-objs-y) $(multi-objs-m)
44
f5fb9765
SR
45# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
46# tell kbuild to descend
47subdir-obj-y := $(filter %/built-in.o, $(obj-y))
1da177e4
LT
48
49# $(obj-dirs) is a list of directories that contain object files
50obj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
51
52# Replace multi-part objects by their individual parts, look at local dir only
53real-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)
54real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
55
56# Add subdir path
57
58extra-y := $(addprefix $(obj)/,$(extra-y))
59always := $(addprefix $(obj)/,$(always))
60targets := $(addprefix $(obj)/,$(targets))
61obj-y := $(addprefix $(obj)/,$(obj-y))
62obj-m := $(addprefix $(obj)/,$(obj-m))
63lib-y := $(addprefix $(obj)/,$(lib-y))
64subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y))
65real-objs-y := $(addprefix $(obj)/,$(real-objs-y))
66real-objs-m := $(addprefix $(obj)/,$(real-objs-m))
67single-used-m := $(addprefix $(obj)/,$(single-used-m))
68multi-used-y := $(addprefix $(obj)/,$(multi-used-y))
69multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
70multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))
71multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))
72subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
73obj-dirs := $(addprefix $(obj)/,$(obj-dirs))
74
1da177e4
LT
75# These flags are needed for modversions and compiling, so we define them here
76# already
77# $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
78# end up in (or would, if it gets compiled in)
79# Note: It's possible that one object gets potentially linked into more
80# than one module. In that case KBUILD_MODNAME will be set to foo_bar,
81# where foo and bar are the name of the modules.
f83b5e32 82name-fix = $(subst $(comma),_,$(subst -,_,$1))
5e8d780d 83basename_flags = -D"KBUILD_BASENAME=KBUILD_STR($(call name-fix,$(basetarget)))"
f83b5e32
UR
84modname_flags = $(if $(filter 1,$(words $(modname))),\
85 -D"KBUILD_MODNAME=KBUILD_STR($(call name-fix,$(modname)))")
1da177e4 86
a0f97e06 87_c_flags = $(KBUILD_CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(basetarget).o)
222d394d 88_a_flags = $(KBUILD_AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(basetarget).o)
06c5040c 89_cpp_flags = $(KBUILD_CPPFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS_$(@F))
1da177e4
LT
90
91# If building the kernel in a separate objtree expand all occurrences
92# of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
93
94ifeq ($(KBUILD_SRC),)
95__c_flags = $(_c_flags)
96__a_flags = $(_a_flags)
97__cpp_flags = $(_cpp_flags)
98else
99
1da177e4
LT
100# -I$(obj) locates generated .h files
101# $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
102# and locates generated .h files
103# FIXME: Replace both with specific CFLAGS* statements in the makefiles
104__c_flags = $(call addtree,-I$(obj)) $(call flags,_c_flags)
105__a_flags = $(call flags,_a_flags)
106__cpp_flags = $(call flags,_cpp_flags)
107endif
108
06c5040c 109c_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(KBUILD_CPPFLAGS) \
1da177e4 110 $(__c_flags) $(modkern_cflags) \
f83b5e32 111 -D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags)
1da177e4 112
06c5040c 113a_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(KBUILD_CPPFLAGS) \
1da177e4
LT
114 $(__a_flags) $(modkern_aflags)
115
116cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
117
118ld_flags = $(LDFLAGS) $(EXTRA_LDFLAGS)
119
120# Finds the multi-part object the current object will be linked into
121modname-multi = $(sort $(foreach m,$(multi-used),\
122 $(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
123
124# Shipped files
125# ===========================================================================
126
127quiet_cmd_shipped = SHIPPED $@
128cmd_shipped = cat $< > $@
129
130$(obj)/%:: $(src)/%_shipped
131 $(call cmd,shipped)
132
133# Commands useful for building a boot image
134# ===========================================================================
135#
136# Use as following:
137#
138# target: source(s) FORCE
139# $(if_changed,ld/objcopy/gzip)
140#
836caba7 141# and add target to extra-y so that we know we have to
1da177e4
LT
142# read in the saved command line
143
144# Linking
145# ---------------------------------------------------------------------------
146
147quiet_cmd_ld = LD $@
148cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
149 $(filter-out FORCE,$^) -o $@
150
151# Objcopy
152# ---------------------------------------------------------------------------
153
154quiet_cmd_objcopy = OBJCOPY $@
155cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
156
157# Gzip
158# ---------------------------------------------------------------------------
159
160quiet_cmd_gzip = GZIP $@
161cmd_gzip = gzip -f -9 < $< > $@
162
1da177e4 163