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