Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kbuild: introduce ccflags-remove-y and asflags-remove-y

CFLAGS_REMOVE_<file>.o filters out flags when compiling a particular
object, but there is no convenient way to do that for every object in
a directory.

Add ccflags-remove-y and asflags-remove-y to make it easily.

Use ccflags-remove-y to clean up some Makefiles.

The add/remove order works as follows:

[1] KBUILD_CFLAGS specifies compiler flags used globally

[2] ccflags-y adds compiler flags for all objects in the
current Makefile

[3] ccflags-remove-y removes compiler flags for all objects in the
current Makefile (New feature)

[4] CFLAGS_<file> adds compiler flags per file.

[5] CFLAGS_REMOVE_<file> removes compiler flags per file.

Having [3] before [4] allows us to remove flags from most (but not all)
objects in the current Makefile.

For example, kernel/trace/Makefile removes $(CC_FLAGS_FTRACE)
from all objects in the directory, then adds it back to
trace_selftest_dynamic.o and CFLAGS_trace_kprobe_selftest.o

The same applies to lib/livepatch/Makefile.

Please note ccflags-remove-y has no effect to the sub-directories.
In contrast, the previous notation got rid of compiler flags also from
all the sub-directories.

The following are not affected because they have no sub-directories:

arch/arm/boot/compressed/
arch/powerpc/xmon/
arch/sh/
kernel/trace/

However, lib/ has several sub-directories.

To keep the behavior, I added ccflags-remove-y to all Makefiles
in subdirectories of lib/, except the following:

lib/vdso/Makefile - Kbuild does not descend into this Makefile
lib/raid/test/Makefile - This is not used for the kernel build

I think commit 2464a609ded0 ("ftrace: do not trace library functions")
excluded too much. In the next commit, I will remove ccflags-remove-y
from the sub-directories of lib/.

Suggested-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)
Acked-by: Brendan Higgins <brendanhiggins@google.com> (KUnit)
Tested-by: Anders Roxell <anders.roxell@linaro.org>

+64 -23
+14
Documentation/kbuild/makefiles.rst
··· 368 368 369 369 subdir-ccflags-y := -Werror 370 370 371 + ccflags-remove-y, asflags-remove-y 372 + These flags are used to remove particular flags for the compiler, 373 + assembler invocations. 374 + 375 + Example:: 376 + 377 + ccflags-remove-$(CONFIG_MCOUNT) += -pg 378 + 371 379 CFLAGS_$@, AFLAGS_$@ 372 380 CFLAGS_$@ and AFLAGS_$@ only apply to commands in current 373 381 kbuild makefile. 374 382 375 383 $(CFLAGS_$@) specifies per-file options for $(CC). The $@ 376 384 part has a literal value which specifies the file that it is for. 385 + 386 + CFLAGS_$@ has the higher priority than ccflags-remove-y; CFLAGS_$@ 387 + can re-add compiler flags that were removed by ccflags-remove-y. 377 388 378 389 Example:: 379 390 ··· 397 386 398 387 $(AFLAGS_$@) is a similar feature for source files in assembly 399 388 languages. 389 + 390 + AFLAGS_$@ has the higher priority than asflags-remove-y; AFLAGS_$@ 391 + can re-add assembler flags that were removed by asflags-remove-y. 400 392 401 393 Example:: 402 394
+1 -5
arch/arm/boot/compressed/Makefile
··· 102 102 103 103 KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING 104 104 105 - ifeq ($(CONFIG_FUNCTION_TRACER),y) 106 - ORIG_CFLAGS := $(KBUILD_CFLAGS) 107 - KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS)) 108 - endif 109 - 110 105 ccflags-y := -fpic $(call cc-option,-mno-single-pic-base,) -fno-builtin \ 111 106 -I$(obj) $(DISABLE_ARM_SSP_PER_TASK_PLUGIN) 107 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += -pg 112 108 asflags-y := -DZIMAGE 113 109 114 110 # Supply kernel BSS size to the decompressor via a linker symbol.
+1 -2
arch/powerpc/xmon/Makefile
··· 7 7 KASAN_SANITIZE := n 8 8 9 9 # Disable ftrace for the entire directory 10 - ORIG_CFLAGS := $(KBUILD_CFLAGS) 11 - KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS)) 10 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 12 11 13 12 ifdef CONFIG_CC_IS_CLANG 14 13 # clang stores addresses on the stack causing the frame size to blow
+1 -4
arch/sh/boot/compressed/Makefile
··· 28 28 $(CONFIG_BOOT_LINK_OFFSET)]') 29 29 endif 30 30 31 - ifeq ($(CONFIG_MCOUNT),y) 32 - ORIG_CFLAGS := $(KBUILD_CFLAGS) 33 - KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS)) 34 - endif 31 + ccflags-remove-$(CONFIG_MCOUNT) += -pg 35 32 36 33 LDFLAGS_vmlinux := --oformat $(ld-bfd) -Ttext $(IMAGE_OFFSET) -e startup \ 37 34 -T $(obj)/../../kernel/vmlinux.lds
+2 -2
kernel/trace/Makefile
··· 2 2 3 3 # Do not instrument the tracer itself: 4 4 5 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 6 + 5 7 ifdef CONFIG_FUNCTION_TRACER 6 - ORIG_CFLAGS := $(KBUILD_CFLAGS) 7 - KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS)) 8 8 9 9 # Avoid recursion due to instrumentation. 10 10 KCSAN_SANITIZE := n
+3
lib/842/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 + 3 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 4 + 2 5 obj-$(CONFIG_842_COMPRESS) += 842_compress.o 3 6 obj-$(CONFIG_842_DECOMPRESS) += 842_decompress.o
+1 -4
lib/Makefile
··· 3 3 # Makefile for some libs needed in the kernel. 4 4 # 5 5 6 - ifdef CONFIG_FUNCTION_TRACER 7 - ORIG_CFLAGS := $(KBUILD_CFLAGS) 8 - KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS)) 9 - endif 6 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 10 7 11 8 # These files are disabled because they produce lots of non-interesting and/or 12 9 # flaky coverage that is not a function of syscall inputs. For example,
+2
lib/crypto/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 3 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 4 + 3 5 # chacha is used by the /dev/random driver which is always builtin 4 6 obj-y += chacha.o 5 7 obj-$(CONFIG_CRYPTO_LIB_CHACHA_GENERIC) += libchacha.o
+2
lib/dim/Makefile
··· 2 2 # DIM Dynamic Interrupt Moderation library 3 3 # 4 4 5 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 6 + 5 7 obj-$(CONFIG_DIMLIB) += dim.o 6 8 7 9 dim-y := dim.o net_dim.o rdma_dim.o
+2
lib/fonts/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 # Font handling 3 3 4 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 5 + 4 6 font-objs := fonts.o 5 7 6 8 font-objs-$(CONFIG_FONT_SUN8x16) += font_sun8x16.o
+3
lib/kunit/Makefile
··· 1 + 2 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 3 + 1 4 obj-$(CONFIG_KUNIT) += kunit.o 2 5 3 6 kunit-objs += test.o \
+2
lib/livepatch/Makefile
··· 2 2 # 3 3 # Makefile for livepatch test code. 4 4 5 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 6 + 5 7 obj-$(CONFIG_TEST_LIVEPATCH) += test_klp_atomic_replace.o \ 6 8 test_klp_callbacks_demo.o \ 7 9 test_klp_callbacks_demo2.o \
+1
lib/lz4/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 2 ccflags-y += -O3 3 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 3 4 4 5 obj-$(CONFIG_LZ4_COMPRESS) += lz4_compress.o 5 6 obj-$(CONFIG_LZ4HC_COMPRESS) += lz4hc_compress.o
+2
lib/lzo/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 3 + 2 4 lzo_compress-objs := lzo1x_compress.o 3 5 lzo_decompress-objs := lzo1x_decompress_safe.o 4 6
+2
lib/math/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 3 + 2 4 obj-y += div64.o gcd.o lcm.o int_pow.o int_sqrt.o reciprocal_div.o 3 5 4 6 obj-$(CONFIG_CORDIC) += cordic.o
+2
lib/mpi/Makefile
··· 3 3 # MPI multiprecision maths library (from gpg) 4 4 # 5 5 6 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 7 + 6 8 obj-$(CONFIG_MPILIB) = mpi.o 7 9 8 10 mpi-y = \
+3
lib/raid6/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 + 3 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 4 + 2 5 obj-$(CONFIG_RAID6_PQ) += raid6_pq.o 3 6 4 7 raid6_pq-y += algos.o recov.o tables.o int1.o int2.o int4.o \
+2
lib/reed_solomon/Makefile
··· 3 3 # This is a modified version of reed solomon lib, 4 4 # 5 5 6 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 7 + 6 8 obj-$(CONFIG_REED_SOLOMON) += reed_solomon.o 7 9 obj-$(CONFIG_REED_SOLOMON_TEST) += test_rslib.o
+3
lib/xz/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 + 3 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 4 + 2 5 obj-$(CONFIG_XZ_DEC) += xz_dec.o 3 6 xz_dec-y := xz_dec_syms.o xz_dec_stream.o xz_dec_lzma2.o 4 7 xz_dec-$(CONFIG_XZ_DEC_BCJ) += xz_dec_bcj.o
+2
lib/zlib_deflate/Makefile
··· 7 7 # decompression code. 8 8 # 9 9 10 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 11 + 10 12 obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate.o 11 13 12 14 zlib_deflate-objs := deflate.o deftree.o deflate_syms.o
+2
lib/zlib_dfltcc/Makefile
··· 6 6 # This is the code for s390 zlib hardware support. 7 7 # 8 8 9 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 10 + 9 11 obj-$(CONFIG_ZLIB_DFLTCC) += zlib_dfltcc.o 10 12 11 13 zlib_dfltcc-objs := dfltcc.o dfltcc_deflate.o dfltcc_inflate.o dfltcc_syms.o
+2
lib/zlib_inflate/Makefile
··· 14 14 # uncompression can be done without blocking on allocation). 15 15 # 16 16 17 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 18 + 17 19 obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate.o 18 20 19 21 zlib_inflate-objs := inffast.o inflate.o infutil.o \
+1
lib/zstd/Makefile
··· 3 3 obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd_decompress.o 4 4 5 5 ccflags-y += -O3 6 + ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE) 6 7 7 8 zstd_compress-y := fse_compress.o huf_compress.o compress.o \ 8 9 entropy_common.o fse_decompress.o zstd_common.o
+8 -6
scripts/Makefile.lib
··· 111 111 modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname)) 112 112 modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile)) 113 113 114 - orig_c_flags = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) \ 115 - $(ccflags-y) $(CFLAGS_$(target-stem).o) 116 - _c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), $(orig_c_flags)) 117 - orig_a_flags = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) \ 118 - $(asflags-y) $(AFLAGS_$(target-stem).o) 119 - _a_flags = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), $(orig_a_flags)) 114 + _c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \ 115 + $(filter-out $(ccflags-remove-y), \ 116 + $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \ 117 + $(CFLAGS_$(target-stem).o)) 118 + _a_flags = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \ 119 + $(filter-out $(asflags-remove-y), \ 120 + $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \ 121 + $(AFLAGS_$(target-stem).o)) 120 122 _cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds) 121 123 122 124 #