blob: 2ef687ea895a840c8209e9a006bc397886973072 [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:
52# define filechk_sample
53# echo $KERNELRELEASE
54# endef
55# version.h : Makefile
56# $(call filechk,sample)
57# The rule defined shall write to stdout the content of the new file.
58# The existing file will be compared with the new one.
59# - If no file exist it is created
60# - If the content differ the new file is used
61# - If they are equal no change, and no timestamp update
62# - stdin is piped in from the first prerequisite ($<) so one has
63# to specify a valid file as first prerequisite (often the kbuild file)
64define filechk
65 $(Q)set -e; \
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090066 mkdir -p $(dir $@); \
67 $(filechk_$(1)) < $< > $@.tmp; \
68 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
69 rm -f $@.tmp; \
70 else \
71 $(kecho) ' UPD $@'; \
72 mv -f $@.tmp $@; \
73 fi
74endef
75
76######
77# gcc support functions
78# See documentation in Documentation/kbuild/makefiles.txt
79
80# cc-cross-prefix
81# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
82# Return first prefix where a prefix$(CC) is found in PATH.
83# If no $(CC) found in PATH with listed prefixes return nothing
84cc-cross-prefix = \
85 $(word 1, $(foreach c,$(1), \
86 $(shell set -e; \
87 if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \
88 echo $(c); \
89 fi)))
90
91# output directory for tests below
92TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
93
94# try-run
95# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
96# Exit code chooses option. "$$TMP" is can be used as temporary file and
97# is automatically cleaned up.
Masahiro Yamada4ba02882014-02-04 17:24:44 +090098# modifed for U-Boot: prevent cc-option from leaving .*.su files
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +090099try-run = $(shell set -e; \
100 TMP="$(TMPOUT).$$$$.tmp"; \
101 TMPO="$(TMPOUT).$$$$.o"; \
Masahiro Yamada4ba02882014-02-04 17:24:44 +0900102 TMPSU="$(TMPOUT).$$$$.su"; \
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900103 if ($(1)) >/dev/null 2>&1; \
104 then echo "$(2)"; \
105 else echo "$(3)"; \
106 fi; \
Masahiro Yamada4ba02882014-02-04 17:24:44 +0900107 rm -f "$$TMP" "$$TMPO" "$$TMPSU")
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900108
109# as-option
110# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
111
112as-option = $(call try-run,\
113 $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
114
115# as-instr
116# Usage: cflags-y += $(call as-instr,instr,option1,option2)
117
118as-instr = $(call try-run,\
119 printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
120
Tom Rini5d4ecf22020-03-27 11:46:27 -0400121# __cc-option
122# Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
123__cc-option = $(call try-run,\
124 $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
125
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900126# cc-option
127# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
Tom Rini5d4ecf22020-03-27 11:46:27 -0400128cc-option = $(call __cc-option, $(CC),\
129 $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS),$(1),$(2))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900130
Tom Rini5d4ecf22020-03-27 11:46:27 -0400131# hostcc-option
132# Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586)
133hostcc-option = $(call __cc-option, $(HOSTCC),\
134 $(HOSTCFLAGS) $(HOST_EXTRACFLAGS),$(1),$(2))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900135
136# cc-option-yn
137# Usage: flag := $(call cc-option-yn,-march=winchip-c6)
138cc-option-yn = $(call try-run,\
Jeroen Hofstee43a8d112014-07-30 21:54:51 +0200139 $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900140
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900141# cc-disable-warning
142# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
143cc-disable-warning = $(call try-run,\
Tom Rini5d4ecf22020-03-27 11:46:27 -0400144 $(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 +0900145
Tom Rini1611b842016-01-19 20:39:01 -0500146# cc-name
147# Expands to either gcc or clang
148cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
149
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900150# cc-version
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900151cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
152
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900153# cc-ifversion
154# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
Masahiro Yamada56fc3ba2015-07-05 01:56:55 +0900155cc-ifversion = $(shell [ $(cc-version) $(1) $(2) ] && echo $(3) || echo $(4))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900156
Masahiro Yamada7449ab12014-02-04 17:24:18 +0900157# added for U-Boot
158binutils-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/binutils-version.sh $(AS))
Simon Glassbb2a0e12021-09-22 11:34:43 -0600159dtc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/dtc-version.sh $(DTC))
Masahiro Yamada7449ab12014-02-04 17:24:18 +0900160
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900161# cc-ldoption
162# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
163cc-ldoption = $(call try-run,\
Tom Rini5d4ecf22020-03-27 11:46:27 -0400164 $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900165
166# ld-option
Tom Rini5d4ecf22020-03-27 11:46:27 -0400167# Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
168ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900169
170# ar-option
171# Usage: KBUILD_ARFLAGS := $(call ar-option,D)
172# Important: no spaces around options
173ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2))
174
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900175# ld-version
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900176# Note this is mainly for HJ Lu's 3 number binutil versions
177ld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh)
178
179# ld-ifversion
180# Usage: $(call ld-ifversion, -ge, 22252, y)
Masahiro Yamada56fc3ba2015-07-05 01:56:55 +0900181ld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900182
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900183######
184
185###
186# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
187# Usage:
188# $(Q)$(MAKE) $(build)=dir
Masahiro Yamadaede865b2014-10-30 11:06:13 +0900189build := -f $(srctree)/scripts/Makefile.build obj
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900190
191###
192# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj=
193# Usage:
194# $(Q)$(MAKE) $(modbuiltin)=dir
Masahiro Yamadaede865b2014-10-30 11:06:13 +0900195modbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900196
Masahiro Yamada56fc3ba2015-07-05 01:56:55 +0900197###
198# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
199# Usage:
200# $(Q)$(MAKE) $(dtbinst)=dir
Tom Rini50800632021-06-17 18:07:25 -0400201dtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
Masahiro Yamada56fc3ba2015-07-05 01:56:55 +0900202
203###
204# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
205# Usage:
206# $(Q)$(MAKE) $(clean)=dir
207clean := -f $(srctree)/scripts/Makefile.clean obj
208
209###
210# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.headersinst obj=
211# Usage:
212# $(Q)$(MAKE) $(hdr-inst)=dir
213hdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
214
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900215# Prefix -I with $(srctree) if it is not an absolute path.
216# skip if -I has no parameter
217addtree = $(if $(patsubst -I%,%,$(1)), \
Tom Rini40f11702020-03-11 18:11:17 -0400218$(if $(filter-out -I/% -I./% -I../%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1)),$(1)),$(1))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900219
220# Find all -I options and call addtree
221flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
222
223# echo command.
224# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
225echo-cmd = $(if $($(quiet)cmd_$(1)),\
226 echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
227
228# printing commands
229cmd = @$(echo-cmd) $(cmd_$(1))
230
231# Add $(obj)/ for paths that are not absolute
232objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
233
234###
235# if_changed - execute command if any prerequisite is newer than
236# target, or command line has changed
237# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
238# including used config symbols
239# if_changed_rule - as if_changed but execute rule instead
Simon Glass61888dd2022-11-09 19:14:41 -0700240# See doc/develop/makefiles.rst for more info
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900241
242ifneq ($(KBUILD_NOCMDDEP),1)
Masahiro Yamada77ddac82020-04-17 16:21:36 +0900243# Check if both arguments are the same including their order. Result is empty
244# string if equal. User may override this check using make KBUILD_NOCMDDEP=1
245arg-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
246 $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900247else
248arg-check = $(if $(strip $(cmd_$@)),,1)
249endif
250
Masahiro Yamadaede865b2014-10-30 11:06:13 +0900251# Replace >$< with >$$< to preserve $ when reloading the .cmd file
252# (needed for make)
Rasmus Villemoesc17cb562018-09-19 11:35:56 +0900253# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
Masahiro Yamadaede865b2014-10-30 11:06:13 +0900254# (needed for make)
255# Replace >'< with >'\''< to be able to enclose the whole string in '...'
256# (needed for the shell)
Rasmus Villemoesc17cb562018-09-19 11:35:56 +0900257make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900258
259# Find any prerequisites that is newer than target or that does not exist.
260# PHONY targets skipped in both cases.
261any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
262
263# Execute command if command has changed or prerequisite(s) are updated.
264#
265if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
266 @set -e; \
267 $(echo-cmd) $(cmd_$(1)); \
Masahiro Yamada77ddac82020-04-17 16:21:36 +0900268 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900269
270# Execute the command and also postprocess generated .d dependencies file.
271if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
272 @set -e; \
Ilias Apalodimas342cc872025-05-20 08:21:12 +0300273 $(cmd_and_fixdep), @:)
274
275cmd_and_fixdep = \
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900276 $(echo-cmd) $(cmd_$(1)); \
277 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
278 rm -f $(depfile); \
Ilias Apalodimas342cc872025-05-20 08:21:12 +0300279 mv -f $(dot-target).tmp $(dot-target).cmd;
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900280
281# Usage: $(call if_changed_rule,foo)
282# Will check if $(cmd_foo) or any of the prerequisites changed,
283# and if so will execute $(rule_foo).
284if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
285 @set -e; \
Masahiro Yamada77ddac82020-04-17 16:21:36 +0900286 $(rule_$(1)), @:)
Masahiro Yamada6e0a77f2014-02-04 17:24:17 +0900287
288###
289# why - tell why a a target got build
290# enabled by make V=2
291# Output (listed in the order they are checked):
292# (1) - due to target is PHONY
293# (2) - due to target missing
294# (3) - due to: file1.h file2.h
295# (4) - due to command line change
296# (5) - due to missing .cmd file
297# (6) - due to target not in $(targets)
298# (1) PHONY targets are always build
299# (2) No target, so we better build it
300# (3) Prerequisite is newer than target
301# (4) The command line stored in the file named dir/.target.cmd
302# differed from actual command line. This happens when compiler
303# options changes
304# (5) No dir/.target.cmd file (used to store command line)
305# (6) No dir/.target.cmd file and target not listed in $(targets)
306# This is a good hint that there is a bug in the kbuild file
307ifeq ($(KBUILD_VERBOSE),2)
308why = \
309 $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
310 $(if $(wildcard $@), \
311 $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
312 $(if $(arg-check), \
313 $(if $(cmd_$@),- due to command line change, \
314 $(if $(filter $@, $(targets)), \
315 - due to missing .cmd file, \
316 - due to $(notdir $@) not in $$(targets) \
317 ) \
318 ) \
319 ) \
320 ), \
321 - due to target missing \
322 ) \
323 )
324
325echo-why = $(call escsq, $(strip $(why)))
326endif
Masahiro Yamada10a7a6c2015-08-12 07:31:42 +0900327
Masahiro Yamadafa380702019-01-11 19:42:26 +0900328# delete partially updated (i.e. corrupted) files on error
329.DELETE_ON_ERROR:
330
Masahiro Yamada01c39ff2019-01-11 19:42:27 +0900331# do not delete intermediate files automatically
332.SECONDARY:
333
Tom Riniab4bd302025-04-01 16:55:23 -0600334ifeq ($(CONFIG_SPL_BUILD),y)
335PHASE_ := SPL_
336else
Simon Glass7eec8842022-04-30 00:56:52 -0600337ifeq ($(CONFIG_VPL_BUILD),y)
Simon Glass4cafa212024-09-29 19:49:54 -0600338PHASE_ := VPL_
Simon Glass7eec8842022-04-30 00:56:52 -0600339else
Simon Glass74254d02017-04-02 09:50:30 -0600340ifeq ($(CONFIG_TPL_BUILD),y)
Simon Glass4cafa212024-09-29 19:49:54 -0600341PHASE_ := TPL_
Simon Glass74254d02017-04-02 09:50:30 -0600342else
Tom Riniab4bd302025-04-01 16:55:23 -0600343PHASE_ :=
Simon Glass74254d02017-04-02 09:50:30 -0600344endif
Simon Glass7eec8842022-04-30 00:56:52 -0600345endif
Masahiro Yamada10a7a6c2015-08-12 07:31:42 +0900346endif