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