xref: /linux/scripts/Makefile.lib (revision bc22c17e12c130dc929218a95aa347e0f3fd05dc)
1f77bf014SSam Ravnborg# Backward compatibility
2f77bf014SSam Ravnborgasflags-y  += $(EXTRA_AFLAGS)
3f77bf014SSam Ravnborgccflags-y  += $(EXTRA_CFLAGS)
4f77bf014SSam Ravnborgcppflags-y += $(EXTRA_CPPFLAGS)
5f77bf014SSam Ravnborgldflags-y  += $(EXTRA_LDFLAGS)
6f77bf014SSam Ravnborg
71da177e4SLinus Torvalds# Figure out what we need to build from the various variables
81da177e4SLinus Torvalds# ===========================================================================
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds# When an object is listed to be built compiled-in and modular,
111da177e4SLinus Torvalds# only build the compiled-in version
121da177e4SLinus Torvalds
131da177e4SLinus Torvaldsobj-m := $(filter-out $(obj-y),$(obj-m))
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds# Libraries are always collected in one lib file.
161da177e4SLinus Torvalds# Filter out objects already built-in
171da177e4SLinus Torvalds
181da177e4SLinus Torvaldslib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds# Handle objects in subdirs
221da177e4SLinus Torvalds# ---------------------------------------------------------------------------
231da177e4SLinus Torvalds# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
241da177e4SLinus Torvalds#   and add the directory to the list of dirs to descend into: $(subdir-y)
251da177e4SLinus Torvalds# o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
261da177e4SLinus Torvalds#   and add the directory to the list of dirs to descend into: $(subdir-m)
271da177e4SLinus Torvalds
28551559e1STejun Heo# Determine modorder.
29551559e1STejun Heo# Unfortunately, we don't have information about ordering between -y
30551559e1STejun Heo# and -m subdirs.  Just put -y's first.
31551559e1STejun Heomodorder	:= $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m:.o=.ko))
32551559e1STejun Heo
331da177e4SLinus Torvalds__subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y)))
341da177e4SLinus Torvaldssubdir-y	+= $(__subdir-y)
351da177e4SLinus Torvalds__subdir-m	:= $(patsubst %/,%,$(filter %/, $(obj-m)))
361da177e4SLinus Torvaldssubdir-m	+= $(__subdir-m)
371da177e4SLinus Torvaldsobj-y		:= $(patsubst %/, %/built-in.o, $(obj-y))
381da177e4SLinus Torvaldsobj-m		:= $(filter-out %/, $(obj-m))
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds# Subdirectories we need to descend into
411da177e4SLinus Torvalds
421da177e4SLinus Torvaldssubdir-ym	:= $(sort $(subdir-y) $(subdir-m))
431da177e4SLinus Torvalds
441da177e4SLinus Torvalds# if $(foo-objs) exists, foo.o is a composite object
451da177e4SLinus Torvaldsmulti-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
461da177e4SLinus Torvaldsmulti-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
471da177e4SLinus Torvaldsmulti-used   := $(multi-used-y) $(multi-used-m)
481da177e4SLinus Torvaldssingle-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds# Build list of the parts of our composite objects, our composite
511da177e4SLinus Torvalds# objects depend on those (obviously)
521da177e4SLinus Torvaldsmulti-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
531da177e4SLinus Torvaldsmulti-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
541da177e4SLinus Torvaldsmulti-objs   := $(multi-objs-y) $(multi-objs-m)
551da177e4SLinus Torvalds
56f5fb9765SSam Ravnborg# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
57f5fb9765SSam Ravnborg# tell kbuild to descend
58f5fb9765SSam Ravnborgsubdir-obj-y := $(filter %/built-in.o, $(obj-y))
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds# $(obj-dirs) is a list of directories that contain object files
611da177e4SLinus Torvaldsobj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
621da177e4SLinus Torvalds
631da177e4SLinus Torvalds# Replace multi-part objects by their individual parts, look at local dir only
641da177e4SLinus Torvaldsreal-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)
651da177e4SLinus Torvaldsreal-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
661da177e4SLinus Torvalds
671da177e4SLinus Torvalds# Add subdir path
681da177e4SLinus Torvalds
691da177e4SLinus Torvaldsextra-y		:= $(addprefix $(obj)/,$(extra-y))
701da177e4SLinus Torvaldsalways		:= $(addprefix $(obj)/,$(always))
711da177e4SLinus Torvaldstargets		:= $(addprefix $(obj)/,$(targets))
72551559e1STejun Heomodorder	:= $(addprefix $(obj)/,$(modorder))
731da177e4SLinus Torvaldsobj-y		:= $(addprefix $(obj)/,$(obj-y))
741da177e4SLinus Torvaldsobj-m		:= $(addprefix $(obj)/,$(obj-m))
751da177e4SLinus Torvaldslib-y		:= $(addprefix $(obj)/,$(lib-y))
761da177e4SLinus Torvaldssubdir-obj-y	:= $(addprefix $(obj)/,$(subdir-obj-y))
771da177e4SLinus Torvaldsreal-objs-y	:= $(addprefix $(obj)/,$(real-objs-y))
781da177e4SLinus Torvaldsreal-objs-m	:= $(addprefix $(obj)/,$(real-objs-m))
791da177e4SLinus Torvaldssingle-used-m	:= $(addprefix $(obj)/,$(single-used-m))
801da177e4SLinus Torvaldsmulti-used-y	:= $(addprefix $(obj)/,$(multi-used-y))
811da177e4SLinus Torvaldsmulti-used-m	:= $(addprefix $(obj)/,$(multi-used-m))
821da177e4SLinus Torvaldsmulti-objs-y	:= $(addprefix $(obj)/,$(multi-objs-y))
831da177e4SLinus Torvaldsmulti-objs-m	:= $(addprefix $(obj)/,$(multi-objs-m))
841da177e4SLinus Torvaldssubdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
851da177e4SLinus Torvaldsobj-dirs	:= $(addprefix $(obj)/,$(obj-dirs))
861da177e4SLinus Torvalds
871da177e4SLinus Torvalds# These flags are needed for modversions and compiling, so we define them here
881da177e4SLinus Torvalds# already
891da177e4SLinus Torvalds# $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
901da177e4SLinus Torvalds# end up in (or would, if it gets compiled in)
911da177e4SLinus Torvalds# Note: It's possible that one object gets potentially linked into more
921da177e4SLinus Torvalds#       than one module. In that case KBUILD_MODNAME will be set to foo_bar,
931da177e4SLinus Torvalds#       where foo and bar are the name of the modules.
94f83b5e32SUstyugov Romanname-fix = $(subst $(comma),_,$(subst -,_,$1))
955e8d780dSSam Ravnborgbasename_flags = -D"KBUILD_BASENAME=KBUILD_STR($(call name-fix,$(basetarget)))"
96f83b5e32SUstyugov Romanmodname_flags  = $(if $(filter 1,$(words $(modname))),\
97f83b5e32SUstyugov Roman                 -D"KBUILD_MODNAME=KBUILD_STR($(call name-fix,$(modname)))")
981da177e4SLinus Torvalds
99346e15beSJason Baron#hash values
100346e15beSJason Baronifdef CONFIG_DYNAMIC_PRINTK_DEBUG
101346e15beSJason Barondebug_flags = -D"DEBUG_HASH=$(shell ./scripts/basic/hash djb2 $(@D)$(modname))"\
102346e15beSJason Baron              -D"DEBUG_HASH2=$(shell ./scripts/basic/hash r5 $(@D)$(modname))"
103346e15beSJason Baronelse
104346e15beSJason Barondebug_flags =
105346e15beSJason Baronendif
106346e15beSJason Baron
107d8672b40SSam Ravnborgorig_c_flags   = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)                     \
108d8672b40SSam Ravnborg                 $(ccflags-y) $(CFLAGS_$(basetarget).o)
109656ee82cSSteven Rostedt_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(orig_c_flags))
110d8672b40SSam Ravnborg_a_flags       = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS)                     \
111d8672b40SSam Ravnborg                 $(asflags-y) $(AFLAGS_$(basetarget).o)
112f77bf014SSam Ravnborg_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
1131da177e4SLinus Torvalds
1141da177e4SLinus Torvalds# If building the kernel in a separate objtree expand all occurrences
1151da177e4SLinus Torvalds# of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvaldsifeq ($(KBUILD_SRC),)
1181da177e4SLinus Torvalds__c_flags	= $(_c_flags)
1191da177e4SLinus Torvalds__a_flags	= $(_a_flags)
1201da177e4SLinus Torvalds__cpp_flags     = $(_cpp_flags)
1211da177e4SLinus Torvaldselse
1221da177e4SLinus Torvalds
1231da177e4SLinus Torvalds# -I$(obj) locates generated .h files
1241da177e4SLinus Torvalds# $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
1251da177e4SLinus Torvalds#   and locates generated .h files
1261da177e4SLinus Torvalds# FIXME: Replace both with specific CFLAGS* statements in the makefiles
1271da177e4SLinus Torvalds__c_flags	= $(call addtree,-I$(obj)) $(call flags,_c_flags)
1281da177e4SLinus Torvalds__a_flags	=                          $(call flags,_a_flags)
1291da177e4SLinus Torvalds__cpp_flags     =                          $(call flags,_cpp_flags)
1301da177e4SLinus Torvaldsendif
1311da177e4SLinus Torvalds
132d8672b40SSam Ravnborgc_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
1331da177e4SLinus Torvalds		 $(__c_flags) $(modkern_cflags)                           \
134346e15beSJason Baron		 -D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags) \
135346e15beSJason Baron		  $(debug_flags)
1361da177e4SLinus Torvalds
137d8672b40SSam Ravnborga_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
1381da177e4SLinus Torvalds		 $(__a_flags) $(modkern_aflags)
1391da177e4SLinus Torvalds
140d8672b40SSam Ravnborgcpp_flags      = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
141d8672b40SSam Ravnborg		 $(__cpp_flags)
1421da177e4SLinus Torvalds
143f77bf014SSam Ravnborgld_flags       = $(LDFLAGS) $(ldflags-y)
1441da177e4SLinus Torvalds
1451da177e4SLinus Torvalds# Finds the multi-part object the current object will be linked into
1461da177e4SLinus Torvaldsmodname-multi = $(sort $(foreach m,$(multi-used),\
1471da177e4SLinus Torvalds		$(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
1481da177e4SLinus Torvalds
1491da177e4SLinus Torvalds# Shipped files
1501da177e4SLinus Torvalds# ===========================================================================
1511da177e4SLinus Torvalds
1521da177e4SLinus Torvaldsquiet_cmd_shipped = SHIPPED $@
1531da177e4SLinus Torvaldscmd_shipped = cat $< > $@
1541da177e4SLinus Torvalds
1551da177e4SLinus Torvalds$(obj)/%:: $(src)/%_shipped
1561da177e4SLinus Torvalds	$(call cmd,shipped)
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvalds# Commands useful for building a boot image
1591da177e4SLinus Torvalds# ===========================================================================
1601da177e4SLinus Torvalds#
1611da177e4SLinus Torvalds#	Use as following:
1621da177e4SLinus Torvalds#
1631da177e4SLinus Torvalds#	target: source(s) FORCE
1641da177e4SLinus Torvalds#		$(if_changed,ld/objcopy/gzip)
1651da177e4SLinus Torvalds#
166836caba7SSam Ravnborg#	and add target to extra-y so that we know we have to
1671da177e4SLinus Torvalds#	read in the saved command line
1681da177e4SLinus Torvalds
1691da177e4SLinus Torvalds# Linking
1701da177e4SLinus Torvalds# ---------------------------------------------------------------------------
1711da177e4SLinus Torvalds
1721da177e4SLinus Torvaldsquiet_cmd_ld = LD      $@
173f77bf014SSam Ravnborgcmd_ld = $(LD) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \
1741da177e4SLinus Torvalds	       $(filter-out FORCE,$^) -o $@
1751da177e4SLinus Torvalds
1761da177e4SLinus Torvalds# Objcopy
1771da177e4SLinus Torvalds# ---------------------------------------------------------------------------
1781da177e4SLinus Torvalds
1791da177e4SLinus Torvaldsquiet_cmd_objcopy = OBJCOPY $@
1801da177e4SLinus Torvaldscmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
1811da177e4SLinus Torvalds
1821da177e4SLinus Torvalds# Gzip
1831da177e4SLinus Torvalds# ---------------------------------------------------------------------------
1841da177e4SLinus Torvalds
1851da177e4SLinus Torvaldsquiet_cmd_gzip = GZIP    $@
1861da177e4SLinus Torvaldscmd_gzip = gzip -f -9 < $< > $@
1871da177e4SLinus Torvalds
1881da177e4SLinus Torvalds
189*bc22c17eSAlain Knaff# Bzip2
190*bc22c17eSAlain Knaff# ---------------------------------------------------------------------------
191*bc22c17eSAlain Knaff
192*bc22c17eSAlain Knaff# Bzip2 does not include size in file... so we have to fake that
193*bc22c17eSAlain Knaffsize_append=$(CONFIG_SHELL) $(srctree)/scripts/bin_size
194*bc22c17eSAlain Knaff
195*bc22c17eSAlain Knaffquiet_cmd_bzip2 = BZIP2    $@
196*bc22c17eSAlain Knaffcmd_bzip2 = (bzip2 -9 < $< ; $(size_append) $<) > $@ || (rm -f $@ ; false)
197*bc22c17eSAlain Knaff
198*bc22c17eSAlain Knaff# Lzma
199*bc22c17eSAlain Knaff# ---------------------------------------------------------------------------
200*bc22c17eSAlain Knaff
201*bc22c17eSAlain Knaffquiet_cmd_lzma = LZMA    $@
202*bc22c17eSAlain Knaffcmd_lzma = (lzma -9 -c $< ; $(size_append) $<) >$@ || (rm -f $@ ; false)
203