xref: /linux/scripts/Makefile.lib (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
11da177e4SLinus Torvalds# Backward compatibility - to be removed...
21da177e4SLinus Torvaldsextra-y	+= $(EXTRA_TARGETS)
31da177e4SLinus Torvalds# Figure out what we need to build from the various variables
41da177e4SLinus Torvalds# ===========================================================================
51da177e4SLinus Torvalds
61da177e4SLinus Torvalds# When an object is listed to be built compiled-in and modular,
71da177e4SLinus Torvalds# only build the compiled-in version
81da177e4SLinus Torvalds
91da177e4SLinus Torvaldsobj-m := $(filter-out $(obj-y),$(obj-m))
101da177e4SLinus Torvalds
111da177e4SLinus Torvalds# Libraries are always collected in one lib file.
121da177e4SLinus Torvalds# Filter out objects already built-in
131da177e4SLinus Torvalds
141da177e4SLinus Torvaldslib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
151da177e4SLinus Torvalds
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds# Handle objects in subdirs
181da177e4SLinus Torvalds# ---------------------------------------------------------------------------
191da177e4SLinus Torvalds# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
201da177e4SLinus Torvalds#   and add the directory to the list of dirs to descend into: $(subdir-y)
211da177e4SLinus Torvalds# o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
221da177e4SLinus Torvalds#   and add the directory to the list of dirs to descend into: $(subdir-m)
231da177e4SLinus Torvalds
241da177e4SLinus Torvalds__subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y)))
251da177e4SLinus Torvaldssubdir-y	+= $(__subdir-y)
261da177e4SLinus Torvalds__subdir-m	:= $(patsubst %/,%,$(filter %/, $(obj-m)))
271da177e4SLinus Torvaldssubdir-m	+= $(__subdir-m)
281da177e4SLinus Torvaldsobj-y		:= $(patsubst %/, %/built-in.o, $(obj-y))
291da177e4SLinus Torvaldsobj-m		:= $(filter-out %/, $(obj-m))
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds# Subdirectories we need to descend into
321da177e4SLinus Torvalds
331da177e4SLinus Torvaldssubdir-ym	:= $(sort $(subdir-y) $(subdir-m))
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds# if $(foo-objs) exists, foo.o is a composite object
361da177e4SLinus Torvaldsmulti-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
371da177e4SLinus Torvaldsmulti-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
381da177e4SLinus Torvaldsmulti-used   := $(multi-used-y) $(multi-used-m)
391da177e4SLinus Torvaldssingle-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds# Build list of the parts of our composite objects, our composite
421da177e4SLinus Torvalds# objects depend on those (obviously)
431da177e4SLinus Torvaldsmulti-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
441da177e4SLinus Torvaldsmulti-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
451da177e4SLinus Torvaldsmulti-objs   := $(multi-objs-y) $(multi-objs-m)
461da177e4SLinus Torvalds
471da177e4SLinus Torvalds# $(subdir-obj-y) is the list of objects in $(obj-y) which do not live
481da177e4SLinus Torvalds# in the local directory
491da177e4SLinus Torvaldssubdir-obj-y := $(foreach o,$(obj-y),$(if $(filter-out $(o),$(notdir $(o))),$(o)))
501da177e4SLinus Torvalds
511da177e4SLinus Torvalds# $(obj-dirs) is a list of directories that contain object files
521da177e4SLinus Torvaldsobj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
531da177e4SLinus Torvalds
541da177e4SLinus Torvalds# Replace multi-part objects by their individual parts, look at local dir only
551da177e4SLinus 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)
561da177e4SLinus Torvaldsreal-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
571da177e4SLinus Torvalds
581da177e4SLinus Torvalds# Add subdir path
591da177e4SLinus Torvalds
601da177e4SLinus Torvaldsextra-y		:= $(addprefix $(obj)/,$(extra-y))
611da177e4SLinus Torvaldsalways		:= $(addprefix $(obj)/,$(always))
621da177e4SLinus Torvaldstargets		:= $(addprefix $(obj)/,$(targets))
631da177e4SLinus Torvaldsobj-y		:= $(addprefix $(obj)/,$(obj-y))
641da177e4SLinus Torvaldsobj-m		:= $(addprefix $(obj)/,$(obj-m))
651da177e4SLinus Torvaldslib-y		:= $(addprefix $(obj)/,$(lib-y))
661da177e4SLinus Torvaldssubdir-obj-y	:= $(addprefix $(obj)/,$(subdir-obj-y))
671da177e4SLinus Torvaldsreal-objs-y	:= $(addprefix $(obj)/,$(real-objs-y))
681da177e4SLinus Torvaldsreal-objs-m	:= $(addprefix $(obj)/,$(real-objs-m))
691da177e4SLinus Torvaldssingle-used-m	:= $(addprefix $(obj)/,$(single-used-m))
701da177e4SLinus Torvaldsmulti-used-y	:= $(addprefix $(obj)/,$(multi-used-y))
711da177e4SLinus Torvaldsmulti-used-m	:= $(addprefix $(obj)/,$(multi-used-m))
721da177e4SLinus Torvaldsmulti-objs-y	:= $(addprefix $(obj)/,$(multi-objs-y))
731da177e4SLinus Torvaldsmulti-objs-m	:= $(addprefix $(obj)/,$(multi-objs-m))
741da177e4SLinus Torvaldssubdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
751da177e4SLinus Torvaldsobj-dirs	:= $(addprefix $(obj)/,$(obj-dirs))
761da177e4SLinus Torvalds
771da177e4SLinus Torvalds# These flags are needed for modversions and compiling, so we define them here
781da177e4SLinus Torvalds# already
791da177e4SLinus Torvalds# $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
801da177e4SLinus Torvalds# end up in (or would, if it gets compiled in)
811da177e4SLinus Torvalds# Note: It's possible that one object gets potentially linked into more
821da177e4SLinus Torvalds#       than one module. In that case KBUILD_MODNAME will be set to foo_bar,
831da177e4SLinus Torvalds#       where foo and bar are the name of the modules.
84f83b5e32SUstyugov Romanname-fix = $(subst $(comma),_,$(subst -,_,$1))
85*5e8d780dSSam Ravnborgbasename_flags = -D"KBUILD_BASENAME=KBUILD_STR($(call name-fix,$(basetarget)))"
86f83b5e32SUstyugov Romanmodname_flags  = $(if $(filter 1,$(words $(modname))),\
87f83b5e32SUstyugov Roman                 -D"KBUILD_MODNAME=KBUILD_STR($(call name-fix,$(modname)))")
881da177e4SLinus Torvalds
89*5e8d780dSSam Ravnborg_c_flags       = $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(basetarget).o)
90*5e8d780dSSam Ravnborg_a_flags       = $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(basetarget).o)
911da177e4SLinus Torvalds_cpp_flags     = $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS_$(@F))
921da177e4SLinus Torvalds
931da177e4SLinus Torvalds# If building the kernel in a separate objtree expand all occurrences
941da177e4SLinus Torvalds# of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
951da177e4SLinus Torvalds
961da177e4SLinus Torvaldsifeq ($(KBUILD_SRC),)
971da177e4SLinus Torvalds__c_flags	= $(_c_flags)
981da177e4SLinus Torvalds__a_flags	= $(_a_flags)
991da177e4SLinus Torvalds__cpp_flags     = $(_cpp_flags)
1001da177e4SLinus Torvaldselse
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds# -I$(obj) locates generated .h files
1031da177e4SLinus Torvalds# $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
1041da177e4SLinus Torvalds#   and locates generated .h files
1051da177e4SLinus Torvalds# FIXME: Replace both with specific CFLAGS* statements in the makefiles
1061da177e4SLinus Torvalds__c_flags	= $(call addtree,-I$(obj)) $(call flags,_c_flags)
1071da177e4SLinus Torvalds__a_flags	=                          $(call flags,_a_flags)
1081da177e4SLinus Torvalds__cpp_flags     =                          $(call flags,_cpp_flags)
1091da177e4SLinus Torvaldsendif
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvaldsc_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
1121da177e4SLinus Torvalds		 $(__c_flags) $(modkern_cflags) \
113f83b5e32SUstyugov Roman		 -D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags)
1141da177e4SLinus Torvalds
1151da177e4SLinus Torvaldsa_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
1161da177e4SLinus Torvalds		 $(__a_flags) $(modkern_aflags)
1171da177e4SLinus Torvalds
1181da177e4SLinus Torvaldscpp_flags      = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
1191da177e4SLinus Torvalds
1201da177e4SLinus Torvaldsld_flags       = $(LDFLAGS) $(EXTRA_LDFLAGS)
1211da177e4SLinus Torvalds
1221da177e4SLinus Torvalds# Finds the multi-part object the current object will be linked into
1231da177e4SLinus Torvaldsmodname-multi = $(sort $(foreach m,$(multi-used),\
1241da177e4SLinus Torvalds		$(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
1251da177e4SLinus Torvalds
1261da177e4SLinus Torvalds# Shipped files
1271da177e4SLinus Torvalds# ===========================================================================
1281da177e4SLinus Torvalds
1291da177e4SLinus Torvaldsquiet_cmd_shipped = SHIPPED $@
1301da177e4SLinus Torvaldscmd_shipped = cat $< > $@
1311da177e4SLinus Torvalds
1321da177e4SLinus Torvalds$(obj)/%:: $(src)/%_shipped
1331da177e4SLinus Torvalds	$(call cmd,shipped)
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds# Commands useful for building a boot image
1361da177e4SLinus Torvalds# ===========================================================================
1371da177e4SLinus Torvalds#
1381da177e4SLinus Torvalds#	Use as following:
1391da177e4SLinus Torvalds#
1401da177e4SLinus Torvalds#	target: source(s) FORCE
1411da177e4SLinus Torvalds#		$(if_changed,ld/objcopy/gzip)
1421da177e4SLinus Torvalds#
1431da177e4SLinus Torvalds#	and add target to EXTRA_TARGETS so that we know we have to
1441da177e4SLinus Torvalds#	read in the saved command line
1451da177e4SLinus Torvalds
1461da177e4SLinus Torvalds# Linking
1471da177e4SLinus Torvalds# ---------------------------------------------------------------------------
1481da177e4SLinus Torvalds
1491da177e4SLinus Torvaldsquiet_cmd_ld = LD      $@
1501da177e4SLinus Torvaldscmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
1511da177e4SLinus Torvalds	       $(filter-out FORCE,$^) -o $@
1521da177e4SLinus Torvalds
1531da177e4SLinus Torvalds# Objcopy
1541da177e4SLinus Torvalds# ---------------------------------------------------------------------------
1551da177e4SLinus Torvalds
1561da177e4SLinus Torvaldsquiet_cmd_objcopy = OBJCOPY $@
1571da177e4SLinus Torvaldscmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
1581da177e4SLinus Torvalds
1591da177e4SLinus Torvalds# Gzip
1601da177e4SLinus Torvalds# ---------------------------------------------------------------------------
1611da177e4SLinus Torvalds
1621da177e4SLinus Torvaldsquiet_cmd_gzip = GZIP    $@
1631da177e4SLinus Torvaldscmd_gzip = gzip -f -9 < $< > $@
1641da177e4SLinus Torvalds
1651da177e4SLinus Torvalds
166