| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | # =========================================================================== | 
 | 2 | # kbuild: Generic definitions | 
 | 3 | # =========================================================================== | 
 | 4 |  | 
 | 5 | # Standard vars | 
 | 6 |  | 
 | 7 | comma   := , | 
 | 8 | empty   := | 
 | 9 | space   := $(empty) $(empty) | 
 | 10 |  | 
 | 11 | # Backward compatibility - to be removed... | 
 | 12 | extra-y	+= $(EXTRA_TARGETS) | 
 | 13 | # Figure out what we need to build from the various variables | 
 | 14 | # =========================================================================== | 
 | 15 |  | 
 | 16 | # When an object is listed to be built compiled-in and modular, | 
 | 17 | # only build the compiled-in version | 
 | 18 |  | 
 | 19 | obj-m := $(filter-out $(obj-y),$(obj-m)) | 
 | 20 |  | 
 | 21 | # Libraries are always collected in one lib file. | 
 | 22 | # Filter out objects already built-in | 
 | 23 |  | 
 | 24 | lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m))) | 
 | 25 |  | 
 | 26 |  | 
 | 27 | # Handle objects in subdirs | 
 | 28 | # --------------------------------------------------------------------------- | 
 | 29 | # o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o | 
 | 30 | #   and add the directory to the list of dirs to descend into: $(subdir-y) | 
 | 31 | # o if we encounter foo/ in $(obj-m), remove it from $(obj-m)  | 
 | 32 | #   and add the directory to the list of dirs to descend into: $(subdir-m) | 
 | 33 |  | 
 | 34 | __subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y))) | 
 | 35 | subdir-y	+= $(__subdir-y) | 
 | 36 | __subdir-m	:= $(patsubst %/,%,$(filter %/, $(obj-m))) | 
 | 37 | subdir-m	+= $(__subdir-m) | 
 | 38 | obj-y		:= $(patsubst %/, %/built-in.o, $(obj-y)) | 
 | 39 | obj-m		:= $(filter-out %/, $(obj-m)) | 
 | 40 |  | 
 | 41 | # Subdirectories we need to descend into | 
 | 42 |  | 
 | 43 | subdir-ym	:= $(sort $(subdir-y) $(subdir-m)) | 
 | 44 |  | 
 | 45 | # if $(foo-objs) exists, foo.o is a composite object  | 
 | 46 | multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m)))) | 
 | 47 | multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m)))) | 
 | 48 | multi-used   := $(multi-used-y) $(multi-used-m) | 
 | 49 | single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m))) | 
 | 50 |  | 
 | 51 | # Build list of the parts of our composite objects, our composite | 
 | 52 | # objects depend on those (obviously) | 
 | 53 | multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y))) | 
 | 54 | multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y))) | 
 | 55 | multi-objs   := $(multi-objs-y) $(multi-objs-m) | 
 | 56 |  | 
 | 57 | # $(subdir-obj-y) is the list of objects in $(obj-y) which do not live | 
 | 58 | # in the local directory | 
 | 59 | subdir-obj-y := $(foreach o,$(obj-y),$(if $(filter-out $(o),$(notdir $(o))),$(o))) | 
 | 60 |  | 
 | 61 | # $(obj-dirs) is a list of directories that contain object files | 
 | 62 | obj-dirs := $(dir $(multi-objs) $(subdir-obj-y)) | 
 | 63 |  | 
 | 64 | # Replace multi-part objects by their individual parts, look at local dir only | 
 | 65 | real-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) | 
 | 66 | real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) | 
 | 67 |  | 
 | 68 | # Add subdir path | 
 | 69 |  | 
 | 70 | extra-y		:= $(addprefix $(obj)/,$(extra-y)) | 
 | 71 | always		:= $(addprefix $(obj)/,$(always)) | 
 | 72 | targets		:= $(addprefix $(obj)/,$(targets)) | 
 | 73 | obj-y		:= $(addprefix $(obj)/,$(obj-y)) | 
 | 74 | obj-m		:= $(addprefix $(obj)/,$(obj-m)) | 
 | 75 | lib-y		:= $(addprefix $(obj)/,$(lib-y)) | 
 | 76 | subdir-obj-y	:= $(addprefix $(obj)/,$(subdir-obj-y)) | 
 | 77 | real-objs-y	:= $(addprefix $(obj)/,$(real-objs-y)) | 
 | 78 | real-objs-m	:= $(addprefix $(obj)/,$(real-objs-m)) | 
 | 79 | single-used-m	:= $(addprefix $(obj)/,$(single-used-m)) | 
 | 80 | multi-used-y	:= $(addprefix $(obj)/,$(multi-used-y)) | 
 | 81 | multi-used-m	:= $(addprefix $(obj)/,$(multi-used-m)) | 
 | 82 | multi-objs-y	:= $(addprefix $(obj)/,$(multi-objs-y)) | 
 | 83 | multi-objs-m	:= $(addprefix $(obj)/,$(multi-objs-m)) | 
 | 84 | subdir-ym	:= $(addprefix $(obj)/,$(subdir-ym)) | 
 | 85 | obj-dirs	:= $(addprefix $(obj)/,$(obj-dirs)) | 
 | 86 |  | 
 | 87 | # The temporary file to save gcc -MD generated dependencies must not | 
 | 88 | # contain a comma | 
 | 89 | depfile = $(subst $(comma),_,$(@D)/.$(@F).d) | 
 | 90 |  | 
 | 91 | # These flags are needed for modversions and compiling, so we define them here | 
 | 92 | # already | 
 | 93 | # $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will  | 
 | 94 | # end up in (or would, if it gets compiled in) | 
 | 95 | # Note: It's possible that one object gets potentially linked into more | 
 | 96 | #       than one module. In that case KBUILD_MODNAME will be set to foo_bar, | 
 | 97 | #       where foo and bar are the name of the modules. | 
 | 98 | basename_flags = -DKBUILD_BASENAME=$(subst $(comma),_,$(subst -,_,$(*F))) | 
 | 99 | modname_flags  = $(if $(filter 1,$(words $(modname))),-DKBUILD_MODNAME=$(subst $(comma),_,$(subst -,_,$(modname)))) | 
 | 100 |  | 
 | 101 | _c_flags       = $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(*F).o) | 
 | 102 | _a_flags       = $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(*F).o) | 
 | 103 | _cpp_flags     = $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS_$(@F)) | 
 | 104 |  | 
 | 105 | # If building the kernel in a separate objtree expand all occurrences | 
 | 106 | # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/'). | 
 | 107 |  | 
 | 108 | ifeq ($(KBUILD_SRC),) | 
 | 109 | __c_flags	= $(_c_flags) | 
 | 110 | __a_flags	= $(_a_flags) | 
 | 111 | __cpp_flags     = $(_cpp_flags) | 
 | 112 | else | 
 | 113 |  | 
 | 114 | # Prefix -I with $(srctree) if it is not an absolute path | 
 | 115 | addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1) | 
 | 116 | # Find all -I options and call addtree | 
 | 117 | flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) | 
 | 118 |  | 
 | 119 | # -I$(obj) locates generated .h files | 
 | 120 | # $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files | 
 | 121 | #   and locates generated .h files | 
 | 122 | # FIXME: Replace both with specific CFLAGS* statements in the makefiles | 
 | 123 | __c_flags	= $(call addtree,-I$(obj)) $(call flags,_c_flags) | 
 | 124 | __a_flags	=                          $(call flags,_a_flags) | 
 | 125 | __cpp_flags     =                          $(call flags,_cpp_flags) | 
 | 126 | endif | 
 | 127 |  | 
 | 128 | c_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \ | 
 | 129 | 		 $(__c_flags) $(modkern_cflags) \ | 
 | 130 | 		 $(basename_flags) $(modname_flags) | 
 | 131 |  | 
 | 132 | a_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \ | 
 | 133 | 		 $(__a_flags) $(modkern_aflags) | 
 | 134 |  | 
 | 135 | cpp_flags      = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags) | 
 | 136 |  | 
 | 137 | ld_flags       = $(LDFLAGS) $(EXTRA_LDFLAGS) | 
 | 138 |  | 
 | 139 | # Finds the multi-part object the current object will be linked into | 
 | 140 | modname-multi = $(sort $(foreach m,$(multi-used),\ | 
 | 141 | 		$(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=)))) | 
 | 142 |  | 
 | 143 | # Shipped files | 
 | 144 | # =========================================================================== | 
 | 145 |  | 
 | 146 | quiet_cmd_shipped = SHIPPED $@ | 
 | 147 | cmd_shipped = cat $< > $@ | 
 | 148 |  | 
 | 149 | $(obj)/%:: $(src)/%_shipped | 
 | 150 | 	$(call cmd,shipped) | 
 | 151 |  | 
 | 152 | # Commands useful for building a boot image | 
 | 153 | # =========================================================================== | 
 | 154 | #  | 
 | 155 | #	Use as following: | 
 | 156 | # | 
 | 157 | #	target: source(s) FORCE | 
 | 158 | #		$(if_changed,ld/objcopy/gzip) | 
 | 159 | # | 
 | 160 | #	and add target to EXTRA_TARGETS so that we know we have to | 
 | 161 | #	read in the saved command line | 
 | 162 |  | 
 | 163 | # Linking | 
 | 164 | # --------------------------------------------------------------------------- | 
 | 165 |  | 
 | 166 | quiet_cmd_ld = LD      $@ | 
 | 167 | cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \ | 
 | 168 | 	       $(filter-out FORCE,$^) -o $@  | 
 | 169 |  | 
 | 170 | # Objcopy | 
 | 171 | # --------------------------------------------------------------------------- | 
 | 172 |  | 
 | 173 | quiet_cmd_objcopy = OBJCOPY $@ | 
 | 174 | cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@ | 
 | 175 |  | 
 | 176 | # Gzip | 
 | 177 | # --------------------------------------------------------------------------- | 
 | 178 |  | 
 | 179 | quiet_cmd_gzip = GZIP    $@ | 
 | 180 | cmd_gzip = gzip -f -9 < $< > $@ | 
 | 181 |  | 
 | 182 | # =========================================================================== | 
 | 183 | # Generic stuff | 
 | 184 | # =========================================================================== | 
 | 185 |  | 
 | 186 | ifneq ($(KBUILD_NOCMDDEP),1) | 
 | 187 | # Check if both arguments has same arguments. Result in empty string if equal | 
 | 188 | # User may override this check using make KBUILD_NOCMDDEP=1 | 
 | 189 | arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) ) | 
 | 190 |  | 
 | 191 | endif | 
 | 192 |  | 
 | 193 | # echo command. Short version is $(quiet) equals quiet, otherwise full command | 
 | 194 | echo-cmd = $(if $($(quiet)cmd_$(1)), \ | 
 | 195 | 	echo '  $(subst ','\'',$($(quiet)cmd_$(1)))';) | 
 | 196 |  | 
 | 197 | # function to only execute the passed command if necessary | 
 | 198 | # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file | 
 | 199 | # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars | 
 | 200 | #  | 
 | 201 | if_changed = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \ | 
 | 202 | 	@set -e; \ | 
 | 203 | 	$(echo-cmd) \ | 
 | 204 | 	$(cmd_$(1)); \ | 
 | 205 | 	echo 'cmd_$@ := $(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).cmd) | 
 | 206 |  | 
 | 207 |  | 
 | 208 | # execute the command and also postprocess generated .d dependencies | 
 | 209 | # file | 
 | 210 |  | 
 | 211 | if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\ | 
 | 212 | 	$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),                  \ | 
 | 213 | 	@set -e; \ | 
 | 214 | 	$(echo-cmd) \ | 
 | 215 | 	$(cmd_$(1)); \ | 
 | 216 | 	scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \ | 
 | 217 | 	rm -f $(depfile); \ | 
 | 218 | 	mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd) | 
 | 219 |  | 
 | 220 | # Usage: $(call if_changed_rule,foo) | 
 | 221 | # will check if $(cmd_foo) changed, or any of the prequisites changed, | 
 | 222 | # and if so will execute $(rule_foo) | 
 | 223 |  | 
 | 224 | if_changed_rule = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\ | 
 | 225 | 			@set -e; \ | 
 | 226 | 			$(rule_$(1))) | 
 | 227 |  | 
 | 228 | # If quiet is set, only print short version of command | 
 | 229 |  | 
 | 230 | cmd = @$(if $($(quiet)cmd_$(1)),echo '  $(subst ','\'',$($(quiet)cmd_$(1)))' &&) $(cmd_$(1)) | 
 | 231 |  | 
 | 232 | #	$(call descend,<dir>,<target>) | 
 | 233 | #	Recursively call a sub-make in <dir> with target <target>  | 
 | 234 | # Usage is deprecated, because make do not see this as an invocation of make. | 
 | 235 | descend =$(Q)$(MAKE) -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj=$(1) $(2) | 
 | 236 |  | 
 | 237 | # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= | 
 | 238 | # Usage: | 
 | 239 | # $(Q)$(MAKE) $(build)=dir | 
 | 240 | build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj | 
 | 241 |  | 
 | 242 | # filechk is used to check if the content of a generated file is updated. | 
 | 243 | # Sample usage: | 
 | 244 | # define filechk_sample | 
 | 245 | #	echo $KERNELRELEASE | 
 | 246 | # endef | 
 | 247 | # version.h : Makefile | 
 | 248 | #	$(call filechk,sample) | 
 | 249 | # The rule defined shall write to stdout the content of the new file. | 
 | 250 | # The existing file will be compared with the new one. | 
 | 251 | # - If no file exist it is created | 
 | 252 | # - If the content differ the new file is used | 
 | 253 | # - If they are equal no change, and no timestamp update | 
 | 254 |  | 
 | 255 | define filechk | 
 | 256 | 	$(Q)set -e;				\ | 
 | 257 | 	echo '  CHK     $@';			\ | 
 | 258 | 	mkdir -p $(dir $@);			\ | 
 | 259 | 	$(filechk_$(1)) $(2) > $@.tmp;		\ | 
 | 260 | 	if [ -r $@ ] && cmp -s $@ $@.tmp; then	\ | 
 | 261 | 		rm -f $@.tmp;			\ | 
 | 262 | 	else					\ | 
 | 263 | 		echo '  UPD     $@';		\ | 
 | 264 | 		mv -f $@.tmp $@;		\ | 
 | 265 | 	fi | 
 | 266 | endef | 
 | 267 |  |