kconfig: clean up nested if-conditionals in check_conf()
[linux-2.6-microblaze.git] / scripts / Makefile.lib
1 # SPDX-License-Identifier: GPL-2.0
2 # Backward compatibility
3 asflags-y  += $(EXTRA_AFLAGS)
4 ccflags-y  += $(EXTRA_CFLAGS)
5 cppflags-y += $(EXTRA_CPPFLAGS)
6 ldflags-y  += $(EXTRA_LDFLAGS)
7 ifneq ($(always),)
8 $(warning 'always' is deprecated. Please use 'always-y' instead)
9 always-y   += $(always)
10 endif
11 ifneq ($(hostprogs-y),)
12 $(warning 'hostprogs-y' is deprecated. Please use 'hostprogs' instead)
13 hostprogs  += $(hostprogs-y)
14 endif
15 ifneq ($(hostprogs-m),)
16 $(warning 'hostprogs-m' is deprecated. Please use 'hostprogs' instead)
17 hostprogs  += $(hostprogs-m)
18 endif
19
20 # flags that take effect in current and sub directories
21 KBUILD_AFLAGS += $(subdir-asflags-y)
22 KBUILD_CFLAGS += $(subdir-ccflags-y)
23
24 # Figure out what we need to build from the various variables
25 # ===========================================================================
26
27 # When an object is listed to be built compiled-in and modular,
28 # only build the compiled-in version
29 obj-m := $(filter-out $(obj-y),$(obj-m))
30
31 # Libraries are always collected in one lib file.
32 # Filter out objects already built-in
33 lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
34
35 # Subdirectories we need to descend into
36 subdir-ym := $(sort $(subdir-y) $(subdir-m) \
37                         $(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m))))
38
39 # Handle objects in subdirs:
40 # - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and
41 #   foo/modules.order
42 # - If we encounter foo/ in $(obj-m), replace it by foo/modules.order
43 #
44 # Generate modules.order to determine modorder. Unfortunately, we don't have
45 # information about ordering between -y and -m subdirs. Just put -y's first.
46
47 ifdef need-modorder
48 obj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m))
49 else
50 obj-m := $(filter-out %/, $(obj-m))
51 endif
52
53 ifdef need-builtin
54 obj-y           := $(patsubst %/, %/built-in.a, $(obj-y))
55 else
56 obj-y           := $(filter-out %/, $(obj-y))
57 endif
58
59 # Expand $(foo-objs) $(foo-y) by calling $(call suffix-search,foo.o,-objs -y)
60 suffix-search = $(foreach s,$(2),$($(1:.o=$s)))
61 # If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
62 multi-search = $(sort $(foreach m,$(1), $(if $(strip $(call suffix-search,$(m),$(2) -)), $(m))))
63 multi-used-y := $(call multi-search,$(obj-y),-objs -y)
64 multi-used-m := $(call multi-search,$(obj-m),-objs -y -m)
65 multi-used   := $(multi-used-y) $(multi-used-m)
66
67 # Replace multi-part objects by their individual parts,
68 # including built-in.a from subdirectories
69 real-search = $(foreach m,$(1), $(if $(strip $(call suffix-search,$(m),$(2) -)),$(call suffix-search,$(m),$(2)),$(m)))
70 real-obj-y := $(call real-search, $(obj-y),-objs -y)
71 real-obj-m := $(call real-search, $(obj-m),-objs -y -m)
72
73 always-y += $(always-m)
74
75 # hostprogs-always-y += foo
76 # ... is a shorthand for
77 # hostprogs += foo
78 # always-y  += foo
79 hostprogs += $(hostprogs-always-y) $(hostprogs-always-m)
80 always-y += $(hostprogs-always-y) $(hostprogs-always-m)
81
82 # userprogs-always-y is likewise.
83 userprogs += $(userprogs-always-y) $(userprogs-always-m)
84 always-y += $(userprogs-always-y) $(userprogs-always-m)
85
86 # DTB
87 # If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built
88 always-y                        += $(dtb-y)
89 always-$(CONFIG_OF_ALL_DTBS)    += $(dtb-)
90
91 ifneq ($(CHECK_DTBS),)
92 always-y += $(patsubst %.dtb,%.dt.yaml, $(dtb-y))
93 always-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-))
94 endif
95
96 # Add subdir path
97
98 extra-y         := $(addprefix $(obj)/,$(extra-y))
99 always-y        := $(addprefix $(obj)/,$(always-y))
100 targets         := $(addprefix $(obj)/,$(targets))
101 obj-m           := $(addprefix $(obj)/,$(obj-m))
102 lib-y           := $(addprefix $(obj)/,$(lib-y))
103 real-obj-y      := $(addprefix $(obj)/,$(real-obj-y))
104 real-obj-m      := $(addprefix $(obj)/,$(real-obj-m))
105 multi-used-m    := $(addprefix $(obj)/,$(multi-used-m))
106 subdir-ym       := $(addprefix $(obj)/,$(subdir-ym))
107
108 # Finds the multi-part object the current object will be linked into.
109 # If the object belongs to two or more multi-part objects, list them all.
110 modname-multi = $(sort $(foreach m,$(multi-used),\
111                 $(if $(filter $*.o, $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$(m:.o=))))
112
113 __modname = $(if $(modname-multi),$(modname-multi),$(basetarget))
114
115 modname = $(subst $(space),:,$(__modname))
116 modfile = $(addprefix $(obj)/,$(__modname))
117
118 # target with $(obj)/ and its suffix stripped
119 target-stem = $(basename $(patsubst $(obj)/%,%,$@))
120
121 # These flags are needed for modversions and compiling, so we define them here
122 # $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
123 # end up in (or would, if it gets compiled in)
124 name-fix = $(call stringify,$(subst $(comma),_,$(subst -,_,$1)))
125 basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
126 modname_flags  = -DKBUILD_MODNAME=$(call name-fix,$(modname))
127 modfile_flags  = -DKBUILD_MODFILE=$(call stringify,$(modfile))
128
129 _c_flags       = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
130                      $(filter-out $(ccflags-remove-y), \
131                          $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
132                      $(CFLAGS_$(target-stem).o))
133 _a_flags       = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
134                      $(filter-out $(asflags-remove-y), \
135                          $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
136                      $(AFLAGS_$(target-stem).o))
137 _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
138
139 #
140 # Enable gcov profiling flags for a file, directory or for all files depending
141 # on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
142 # (in this order)
143 #
144 ifeq ($(CONFIG_GCOV_KERNEL),y)
145 _c_flags += $(if $(patsubst n%,, \
146                 $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
147                 $(CFLAGS_GCOV))
148 endif
149
150 #
151 # Enable address sanitizer flags for kernel except some files or directories
152 # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
153 #
154 ifeq ($(CONFIG_KASAN),y)
155 ifneq ($(CONFIG_KASAN_HW_TAGS),y)
156 _c_flags += $(if $(patsubst n%,, \
157                 $(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
158                 $(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
159 endif
160 endif
161
162 ifeq ($(CONFIG_UBSAN),y)
163 _c_flags += $(if $(patsubst n%,, \
164                 $(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \
165                 $(CFLAGS_UBSAN))
166 endif
167
168 ifeq ($(CONFIG_KCOV),y)
169 _c_flags += $(if $(patsubst n%,, \
170         $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
171         $(CFLAGS_KCOV))
172 endif
173
174 #
175 # Enable KCSAN flags except some files or directories we don't want to check
176 # (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
177 #
178 ifeq ($(CONFIG_KCSAN),y)
179 _c_flags += $(if $(patsubst n%,, \
180         $(KCSAN_SANITIZE_$(basetarget).o)$(KCSAN_SANITIZE)y), \
181         $(CFLAGS_KCSAN))
182 endif
183
184 # $(srctree)/$(src) for including checkin headers from generated source files
185 # $(objtree)/$(obj) for including generated headers from checkin source files
186 ifeq ($(KBUILD_EXTMOD),)
187 ifdef building_out_of_srctree
188 _c_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
189 _a_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
190 _cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj)
191 endif
192 endif
193
194 part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
195 quiet_modtag = $(if $(part-of-module),[M],   )
196
197 modkern_cflags =                                          \
198         $(if $(part-of-module),                           \
199                 $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
200                 $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
201
202 modkern_aflags = $(if $(part-of-module),                                \
203                         $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE),       \
204                         $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL))
205
206 c_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
207                  -include $(srctree)/include/linux/compiler_types.h       \
208                  $(_c_flags) $(modkern_cflags)                           \
209                  $(basename_flags) $(modname_flags)
210
211 a_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
212                  $(_a_flags) $(modkern_aflags)
213
214 cpp_flags      = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
215                  $(_cpp_flags)
216
217 ld_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
218
219 DTC_INCLUDE    := $(srctree)/scripts/dtc/include-prefixes
220
221 dtc_cpp_flags  = -Wp,-MMD,$(depfile).pre.tmp -nostdinc                    \
222                  $(addprefix -I,$(DTC_INCLUDE))                          \
223                  -undef -D__DTS__
224
225 # Useful for describing the dependency of composite objects
226 # Usage:
227 #   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
228 define multi_depend
229 $(foreach m, $(notdir $1), \
230         $(eval $(obj)/$m: \
231         $(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
232 endef
233
234 quiet_cmd_copy = COPY    $@
235       cmd_copy = cp $< $@
236
237 # Shipped files
238 # ===========================================================================
239
240 quiet_cmd_shipped = SHIPPED $@
241 cmd_shipped = cat $< > $@
242
243 $(obj)/%: $(src)/%_shipped
244         $(call cmd,shipped)
245
246 # Commands useful for building a boot image
247 # ===========================================================================
248 #
249 #       Use as following:
250 #
251 #       target: source(s) FORCE
252 #               $(if_changed,ld/objcopy/gzip)
253 #
254 #       and add target to 'targets' so that we know we have to
255 #       read in the saved command line
256
257 # Linking
258 # ---------------------------------------------------------------------------
259
260 quiet_cmd_ld = LD      $@
261       cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
262
263 # Archive
264 # ---------------------------------------------------------------------------
265
266 quiet_cmd_ar = AR      $@
267       cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
268
269 # Objcopy
270 # ---------------------------------------------------------------------------
271
272 quiet_cmd_objcopy = OBJCOPY $@
273 cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
274
275 # Gzip
276 # ---------------------------------------------------------------------------
277
278 quiet_cmd_gzip = GZIP    $@
279       cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
280
281 # DTC
282 # ---------------------------------------------------------------------------
283 DTC ?= $(objtree)/scripts/dtc/dtc
284 DTC_FLAGS += -Wno-interrupt_provider
285
286 # Disable noisy checks by default
287 ifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),)
288 DTC_FLAGS += -Wno-unit_address_vs_reg \
289         -Wno-unit_address_format \
290         -Wno-avoid_unnecessary_addr_size \
291         -Wno-alias_paths \
292         -Wno-graph_child_address \
293         -Wno-simple_bus_reg \
294         -Wno-unique_unit_address \
295         -Wno-pci_device_reg
296 endif
297
298 ifneq ($(findstring 2,$(KBUILD_EXTRA_WARN)),)
299 DTC_FLAGS += -Wnode_name_chars_strict \
300         -Wproperty_name_chars_strict \
301         -Winterrupt_provider
302 endif
303
304 DTC_FLAGS += $(DTC_FLAGS_$(basetarget))
305
306 # Generate an assembly file to wrap the output of the device tree compiler
307 quiet_cmd_dt_S_dtb= DTB     $@
308 cmd_dt_S_dtb=                                           \
309 {                                                       \
310         echo '\#include <asm-generic/vmlinux.lds.h>';   \
311         echo '.section .dtb.init.rodata,"a"';           \
312         echo '.balign STRUCT_ALIGNMENT';                \
313         echo '.global __dtb_$(subst -,_,$(*F))_begin';  \
314         echo '__dtb_$(subst -,_,$(*F))_begin:';         \
315         echo '.incbin "$<" ';                           \
316         echo '__dtb_$(subst -,_,$(*F))_end:';           \
317         echo '.global __dtb_$(subst -,_,$(*F))_end';    \
318         echo '.balign STRUCT_ALIGNMENT';                \
319 } > $@
320
321 $(obj)/%.dtb.S: $(obj)/%.dtb FORCE
322         $(call if_changed,dt_S_dtb)
323
324 quiet_cmd_dtc = DTC     $@
325 cmd_dtc = $(HOSTCC) -E $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
326         $(DTC) -O $(patsubst .%,%,$(suffix $@)) -o $@ -b 0 \
327                 $(addprefix -i,$(dir $<) $(DTC_INCLUDE)) $(DTC_FLAGS) \
328                 -d $(depfile).dtc.tmp $(dtc-tmp) ; \
329         cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)
330
331 $(obj)/%.dtb: $(src)/%.dts $(DTC) FORCE
332         $(call if_changed_dep,dtc)
333
334 DT_CHECKER ?= dt-validate
335 DT_BINDING_DIR := Documentation/devicetree/bindings
336 # DT_TMP_SCHEMA may be overridden from Documentation/devicetree/bindings/Makefile
337 DT_TMP_SCHEMA ?= $(objtree)/$(DT_BINDING_DIR)/processed-schema.json
338
339 quiet_cmd_dtb_check =   CHECK   $@
340       cmd_dtb_check =   $(DT_CHECKER) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@
341
342 define rule_dtc
343         $(call cmd_and_fixdep,dtc)
344         $(call cmd,dtb_check)
345 endef
346
347 $(obj)/%.dt.yaml: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE
348         $(call if_changed_rule,dtc,yaml)
349
350 dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
351
352 # Bzip2
353 # ---------------------------------------------------------------------------
354
355 # Bzip2 and LZMA do not include size in file... so we have to fake that;
356 # append the size as a 32-bit littleendian number as gzip does.
357 size_append = printf $(shell                                            \
358 dec_size=0;                                                             \
359 for F in $(real-prereqs); do                                    \
360         fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);  \
361         dec_size=$$(expr $$dec_size + $$fsize);                         \
362 done;                                                                   \
363 printf "%08x\n" $$dec_size |                                            \
364         sed 's/\(..\)/\1 /g' | {                                        \
365                 read ch0 ch1 ch2 ch3;                                   \
366                 for ch in $$ch3 $$ch2 $$ch1 $$ch0; do                   \
367                         printf '%s%03o' '\\' $$((0x$$ch));              \
368                 done;                                                   \
369         }                                                               \
370 )
371
372 quiet_cmd_bzip2 = BZIP2   $@
373       cmd_bzip2 = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
374
375 # Lzma
376 # ---------------------------------------------------------------------------
377
378 quiet_cmd_lzma = LZMA    $@
379       cmd_lzma = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
380
381 quiet_cmd_lzo = LZO     $@
382       cmd_lzo = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
383
384 quiet_cmd_lz4 = LZ4     $@
385       cmd_lz4 = { cat $(real-prereqs) | $(LZ4) -l -c1 stdin stdout; \
386                   $(size_append); } > $@
387
388 # U-Boot mkimage
389 # ---------------------------------------------------------------------------
390
391 MKIMAGE := $(srctree)/scripts/mkuboot.sh
392
393 # SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
394 # the number of overrides in arch makefiles
395 UIMAGE_ARCH ?= $(SRCARCH)
396 UIMAGE_COMPRESSION ?= $(if $(2),$(2),none)
397 UIMAGE_OPTS-y ?=
398 UIMAGE_TYPE ?= kernel
399 UIMAGE_LOADADDR ?= arch_must_set_this
400 UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
401 UIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)'
402
403 quiet_cmd_uimage = UIMAGE  $@
404       cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
405                         -C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
406                         -T $(UIMAGE_TYPE) \
407                         -a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
408                         -n $(UIMAGE_NAME) -d $< $@
409
410 # XZ
411 # ---------------------------------------------------------------------------
412 # Use xzkern to compress the kernel image and xzmisc to compress other things.
413 #
414 # xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
415 # of the kernel decompressor. A BCJ filter is used if it is available for
416 # the target architecture. xzkern also appends uncompressed size of the data
417 # using size_append. The .xz format has the size information available at
418 # the end of the file too, but it's in more complex format and it's good to
419 # avoid changing the part of the boot code that reads the uncompressed size.
420 # Note that the bytes added by size_append will make the xz tool think that
421 # the file is corrupt. This is expected.
422 #
423 # xzmisc doesn't use size_append, so it can be used to create normal .xz
424 # files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
425 # big dictionary would increase the memory usage too much in the multi-call
426 # decompression mode. A BCJ filter isn't used either.
427 quiet_cmd_xzkern = XZKERN  $@
428       cmd_xzkern = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
429                      $(size_append); } > $@
430
431 quiet_cmd_xzmisc = XZMISC  $@
432       cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
433
434 # ZSTD
435 # ---------------------------------------------------------------------------
436 # Appends the uncompressed size of the data using size_append. The .zst
437 # format has the size information available at the beginning of the file too,
438 # but it's in a more complex format and it's good to avoid changing the part
439 # of the boot code that reads the uncompressed size.
440 #
441 # Note that the bytes added by size_append will make the zstd tool think that
442 # the file is corrupt. This is expected.
443 #
444 # zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
445 # 128 MB. zstd22 is used for kernel compression because it is decompressed in a
446 # single pass, so zstd doesn't need to allocate a window buffer. When streaming
447 # decompression is used, like initramfs decompression, zstd22 should likely not
448 # be used because it would require zstd to allocate a 128 MB buffer.
449
450 quiet_cmd_zstd = ZSTD    $@
451       cmd_zstd = { cat $(real-prereqs) | $(ZSTD) -19; $(size_append); } > $@
452
453 quiet_cmd_zstd22 = ZSTD22  $@
454       cmd_zstd22 = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
455
456 # ASM offsets
457 # ---------------------------------------------------------------------------
458
459 # Default sed regexp - multiline due to syntax constraints
460 #
461 # Use [:space:] because LLVM's integrated assembler inserts <tab> around
462 # the .ascii directive whereas GCC keeps the <space> as-is.
463 define sed-offsets
464         's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
465         /^->/{s:->#\(.*\):/* \1 */:; \
466         s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
467         s:->::; p;}'
468 endef
469
470 # Use filechk to avoid rebuilds when a header changes, but the resulting file
471 # does not
472 define filechk_offsets
473          echo "#ifndef $2"; \
474          echo "#define $2"; \
475          echo "/*"; \
476          echo " * DO NOT MODIFY."; \
477          echo " *"; \
478          echo " * This file was generated by Kbuild"; \
479          echo " */"; \
480          echo ""; \
481          sed -ne $(sed-offsets) < $<; \
482          echo ""; \
483          echo "#endif"
484 endef