Skip to content

Commit

Permalink
kbuild: introduce ccflags-remove-y and asflags-remove-y
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
masahir0y committed Aug 9, 2020
1 parent 3ec8a5b commit 15d5761
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 23 deletions.
14 changes: 14 additions & 0 deletions Documentation/kbuild/makefiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,24 @@ more details, with real examples.

subdir-ccflags-y := -Werror

ccflags-remove-y, asflags-remove-y
These flags are used to remove particular flags for the compiler,
assembler invocations.

Example::

ccflags-remove-$(CONFIG_MCOUNT) += -pg

CFLAGS_$@, AFLAGS_$@
CFLAGS_$@ and AFLAGS_$@ only apply to commands in current
kbuild makefile.

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

CFLAGS_$@ has the higher priority than ccflags-remove-y; CFLAGS_$@
can re-add compiler flags that were removed by ccflags-remove-y.

Example::

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

AFLAGS_$@ has the higher priority than asflags-remove-y; AFLAGS_$@
can re-add assembler flags that were removed by asflags-remove-y.

Example::

# arch/arm/kernel/Makefile
Expand Down
6 changes: 1 addition & 5 deletions arch/arm/boot/compressed/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,9 @@ clean-files += piggy_data lib1funcs.S ashldi3.S bswapsdi2.S hyp-stub.S

KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING

ifeq ($(CONFIG_FUNCTION_TRACER),y)
ORIG_CFLAGS := $(KBUILD_CFLAGS)
KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
endif

ccflags-y := -fpic $(call cc-option,-mno-single-pic-base,) -fno-builtin \
-I$(obj) $(DISABLE_ARM_SSP_PER_TASK_PLUGIN)
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += -pg
asflags-y := -DZIMAGE

# Supply kernel BSS size to the decompressor via a linker symbol.
Expand Down
3 changes: 1 addition & 2 deletions arch/powerpc/xmon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ UBSAN_SANITIZE := n
KASAN_SANITIZE := n

# Disable ftrace for the entire directory
ORIG_CFLAGS := $(KBUILD_CFLAGS)
KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS))
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

ifdef CONFIG_CC_IS_CLANG
# clang stores addresses on the stack causing the frame size to blow
Expand Down
5 changes: 1 addition & 4 deletions arch/sh/boot/compressed/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ IMAGE_OFFSET := $(shell /bin/bash -c 'printf "0x%08x" \
$(CONFIG_BOOT_LINK_OFFSET)]')
endif

ifeq ($(CONFIG_MCOUNT),y)
ORIG_CFLAGS := $(KBUILD_CFLAGS)
KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
endif
ccflags-remove-$(CONFIG_MCOUNT) += -pg

LDFLAGS_vmlinux := --oformat $(ld-bfd) -Ttext $(IMAGE_OFFSET) -e startup \
-T $(obj)/../../kernel/vmlinux.lds
Expand Down
4 changes: 2 additions & 2 deletions kernel/trace/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

# Do not instrument the tracer itself:

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

ifdef CONFIG_FUNCTION_TRACER
ORIG_CFLAGS := $(KBUILD_CFLAGS)
KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS))

# Avoid recursion due to instrumentation.
KCSAN_SANITIZE := n
Expand Down
3 changes: 3 additions & 0 deletions lib/842/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_842_COMPRESS) += 842_compress.o
obj-$(CONFIG_842_DECOMPRESS) += 842_decompress.o
5 changes: 1 addition & 4 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Makefile for some libs needed in the kernel.
#

ifdef CONFIG_FUNCTION_TRACER
ORIG_CFLAGS := $(KBUILD_CFLAGS)
KBUILD_CFLAGS = $(subst $(CC_FLAGS_FTRACE),,$(ORIG_CFLAGS))
endif
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

# These files are disabled because they produce lots of non-interesting and/or
# flaky coverage that is not a function of syscall inputs. For example,
Expand Down
2 changes: 2 additions & 0 deletions lib/crypto/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

# chacha is used by the /dev/random driver which is always builtin
obj-y += chacha.o
obj-$(CONFIG_CRYPTO_LIB_CHACHA_GENERIC) += libchacha.o
Expand Down
2 changes: 2 additions & 0 deletions lib/dim/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# DIM Dynamic Interrupt Moderation library
#

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_DIMLIB) += dim.o

dim-y := dim.o net_dim.o rdma_dim.o
2 changes: 2 additions & 0 deletions lib/fonts/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: GPL-2.0
# Font handling

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

font-objs := fonts.o

font-objs-$(CONFIG_FONT_SUN8x16) += font_sun8x16.o
Expand Down
3 changes: 3 additions & 0 deletions lib/kunit/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_KUNIT) += kunit.o

kunit-objs += test.o \
Expand Down
2 changes: 2 additions & 0 deletions lib/livepatch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# Makefile for livepatch test code.

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_TEST_LIVEPATCH) += test_klp_atomic_replace.o \
test_klp_callbacks_demo.o \
test_klp_callbacks_demo2.o \
Expand Down
1 change: 1 addition & 0 deletions lib/lz4/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
ccflags-y += -O3
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_LZ4_COMPRESS) += lz4_compress.o
obj-$(CONFIG_LZ4HC_COMPRESS) += lz4hc_compress.o
Expand Down
2 changes: 2 additions & 0 deletions lib/lzo/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

lzo_compress-objs := lzo1x_compress.o
lzo_decompress-objs := lzo1x_decompress_safe.o

Expand Down
2 changes: 2 additions & 0 deletions lib/math/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-y += div64.o gcd.o lcm.o int_pow.o int_sqrt.o reciprocal_div.o

obj-$(CONFIG_CORDIC) += cordic.o
Expand Down
2 changes: 2 additions & 0 deletions lib/mpi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# MPI multiprecision maths library (from gpg)
#

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_MPILIB) = mpi.o

mpi-y = \
Expand Down
3 changes: 3 additions & 0 deletions lib/raid6/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_RAID6_PQ) += raid6_pq.o

raid6_pq-y += algos.o recov.o tables.o int1.o int2.o int4.o \
Expand Down
2 changes: 2 additions & 0 deletions lib/reed_solomon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
# This is a modified version of reed solomon lib,
#

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_REED_SOLOMON) += reed_solomon.o
obj-$(CONFIG_REED_SOLOMON_TEST) += test_rslib.o
3 changes: 3 additions & 0 deletions lib/xz/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_XZ_DEC) += xz_dec.o
xz_dec-y := xz_dec_syms.o xz_dec_stream.o xz_dec_lzma2.o
xz_dec-$(CONFIG_XZ_DEC_BCJ) += xz_dec_bcj.o
Expand Down
2 changes: 2 additions & 0 deletions lib/zlib_deflate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# decompression code.
#

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate.o

zlib_deflate-objs := deflate.o deftree.o deflate_syms.o
2 changes: 2 additions & 0 deletions lib/zlib_dfltcc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# This is the code for s390 zlib hardware support.
#

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_ZLIB_DFLTCC) += zlib_dfltcc.o

zlib_dfltcc-objs := dfltcc.o dfltcc_deflate.o dfltcc_inflate.o dfltcc_syms.o
2 changes: 2 additions & 0 deletions lib/zlib_inflate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# uncompression can be done without blocking on allocation).
#

ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate.o

zlib_inflate-objs := inffast.o inflate.o infutil.o \
Expand Down
1 change: 1 addition & 0 deletions lib/zstd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ obj-$(CONFIG_ZSTD_COMPRESS) += zstd_compress.o
obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd_decompress.o

ccflags-y += -O3
ccflags-remove-$(CONFIG_FUNCTION_TRACER) += $(CC_FLAGS_FTRACE)

zstd_compress-y := fse_compress.o huf_compress.o compress.o \
entropy_common.o fse_decompress.o zstd_common.o
Expand Down
14 changes: 8 additions & 6 deletions scripts/Makefile.lib
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname))
modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile))

orig_c_flags = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) \
$(ccflags-y) $(CFLAGS_$(target-stem).o)
_c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), $(orig_c_flags))
orig_a_flags = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) \
$(asflags-y) $(AFLAGS_$(target-stem).o)
_a_flags = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), $(orig_a_flags))
_c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
$(filter-out $(ccflags-remove-y), \
$(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
$(CFLAGS_$(target-stem).o))
_a_flags = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
$(filter-out $(asflags-remove-y), \
$(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
$(AFLAGS_$(target-stem).o))
_cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)

#
Expand Down

0 comments on commit 15d5761

Please sign in to comment.