aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/Kbuild.include
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/Kbuild.include')
-rw-r--r--scripts/Kbuild.include106
1 files changed, 90 insertions, 16 deletions
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 9ffd3dda3889..065324a8046f 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -8,6 +8,8 @@ squote := '
8empty := 8empty :=
9space := $(empty) $(empty) 9space := $(empty) $(empty)
10space_escape := _-_SPACE_-_ 10space_escape := _-_SPACE_-_
11right_paren := )
12left_paren := (
11 13
12### 14###
13# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 15# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
@@ -80,6 +82,71 @@ cc-cross-prefix = \
80 echo $(c); \ 82 echo $(c); \
81 fi))) 83 fi)))
82 84
85# Tools for caching Makefile variables that are "expensive" to compute.
86#
87# Here we want to help deal with variables that take a long time to compute
88# by making it easy to store these variables in a cache.
89#
90# The canonical example here is testing for compiler flags. On a simple system
91# each call to the compiler takes 10 ms, but on a system with a compiler that's
92# called through various wrappers it can take upwards of 100 ms. If we have
93# 100 calls to the compiler this can take 1 second (on a simple system) or 10
94# seconds (on a complicated system).
95#
96# The "cache" will be in Makefile syntax and can be directly included.
97# Any time we try to reference a variable that's not in the cache we'll
98# calculate it and store it in the cache for next time.
99
100# Include values from last time
101make-cache := $(if $(KBUILD_EXTMOD),$(KBUILD_EXTMOD)/,$(if $(obj),$(obj)/)).cache.mk
102$(make-cache): ;
103-include $(make-cache)
104
105cached-data := $(filter __cached_%, $(.VARIABLES))
106
107# If cache exceeds 1000 lines, shrink it down to 500.
108ifneq ($(word 1000,$(cached-data)),)
109$(shell tail -n 500 $(make-cache) > $(make-cache).tmp; \
110 mv $(make-cache).tmp $(make-cache))
111endif
112
113create-cache-dir := $(if $(KBUILD_SRC),$(if $(cache-data),,1))
114
115# Usage: $(call __sanitize-opt,Hello=Hola$(comma)Goodbye Adios)
116#
117# Convert all '$', ')', '(', '\', '=', ' ', ',', ':' to '_'
118__sanitize-opt = $(subst $$,_,$(subst $(right_paren),_,$(subst $(left_paren),_,$(subst \,_,$(subst =,_,$(subst $(space),_,$(subst $(comma),_,$(subst :,_,$(1)))))))))
119
120# Usage: $(call shell-cached,shell_command)
121# Example: $(call shell-cached,md5sum /usr/bin/gcc)
122#
123# If we've already seen a call to this exact shell command (even in a
124# previous invocation of make!) we'll return the value. If not, we'll
125# compute it and store the result for future runs.
126#
127# This is a bit of voodoo, but basic explanation is that if the variable
128# was undefined then we'll evaluate the shell command and store the result
129# into the variable. We'll then store that value in the cache and finally
130# output the value.
131#
132# NOTE: The $$(2) here isn't actually a parameter to __run-and-store. We
133# happen to know that the caller will have their shell command in $(2) so the
134# result of "call"ing this will produce a reference to that $(2). The reason
135# for this strangeness is to avoid an extra level of eval (and escaping) of
136# $(2).
137define __run-and-store
138ifeq ($(origin $(1)),undefined)
139 $$(eval $(1) := $$(shell $$(2)))
140ifeq ($(create-cache-dir),1)
141 $$(shell mkdir -p $(dir $(make-cache)))
142 $$(eval create-cache-dir :=)
143endif
144 $$(shell echo '$(1) := $$($(1))' >> $(make-cache))
145endif
146endef
147__shell-cached = $(eval $(call __run-and-store,$(1)))$($(1))
148shell-cached = $(call __shell-cached,__cached_$(call __sanitize-opt,$(1)),$(1))
149
83# output directory for tests below 150# output directory for tests below
84TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/) 151TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
85 152
@@ -87,30 +154,36 @@ TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
87# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) 154# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
88# Exit code chooses option. "$$TMP" serves as a temporary file and is 155# Exit code chooses option. "$$TMP" serves as a temporary file and is
89# automatically cleaned up. 156# automatically cleaned up.
90try-run = $(shell set -e; \ 157__try-run = set -e; \
91 TMP="$(TMPOUT).$$$$.tmp"; \ 158 TMP="$(TMPOUT).$$$$.tmp"; \
92 TMPO="$(TMPOUT).$$$$.o"; \ 159 TMPO="$(TMPOUT).$$$$.o"; \
93 if ($(1)) >/dev/null 2>&1; \ 160 if ($(1)) >/dev/null 2>&1; \
94 then echo "$(2)"; \ 161 then echo "$(2)"; \
95 else echo "$(3)"; \ 162 else echo "$(3)"; \
96 fi; \ 163 fi; \
97 rm -f "$$TMP" "$$TMPO") 164 rm -f "$$TMP" "$$TMPO"
165
166try-run = $(shell $(__try-run))
167
168# try-run-cached
169# This works like try-run, but the result is cached.
170try-run-cached = $(call shell-cached,$(__try-run))
98 171
99# as-option 172# as-option
100# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,) 173# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
101 174
102as-option = $(call try-run,\ 175as-option = $(call try-run-cached,\
103 $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2)) 176 $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
104 177
105# as-instr 178# as-instr
106# Usage: cflags-y += $(call as-instr,instr,option1,option2) 179# Usage: cflags-y += $(call as-instr,instr,option1,option2)
107 180
108as-instr = $(call try-run,\ 181as-instr = $(call try-run-cached,\
109 printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3)) 182 printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
110 183
111# __cc-option 184# __cc-option
112# Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586) 185# Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
113__cc-option = $(call try-run,\ 186__cc-option = $(call try-run-cached,\
114 $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4)) 187 $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
115 188
116# Do not attempt to build with gcc plugins during cc-option tests. 189# Do not attempt to build with gcc plugins during cc-option tests.
@@ -130,23 +203,23 @@ hostcc-option = $(call __cc-option, $(HOSTCC),\
130 203
131# cc-option-yn 204# cc-option-yn
132# Usage: flag := $(call cc-option-yn,-march=winchip-c6) 205# Usage: flag := $(call cc-option-yn,-march=winchip-c6)
133cc-option-yn = $(call try-run,\ 206cc-option-yn = $(call try-run-cached,\
134 $(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n) 207 $(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
135 208
136# cc-disable-warning 209# cc-disable-warning
137# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable) 210# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
138cc-disable-warning = $(call try-run,\ 211cc-disable-warning = $(call try-run-cached,\
139 $(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1))) 212 $(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
140 213
141# cc-name 214# cc-name
142# Expands to either gcc or clang 215# Expands to either gcc or clang
143cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc) 216cc-name = $(call shell-cached,$(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
144 217
145# cc-version 218# cc-version
146cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) 219cc-version = $(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
147 220
148# cc-fullversion 221# cc-fullversion
149cc-fullversion = $(shell $(CONFIG_SHELL) \ 222cc-fullversion = $(call shell-cached,$(CONFIG_SHELL) \
150 $(srctree)/scripts/gcc-version.sh -p $(CC)) 223 $(srctree)/scripts/gcc-version.sh -p $(CC))
151 224
152# cc-ifversion 225# cc-ifversion
@@ -159,22 +232,23 @@ cc-if-fullversion = $(shell [ $(cc-fullversion) $(1) $(2) ] && echo $(3) || echo
159 232
160# cc-ldoption 233# cc-ldoption
161# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both) 234# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
162cc-ldoption = $(call try-run,\ 235cc-ldoption = $(call try-run-cached,\
163 $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2)) 236 $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
164 237
165# ld-option 238# ld-option
166# Usage: LDFLAGS += $(call ld-option, -X) 239# Usage: LDFLAGS += $(call ld-option, -X)
167ld-option = $(call try-run,\ 240ld-option = $(call try-run-cached,\
168 $(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2)) 241 $(CC) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -x c /dev/null -c -o "$$TMPO"; \
242 $(LD) $(LDFLAGS) $(1) "$$TMPO" -o "$$TMP",$(1),$(2))
169 243
170# ar-option 244# ar-option
171# Usage: KBUILD_ARFLAGS := $(call ar-option,D) 245# Usage: KBUILD_ARFLAGS := $(call ar-option,D)
172# Important: no spaces around options 246# Important: no spaces around options
173ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2)) 247ar-option = $(call try-run-cached, $(AR) rc$(1) "$$TMP",$(1),$(2))
174 248
175# ld-version 249# ld-version
176# Note this is mainly for HJ Lu's 3 number binutil versions 250# Note this is mainly for HJ Lu's 3 number binutil versions
177ld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh) 251ld-version = $(call shell-cached,$(LD) --version | $(srctree)/scripts/ld-version.sh)
178 252
179# ld-ifversion 253# ld-ifversion
180# Usage: $(call ld-ifversion, -ge, 22252, y) 254# Usage: $(call ld-ifversion, -ge, 22252, y)