Back to home page

LXR

 
 

    


0001 ####
0002 # kbuild: Generic definitions
0003 
0004 # Convenient variables
0005 comma   := ,
0006 quote   := "
0007 squote  := '
0008 empty   :=
0009 space   := $(empty) $(empty)
0010 space_escape := _-_SPACE_-_
0011 
0012 ###
0013 # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
0014 dot-target = $(dir $@).$(notdir $@)
0015 
0016 ###
0017 # The temporary file to save gcc -MD generated dependencies must not
0018 # contain a comma
0019 depfile = $(subst $(comma),_,$(dot-target).d)
0020 
0021 ###
0022 # filename of target with directory and extension stripped
0023 basetarget = $(basename $(notdir $@))
0024 
0025 ###
0026 # filename of first prerequisite with directory and extension stripped
0027 baseprereq = $(basename $(notdir $<))
0028 
0029 ###
0030 # Escape single quote for use in echo statements
0031 escsq = $(subst $(squote),'\$(squote)',$1)
0032 
0033 ###
0034 # Easy method for doing a status message
0035        kecho := :
0036  quiet_kecho := echo
0037 silent_kecho := :
0038 kecho := $($(quiet)kecho)
0039 
0040 ###
0041 # filechk is used to check if the content of a generated file is updated.
0042 # Sample usage:
0043 # define filechk_sample
0044 #       echo $KERNELRELEASE
0045 # endef
0046 # version.h : Makefile
0047 #       $(call filechk,sample)
0048 # The rule defined shall write to stdout the content of the new file.
0049 # The existing file will be compared with the new one.
0050 # - If no file exist it is created
0051 # - If the content differ the new file is used
0052 # - If they are equal no change, and no timestamp update
0053 # - stdin is piped in from the first prerequisite ($<) so one has
0054 #   to specify a valid file as first prerequisite (often the kbuild file)
0055 define filechk
0056         $(Q)set -e;                             \
0057         $(kecho) '  CHK     $@';                \
0058         mkdir -p $(dir $@);                     \
0059         $(filechk_$(1)) < $< > $@.tmp;          \
0060         if [ -r $@ ] && cmp -s $@ $@.tmp; then  \
0061                 rm -f $@.tmp;                   \
0062         else                                    \
0063                 $(kecho) '  UPD     $@';        \
0064                 mv -f $@.tmp $@;                \
0065         fi
0066 endef
0067 
0068 ######
0069 # gcc support functions
0070 # See documentation in Documentation/kbuild/makefiles.txt
0071 
0072 # cc-cross-prefix
0073 # Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
0074 # Return first prefix where a prefix$(CC) is found in PATH.
0075 # If no $(CC) found in PATH with listed prefixes return nothing
0076 cc-cross-prefix =  \
0077         $(word 1, $(foreach c,$(1),                                   \
0078                 $(shell set -e;                                       \
0079                 if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \
0080                         echo $(c);                                    \
0081                 fi)))
0082 
0083 # output directory for tests below
0084 TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
0085 
0086 # try-run
0087 # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
0088 # Exit code chooses option. "$$TMP" is can be used as temporary file and
0089 # is automatically cleaned up.
0090 try-run = $(shell set -e;               \
0091         TMP="$(TMPOUT).$$$$.tmp";       \
0092         TMPO="$(TMPOUT).$$$$.o";        \
0093         if ($(1)) >/dev/null 2>&1;      \
0094         then echo "$(2)";               \
0095         else echo "$(3)";               \
0096         fi;                             \
0097         rm -f "$$TMP" "$$TMPO")
0098 
0099 # as-option
0100 # Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
0101 
0102 as-option = $(call try-run,\
0103         $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
0104 
0105 # as-instr
0106 # Usage: cflags-y += $(call as-instr,instr,option1,option2)
0107 
0108 as-instr = $(call try-run,\
0109         printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
0110 
0111 # Do not attempt to build with gcc plugins during cc-option tests.
0112 # (And this uses delayed resolution so the flags will be up to date.)
0113 CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
0114 
0115 # cc-option
0116 # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
0117 
0118 cc-option = $(call try-run,\
0119         $(CC) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
0120 
0121 # cc-option-yn
0122 # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
0123 cc-option-yn = $(call try-run,\
0124         $(CC) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
0125 
0126 # cc-option-align
0127 # Prefix align with either -falign or -malign
0128 cc-option-align = $(subst -functions=0,,\
0129         $(call cc-option,-falign-functions=0,-malign-functions=0))
0130 
0131 # cc-disable-warning
0132 # Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
0133 cc-disable-warning = $(call try-run,\
0134         $(CC) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
0135 
0136 # cc-name
0137 # Expands to either gcc or clang
0138 cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
0139 
0140 # cc-version
0141 cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
0142 
0143 # cc-fullversion
0144 cc-fullversion = $(shell $(CONFIG_SHELL) \
0145         $(srctree)/scripts/gcc-version.sh -p $(CC))
0146 
0147 # cc-ifversion
0148 # Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
0149 cc-ifversion = $(shell [ $(cc-version) $(1) $(2) ] && echo $(3) || echo $(4))
0150 
0151 # cc-ldoption
0152 # Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
0153 cc-ldoption = $(call try-run,\
0154         $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
0155 
0156 # ld-option
0157 # Usage: LDFLAGS += $(call ld-option, -X)
0158 ld-option = $(call try-run,\
0159         $(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2))
0160 
0161 # ar-option
0162 # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
0163 # Important: no spaces around options
0164 ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2))
0165 
0166 # ld-version
0167 # Note this is mainly for HJ Lu's 3 number binutil versions
0168 ld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh)
0169 
0170 # ld-ifversion
0171 # Usage:  $(call ld-ifversion, -ge, 22252, y)
0172 ld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
0173 
0174 ######
0175 
0176 ###
0177 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
0178 # Usage:
0179 # $(Q)$(MAKE) $(build)=dir
0180 build := -f $(srctree)/scripts/Makefile.build obj
0181 
0182 ###
0183 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj=
0184 # Usage:
0185 # $(Q)$(MAKE) $(modbuiltin)=dir
0186 modbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj
0187 
0188 ###
0189 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
0190 # Usage:
0191 # $(Q)$(MAKE) $(dtbinst)=dir
0192 dtbinst := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.dtbinst obj
0193 
0194 ###
0195 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
0196 # Usage:
0197 # $(Q)$(MAKE) $(clean)=dir
0198 clean := -f $(srctree)/scripts/Makefile.clean obj
0199 
0200 ###
0201 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.headersinst obj=
0202 # Usage:
0203 # $(Q)$(MAKE) $(hdr-inst)=dir
0204 hdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
0205 
0206 # Prefix -I with $(srctree) if it is not an absolute path.
0207 # skip if -I has no parameter
0208 addtree = $(if $(patsubst -I%,%,$(1)), \
0209 $(if $(filter-out -I/% -I./% -I../%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1)),$(1)))
0210 
0211 # Find all -I options and call addtree
0212 flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
0213 
0214 # echo command.
0215 # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
0216 echo-cmd = $(if $($(quiet)cmd_$(1)),\
0217         echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
0218 
0219 # printing commands
0220 cmd = @$(echo-cmd) $(cmd_$(1))
0221 
0222 # Add $(obj)/ for paths that are not absolute
0223 objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
0224 
0225 ###
0226 # if_changed      - execute command if any prerequisite is newer than
0227 #                   target, or command line has changed
0228 # if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
0229 #                   including used config symbols
0230 # if_changed_rule - as if_changed but execute rule instead
0231 # See Documentation/kbuild/makefiles.txt for more info
0232 
0233 ifneq ($(KBUILD_NOCMDDEP),1)
0234 # Check if both arguments are the same including their order. Result is empty
0235 # string if equal. User may override this check using make KBUILD_NOCMDDEP=1
0236 arg-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
0237                          $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
0238 else
0239 arg-check = $(if $(strip $(cmd_$@)),,1)
0240 endif
0241 
0242 # Replace >$< with >$$< to preserve $ when reloading the .cmd file
0243 # (needed for make)
0244 # Replace >#< with >\#< to avoid starting a comment in the .cmd file
0245 # (needed for make)
0246 # Replace >'< with >'\''< to be able to enclose the whole string in '...'
0247 # (needed for the shell)
0248 make-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1)))))
0249 
0250 # Find any prerequisites that is newer than target or that does not exist.
0251 # PHONY targets skipped in both cases.
0252 any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
0253 
0254 # Execute command if command has changed or prerequisite(s) are updated.
0255 #
0256 if_changed = $(if $(strip $(any-prereq) $(arg-check)),                       \
0257         @set -e;                                                             \
0258         $(echo-cmd) $(cmd_$(1));                                             \
0259         printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
0260 
0261 # Execute the command and also postprocess generated .d dependencies file.
0262 if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ),                  \
0263         @set -e;                                                             \
0264         $(cmd_and_fixdep), @:)
0265 
0266 ifndef CONFIG_TRIM_UNUSED_KSYMS
0267 
0268 cmd_and_fixdep =                                                             \
0269         $(echo-cmd) $(cmd_$(1));                                             \
0270         scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
0271         rm -f $(depfile);                                                    \
0272         mv -f $(dot-target).tmp $(dot-target).cmd;
0273 
0274 else
0275 
0276 # Filter out exported kernel symbol names from the preprocessor output.
0277 # See also __KSYM_DEPS__ in include/linux/export.h.
0278 # We disable the depfile generation here, so as not to overwrite the existing
0279 # depfile while fixdep is parsing it.
0280 flags_nodeps = $(filter-out -Wp$(comma)-M%, $($(1)))
0281 ksym_dep_filter =                                                            \
0282         case "$(1)" in                                                       \
0283           cc_*_c|cpp_i_c)                                                    \
0284             $(CPP) $(call flags_nodeps,c_flags) -D__KSYM_DEPS__ $< ;;        \
0285           as_*_S|cpp_s_S)                                                    \
0286             $(CPP) $(call flags_nodeps,a_flags) -D__KSYM_DEPS__ $< ;;        \
0287           boot*|build*|*cpp_lds_S|dtc|host*|vdso*) : ;;                      \
0288           *) echo "Don't know how to preprocess $(1)" >&2; false ;;          \
0289         esac | tr ";" "\n" | sed -rn 's/^.*=== __KSYM_(.*) ===.*$$/KSYM_\1/p'
0290 
0291 cmd_and_fixdep =                                                             \
0292         $(echo-cmd) $(cmd_$(1));                                             \
0293         $(ksym_dep_filter) |                                                 \
0294                 scripts/basic/fixdep -e $(depfile) $@ '$(make-cmd)'          \
0295                         > $(dot-target).tmp;                                 \
0296         rm -f $(depfile);                                                    \
0297         mv -f $(dot-target).tmp $(dot-target).cmd;
0298 
0299 endif
0300 
0301 # Usage: $(call if_changed_rule,foo)
0302 # Will check if $(cmd_foo) or any of the prerequisites changed,
0303 # and if so will execute $(rule_foo).
0304 if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ),                 \
0305         @set -e;                                                             \
0306         $(rule_$(1)), @:)
0307 
0308 ###
0309 # why - tell why a a target got build
0310 #       enabled by make V=2
0311 #       Output (listed in the order they are checked):
0312 #          (1) - due to target is PHONY
0313 #          (2) - due to target missing
0314 #          (3) - due to: file1.h file2.h
0315 #          (4) - due to command line change
0316 #          (5) - due to missing .cmd file
0317 #          (6) - due to target not in $(targets)
0318 # (1) PHONY targets are always build
0319 # (2) No target, so we better build it
0320 # (3) Prerequisite is newer than target
0321 # (4) The command line stored in the file named dir/.target.cmd
0322 #     differed from actual command line. This happens when compiler
0323 #     options changes
0324 # (5) No dir/.target.cmd file (used to store command line)
0325 # (6) No dir/.target.cmd file and target not listed in $(targets)
0326 #     This is a good hint that there is a bug in the kbuild file
0327 ifeq ($(KBUILD_VERBOSE),2)
0328 why =                                                                        \
0329     $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
0330         $(if $(wildcard $@),                                                 \
0331             $(if $(strip $(any-prereq)),- due to: $(any-prereq),             \
0332                 $(if $(arg-check),                                           \
0333                     $(if $(cmd_$@),- due to command line change,             \
0334                         $(if $(filter $@, $(targets)),                       \
0335                             - due to missing .cmd file,                      \
0336                             - due to $(notdir $@) not in $$(targets)         \
0337                          )                                                   \
0338                      )                                                       \
0339                  )                                                           \
0340              ),                                                              \
0341              - due to target missing                                         \
0342          )                                                                   \
0343      )
0344 
0345 echo-why = $(call escsq, $(strip $(why)))
0346 endif
0347 
0348 ###############################################################################
0349 #
0350 # When a Kconfig string contains a filename, it is suitable for
0351 # passing to shell commands. It is surrounded by double-quotes, and
0352 # any double-quotes or backslashes within it are escaped by
0353 # backslashes.
0354 #
0355 # This is no use for dependencies or $(wildcard). We need to strip the
0356 # surrounding quotes and the escaping from quotes and backslashes, and
0357 # we *do* need to escape any spaces in the string. So, for example:
0358 #
0359 # Usage: $(eval $(call config_filename,FOO))
0360 #
0361 # Defines FOO_FILENAME based on the contents of the CONFIG_FOO option,
0362 # transformed as described above to be suitable for use within the
0363 # makefile.
0364 #
0365 # Also, if the filename is a relative filename and exists in the source
0366 # tree but not the build tree, define FOO_SRCPREFIX as $(srctree)/ to
0367 # be prefixed to *both* command invocation and dependencies.
0368 #
0369 # Note: We also print the filenames in the quiet_cmd_foo text, and
0370 # perhaps ought to have a version specially escaped for that purpose.
0371 # But it's only cosmetic, and $(patsubst "%",%,$(CONFIG_FOO)) is good
0372 # enough.  It'll strip the quotes in the common case where there's no
0373 # space and it's a simple filename, and it'll retain the quotes when
0374 # there's a space. There are some esoteric cases in which it'll print
0375 # the wrong thing, but we don't really care. The actual dependencies
0376 # and commands *do* get it right, with various combinations of single
0377 # and double quotes, backslashes and spaces in the filenames.
0378 #
0379 ###############################################################################
0380 #
0381 define config_filename
0382 ifneq ($$(CONFIG_$(1)),"")
0383 $(1)_FILENAME := $$(subst \\,\,$$(subst \$$(quote),$$(quote),$$(subst $$(space_escape),\$$(space),$$(patsubst "%",%,$$(subst $$(space),$$(space_escape),$$(CONFIG_$(1)))))))
0384 ifneq ($$(patsubst /%,%,$$(firstword $$($(1)_FILENAME))),$$(firstword $$($(1)_FILENAME)))
0385 else
0386 ifeq ($$(wildcard $$($(1)_FILENAME)),)
0387 ifneq ($$(wildcard $$(srctree)/$$($(1)_FILENAME)),)
0388 $(1)_SRCPREFIX := $(srctree)/
0389 endif
0390 endif
0391 endif
0392 endif
0393 endef
0394 #
0395 ###############################################################################