Blame view

scripts/Kbuild.include 9.33 KB
96ac6d435   Greg Kroah-Hartman   treewide: Add SPD...
1
  # SPDX-License-Identifier: GPL-2.0
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
2
3
  ####
  # kbuild: Generic definitions
beda9f3a1   Roman Zippel   [PATCH] kbuild: m...
4
  # Convenient variables
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
5
  comma   := ,
13338935f   Masahiro Yamada   kbuild: move "quo...
6
  quote   := "
d51bfb785   Sam Ravnborg   kbuild: introduce...
7
  squote  := '
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
8
9
  empty   :=
  space   := $(empty) $(empty)
9c8fa9bc0   Masahiro Yamada   kbuild: fix if_ch...
10
  space_escape := _-_SPACE_-_
9564a8cf4   Rasmus Villemoes   Kbuild: fix # esc...
11
  pound := \#
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
12
13
  
  ###
48f1f0589   Sam Ravnborg   kbuild: consisten...
14
15
16
17
  # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
  dot-target = $(dir $@).$(notdir $@)
  
  ###
30a772977   Masahiro Yamada   kbuild: use -MMD ...
18
  # The temporary file to save gcc -MMD generated dependencies must not
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
19
  # contain a comma
48f1f0589   Sam Ravnborg   kbuild: consisten...
20
  depfile = $(subst $(comma),_,$(dot-target).d)
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
21
22
  
  ###
5e8d780d7   Sam Ravnborg   kbuild: fix ia64 ...
23
24
25
26
  # filename of target with directory and extension stripped
  basetarget = $(basename $(notdir $@))
  
  ###
afa974b77   Masahiro Yamada   kbuild: add real-...
27
28
29
30
  # real prerequisites without phony targets
  real-prereqs = $(filter-out $(PHONY), $^)
  
  ###
d51bfb785   Sam Ravnborg   kbuild: introduce...
31
32
33
34
  # Escape single quote for use in echo statements
  escsq = $(subst $(squote),'\$(squote)',$1)
  
  ###
7e826c44f   Masahiro Yamada   kbuild: add strin...
35
36
37
38
  # Quote a string to pass it to C files. foo => '"foo"'
  stringify = $(squote)$(quote)$1$(quote)$(squote)
  
  ###
5410ecc0d   Mike Frysinger   kbuild: introduce...
39
40
41
42
43
44
45
  # Easy method for doing a status message
         kecho := :
   quiet_kecho := echo
  silent_kecho := :
  kecho := $($(quiet)kecho)
  
  ###
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
46
47
  # filechk is used to check if the content of a generated file is updated.
  # Sample usage:
ba97df455   Masahiro Yamada   kbuild: use assig...
48
49
50
  #
  # filechk_sample = echo $(KERNELRELEASE)
  # version.h: FORCE
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
51
  #	$(call filechk,sample)
ba97df455   Masahiro Yamada   kbuild: use assig...
52
  #
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
53
54
55
56
57
  # The rule defined shall write to stdout the content of the new file.
  # The existing file will be compared with the new one.
  # - If no file exist it is created
  # - If the content differ the new file is used
  # - If they are equal no change, and no timestamp update
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
58
  define filechk
e1f86d7b4   Masahiro Yamada   kbuild: warn if F...
59
  	$(check-FORCE)
88fe89a47   Masahiro Yamada   kbuild: remove *....
60
61
62
63
64
65
66
  	$(Q)set -e;						\
  	mkdir -p $(dir $@);					\
  	trap "rm -f $(dot-target).tmp" EXIT;			\
  	{ $(filechk_$(1)); } > $(dot-target).tmp;		\
  	if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then	\
  		$(kecho) '  UPD     $@';			\
  		mv -f $(dot-target).tmp $@;			\
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
67
68
  	fi
  endef
beda9f3a1   Roman Zippel   [PATCH] kbuild: m...
69
  ###
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
70
71
72
  # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  # Usage:
  # $(Q)$(MAKE) $(build)=dir
5b2389b45   Masahiro Yamada   kbuild: simplify ...
73
  build := -f $(srctree)/scripts/Makefile.build obj
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
74

bc081dd6e   Michal Marek   kbuild: generate ...
75
  ###
9fb5e5372   Robert Richter   dts, kbuild: Fact...
76
77
78
  # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
  # Usage:
  # $(Q)$(MAKE) $(dtbinst)=dir
487c7c770   Masahiro Yamada   kbuild: prefix Ma...
79
  dtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
9fb5e5372   Robert Richter   dts, kbuild: Fact...
80

d08372ca2   Linus Torvalds   Merge branch 'kbu...
81
  ###
371fdc77a   Masahiro Yamada   kbuild: collect s...
82
83
84
85
  # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
  # Usage:
  # $(Q)$(MAKE) $(clean)=dir
  clean := -f $(srctree)/scripts/Makefile.clean obj
5de043f4b   Oleg Verych   [PATCH] kbuild: i...
86
87
  # echo command.
  # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
bff288c19   Oleg Verych   [PATCH] kbuild, K...
88
  echo-cmd = $(if $($(quiet)cmd_$(1)),\
5de043f4b   Oleg Verych   [PATCH] kbuild: i...
89
  	echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
174a1dcc9   Masahiro Yamada   kbuild: sink stdo...
90
91
92
93
  # sink stdout for 'make -s'
         redirect :=
   quiet_redirect :=
  silent_redirect := exec >/dev/null;
5de043f4b   Oleg Verych   [PATCH] kbuild: i...
94
  # printing commands
174a1dcc9   Masahiro Yamada   kbuild: sink stdo...
95
  cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(cmd_$(1))
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
96
97
  
  ###
5de043f4b   Oleg Verych   [PATCH] kbuild: i...
98
  # if_changed      - execute command if any prerequisite is newer than
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
99
100
101
102
  #                   target, or command line has changed
  # if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
  #                   including used config symbols
  # if_changed_rule - as if_changed but execute rule instead
cd238effe   Mauro Carvalho Chehab   docs: kbuild: con...
103
  # See Documentation/kbuild/makefiles.rst for more info
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
104
105
  
  ifneq ($(KBUILD_NOCMDDEP),1)
50bcca6ac   Masahiro Yamada   kbuild: rename ar...
106
  # Check if both commands are the same including their order. Result is empty
9c8fa9bc0   Masahiro Yamada   kbuild: fix if_ch...
107
  # string if equal. User may override this check using make KBUILD_NOCMDDEP=1
50bcca6ac   Masahiro Yamada   kbuild: rename ar...
108
  cmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
9c8fa9bc0   Masahiro Yamada   kbuild: fix if_ch...
109
                           $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
c4d5ee139   Michal Marek   kbuild: make KBUI...
110
  else
50bcca6ac   Masahiro Yamada   kbuild: rename ar...
111
  cmd-check = $(if $(strip $(cmd_$@)),,1)
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
112
  endif
164f0d2ef   Michal Marek   kbuild: Fix handl...
113
114
  # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  # (needed for make)
9564a8cf4   Rasmus Villemoes   Kbuild: fix # esc...
115
  # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
164f0d2ef   Michal Marek   kbuild: Fix handl...
116
117
118
  # (needed for make)
  # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  # (needed for the shell)
9564a8cf4   Rasmus Villemoes   Kbuild: fix # esc...
119
  make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
6176aa9ae   Jan Beulich   kbuild: consolida...
120

2d3b1b8f0   Masahiro Yamada   kbuild: drop $(wi...
121
122
123
124
  # Find any prerequisites that are newer than target or that do not exist.
  # (This is not true for now; $? should contain any non-existent prerequisites,
  # but it does not work as expected when .SECONDARY is present. This seems a bug
  # of GNU Make.)
48f1f0589   Sam Ravnborg   kbuild: consisten...
125
  # PHONY targets skipped in both cases.
eba19032f   Masahiro Yamada   kbuild: rename an...
126
  newer-prereqs = $(filter-out $(PHONY),$?)
48f1f0589   Sam Ravnborg   kbuild: consisten...
127

e1f86d7b4   Masahiro Yamada   kbuild: warn if F...
128
129
130
131
132
  # It is a typical mistake to forget the FORCE prerequisite. Check it here so
  # no more breakage will slip in.
  check-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing))
  
  if-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE)
6796e8040   Masahiro Yamada   kbuild: macrofy t...
133

5de043f4b   Oleg Verych   [PATCH] kbuild: i...
134
  # Execute command if command has changed or prerequisite(s) are updated.
6796e8040   Masahiro Yamada   kbuild: macrofy t...
135
  if_changed = $(if $(if-changed-cond),                                        \
67126965e   Masahiro Yamada   kbuild: refactor ...
136
  	$(cmd);                                                              \
2aedcd098   Masahiro Yamada   kbuild: suppress ...
137
138
  	printf '%s
  ' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
139

5de043f4b   Oleg Verych   [PATCH] kbuild: i...
140
  # Execute the command and also postprocess generated .d dependencies file.
6796e8040   Masahiro Yamada   kbuild: macrofy t...
141
  if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
c1a95fda2   Nicolas Pitre   kbuild: add fine ...
142

e4aca4595   Nicolas Pitre   kbuild: de-duplic...
143
  cmd_and_fixdep =                                                             \
3a2429e1f   Masahiro Yamada   kbuild: change if...
144
  	$(cmd);                                                              \
392885ee8   Masahiro Yamada   kbuild: let fixde...
145
  	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
e5d289100   Masahiro Yamada   kbuild: remove tr...
146
  	rm -f $(depfile)
c1a95fda2   Nicolas Pitre   kbuild: add fine ...
147

8ec4b4ff1   Sam Ravnborg   kbuild: introduce...
148
  # Usage: $(call if_changed_rule,foo)
beda9f3a1   Roman Zippel   [PATCH] kbuild: m...
149
150
  # Will check if $(cmd_foo) or any of the prerequisites changed,
  # and if so will execute $(rule_foo).
6796e8040   Masahiro Yamada   kbuild: macrofy t...
151
  if_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
48f1f0589   Sam Ravnborg   kbuild: consisten...
152

45d506bd6   Sam Ravnborg   kbuild: make V=2 ...
153
  ###
312a3d091   Cao jin   kbuild: trivial c...
154
  # why - tell why a target got built
45d506bd6   Sam Ravnborg   kbuild: make V=2 ...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  #       enabled by make V=2
  #       Output (listed in the order they are checked):
  #          (1) - due to target is PHONY
  #          (2) - due to target missing
  #          (3) - due to: file1.h file2.h
  #          (4) - due to command line change
  #          (5) - due to missing .cmd file
  #          (6) - due to target not in $(targets)
  # (1) PHONY targets are always build
  # (2) No target, so we better build it
  # (3) Prerequisite is newer than target
  # (4) The command line stored in the file named dir/.target.cmd
  #     differed from actual command line. This happens when compiler
  #     options changes
  # (5) No dir/.target.cmd file (used to store command line)
  # (6) No dir/.target.cmd file and target not listed in $(targets)
  #     This is a good hint that there is a bug in the kbuild file
  ifeq ($(KBUILD_VERBOSE),2)
  why =                                                                        \
      $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
          $(if $(wildcard $@),                                                 \
eba19032f   Masahiro Yamada   kbuild: rename an...
176
              $(if $(newer-prereqs),- due to: $(newer-prereqs),                \
50bcca6ac   Masahiro Yamada   kbuild: rename ar...
177
                  $(if $(cmd-check),                                           \
45d506bd6   Sam Ravnborg   kbuild: make V=2 ...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
                      $(if $(cmd_$@),- due to command line change,             \
                          $(if $(filter $@, $(targets)),                       \
                              - due to missing .cmd file,                      \
                              - due to $(notdir $@) not in $$(targets)         \
                           )                                                   \
                       )                                                       \
                   )                                                           \
               ),                                                              \
               - due to target missing                                         \
           )                                                                   \
       )
  
  echo-why = $(call escsq, $(strip $(why)))
  endif
3ee550f12   David Woodhouse   modsign: Handle s...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  
  ###############################################################################
  #
  # When a Kconfig string contains a filename, it is suitable for
  # passing to shell commands. It is surrounded by double-quotes, and
  # any double-quotes or backslashes within it are escaped by
  # backslashes.
  #
  # This is no use for dependencies or $(wildcard). We need to strip the
  # surrounding quotes and the escaping from quotes and backslashes, and
  # we *do* need to escape any spaces in the string. So, for example:
  #
  # Usage: $(eval $(call config_filename,FOO))
  #
  # Defines FOO_FILENAME based on the contents of the CONFIG_FOO option,
  # transformed as described above to be suitable for use within the
  # makefile.
  #
  # Also, if the filename is a relative filename and exists in the source
  # tree but not the build tree, define FOO_SRCPREFIX as $(srctree)/ to
  # be prefixed to *both* command invocation and dependencies.
  #
  # Note: We also print the filenames in the quiet_cmd_foo text, and
  # perhaps ought to have a version specially escaped for that purpose.
  # But it's only cosmetic, and $(patsubst "%",%,$(CONFIG_FOO)) is good
  # enough.  It'll strip the quotes in the common case where there's no
  # space and it's a simple filename, and it'll retain the quotes when
  # there's a space. There are some esoteric cases in which it'll print
  # the wrong thing, but we don't really care. The actual dependencies
  # and commands *do* get it right, with various combinations of single
  # and double quotes, backslashes and spaces in the filenames.
  #
  ###############################################################################
  #
3ee550f12   David Woodhouse   modsign: Handle s...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  define config_filename
  ifneq ($$(CONFIG_$(1)),"")
  $(1)_FILENAME := $$(subst \\,\,$$(subst \$$(quote),$$(quote),$$(subst $$(space_escape),\$$(space),$$(patsubst "%",%,$$(subst $$(space),$$(space_escape),$$(CONFIG_$(1)))))))
  ifneq ($$(patsubst /%,%,$$(firstword $$($(1)_FILENAME))),$$(firstword $$($(1)_FILENAME)))
  else
  ifeq ($$(wildcard $$($(1)_FILENAME)),)
  ifneq ($$(wildcard $$(srctree)/$$($(1)_FILENAME)),)
  $(1)_SRCPREFIX := $(srctree)/
  endif
  endif
  endif
  endif
  endef
  #
  ###############################################################################
9c2af1c73   Masahiro Yamada   kbuild: add .DELE...
241
242
243
  
  # delete partially updated (i.e. corrupted) files on error
  .DELETE_ON_ERROR:
8e9b61b29   Masahiro Yamada   kbuild: move .SEC...
244
245
246
  
  # do not delete intermediate files automatically
  .SECONDARY: