2 # kbuild: Generic definitions
8 space := $(empty) $(empty)
11 # The temporary file to save gcc -MD generated dependencies must not
13 depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
16 # basetarget equals the filename of the target with no extension.
17 # So 'foo/bar.o' becomes 'bar'
18 basetarget = $(basename $(notdir $@))
21 # Escape single quote for use in echo statements
22 escsq = $(subst $(squote),'\$(squote)',$1)
25 # filechk is used to check if the content of a generated file is updated.
27 # define filechk_sample
30 # version.h : Makefile
31 # $(call filechk,sample)
32 # The rule defined shall write to stdout the content of the new file.
33 # The existing file will be compared with the new one.
34 # - If no file exist it is created
35 # - If the content differ the new file is used
36 # - If they are equal no change, and no timestamp update
37 # - stdin is piped in from the first prerequisite ($<) so one has
38 # to specify a valid file as first prerequisite (often the kbuild file)
43 $(filechk_$(1)) < $< > $@.tmp; \
44 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
53 # gcc support functions
54 # See documentation in Documentation/kbuild/makefiles.txt
57 # Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,)
59 as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \
60 -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \
61 else echo "$(2)"; fi ;)
64 # Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586)
66 cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
67 > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
70 # Usage: flag := $(call cc-option-yn, -march=winchip-c6)
71 cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
72 > /dev/null 2>&1; then echo "y"; else echo "n"; fi;)
75 # Prefix align with either -falign or -malign
76 cc-option-align = $(subst -functions=0,,\
77 $(call cc-option,-falign-functions=0,-malign-functions=0))
80 # Usage gcc-ver := $(call cc-version, $(CC))
81 cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh \
82 $(if $(1), $(1), $(CC)))
85 # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
86 cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \
90 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
92 # $(Q)$(MAKE) $(build)=dir
93 build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
95 # Prefix -I with $(srctree) if it is not an absolute path
96 addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
97 # Find all -I options and call addtree
98 flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
100 # If quiet is set, only print short version of command
101 cmd = @$(echo-cmd) $(cmd_$(1))
103 # Add $(obj)/ for paths that is not absolute
104 objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
107 # if_changed - execute command if any prerequisite is newer than
108 # target, or command line has changed
109 # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
110 # including used config symbols
111 # if_changed_rule - as if_changed but execute rule instead
112 # See Documentation/kbuild/makefiles.txt for more info
114 ifneq ($(KBUILD_NOCMDDEP),1)
115 # Check if both arguments has same arguments. Result in empty string if equal
116 # User may override this check using make KBUILD_NOCMDDEP=1
117 arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
120 # echo command. Short version is $(quiet) equals quiet, otherwise full command
121 echo-cmd = $(if $($(quiet)cmd_$(1)), \
122 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
124 make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
126 # function to only execute the passed command if necessary
127 # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
128 # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
130 if_changed = $(if $(strip $(filter-out $(PHONY),$?) \
131 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
133 $(echo-cmd) $(cmd_$(1)); \
134 echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
136 # execute the command and also postprocess generated .d dependencies
138 if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \
139 $(filter-out FORCE $(wildcard $^),$^) \
140 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
142 $(echo-cmd) $(cmd_$(1)); \
143 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
145 mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
147 # Usage: $(call if_changed_rule,foo)
148 # will check if $(cmd_foo) changed, or any of the prequisites changed,
149 # and if so will execute $(rule_foo)
150 if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \
151 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\