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 # Escape single quote for use in echo statements
17 escsq = $(subst $(squote),'\$(squote)',$1)
20 # filechk is used to check if the content of a generated file is updated.
22 # define filechk_sample
25 # version.h : Makefile
26 # $(call filechk,sample)
27 # The rule defined shall write to stdout the content of the new file.
28 # The existing file will be compared with the new one.
29 # - If no file exist it is created
30 # - If the content differ the new file is used
31 # - If they are equal no change, and no timestamp update
32 # - stdin is piped in from the first prerequisite ($<) so one has
33 # to specify a valid file as first prerequisite (often the kbuild file)
38 $(filechk_$(1)) < $< > $@.tmp; \
39 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
48 # gcc support functions
49 # See documentation in Documentation/kbuild/makefiles.txt
52 # Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,)
54 as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \
55 -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \
56 else echo "$(2)"; fi ;)
59 # Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586)
61 cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
62 > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
65 # Usage: flag := $(call cc-option-yn, -march=winchip-c6)
66 cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
67 > /dev/null 2>&1; then echo "y"; else echo "n"; fi;)
70 # Prefix align with either -falign or -malign
71 cc-option-align = $(subst -functions=0,,\
72 $(call cc-option,-falign-functions=0,-malign-functions=0))
75 # Usage gcc-ver := $(call cc-version, $(CC))
76 cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh \
77 $(if $(1), $(1), $(CC)))
80 # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
81 cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \
85 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
87 # $(Q)$(MAKE) $(build)=dir
88 build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
90 # If quiet is set, only print short version of command
91 cmd = @$(echo-cmd) $(cmd_$(1))
93 # Add $(obj)/ for paths that is not absolute
94 objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
97 # if_changed - execute command if any prerequisite is newer than
98 # target, or command line has changed
99 # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
100 # including used config symbols
101 # if_changed_rule - as if_changed but execute rule instead
102 # See Documentation/kbuild/makefiles.txt for more info
104 ifneq ($(KBUILD_NOCMDDEP),1)
105 # Check if both arguments has same arguments. Result in empty string if equal
106 # User may override this check using make KBUILD_NOCMDDEP=1
107 arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
110 # echo command. Short version is $(quiet) equals quiet, otherwise full command
111 echo-cmd = $(if $($(quiet)cmd_$(1)), \
112 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
114 make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
116 # function to only execute the passed command if necessary
117 # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
118 # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
120 if_changed = $(if $(strip $(filter-out $(PHONY),$?) \
121 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
123 $(echo-cmd) $(cmd_$(1)); \
124 echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
126 # execute the command and also postprocess generated .d dependencies
128 if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \
129 $(filter-out FORCE $(wildcard $^),$^) \
130 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
132 $(echo-cmd) $(cmd_$(1)); \
133 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
135 mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
137 # Usage: $(call if_changed_rule,foo)
138 # will check if $(cmd_foo) changed, or any of the prequisites changed,
139 # and if so will execute $(rule_foo)
140 if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \
141 $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\