11####
22# kbuild: Generic definitions
33
4- # Convenient constants
4+ # Convenient variables
55comma := ,
66squote := '
77empty :=
@@ -56,44 +56,48 @@ endef
5656# gcc support functions
5757# See documentation in Documentation/kbuild/makefiles.txt
5858
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- if $(1) >/dev/null 2>&1; \
68- then echo "$(2)"; \
69- else echo "$(3 )"; \
70- fi; \
71- rm -f $$OUT)
72- endef
59+ # output directory for tests below
60+ TMPOUT : = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/ )
61+
62+ # try-run
63+ # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
64+ # Exit code chooses option. "$$TMP" is can be used as temporary file and
65+ # is automatically cleaned up.
66+ try-run = $(shell set -e; \
67+ TMP="$(TMPOUT).$$$$.tmp"; \
68+ if ($(1)) >/dev/null 2>&1; \
69+ then echo "$(2 )"; \
70+ else echo "$(3)"; \
71+ fi; \
72+ rm -f "$$TMP")
7373
7474# as-option
7575# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
76- as-option = $(call checker-shell,\
77- $(CC) $(CFLAGS) $(1) -c -xassembler /dev/null -o $$OUT,$(1),$(2))
76+
77+ as-option = $(call try-run,\
78+ $(CC) $(CFLAGS) $(1) -c -xassembler /dev/null -o "$$TMP",$(1),$(2))
7879
7980# as-instr
8081# Usage: cflags-y += $(call as-instr,instr,option1,option2)
81- as-instr = $(call checker-shell,\
82- printf "$(1)" | $(CC) $(AFLAGS) -c -xassembler -o $$OUT -,$(2),$(3))
82+
83+ as-instr = $(call try-run,\
84+ echo -e "$(1)" | $(CC) $(AFLAGS) -c -xassembler -o "$$TMP" -,$(2),$(3))
8385
8486# cc-option
8587# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
86- cc-option = $(call checker-shell,\
87- $(CC) $(CFLAGS) $(if $(3),$(3),$(1)) -S -xc /dev/null -o $$OUT,$(1),$(2))
88+
89+ cc-option = $(call try-run,\
90+ $(CC) $(CFLAGS) $(1) -S -xc /dev/null -o "$$TMP",$(1),$(2))
8891
8992# cc-option-yn
9093# Usage: flag := $(call cc-option-yn,-march=winchip-c6)
91- cc-option-yn = $(call cc-option,"y","n",$(1))
94+ cc-option-yn = $(call try-run,\
95+ $(CC) $(CFLAGS) $(1) -S -xc /dev/null -o "$$TMP",y,n)
9296
9397# cc-option-align
9498# Prefix align with either -falign or -malign
9599cc-option-align = $(subst -functions=0,,\
96- $(call cc-option,-falign-functions=0,-malign-functions=0))
100+ $(call cc-option,-falign-functions=0,-malign-functions=0))
97101
98102# cc-version
99103# Usage gcc-ver := $(call cc-version,$(CC))
@@ -105,24 +109,22 @@ cc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3))
105109
106110# ld-option
107111# Usage: ldflags += $(call ld-option, -Wl$(comma)--hash-style=both)
108- ld-option = $(call checker-shell ,\
109- $(CC) $(1) -nostdlib -xc /dev/null -o $$OUT ,$(1),$(2))
112+ ld-option = $(call try-run ,\
113+ $(CC) $(1) -nostdlib -xc /dev/null -o "$$TMP" ,$(1),$(2))
110114
111115######
112116
117+ ###
113118# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
114119# Usage:
115120# $(Q)$(MAKE) $(build)=dir
116121build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
117122
118- # Prefix -I with $(srctree) if it is not an absolute path,
119- # add original to the end
120- addtree = $(if \
121- $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
123+ # Prefix -I with $(srctree) if it is not an absolute path.
124+ addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
122125
123126# Find all -I options and call addtree
124- flags = $(foreach o,$($(1)),\
125- $(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
127+ flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
126128
127129# echo command.
128130# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
@@ -144,7 +146,7 @@ objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
144146# See Documentation/kbuild/makefiles.txt for more info
145147
146148ifneq ($(KBUILD_NOCMDDEP),1)
147- # Check if both arguments has same arguments. Result is empty string, if equal.
149+ # Check if both arguments has same arguments. Result is empty string if equal.
148150# User may override this check using make KBUILD_NOCMDDEP=1
149151arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
150152 $(filter-out $(cmd_$@), $(cmd_$(1))) )
@@ -168,18 +170,16 @@ if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
168170 echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
169171
170172# Execute the command and also postprocess generated .d dependencies file.
171- #
172173if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
173174 @set -e; \
174175 $(echo-cmd) $(cmd_$(1)); \
175176 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
176177 rm -f $(depfile); \
177178 mv -f $(dot-target).tmp $(dot-target).cmd)
178179
179- # Will check if $(cmd_foo) changed, or any of the prerequisites changed,
180- # and if so will execute $(rule_foo).
181180# Usage: $(call if_changed_rule,foo)
182- #
181+ # Will check if $(cmd_foo) or any of the prerequisites changed,
182+ # and if so will execute $(rule_foo).
183183if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
184184 @set -e; \
185185 $(rule_$(1)))
0 commit comments