Skip to content

Commit 15d5761

Browse files
committed
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 2464a60 ("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 <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]> Acked-by: Steven Rostedt (VMware) <[email protected]> Acked-by: Michael Ellerman <[email protected]> (powerpc) Acked-by: Brendan Higgins <[email protected]> (KUnit) Tested-by: Anders Roxell <[email protected]>
1 parent 3ec8a5b commit 15d5761

File tree

24 files changed

+64
-23
lines changed

24 files changed

+64
-23
lines changed

Documentation/kbuild/makefiles.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,24 @@ more details, with real examples.
368368

369369
subdir-ccflags-y := -Werror
370370

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+
371379
CFLAGS_$@, AFLAGS_$@
372380
CFLAGS_$@ and AFLAGS_$@ only apply to commands in current
373381
kbuild makefile.
374382

375383
$(CFLAGS_$@) specifies per-file options for $(CC). The $@
376384
part has a literal value which specifies the file that it is for.
377385

386+
CFLAGS_$@ has the higher priority than ccflags-remove-y; CFLAGS_$@
387+
can re-add compiler flags that were removed by ccflags-remove-y.
388+
378389
Example::
379390

380391
# drivers/scsi/Makefile
@@ -387,6 +398,9 @@ more details, with real examples.
387398
$(AFLAGS_$@) is a similar feature for source files in assembly
388399
languages.
389400

401+
AFLAGS_$@ has the higher priority than asflags-remove-y; AFLAGS_$@
402+
can re-add assembler flags that were removed by asflags-remove-y.
403+
390404
Example::
391405

392406
# arch/arm/kernel/Makefile

arch/arm/boot/compressed/Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,9 @@ clean-files += piggy_data lib1funcs.S ashldi3.S bswapsdi2.S hyp-stub.S
102102

103103
KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING
104104

105-
ifeq ($(CONFIG_FUNCTION_TRACER),y)
106-
ORIG_CFLAGS := $(KBUILD_CFLAGS)
107-
KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
108-
endif
109-
110105
ccflags-y := -fpic $(call cc-option,-mno-single-pic-base,) -fno-builtin \
111106
-I$(obj) $(DISABLE_ARM_SSP_PER_TASK_PLUGIN)
107+
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += -pg
112108
asflags-y := -DZIMAGE
113109

114110
# Supply kernel BSS size to the decompressor via a linker symbol.

arch/powerpc/xmon/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ UBSAN_SANITIZE := n
77
KASAN_SANITIZE := n
88

99
# 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)
1211

1312
ifdef CONFIG_CC_IS_CLANG
1413
# clang stores addresses on the stack causing the frame size to blow

arch/sh/boot/compressed/Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ IMAGE_OFFSET := $(shell /bin/bash -c 'printf "0x%08x" \
2828
$(CONFIG_BOOT_LINK_OFFSET)]')
2929
endif
3030

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
3532

3633
LDFLAGS_vmlinux := --oformat $(ld-bfd) -Ttext $(IMAGE_OFFSET) -e startup \
3734
-T $(obj)/../../kernel/vmlinux.lds

kernel/trace/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
# Do not instrument the tracer itself:
44

5+
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)
6+
57
ifdef CONFIG_FUNCTION_TRACER
6-
ORIG_CFLAGS := $(KBUILD_CFLAGS)
7-
KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS))
88

99
# Avoid recursion due to instrumentation.
1010
KCSAN_SANITIZE := n

lib/842/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)
4+
25
obj-$(CONFIG_842_COMPRESS) += 842_compress.o
36
obj-$(CONFIG_842_DECOMPRESS) += 842_decompress.o

lib/Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
# Makefile for some libs needed in the kernel.
44
#
55

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)
107

118
# These files are disabled because they produce lots of non-interesting and/or
129
# flaky coverage that is not a function of syscall inputs. For example,

lib/crypto/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3+
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)
4+
35
# chacha is used by the /dev/random driver which is always builtin
46
obj-y += chacha.o
57
obj-$(CONFIG_CRYPTO_LIB_CHACHA_GENERIC) += libchacha.o

lib/dim/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# DIM Dynamic Interrupt Moderation library
33
#
44

5+
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)
6+
57
obj-$(CONFIG_DIMLIB) += dim.o
68

79
dim-y := dim.o net_dim.o rdma_dim.o

lib/fonts/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SPDX-License-Identifier: GPL-2.0
22
# Font handling
33

4+
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)
5+
46
font-objs := fonts.o
57

68
font-objs-$(CONFIG_FONT_SUN8x16) += font_sun8x16.o

0 commit comments

Comments
 (0)