11####
22# kbuild: Generic definitions
33
4- # Convinient variables
4+ # Convenient constants
55comma := ,
66squote := '
77empty :=
@@ -56,93 +56,102 @@ endef
5656# gcc support functions
5757# See documentation in Documentation/kbuild/makefiles.txt
5858
59- # output directory for tests below
60- TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
59+ # checker-shell
60+ # Usage: option = $(call checker-shell, $(CC)...-o $$OUT, option-ok, otherwise)
61+ # Exit code chooses option. $$OUT is safe location for needless output.
62+ define checker-shell
63+ $(shell set -e; \
64+ DIR=$(KBUILD_EXTMOD); \
65+ cd $${DIR:-$(objtree)}; \
66+ OUT=$$PWD/.$$$$.null; \
67+ \
68+ ln -s /dev/null $$OUT; \
69+ if $(1) >/dev/null 2>&1; \
70+ then echo "$(2)"; \
71+ else echo "$(3)"; \
72+ fi; \
73+ rm -f $$OUT)
74+ endef
6175
6276# as-option
6377# Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,)
64-
65- as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \
66- -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \
67- else echo "$(2)"; fi ;)
78+ as-option = $(call checker-shell, \
79+ $(CC) $(CFLAGS) $(1) -c -xassembler /dev/null -o $$OUT, $(1), $(2))
6880
6981# as-instr
7082# Usage: cflags-y += $(call as-instr, instr, option1, option2)
71-
72- as-instr = $(shell if echo -e "$(1)" | \
73- $(CC) $(AFLAGS) -c -xassembler - \
74- -o $(TMPOUT)astest$$$$.out > /dev/null 2>&1; \
75- then rm $(TMPOUT)astest$$$$.out; echo "$(2)"; \
76- else echo "$(3)"; fi)
83+ as-instr = $(call checker-shell, \
84+ printf "$(1)" | $(CC) $(AFLAGS) -c -xassembler -o $$OUT -, $(2), $(3))
7785
7886# cc-option
7987# Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586)
80-
81- cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
82- > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
88+ cc-option = $(call checker-shell, \
89+ $(CC) $(CFLAGS) $(if $(3),$(3),$(1)) -S -xc /dev/null -o $$OUT, $(1), $(2))
8390
8491# cc-option-yn
8592# Usage: flag := $(call cc-option-yn, -march=winchip-c6)
86- cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
87- > /dev/null 2>&1; then echo "y"; else echo "n"; fi;)
93+ cc-option-yn = $(call cc-option, "y", "n", $(1))
8894
8995# cc-option-align
9096# Prefix align with either -falign or -malign
9197cc-option-align = $(subst -functions=0,,\
92- $(call cc-option,-falign-functions=0,-malign-functions=0))
98+ $(call cc-option,-falign-functions=0,-malign-functions=0))
9399
94100# cc-version
95101# Usage gcc-ver := $(call cc-version, $(CC))
96102cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
97103
98104# cc-ifversion
99105# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
100- cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \
101- echo $(3); fi;)
106+ cc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3))
102107
103108# ld-option
104109# Usage: ldflags += $(call ld-option, -Wl$(comma)--hash-style=both)
105- ld-option = $(shell if $(CC) $(1) -nostdlib -xc /dev/null \
106- -o $(TMPOUT)ldtest$$$$.out > /dev/null 2>&1; \
107- then rm $(TMPOUT)ldtest$$$$.out; echo "$(1)"; \
108- else echo "$(2)"; fi)
110+ ld-option = $(call checker-shell, \
111+ $(CC) $(1) -nostdlib -xc /dev/null -o $$OUT, $(1), $(2))
112+
113+ ######
109114
110- ###
111115# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
112116# Usage:
113117# $(Q)$(MAKE) $(build)=dir
114118build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
115119
116- # Prefix -I with $(srctree) if it is not an absolute path
117- addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
120+ # Prefix -I with $(srctree) if it is not an absolute path,
121+ # add original to the end
122+ addtree = $(if \
123+ $(filter-out -I/%, $(1)), $(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
124+
118125# Find all -I options and call addtree
119- flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
126+ flags = $(foreach o,$($(1)), \
127+ $(if $(filter -I%,$(o)), $(call addtree, $(o)), $(o)))
120128
121- # If quiet is set, only print short version of command
129+ # echo command.
130+ # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
131+ echo-cmd = $(if $($(quiet)cmd_$(1)), \
132+ echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
133+
134+ # printing commands
122135cmd = @$(echo-cmd) $(cmd_$(1))
123136
124- # Add $(obj)/ for paths that is not absolute
125- objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
137+ # Add $(obj)/ for paths that are not absolute
138+ objectify = $(foreach o,$(1), $(if $(filter /%,$(o)), $(o), $(obj)/$(o)))
126139
127140###
128- # if_changed - execute command if any prerequisite is newer than
141+ # if_changed - execute command if any prerequisite is newer than
129142# target, or command line has changed
130143# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
131144# including used config symbols
132145# if_changed_rule - as if_changed but execute rule instead
133146# See Documentation/kbuild/makefiles.txt for more info
134147
135148ifneq ($(KBUILD_NOCMDDEP),1)
136- # Check if both arguments has same arguments. Result in empty string if equal
149+ # Check if both arguments has same arguments. Result is empty string, if equal.
137150# User may override this check using make KBUILD_NOCMDDEP=1
138151arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
139152 $(filter-out $(cmd_$@), $(cmd_$(1))) )
140153endif
141154
142- # echo command. Short version is $(quiet) equals quiet, otherwise full command
143- echo-cmd = $(if $($(quiet)cmd_$(1)), \
144- echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
145-
146155# >'< substitution is for echo to work,
147156# >$< substitution to preserve $ when reloading .cmd file
148157# note: when using inline perl scripts [perl -e '...$$t=1;...']
@@ -153,25 +162,26 @@ make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
153162# PHONY targets skipped in both cases.
154163any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
155164
156- # Execute command if command has changed or prerequisitei (s) are updated
165+ # Execute command if command has changed or prerequisite (s) are updated.
157166#
158167if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
159168 @set -e; \
160169 $(echo-cmd) $(cmd_$(1)); \
161170 echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
162171
163- # execute the command and also postprocess generated .d dependencies
164- # file
172+ # Execute the command and also postprocess generated .d dependencies file.
173+ #
165174if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
166175 @set -e; \
167176 $(echo-cmd) $(cmd_$(1)); \
168177 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
169178 rm -f $(depfile); \
170179 mv -f $(dot-target).tmp $(dot-target).cmd)
171180
181+ # Will check if $(cmd_foo) changed, or any of the prerequisites changed,
182+ # and if so will execute $(rule_foo).
172183# Usage: $(call if_changed_rule,foo)
173- # will check if $(cmd_foo) changed, or any of the prequisites changed,
174- # and if so will execute $(rule_foo)
184+ #
175185if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
176186 @set -e; \
177187 $(rule_$(1)))
0 commit comments