blob: fd7a744478fc623834be3d533ad19fe8e492d4fb [file] [log] [blame]
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +09001####
2# kbuild: Generic definitions
3
4# Convenient variables
5comma := ,
Masahiro Yamada9cf2a452014-04-15 13:29:00 +09006quote := "
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +09007squote := '
8empty :=
9space := $(empty) $(empty)
Masahiro Yamada77ddac82020-04-17 16:21:36 +090010space_escape := _-_SPACE_-_
Rasmus Villemoesc17cb562018-09-19 11:35:56 +090011pound := \#
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090012
13###
14# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
15dot-target = $(dir $@).$(notdir $@)
16
17###
18# The temporary file to save gcc -MD generated dependencies must not
19# contain a comma
20depfile = $(subst $(comma),_,$(dot-target).d)
21
22###
23# filename of target with directory and extension stripped
24basetarget = $(basename $(notdir $@))
25
26###
27# filename of first prerequisite with directory and extension stripped
28baseprereq = $(basename $(notdir $<))
29
30###
Ilias Apalodimas2e9fdce2025-05-20 08:21:19 +030031# real prerequisites without phony targets
32real-prereqs = $(filter-out $(PHONY), $^)
33
34###
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090035# Escape single quote for use in echo statements
36escsq = $(subst $(squote),'\$(squote)',$1)
37
38###
Prasad Kummaricd91adc2024-09-06 12:38:07 +053039# real prerequisites without phony targets
40real-prereqs = $(filter-out $(PHONY), $^)
41
42###
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090043# Easy method for doing a status message
44 kecho := :
45 quiet_kecho := echo
46silent_kecho := :
47kecho := $($(quiet)kecho)
48
49###
50# filechk is used to check if the content of a generated file is updated.
51# Sample usage:
Ilias Apalodimase557d802025-06-11 23:24:30 +030052# filechk_sample = echo $(KERNELRELEASE)
53# version.h: FORCE
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090054# $(call filechk,sample)
Ilias Apalodimase557d802025-06-11 23:24:30 +030055#
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090056# The rule defined shall write to stdout the content of the new file.
57# The existing file will be compared with the new one.
58# - If no file exist it is created
59# - If the content differ the new file is used
60# - If they are equal no change, and no timestamp update
61# - stdin is piped in from the first prerequisite ($<) so one has
62# to specify a valid file as first prerequisite (often the kbuild file)
63define filechk
64 $(Q)set -e; \
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090065 mkdir -p $(dir $@); \
66 $(filechk_$(1)) < $< > $@.tmp; \
67 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
68 rm -f $@.tmp; \
69 else \
70 $(kecho) ' UPD $@'; \
71 mv -f $@.tmp $@; \
72 fi
73endef
74
75######
76# gcc support functions
77# See documentation in Documentation/kbuild/makefiles.txt
78
79# cc-cross-prefix
80# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
81# Return first prefix where a prefix$(CC) is found in PATH.
82# If no $(CC) found in PATH with listed prefixes return nothing
83cc-cross-prefix = \
84 $(word 1, $(foreach c,$(1), \
85 $(shell set -e; \
86 if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \
87 echo $(c); \
88 fi)))
89
90# output directory for tests below
91TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
92
93# try-run
94# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
95# Exit code chooses option. "$$TMP" is can be used as temporary file and
96# is automatically cleaned up.
Masahiro Yamada4ba02882014-02-04 17:24:44 +090097# modifed for U-Boot: prevent cc-option from leaving .*.su files
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090098try-run = $(shell set -e; \
99 TMP="$(TMPOUT).$$$$.tmp"; \
100 TMPO="$(TMPOUT).$$$$.o"; \
Masahiro Yamada4ba02882014-02-04 17:24:44 +0900101 TMPSU="$(TMPOUT).$$$$.su"; \
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900102 if ($(1)) >/dev/null 2>&1; \
103 then echo "$(2)"; \
104 else echo "$(3)"; \
105 fi; \
Masahiro Yamada4ba02882014-02-04 17:24:44 +0900106 rm -f "$$TMP" "$$TMPO" "$$TMPSU")
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900107
108# as-option
109# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
110
111as-option = $(call try-run,\
112 $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
113
114# as-instr
115# Usage: cflags-y += $(call as-instr,instr,option1,option2)
116
117as-instr = $(call try-run,\
118 printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
119
Tom Rini5d4ecf22020-03-27 11:46:27 -0400120# __cc-option
121# Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
122__cc-option = $(call try-run,\
123 $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
124
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900125# cc-option
126# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
Tom Rini5d4ecf22020-03-27 11:46:27 -0400127cc-option = $(call __cc-option, $(CC),\
128 $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS),$(1),$(2))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900129
Tom Rini5d4ecf22020-03-27 11:46:27 -0400130# hostcc-option
131# Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586)
132hostcc-option = $(call __cc-option, $(HOSTCC),\
133 $(HOSTCFLAGS) $(HOST_EXTRACFLAGS),$(1),$(2))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900134
135# cc-option-yn
136# Usage: flag := $(call cc-option-yn,-march=winchip-c6)
137cc-option-yn = $(call try-run,\
Jeroen Hofstee43a8d112014-07-30 21:54:51 +0200138 $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900139
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900140# cc-disable-warning
141# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
142cc-disable-warning = $(call try-run,\
Tom Rini5d4ecf22020-03-27 11:46:27 -0400143 $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900144
Tom Rini1611b842016-01-19 20:39:01 -0500145# cc-name
146# Expands to either gcc or clang
147cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
148
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900149# cc-version
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900150cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
151
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900152# cc-ifversion
153# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
Masahiro Yamada56fc3ba2015-07-05 01:56:55 +0900154cc-ifversion = $(shell [ $(cc-version) $(1) $(2) ] && echo $(3) || echo $(4))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900155
Masahiro Yamada7449ab12014-02-04 17:24:18 +0900156# added for U-Boot
157binutils-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/binutils-version.sh $(AS))
Simon Glassbb2a0e12021-09-22 11:34:43 -0600158dtc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/dtc-version.sh $(DTC))
Masahiro Yamada7449ab12014-02-04 17:24:18 +0900159
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900160# cc-ldoption
161# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
162cc-ldoption = $(call try-run,\
Tom Rini5d4ecf22020-03-27 11:46:27 -0400163 $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900164
165# ld-option
Tom Rini5d4ecf22020-03-27 11:46:27 -0400166# Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
167ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900168
169# ar-option
170# Usage: KBUILD_ARFLAGS := $(call ar-option,D)
171# Important: no spaces around options
172ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2))
173
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900174# ld-version
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900175# Note this is mainly for HJ Lu's 3 number binutil versions
176ld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh)
177
178# ld-ifversion
179# Usage: $(call ld-ifversion, -ge, 22252, y)
Masahiro Yamada56fc3ba2015-07-05 01:56:55 +0900180ld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900181
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900182######
183
184###
185# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
186# Usage:
187# $(Q)$(MAKE) $(build)=dir
Masahiro Yamadaede865b2014-10-30 11:06:13 +0900188build := -f $(srctree)/scripts/Makefile.build obj
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900189
190###
191# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj=
192# Usage:
193# $(Q)$(MAKE) $(modbuiltin)=dir
Masahiro Yamadaede865b2014-10-30 11:06:13 +0900194modbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900195
Masahiro Yamada56fc3ba2015-07-05 01:56:55 +0900196###
197# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
198# Usage:
199# $(Q)$(MAKE) $(dtbinst)=dir
Tom Rini50800632021-06-17 18:07:25 -0400200dtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
Masahiro Yamada56fc3ba2015-07-05 01:56:55 +0900201
202###
203# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
204# Usage:
205# $(Q)$(MAKE) $(clean)=dir
206clean := -f $(srctree)/scripts/Makefile.clean obj
207
208###
209# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.headersinst obj=
210# Usage:
211# $(Q)$(MAKE) $(hdr-inst)=dir
212hdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
213
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900214# Prefix -I with $(srctree) if it is not an absolute path.
215# skip if -I has no parameter
216addtree = $(if $(patsubst -I%,%,$(1)), \
Tom Rini40f11702020-03-11 18:11:17 -0400217$(if $(filter-out -I/% -I./% -I../%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1)),$(1)),$(1))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900218
219# Find all -I options and call addtree
220flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
221
222# echo command.
223# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
224echo-cmd = $(if $($(quiet)cmd_$(1)),\
225 echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
226
227# printing commands
Ilias Apalodimas25c46de2025-05-20 08:21:25 +0300228cmd = @set -e; $(echo-cmd) $(cmd_$(1))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900229
230# Add $(obj)/ for paths that are not absolute
231objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
232
233###
234# if_changed - execute command if any prerequisite is newer than
235# target, or command line has changed
236# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
237# including used config symbols
238# if_changed_rule - as if_changed but execute rule instead
Simon Glass61888dd2022-11-09 19:14:41 -0700239# See doc/develop/makefiles.rst for more info
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900240
241ifneq ($(KBUILD_NOCMDDEP),1)
Masahiro Yamada77ddac82020-04-17 16:21:36 +0900242# Check if both arguments are the same including their order. Result is empty
243# string if equal. User may override this check using make KBUILD_NOCMDDEP=1
244arg-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
245 $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900246else
247arg-check = $(if $(strip $(cmd_$@)),,1)
248endif
249
Masahiro Yamadaede865b2014-10-30 11:06:13 +0900250# Replace >$< with >$$< to preserve $ when reloading the .cmd file
251# (needed for make)
Rasmus Villemoesc17cb562018-09-19 11:35:56 +0900252# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
Masahiro Yamadaede865b2014-10-30 11:06:13 +0900253# (needed for make)
254# Replace >'< with >'\''< to be able to enclose the whole string in '...'
255# (needed for the shell)
Rasmus Villemoesc17cb562018-09-19 11:35:56 +0900256make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900257
258# Find any prerequisites that is newer than target or that does not exist.
259# PHONY targets skipped in both cases.
260any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
261
262# Execute command if command has changed or prerequisite(s) are updated.
263#
264if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
265 @set -e; \
266 $(echo-cmd) $(cmd_$(1)); \
Masahiro Yamada77ddac82020-04-17 16:21:36 +0900267 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900268
269# Execute the command and also postprocess generated .d dependencies file.
Ilias Apalodimas25c46de2025-05-20 08:21:25 +0300270if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)),$(cmd_and_fixdep),@:)
Ilias Apalodimas342cc872025-05-20 08:21:12 +0300271
272cmd_and_fixdep = \
Ilias Apalodimas25c46de2025-05-20 08:21:25 +0300273 $(cmd); \
Ilias Apalodimas15d82012025-05-20 08:21:22 +0300274 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
275 rm -f $(depfile)
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900276
277# Usage: $(call if_changed_rule,foo)
278# Will check if $(cmd_foo) or any of the prerequisites changed,
279# and if so will execute $(rule_foo).
Ilias Apalodimas25c46de2025-05-20 08:21:25 +0300280if_changed_rule = $(if $(strip $(any-prereq) $(arg-check)),$(rule_$(1)),@:)
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900281
282###
283# why - tell why a a target got build
284# enabled by make V=2
285# Output (listed in the order they are checked):
286# (1) - due to target is PHONY
287# (2) - due to target missing
288# (3) - due to: file1.h file2.h
289# (4) - due to command line change
290# (5) - due to missing .cmd file
291# (6) - due to target not in $(targets)
292# (1) PHONY targets are always build
293# (2) No target, so we better build it
294# (3) Prerequisite is newer than target
295# (4) The command line stored in the file named dir/.target.cmd
296# differed from actual command line. This happens when compiler
297# options changes
298# (5) No dir/.target.cmd file (used to store command line)
299# (6) No dir/.target.cmd file and target not listed in $(targets)
300# This is a good hint that there is a bug in the kbuild file
301ifeq ($(KBUILD_VERBOSE),2)
302why = \
303 $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
304 $(if $(wildcard $@), \
305 $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
306 $(if $(arg-check), \
307 $(if $(cmd_$@),- due to command line change, \
308 $(if $(filter $@, $(targets)), \
309 - due to missing .cmd file, \
310 - due to $(notdir $@) not in $$(targets) \
311 ) \
312 ) \
313 ) \
314 ), \
315 - due to target missing \
316 ) \
317 )
318
319echo-why = $(call escsq, $(strip $(why)))
320endif
Masahiro Yamada10a7a6c2015-08-12 07:31:42 +0900321
Masahiro Yamadafa380702019-01-11 19:42:26 +0900322# delete partially updated (i.e. corrupted) files on error
323.DELETE_ON_ERROR:
324
Masahiro Yamada01c39ff2019-01-11 19:42:27 +0900325# do not delete intermediate files automatically
326.SECONDARY:
327
Tom Riniab4bd302025-04-01 16:55:23 -0600328ifeq ($(CONFIG_SPL_BUILD),y)
329PHASE_ := SPL_
330else
Simon Glass7eec8842022-04-30 00:56:52 -0600331ifeq ($(CONFIG_VPL_BUILD),y)
Simon Glass4cafa212024-09-29 19:49:54 -0600332PHASE_ := VPL_
Simon Glass7eec8842022-04-30 00:56:52 -0600333else
Simon Glass74254d02017-04-02 09:50:30 -0600334ifeq ($(CONFIG_TPL_BUILD),y)
Simon Glass4cafa212024-09-29 19:49:54 -0600335PHASE_ := TPL_
Simon Glass74254d02017-04-02 09:50:30 -0600336else
Tom Riniab4bd302025-04-01 16:55:23 -0600337PHASE_ :=
Simon Glass74254d02017-04-02 09:50:30 -0600338endif
Simon Glass7eec8842022-04-30 00:56:52 -0600339endif
Masahiro Yamada10a7a6c2015-08-12 07:31:42 +0900340endif