1ifndef __INCLUDE_MK__
2__INCLUDE_MK__ := 1
3
4cur_makefile := $(lastword $(MAKEFILE_LIST))
5
6$(cur_makefile): ;
7
8####
9# Generic definitions
10
11ifeq ($(WIN_PLAT),y)
12ifeq ($(WIN_SHELL),y)
13# make will choose sh.exe as SHELL if it finds sh.exe in the directories of PATH, regardless of
14# the setting in environment or parent (e.g., when git.exe is in the PATH)
15SHELL := cmd.exe
16endif
17endif
18
19# Convenient variables
20lparen	:= (
21rparen	:= )
22comma   := ,
23quote   := "
24squote  := '
25empty   :=
26space   := $(empty) $(empty)
27
28ifeq ($(WIN_PLAT),y)
29devnull := nul
30else
31devnull := /dev/null
32endif
33
34###
35# Remove/copy commands
36ifeq ($(WIN_PLAT),y)
37CMDRMFILE	= del /f /q $(subst /,\,$1) >nul 2>&1
38CMDRMFILER	= cd $(subst /,\,$1) && del /f /q /s $(subst /,\,$2)
39CMDRMDIR	= rmdir /s /q $(subst /,\,$1) >nul 2>&1 || del /f /q /s $(subst /,\,$1)\*
40CMDCPFILE	= copy /y $(subst /,\,$1 $2)
41else
42CMDRMFILE	= rm -f $1
43CMDRMFILER	= find $1 $(RCS_FIND_IGNORE) \
44		   \( $(addprefix -name ,'$(firstword $2)') \
45		      $(addprefix -o -name ',$(addsuffix ',$(filter-out $(firstword $2),$2))) \) \
46		    -type f -print | xargs rm -f
47CMDRMDIR	= rm -fr $1
48CMDCPFILE	= cp -f $1 $2
49endif
50
51###
52# Build-in obj suffix
53ifeq ($(BUILT-IN-OBJ),1)
54built_in_suffix := .o
55else
56built_in_suffix := .a
57endif
58
59###
60# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
61dot-target = $(dir $(1)).$(notdir $(1))
62
63###
64# The temporary file to save gcc -MF generated dependencies must not
65# contain a comma
66get_depfile_name = $(subst $(comma),_,$(dot-target).d)
67depfile = $(call get_depfile_name,$@)
68
69###
70# filename of target with directory and extension stripped
71basetarget = $(basename $(notdir $@))
72
73###
74# filename of first prerequisite with directory and extension stripped
75baseprereq = $(basename $(notdir $<))
76
77###
78# Escape special characters for use in echo statements
79ifeq ($(WIN_PLAT),y)
80# Escape redirection character in echo in Windows
81escchar = $(subst $(lparen),^$(lparen),$(subst $(rparen),^$(rparen),$(subst &,^&,$(subst |,^|,$(subst <,^<,$(subst >,^>,$1))))))
82else
83# Escape single quote for use in echo statements
84escchar = $(subst $(squote),'\$(squote)',$1)
85endif
86
87###
88# Easy method for doing a status message
89       kecho := :
90ifeq ($(WIN_PLAT),y)
91 quiet_kecho := echo.
92else
93 quiet_kecho := echo
94endif
95silent_kecho := :
96kecho := $($(quiet)kecho)
97
98ifeq ($(WIN_PLAT),y)
99echo-help = @echo.  $(call escchar,$(1)) &
100else
101echo-help = @echo '  $(call escchar,$(1))'
102endif
103
104###
105# try-run
106ifeq ($(WIN_PLAT),y)
107try-run = $(shell ($(1) >$(devnull) 2>&1) && echo. $(2) || echo. $(3))
108# Or, using define to construct multi-line WIN commands,
109# with if command: if not errorlevel 1
110else
111try-run = $(shell if ($(1)) >$(devnull) 2>&1; \
112                  then echo "$(2)"; \
113                  else echo "$(3)"; \
114                  fi)
115endif
116
117###
118# cc-option
119cc-option = $(call try-run, \
120	$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c \
121		-o $(devnull) $(devnull),$(1),$(2))
122
123###
124# Shorthand for $(Q)$(MAKE) -f scripts/build.mk obj=
125# Usage:
126# $(Q)$(MAKE) $(build)=dir
127build := -f $(srctree)/scripts/build.mk obj
128
129# Prefix -I with $(srctree) if it is not an absolute path.
130# skip if -I has no parameter
131#addtree = $(if $(patsubst -I%,%,$(1)), \
132#$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1))
133ifeq ($(WIN_PLAT),y)
134addtree = $(if $(patsubst -I%,%,$(1)), \
135    $(if $(filter-out -I$(KBUILD_ROOT)/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1)),$(1)))
136else
137addtree = $(if $(patsubst -I%,%,$(1)), \
138    $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1)),$(1)))
139endif
140
141# Find all -I options and call addtree
142flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
143
144# echo command.
145# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
146ifeq ($(WIN_PLAT),y)
147echo-cmd = $(if $($(quiet)cmd_$(1)),\
148	echo.  $(call escchar,$($(quiet)cmd_$(1)))$(echo-why) &&)
149echo-cmd-nowhy = $(if $($(quiet)cmd_$(1)),\
150	echo.  $(call escchar,$($(quiet)cmd_$(1))) &&)
151else
152echo-cmd = $(if $($(quiet)cmd_$(1)),\
153	echo '  $(call escchar,$($(quiet)cmd_$(1)))$(echo-why)' ;)
154echo-cmd-nowhy = $(if $($(quiet)cmd_$(1)),\
155	echo '  $(call escchar,$($(quiet)cmd_$(1)))' ;)
156endif
157
158# printing commands
159cmd = @$(echo-cmd) $(cmd_$(1))
160
161# Add $(obj)/ for paths that are not absolute
162objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
163
164###
165# if_changed      - execute command if any prerequisite is newer than
166#                   target, or command line has changed
167# if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
168#                   including used config symbols
169# if_changed_rule - as if_changed but execute rule instead
170# See Documentation/kbuild/makefiles.txt for more info
171
172ifneq ($(KBUILD_NOCMDDEP),1)
173# Check if both arguments has same arguments. Result is empty string if equal.
174# User may override this check using make KBUILD_NOCMDDEP=1
175arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
176                    $(filter-out $(cmd_$@),   $(cmd_$(1))) )
177else
178arg-check = $(if $(strip $(cmd_$@)),,1)
179endif
180
181# Replace >$< with >$$< to preserve $ when reloading the .cmd file
182# (needed for make)
183# Replace >#< with >\#< to avoid starting a comment in the .cmd file
184# (needed for make)
185# Replace >'< with >'\''< to be able to enclose the whole string in '...'
186# (needed for the shell)
187make-cmd = $(call escchar,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1)))))
188
189# Find any prerequisites that is newer than target or that does not exist.
190# PHONY targets skipped in both cases.
191any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
192
193ifeq ($(WIN_PLAT),y)
194depfile-new = echo. > $(depfile) && \
195              echo cmd_$@ := $(make-cmd) >> $(depfile) && \
196	      echo. >> $(depfile)
197depfile-add = echo. >> $(depfile) && \
198              echo cmd_$@ := $(make-cmd) >> $(depfile) && \
199	      echo. >> $(depfile)
200else
201depfile-new = printf '\n%s\n' 'cmd_$@ := $(make-cmd)' > $(depfile)
202depfile-add = printf '\n%s\n' 'cmd_$@ := $(make-cmd)' >> $(depfile)
203endif
204
205# Execute command if command has changed or prerequisite(s) are updated.
206#
207if_changed = $(if $(strip $(any-prereq) $(arg-check)),                       \
208	@ ( $(echo-cmd) $(cmd_$(1)) ) &&                                     \
209	  ( $(depfile-new) ))
210
211if_changed2 = $(if $(strip $(any-prereq) $(call arg-check,$(2))),             \
212	@ ( $(call echo-cmd,$(1)) $(cmd_$(1)) && \
213	    $(call echo-cmd,$(2)) $(cmd_$(2)) ) &&                           \
214	  ( $(call depfile-new,$(2)) ))
215
216# Execute the command and also postprocess generated .d dependencies file.
217if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)),                   \
218	@ ( $(echo-cmd) $(cmd_$(1)) ) &&                                     \
219	  ( $(depfile-add) ))
220
221# Usage: $(call if_changed_rule,foo)
222# Will check if $(cmd_foo) or any of the prerequisites changed,
223# and if so will execute $(rule_foo).
224if_changed_rule = $(if $(strip $(any-prereq) $(arg-check)),                  \
225	@ ( $(rule_$(1)) ) &&                                                \
226	  ( $(depfile-add) ))
227
228###
229# why - tell why a a target got build
230#       enabled by make V=2
231#       Output (listed in the order they are checked):
232#          (1) - due to target is PHONY
233#          (2) - due to target missing
234#          (3) - due to: file1.h file2.h
235#          (4) - due to command line change
236#          (5) - due to missing .cmd file
237#          (6) - due to target not in $(targets)
238# (1) PHONY targets are always build
239# (2) No target, so we better build it
240# (3) Prerequisite is newer than target
241# (4) The command line stored in the file named dir/.target.cmd
242#     differed from actual command line. This happens when compiler
243#     options changes
244# (5) No dir/.target.d file (used to store command line)
245# (6) No dir/.target.d file and target not listed in $(targets)
246#     This is a good hint that there is a bug in the kbuild file
247ifeq ($(KBUILD_VERBOSE),2)
248why =                                                                        \
249    $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
250        $(if $(wildcard $@),                                                 \
251            $(if $(strip $(any-prereq)),- due to: $(any-prereq),             \
252                $(if $(arg-check),                                           \
253                    $(if $(cmd_$@),- due to command line change,             \
254                        $(if $(filter $@, $(targets)),                       \
255                            - due to missing .d file,                        \
256                            - due to $(notdir $@) not in $$(targets)         \
257                         )                                                   \
258                     )                                                       \
259                 )                                                           \
260             ),                                                              \
261             - due to target missing                                         \
262         )                                                                   \
263     )
264
265echo-why = $(call escchar, $(strip $(why)))
266endif
267
268endif # __INCLUDE_MK__
269