Skip to contentSkip to conversation

tools build: Append -fzero-init-padding-bits=all option

PATCH Superseded Leo Yan 11 participants 57 messages Mar 11 – Mar 22

Subsystems (23)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

v4 cover tools build: Append -fzero-init-padding-bits=all option

Thank you for reviewing v3 and I appreciate much Ian's suggestions, most of which have been adopted into this series.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

In this version, the changes are organized into three parts:

Patches 01 – 07: Preparation before adding the new compiler option.
Fix typos, adjust Makefiles to ensure the newly introduced option does not cause regressions.
Patch 08: Propagate -fzero-init-padding-bits=all to
EXTRA_CFLAGS and HOST_EXTRACFLAGS for the CC and HOSTCC compilers, respectively.
Patches 09 – 30: Apply EXTRA_CFLAGS and HOST_EXTRACFLAGS in
project Makefiles.

The change has been verified:

Test 1: Cross compiling perf

	host: gcc 15.2.0 (x86_64), target: aarch64-linux-gnu-gcc

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- LDFLAGS="-static" \
-C tools/perf VF=1 CORESIGHT=1 NO_JEVENTS=1 DEBUG=1

Test 2: Cross compiling selftest bpf and sched_ext

	host: gcc 15.2.0 (x86_64), target: aarch64-linux-gnu-gcc

export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make -C tools/testing/selftests/ TARGETS="bpf sched_ext" SKIP_TARGETS=""

Test 3: Native compiling selftest bpf and sched_ext

	host: gcc 15.2.0 (x86_64)

make -C tools/testing/selftests/ TARGETS="bpf sched_ext" SKIP_TARGETS=""

---
Changes in v4:
- Placed EXTRA_CFLAGS last so it can override the default options (Ian).
- Cached evaluated values to avoid invoking cc-option when each
reference EXTRA_CFLAGS/HOST_EXTRACFLAGS (Ian).
- Dropped useless patches for feature, verification and nolibc
(Gabriele / Ian / Thomas).
- Fixed typos for thermal (Ian).
- Removed duplicate $(CLANG_CROSS_FLAGS) in libbpf (Ian).
- Collected maintainers' reviewed and ACK tags.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Extended to support cross compilation (Quentin).
- Link to v2: https://lore.kernel.org/r/[email protected]

---
Leo Yan (30):
tools: lib: thermal: Fix typo
tools/thermal: Fix typo
tools: lib: thermal: Initialize CFLAGS before including Makefile.include tools/thermal: Initialize CFLAGS before including Makefile.include
bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS
libbpf: Remove duplicate $(CLANG_CROSS_FLAGS)
libbpf: Initialize CFLAGS before including Makefile.include tools build: Append -fzero-init-padding-bits=all to extra cflags
bpftool: Append extra host flags
perf build: Append extra host flags
tools/bpf: build: Append extra cflags
tools build: Append extra host cflags
tools: bootconfig: Append extra cflags
tools: counter: Append extra cflags
tools: dma: Append extra cflags
tools: gpio: Append extra cflags
tools: hv: Append extra cflags
tools: iio: Append extra cflags
tools: mm: Append extra cflags
tools: objtool: Append extra host cflags
tools: power: acpi: Append extra cflags
tools: power: x86/intel-speed-select: Append extra cflags
tools: sched_ext: Append extra cflags
tools: spi: Append extra cflags
tools: tracing: Append extra cflags
tools: usb: Append extra cflags
selftests/bpf: Append extra cflags
selftests/hid: Append extra cflags
selftests/nolibc: Append extra cflags
selftests/sched_ext: Append extra cflags

 tools/bootconfig/Makefile                   |  1 +
 tools/bpf/Makefile                          |  1 +
 tools/bpf/bpftool/Makefile                  |  9 +++++++--
 tools/build/Makefile                        |  6 ++++--
 tools/counter/Makefile                      |  1 +
 tools/dma/Makefile                          |  1 +
 tools/gpio/Makefile                         |  1 +
 tools/hv/Makefile                           |  1 +
 tools/iio/Makefile                          |  1 +
 tools/lib/bpf/Makefile                      | 17 ++++++++--------
 tools/lib/thermal/Makefile                  | 20 ++++++++++---------
 tools/mm/Makefile                           |  1 +
 tools/objtool/Makefile                      |  2 ++
 tools/perf/Makefile.config                  |  2 +-
 tools/power/acpi/Makefile.config            |  1 +
 tools/power/x86/intel-speed-select/Makefile |  1 +
 tools/sched_ext/Makefile                    |  1 +
 tools/scripts/Makefile.include              | 30 +++++++++++++++++++++++++++++
 tools/spi/Makefile                          |  1 +
 tools/testing/selftests/bpf/Makefile        | 10 ++++++++--
 tools/testing/selftests/hid/Makefile        |  1 +
 tools/testing/selftests/nolibc/Makefile     |  6 ++----
 tools/testing/selftests/sched_ext/Makefile  |  1 +
 tools/thermal/lib/Makefile                  | 20 ++++++++++---------
 tools/tracing/latency/Makefile              |  1 +
 tools/usb/Makefile                          |  1 +
 26 files changed, 101 insertions(+), 37 deletions(-)

---
base-commit: 4ae12d8bd9a830799db335ee661d6cbc6597f838
change-id: 20260224-tools_build_fix_zero_init-dc5261bd8b8b

Best regards,

Signature · 1 line
Leo Yan <[email protected]>
author
#1 lore raw

PATCH v4 1/30 tools: lib: thermal: Fix typo

s/CFGLAS/CFLAGS

Fixes 47c4b0de080a ("tools/lib/thermal: Add a thermal library")
---
 tools/lib/thermal/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/thermal/Makefile +2 -2
index 41aa7a324ff4d76351b89d7d7cf382df3fc14052..09d8f4ba6a0ab7ab9c99ac7f64d52d0268bee340 100644
@@ -66,8 +66,8 @@ override CFLAGS += -fPIC
 override CFLAGS += $(NL3_CFLAGS)
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
-override CFGLAS += -Wl,-L.
-override CFGLAS += -Wl,-lthermal
+override CFLAGS += -Wl,-L.
+override CFLAGS += -Wl,-lthermal
 
 all:
 
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v4 2/30 tools/thermal: Fix typo

In reply to Leo Yan

s/CFGLAS/CFLAGS

Fixes 3b7c5e8adf9c ("tools/thermal: Add util library")
---
 tools/thermal/lib/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/thermal/lib/Makefile +2 -2
index 056d212f25cf51cd8c02260fbe2ef28dda5e4acb..8c9a37f999b826855dad11c0d9574b41d5b24244 100644
@@ -60,8 +60,8 @@ override CFLAGS += $(EXTRA_WARNINGS)
 override CFLAGS += -Werror -Wall
 override CFLAGS += -fPIC
 override CFLAGS += $(INCLUDES)
-override CFGLAS += -Wl,-L.
-override CFGLAS += -Wl,-lthermal
+override CFLAGS += -Wl,-L.
+override CFLAGS += -Wl,-lthermal
 
 all:
 
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v4 3/30 tools: lib: thermal: Initialize CFLAGS before including Makefile.include

In reply to Leo Yan

tools/scripts/Makefile.include may expand EXTRA_CFLAGS in a future change. This could alter the initialization of CFLAGS in the thermal Makefile, as the default options "-g -Wall" would never be set once EXTRA_CFLAGS is expanded.

Prepare for this by moving the CFLAGS initialization before including tools/scripts/Makefile.include, so it is not affected by the extended EXTRA_CFLAGS.

Append EXTRA_CFLAGS to CFLAGS only after including Makefile.include and place it last so that the extra flags propagate properly and can override the default options.

---
 tools/lib/thermal/Makefile | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
tools/lib/thermal/Makefile +9 -7
index 09d8f4ba6a0ab7ab9c99ac7f64d52d0268bee340..b55fa42cefa47628b7f2fac32fc08fcd82580a0e 100644
@@ -23,6 +23,14 @@ INSTALL = install
 DESTDIR ?=
 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
 
+# Defer assigning EXTRA_CFLAGS to CFLAGS until after including
+# tools/scripts/Makefile.include, as it may add flags to EXTRA_CFLAGS.
+ifdef EXTRA_CFLAGS
+  CFLAGS :=
+else
+  CFLAGS := -g -Wall
+endif
+
 include $(srctree)/tools/scripts/Makefile.include
 include $(srctree)/tools/scripts/Makefile.arch
 
@@ -39,13 +47,6 @@ libdir = $(prefix)/$(libdir_relative)
 libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
-# Set compile option CFLAGS
-ifdef EXTRA_CFLAGS
-  CFLAGS := $(EXTRA_CFLAGS)
-else
-  CFLAGS := -g -Wall
-endif
-
 NL3_CFLAGS = $(shell pkg-config --cflags libnl-3.0 2>/dev/null)
 ifeq ($(NL3_CFLAGS),)
 NL3_CFLAGS = -I/usr/include/libnl3
@@ -68,6 +69,7 @@ override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -Wl,-L.
 override CFLAGS += -Wl,-lthermal
+override CFLAGS += $(EXTRA_CFLAGS)
 
 all:
 
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH v4 4/30 tools/thermal: Initialize CFLAGS before including Makefile.include

In reply to Leo Yan

tools/scripts/Makefile.include may expand EXTRA_CFLAGS in a future change. This could alter the initialization of CFLAGS in the thermal Makefile, as the default options "-g -Wall" would never be set once EXTRA_CFLAGS is expanded.

Prepare for this by moving the CFLAGS initialization before including tools/scripts/Makefile.include, so it is not affected by the extended EXTRA_CFLAGS.

Append EXTRA_CFLAGS to CFLAGS only after including Makefile.include and place it last so that the extra flags propagate properly and can override the default options.

---
 tools/thermal/lib/Makefile | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
tools/thermal/lib/Makefile +9 -7
index 8c9a37f999b826855dad11c0d9574b41d5b24244..333b711bc7f0f9610e165ff7dc72c42980c59c99 100644
@@ -23,6 +23,14 @@ INSTALL = install
 DESTDIR ?=
 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
 
+# Defer assigning EXTRA_CFLAGS to CFLAGS until after including
+# tools/scripts/Makefile.include, as it may add flags to EXTRA_CFLAGS.
+ifdef EXTRA_CFLAGS
+  CFLAGS :=
+else
+  CFLAGS := -g -Wall
+endif
+
 include $(srctree)/tools/scripts/Makefile.include
 include $(srctree)/tools/scripts/Makefile.arch
 
@@ -39,13 +47,6 @@ libdir = $(prefix)/$(libdir_relative)
 libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
-# Set compile option CFLAGS
-ifdef EXTRA_CFLAGS
-  CFLAGS := $(EXTRA_CFLAGS)
-else
-  CFLAGS := -g -Wall
-endif
-
 INCLUDES = \
 -I/usr/include/libnl3 \
 -I$(srctree)/tools/lib/thermal/include \
@@ -62,6 +63,7 @@ override CFLAGS += -fPIC
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -Wl,-L.
 override CFLAGS += -Wl,-lthermal
+override CFLAGS += $(EXTRA_CFLAGS)
 
 all:
 
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v4 5/30 bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS

In reply to Leo Yan

Prepare for future changes where EXTRA_CFLAGS may include flags not applicable to the host compiler.

Move the HOST_CFLAGS assignment before appending EXTRA_CFLAGS to CFLAGS so that HOST_CFLAGS does not inherit flags from EXTRA_CFLAGS.

---
 tools/bpf/bpftool/Makefile | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Makefile +6 -2
index 519ea5cb8ab1c0ee31acc67fc5f96b40e21005c2..3e7d8359e1b2a81a29a47544be8539e3b191a0e8 100644
@@ -81,6 +81,12 @@ CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
 ifneq ($(BPFTOOL_VERSION),)
 CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
 endif
+
+# This must be done before appending EXTRA_CFLAGS to CFLAGS to avoid
+# including flags that are not applicable to the host compiler.
+HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
 endif
@@ -88,8 +94,6 @@ ifneq ($(EXTRA_LDFLAGS),)
 LDFLAGS += $(EXTRA_LDFLAGS)
 endif
 
-HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
-		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)
 
 INSTALL ?= install
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH v4 6/30 libbpf: Remove duplicate $(CLANG_CROSS_FLAGS)

In reply to Leo Yan

tools/scripts/Makefile.include already appends $(CLANG_CROSS_FLAGS) to CFLAGS:

  CFLAGS += $(CLANG_CROSS_FLAGS)

Since Makefile.include is included, the libbpf Makefile appends $(CLANG_CROSS_FLAGS) again, resulting in duplicate flags.

Remove the redundant append.

---
 tools/lib/bpf/Makefile | 1 -
 1 file changed, 1 deletion(-)
tools/lib/bpf/Makefile +0 -1
index 168140f8e6461bd06db40e23d21a3fb8847ccbf4..b1fbd0211c5edde7be3d98630b16e53e028d9116 100644
@@ -84,7 +84,6 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
-override CFLAGS += $(CLANG_CROSS_FLAGS)
 
 # flags specific for shared library
 SHLIB_FLAGS := -DSHARED -fPIC
Signature · 1 line
2.34.1
author
#17 lore raw

PATCH v4 7/30 libbpf: Initialize CFLAGS before including Makefile.include

In reply to Leo Yan

tools/scripts/Makefile.include may expand EXTRA_CFLAGS in a future change. This could alter the initialization of CFLAGS, as the default options "-g -O2" would never be set once EXTRA_CFLAGS is expanded.

Prepare for this by moving the CFLAGS initialization before including tools/scripts/Makefile.include, so it is not affected by the extended EXTRA_CFLAGS.

Append EXTRA_CFLAGS to CFLAGS only after including Makefile.include and place it last so that the extra flags propagate properly and can override the default options.

---
 tools/lib/bpf/Makefile | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
tools/lib/bpf/Makefile +9 -7
index b1fbd0211c5edde7be3d98630b16e53e028d9116..eca584fb061e16013e76827e4203f6be0477a73e 100644
@@ -49,6 +49,14 @@ man_dir_SQ = '$(subst ','\'',$(man_dir))'
 export man_dir man_dir_SQ INSTALL
 export DESTDIR DESTDIR_SQ
 
+# Defer assigning EXTRA_CFLAGS to CFLAGS until after including
+# tools/scripts/Makefile.include, as it may add flags to EXTRA_CFLAGS.
+ifdef EXTRA_CFLAGS
+  CFLAGS :=
+else
+  CFLAGS := -g -O2
+endif
+
 include $(srctree)/tools/scripts/Makefile.include
 
 # copy a bit from Linux kbuild
@@ -70,13 +78,6 @@ LIB_TARGET	= libbpf.a libbpf.so.$(LIBBPF_VERSION)
 LIB_FILE	= libbpf.a libbpf.so*
 PC_FILE		= libbpf.pc
 
-# Set compile option CFLAGS
-ifdef EXTRA_CFLAGS
-  CFLAGS := $(EXTRA_CFLAGS)
-else
-  CFLAGS := -g -O2
-endif
-
 # Append required CFLAGS
 override CFLAGS += -std=gnu89
 override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
@@ -84,6 +85,7 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
+override CFLAGS += $(EXTRA_CFLAGS)
 
 # flags specific for shared library
 SHLIB_FLAGS := -DSHARED -fPIC
Signature · 1 line
2.34.1
author
#20 lore raw

PATCH v4 8/30 tools build: Append -fzero-init-padding-bits=all to extra cflags

In reply to Leo Yan

GCC-15 release claims [1]:

{0} initializer in C or C++ for unions no longer guarantees clearing of the whole union (except for static storage duration initialization), it just initializes the first union member to zero. If initialization of the whole union including padding bits is desirable, use {} (valid in C23 or C++) or use -fzero-init-padding-bits=unions option to restore old GCC behavior.

As a result, this new behaviour might cause unexpected data when we initialize a union with using the '{ 0 }' initializer.

Since commit dce4aab8441d ("kbuild: Use -fzero-init-padding-bits=all"), the kernel has enabled -fzero-init-padding-bits=all to zero padding bits in unions and structures. This commit applies the same option for tools building.

The option is not supported by any version older than GCC 15, nor is it supported by LLVM. This patch adds the cc-option and host-cc-option functions to dynamically detect compiler option and append it to the EXTRA_CFLAGS and HOST_EXTRACFLAGS respectively.

[1] https://gcc.gnu.org/gcc-15/changes.html

---
 tools/scripts/Makefile.include | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/scripts/Makefile.include +30 -0
index b5ecf137febcae59f506e107a7f2e2ad72f4bef4..a2397ceae512c1bc54adb15cb1a111ff34e28e43 100644
@@ -137,6 +137,36 @@ else
 EXTRA_WARNINGS += -Wshadow
 endif
 
+# output directory for tests below
+TMPOUT = .tmp_$$$$
+
+# try-run
+# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
+# Exit code chooses option. "$$TMP" serves as a temporary file and is
+# automatically cleaned up.
+try-run = $(shell set -e;		\
+	TMP=$(TMPOUT)/tmp;		\
+	trap "rm -rf $(TMPOUT)" EXIT;	\
+	mkdir -p $(TMPOUT);		\
+	if ($(1)) >/dev/null 2>&1;	\
+	then echo "$(2)";		\
+	else echo "$(3)";		\
+	fi)
+
+# cc-option
+# Usage: CFLAGS += $(call cc-option,-march=winchip-c6,-march=i586)
+cc-option = $(call try-run, \
+	$(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+host-cc-option = $(call try-run, \
+	$(HOSTCC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+# Explicitly clear padding bits with the initializer '{ 0 }'
+FLAG_ZERO_INIT := $(call cc-option,-fzero-init-padding-bits=all)
+override EXTRA_CFLAGS += $(FLAG_ZERO_INIT)
+HOST_FLAG_ZERO_INIT := $(call host-cc-option,-fzero-init-padding-bits=all)
+override HOST_EXTRACFLAGS += $(HOST_FLAG_ZERO_INIT)
+
 ifneq ($(findstring $(MAKEFLAGS), w),w)
 PRINT_DIR = --no-print-directory
 else
Signature · 1 line
2.34.1
author
#22 lore raw

PATCH v4 9/30 bpftool: Append extra host flags

In reply to Leo Yan

Append HOST_EXTRACFLAGS to HOST_CFLAGS so that additional flags can be applied to the host compiler.

---
 tools/bpf/bpftool/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/bpftool/Makefile +1 -0
index 3e7d8359e1b2a81a29a47544be8539e3b191a0e8..762fcf0e62a189625053798dddeaf17d409fe248 100644
@@ -86,6 +86,7 @@ endif
 # including flags that are not applicable to the host compiler.
 HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
 		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+HOST_CFLAGS += $(HOST_EXTRACFLAGS)
 
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
Signature · 1 line
2.34.1
author
#23 lore raw

PATCH v4 10/30 perf build: Append extra host flags

In reply to Leo Yan

Append HOST_EXTRACFLAGS to HOST_CFLAGS so that additional flags can be applied to the host compiler.

---
 tools/perf/Makefile.config | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/Makefile.config +1 -1
index a8dc72cfe48eec52bd9e2352c46c88f5d8047765..f4004ec148a19c784bfe3ee9b5cc3901a3db25a3 100644
@@ -17,7 +17,7 @@ detected     = $(shell echo "$(1)=y"       >> $(OUTPUT).config-detected)
 detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
-HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
+HOSTCFLAGS := $(HOST_EXTRACFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
 # This is required because the kernel is built with this and some of the code
 # borrowed from kernel headers depends on it, e.g. put_unaligned_*().
Signature · 1 line
2.34.1
author
#25 lore raw

PATCH v4 11/30 tools/bpf: build: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/bpf/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/Makefile +1 -0
index fd2585af1252669c59228af3a77320e449e1a1ab..9c19e81f3c274767c3b9b94c907cbbe3ce9fe78c 100644
@@ -11,6 +11,7 @@ INSTALL ?= install
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi \
 	  -I$(srctree)/tools/include
+CFLAGS += $(EXTRA_CFLAGS)
 
 # This will work when bpf is built in tools env. where srctree
 # isn't set and when invoked from selftests build, where srctree
Signature · 1 line
2.34.1
author
#26 lore raw

PATCH v4 12/30 tools build: Append extra host cflags

In reply to Leo Yan

Append HOST_EXTRACFLAGS to HOSTCFLAGS so that additional flags can be applied to the host compiler.

---
 tools/build/Makefile | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/build/Makefile +4 -2
index 3a5a3808ab2a1dedd40f35ea322913e8a0788130..a86ce3d99e2bb1c005942d9c32191e6eaa55cf50 100644
@@ -40,14 +40,16 @@ endif
 FIXDEP		:= $(OUTPUT)fixdep
 FIXDEP_IN	:= $(OUTPUT)fixdep-in.o
 
+HOSTCFLAGS	:= $(HOST_EXTRACFLAGS) $(KBUILD_HOSTCFLAGS)
+
 # To track fixdep's dependencies properly, fixdep needs to run on itself.
 # Build it twice the first time.
 $(FIXDEP_IN): FORCE
 	$(Q)if [ ! -f $(FIXDEP) ]; then						\
-		$(MAKE) $(build)=fixdep HOSTCFLAGS="$(KBUILD_HOSTCFLAGS)";	\
+		$(MAKE) $(build)=fixdep HOSTCFLAGS="$(HOSTCFLAGS)";		\
 		rm -f $(FIXDEP).o;						\
 	fi
-	$(Q)$(MAKE) $(build)=fixdep HOSTCFLAGS="$(KBUILD_HOSTCFLAGS)"
+	$(Q)$(MAKE) $(build)=fixdep HOSTCFLAGS="$(HOSTCFLAGS)"
 
 
 $(FIXDEP): $(FIXDEP_IN)
Signature · 1 line
2.34.1
author
#28 lore raw

PATCH v4 13/30 tools: bootconfig: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/bootconfig/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/bootconfig/Makefile +1 -0
index 90eb47c9d8de67b0ccc74a8de0af3a7e0b97feeb..4490df707585acf356153b78cb87760f622a6190 100644
@@ -11,6 +11,7 @@ endif
 
 LIBSRC = $(srctree)/lib/bootconfig.c $(srctree)/include/linux/bootconfig.h
 override CFLAGS += -Wall -g -I$(CURDIR)/include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := bootconfig
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#29 lore raw

PATCH v4 14/30 tools: counter: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/counter/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/counter/Makefile +1 -0
index d82d35a520f610260abb43f6ae28e56d73268231..1495c0e03d9c00653a24bc92b24d83340dbb4909 100644
@@ -14,6 +14,7 @@ MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include \
 	 -I$(srctree)/tools/include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := counter_example counter_watch_events
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#30 lore raw

PATCH v4 15/30 tools: dma: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/dma/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/dma/Makefile +1 -0
index e4abf37bf020ca613b6dca340299198cb887126a..86d3d2e361110b664b885c0e9423660a2d5e8221 100644
@@ -17,6 +17,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := dma_map_benchmark
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#31 lore raw

PATCH v4 16/30 tools: gpio: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/gpio/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/gpio/Makefile +1 -0
index 342e056c8c665ac075041ff7c7ca7ba94c691187..05a0057ec361626f41ca87e5295691364f03d2cb 100644
@@ -17,6 +17,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon gpio-watch
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#32 lore raw

PATCH v4 17/30 tools: hv: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/hv/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/hv/Makefile +1 -0
index 34ffcec264ab0fdf7075d25456ef012cb866796b..a85f2208a8e3c9532b0d2cc750fc2a872ab4bb07 100644
@@ -18,6 +18,7 @@ MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
 override CFLAGS += -Wno-address-of-packed-member
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := hv_kvp_daemon hv_vss_daemon
 ifneq ($(ARCH), aarch64)
Signature · 1 line
2.34.1
author
#33 lore raw

PATCH v4 18/30 tools: iio: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/iio/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/iio/Makefile +1 -0
index 3bcce0b7d10f42208765be83dac047faa2e0ceb1..f0428421792d1cbe5ff71b01b67f23eb908fa714 100644
@@ -13,6 +13,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := iio_event_monitor lsiio iio_generic_buffer
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#35 lore raw

PATCH v4 19/30 tools: mm: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/mm/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/mm/Makefile +1 -0
index f5725b5c23aa261994b5b42f37c443bee2edebf2..d48491da2edc95fca57c0707c42961fde7b02072 100644
@@ -10,6 +10,7 @@ LIB_DIR = ../lib/api
 LIBS = $(LIB_DIR)/libapi.a
 
 CFLAGS += -Wall -Wextra -I../lib/ -pthread
+CFLAGS += $(EXTRA_CFLAGS)
 LDFLAGS += $(LIBS) -pthread
 
 all: $(BUILD_TARGETS)
Signature · 1 line
2.34.1
author
#36 lore raw

PATCH v4 20/30 tools: objtool: Append extra host cflags

In reply to Leo Yan

Append HOST_EXTRACFLAGS to HOSTCFLAGS so that additional flags can be applied to the host compiler.

---
 tools/objtool/Makefile | 2 ++
 1 file changed, 2 insertions(+)
tools/objtool/Makefile +2 -0
index 76bcd4e85de345048bba0a6e0066c3cf61dac979..716e84cc33eaccb0e4ae592746eb1888edc2f850 100644
@@ -60,6 +60,8 @@ INCLUDES := -I$(srctree)/tools/include \
 	    -I$(srctree)/tools/objtool/arch/$(SRCARCH)/include \
 	    -I$(LIBSUBCMD_OUTPUT)/include
 
+HOSTCFLAGS      += $(HOST_EXTRACFLAGS)
+
 OBJTOOL_CFLAGS  := -std=gnu11 -fomit-frame-pointer -O2 -g $(WARNINGS)	\
 		   $(INCLUDES) $(LIBELF_FLAGS) $(LIBXXHASH_CFLAGS) $(HOSTCFLAGS)
 
Signature · 1 line
2.34.1
author
#37 lore raw

PATCH v4 21/30 tools: power: acpi: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/power/acpi/Makefile.config | 1 +
 1 file changed, 1 insertion(+)
tools/power/acpi/Makefile.config +1 -0
index cd7106876a5f39dfda38e286c54c3a7c268b34a2..11088deab42ee5a793f978feb9f4f7bb7024c48c 100644
@@ -69,6 +69,7 @@ KERNEL_INCLUDE := $(OUTPUT)include
 ACPICA_INCLUDE := $(srctree)/../../../drivers/acpi/acpica
 CFLAGS += -D_LINUX -I$(KERNEL_INCLUDE) -I$(ACPICA_INCLUDE)
 CFLAGS += $(WARNINGS)
+CFLAGS += $(EXTRA_CFLAGS)
 MKDIR = mkdir
 
 ifeq ($(strip $(V)),false)
Signature · 1 line
2.34.1
author
#38 lore raw

PATCH v4 22/30 tools: power: x86/intel-speed-select: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/power/x86/intel-speed-select/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/power/x86/intel-speed-select/Makefile +1 -0
index 6b299aae2ded89c3a70a3649996e258b98001ee7..5947afce9fde7c054647fa3ee0c661b60b863f29 100644
@@ -20,6 +20,7 @@ NL3_CFLAGS = -I/usr/include/libnl3
 endif
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include $(NL3_CFLAGS)
+override CFLAGS += $(EXTRA_CFLAGS)
 override LDFLAGS += -lnl-genl-3 -lnl-3
 
 ALL_TARGETS := intel-speed-select
Signature · 1 line
2.34.1
author
#39 lore raw

PATCH v4 23/30 tools: sched_ext: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/sched_ext/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/sched_ext/Makefile +1 -0
index 21554f0896923fa9c2e5b98c85d65d964152e592..e0d705b2fe0ca763fece5137ea43ccb776d18012 100644
@@ -92,6 +92,7 @@ endif
 CFLAGS += -g -O2 -rdynamic -pthread -Wall -Werror $(GENFLAGS)			\
 	  -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)				\
 	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(CURDIR)/include
+CFLAGS += $(EXTRA_CFLAGS)
 
 # Silence some warnings when compiled with clang
 ifneq ($(LLVM),)
Signature · 1 line
2.34.1
author
#40 lore raw

PATCH v4 24/30 tools: spi: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/spi/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/spi/Makefile +1 -0
index 7fccd245a53515ab019529c8e5e91a044eaaa68b..82f9bc6365911b962422633147b108fabc70a2db 100644
@@ -13,6 +13,7 @@ endif
 MAKEFLAGS += -r
 
 CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := spidev_test spidev_fdx
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#41 lore raw

PATCH v4 25/30 tools: tracing: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/tracing/latency/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/tracing/latency/Makefile +1 -0
index 257a56b1899f23837de533353e9c2cebdb6035bd..d32a6a13753035355f0e3a05bf88870b8aa55f64 100644
@@ -57,6 +57,7 @@ ifeq ($(config),1)
 endif
 
 CFLAGS		+= $(INCLUDES) $(LIB_INCLUDES)
+CFLAGS		+= $(EXTRA_CFLAGS)
 
 export CFLAGS OUTPUT srctree
 
Signature · 1 line
2.34.1
author
#42 lore raw

PATCH v4 26/30 tools: usb: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/usb/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/usb/Makefile +1 -0
index c6235667dd46223b646bbab5c82c810221750182..42362dcdf2743dace62a5385a7d2707c42bdd3c0 100644
@@ -14,6 +14,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -Wextra -g -D_GNU_SOURCE -I$(OUTPUT)include -I$(srctree)/tools/include
+override CFLAGS += $(EXTRA_CFLAGS)
 override LDFLAGS += -lpthread
 
 ALL_TARGETS := testusb ffs-test
Signature · 1 line
2.34.1
author
#43 lore raw

PATCH v4 27/30 selftests/bpf: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

urandom_read is built with clang. Introduce URANDOM_READ_CFLAGS to copy the compiler flags without appending EXTRA_CFLAGS, which may contain incompatible options with clang.

---
 tools/testing/selftests/bpf/Makefile | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +8 -2
index 72a9ba41f95ebb825d25799c07ba3f0311243e45..27112f08c9a5b6cabc41902207d00897070b41b4 100644
@@ -71,6 +71,12 @@ ifneq ($(LLVM),)
 CFLAGS += -Wno-unused-command-line-argument
 endif
 
+# urandom_read is built with clang. Copy $(CFLAGS) before EXTRA_CFLAGS are
+# appended so that it does not inherit potentially incompatible options.
+URANDOM_READ_CFLAGS := $(CFLAGS)
+
+CFLAGS += $(EXTRA_CFLAGS)
+
 # Check whether bpf cpu=v4 is supported or not by clang
 ifneq ($(shell $(CLANG) --target=bpf -mcpu=help 2>&1 | grep 'v4'),)
 CLANG_CPUV4 := 1
@@ -255,7 +261,7 @@ endif
 $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
 	$(call msg,LIB,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
+		     $(filter-out -static,$(URANDOM_READ_CFLAGS) $(LDFLAGS)) \
 		     $(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \
 		     -Wno-unused-command-line-argument \
 		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
@@ -265,7 +271,7 @@ $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom
 $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
 	$(call msg,BINARY,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
+		     $(filter-out -static,$(URANDOM_READ_CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
 		     -Wno-unused-command-line-argument \
 		     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT) \
 		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
Signature · 1 line
2.34.1
author
#46 lore raw

PATCH v4 28/30 selftests/hid: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/testing/selftests/hid/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/hid/Makefile +1 -0
index 50ec9e0406abaf316fba232c5da1967e27d0fa9b..686c5f79ca592c2ce94e37b39ab5c3ebe8483f79 100644
@@ -26,6 +26,7 @@ HOSTPKG_CONFIG := pkg-config
 
 CFLAGS += -g -O0 -rdynamic -Wall -Werror -I$(OUTPUT)
 CFLAGS += -I$(OUTPUT)/tools/include
+CFLAGS += $(EXTRA_CFLAGS)
 
 LDLIBS += -lelf -lz -lrt -lpthread
 
Signature · 1 line
2.34.1
author
#48 lore raw

PATCH v4 29/30 selftests/nolibc: Append extra cflags

In reply to Leo Yan

Include tools/scripts/Makefile.include, which may provide additional flags in EXTRA_CFLAGS, and append to CFLAGS.

Since Makefile.include already defines cc-option, remove the redundant definition and include.

---
 tools/testing/selftests/nolibc/Makefile | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/testing/selftests/nolibc/Makefile +2 -4
index 0370489d938b540721a4fb814ebb68c463760e73..d1a9c1d5988bdc6a8778ef418aaa4c5ac1d26a10 100644
@@ -3,15 +3,13 @@
 TEST_GEN_PROGS := nolibc-test libc-test
 
 include ../lib.mk
-include $(top_srcdir)/scripts/Makefile.compiler
-
-cc-option = $(call __cc-option, $(CC),,$(1),$(2))
+include $(top_srcdir)/tools/scripts/Makefile.include
 
 include Makefile.include
 
 $(OUTPUT)/nolibc-test: CFLAGS = -nostdlib -nostdinc -static \
 	 -isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \
-	 $(CFLAGS_NOLIBC_TEST)
+	 $(CFLAGS_NOLIBC_TEST) $(EXTRA_CFLAGS)
 $(OUTPUT)/nolibc-test: LDLIBS = $(if $(LLVM),,-lgcc)
 $(OUTPUT)/nolibc-test: nolibc-test.c nolibc-test-linkage.c | headers
 
Signature · 1 line
2.34.1
author
#51 lore raw

PATCH v4 30/30 selftests/sched_ext: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/testing/selftests/sched_ext/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/sched_ext/Makefile +1 -0
index 006300ac6dff88a48104977398d8517753e7e385..2c8fbc9f8666ec4d6f75dbdd7602bd4f2b502aed 100644
@@ -56,6 +56,7 @@ endif
 CFLAGS += -g -O2 -rdynamic -pthread -Wall -Werror $(GENFLAGS)			\
 	  -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)				\
 	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(CURDIR)/include -I$(SCXTOOLSINCDIR)
+CFLAGS += $(EXTRA_CFLAGS)
 
 # Silence some warnings when compiled with clang
 ifneq ($(LLVM),)
Signature · 1 line
2.34.1
#52 lore raw
In reply to Leo Yan

Hi Leo,

Quoted 8 lines · Leo Yan

Thank you for reviewing v3 and I appreciate much Ian's suggestions, most of which have been adopted into this series.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

In this version, the changes are organized into three parts:

Quoted 9 lines

Patches 01 – 07: Preparation before adding the new compiler option.
Fix typos, adjust Makefiles to ensure the newly introduced option does not cause regressions.
Patch 08: Propagate -fzero-init-padding-bits=all to
EXTRA_CFLAGS and HOST_EXTRACFLAGS for the CC and HOSTCC compilers, respectively.
Patches 09 – 30: Apply EXTRA_CFLAGS and HOST_EXTRACFLAGS in
project Makefiles.

Through which tree do you expect these patch to be picked up ? Each maintainer picks the patches related to their subsystem ?

maintainer
#53 lore raw
Quoted 20 lines · On Wed, 11 Mar 2026 09:44:16 +0100 Daniel Lezcano…

Hi Leo,

Quoted 8 lines · Leo Yan

Thank you for reviewing v3 and I appreciate much Ian's suggestions, most of which have been adopted into this series.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

In this version, the changes are organized into three parts:

Quoted 9 lines

Patches 01 – 07: Preparation before adding the new compiler option.
Fix typos, adjust Makefiles to ensure the newly introduced option does not cause regressions.
Patch 08: Propagate -fzero-init-padding-bits=all to
EXTRA_CFLAGS and HOST_EXTRACFLAGS for the CC and HOSTCC compilers, respectively.
Patches 09 – 30: Apply EXTRA_CFLAGS and HOST_EXTRACFLAGS in
project Makefiles.

Quoted 2 lines

Through which tree do you expect these patch to be picked up ? Each maintainer picks the patches related to their subsystem ?

If that's the case it would be helpful to +CC the relevant subsystem lists on the patches that you expect to go that path.

Jonathan

#15 lore raw

Re: [PATCH v4 06/30] libbpf: Remove duplicate $(CLANG_CROSS_FLAGS)

In reply to Leo Yan
At this commit, doesn't this removal lose CLANG_CROSS_FLAGS entirely from the libbpf build?

tools/lib/bpf/Makefile override CFLAGS += -Werror -Wall · line 87

Quoted 9 lines
tools/lib/bpf/Makefile +0 -1
index 168140f8e646..b1fbd0211c5e 100644
@@ -84,7 +84,6 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
-override CFLAGS += $(CLANG_CROSS_FLAGS)

At this commit, doesn't this removal lose CLANG_CROSS_FLAGS entirely from the libbpf build?

The libbpf Makefile includes Makefile.include at the top, which does append CLANG_CROSS_FLAGS to CFLAGS. But then the libbpf Makefile overwrites CFLAGS with :=

    CFLAGS := -g -O2

which destroys everything Makefile.include appended. The removed line above was the only thing that re-added the cross-compile flags after that overwrite.

The next commit in the series (233d70853154 "libbpf: Initialize CFLAGS before including Makefile.include") fixes this by moving the CFLAGS initialization before the include, so Makefile.include's addition survives. But at this commit, cross-compilation with Clang appears broken.

Could these two patches be squashed or reordered so that the initialization move comes first?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22943905432

author
#56 lore raw
In reply to Daniel Lezcano

Hi Daniel,

On Wed, Mar 11, 2026 at 09:44:16AM +0100, Daniel Lezcano wrote:

[...]

Quoted 11 lines
Quoted 11 lines

In this version, the changes are organized into three parts:

Patches 01 – 07: Preparation before adding the new compiler option.
Fix typos, adjust Makefiles to ensure the newly introduced option does not cause regressions.
Patch 08: Propagate -fzero-init-padding-bits=all to
EXTRA_CFLAGS and HOST_EXTRACFLAGS for the CC and HOSTCC compilers, respectively.
Patches 09 – 30: Apply EXTRA_CFLAGS and HOST_EXTRACFLAGS in
project Makefiles.

Quoted 2 lines

Through which tree do you expect these patch to be picked up ? Each maintainer picks the patches related to their subsystem ?

I can propose a pickup sequence, but this may need confirmation from the tools maintainers (especially perf, bpftool, BPF).

I expect patches 01-08 can be merged in one go, as this would be safe not break anything. Afterwards, patches 09–30 can be picked up separately by subsystem maintainers, or merged together.

So far, IIUC, there are no maintainers for the tools build. If no objection from maintainers to merge in one go, I think we have chance via perf-tools-next tree. Arnaldo, Namhyung, how about you think ? (sorry this might introduce extra efforts for you).

Thanks,
Leo

author
#54 lore raw
In reply to Jonathan Cameron
Quoted 23 lines · Jonathan Cameron
Quoted 20 lines · On Wed, 11 Mar 2026 09:44:16 +0100 Daniel Lezcano…

Hi Leo,

Quoted 17 lines · Leo Yan

Thank you for reviewing v3 and I appreciate much Ian's suggestions, most of which have been adopted into this series.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

In this version, the changes are organized into three parts:

Patches 01 – 07: Preparation before adding the new compiler option.
Fix typos, adjust Makefiles to ensure the newly introduced option does not cause regressions.
Patch 08: Propagate -fzero-init-padding-bits=all to
EXTRA_CFLAGS and HOST_EXTRACFLAGS for the CC and HOSTCC compilers, respectively.
Patches 09 – 30: Apply EXTRA_CFLAGS and HOST_EXTRACFLAGS in
project Makefiles.

Quoted 5 lines
Quoted 2 lines

Through which tree do you expect these patch to be picked up ? Each maintainer picks the patches related to their subsystem ?

If that's the case it would be helpful to +CC the relevant subsystem lists on the patches that you expect to go that path.

I deliberately looped mainatiners but not CC'ed each subsystem lists, as it is a long list so I don't want to spam them.

If we conclude to go via subsystem trees, I will CC subsystem lists. Thanks for reminding.

author
#16 lore raw

Re: [PATCH v4 06/30] libbpf: Remove duplicate $(CLANG_CROSS_FLAGS)

In reply to [email protected]
Quoted 23 lines · On Wed, Mar 11, 2026 at 09:02:13AM +0000, [email protected] wrote:

tools/lib/bpf/Makefile override CFLAGS += -Werror -Wall · line 87

Quoted 9 lines
tools/lib/bpf/Makefile +0 -1
index 168140f8e646..b1fbd0211c5e 100644
@@ -84,7 +84,6 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
-override CFLAGS += $(CLANG_CROSS_FLAGS)

At this commit, doesn't this removal lose CLANG_CROSS_FLAGS entirely from the libbpf build?

The libbpf Makefile includes Makefile.include at the top, which does append CLANG_CROSS_FLAGS to CFLAGS. But then the libbpf Makefile overwrites CFLAGS with :=

    CFLAGS := -g -O2

which destroys everything Makefile.include appended. The removed line above was the only thing that re-added the cross-compile flags after that overwrite.

Quoted 8 lines

The next commit in the series (233d70853154 "libbpf: Initialize CFLAGS before including Makefile.include") fixes this by moving the CFLAGS initialization before the include, so Makefile.include's addition survives. But at this commit, cross-compilation with Clang appears broken.

Could these two patches be squashed or reordered so that the initialization move comes first?

To be honest, I feel much more comfortable being corrected by a human rather than a bot :)

The comment is fair enough, I will squash the two patches.

Thanks,
Leo

maintainer
#55 lore raw
In reply to Leo Yan
Quoted 24 lines · On Wed, 11 Mar 2026 09:23:31 +0000 Leo Yan <[email protected]> wrote:
Quoted 23 lines · Jonathan Cameron
Quoted 20 lines · On Wed, 11 Mar 2026 09:44:16 +0100 Daniel Lezcano…

Hi Leo,

Quoted 17 lines · Leo Yan

Thank you for reviewing v3 and I appreciate much Ian's suggestions, most of which have been adopted into this series.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

In this version, the changes are organized into three parts:

Patches 01 – 07: Preparation before adding the new compiler option.
Fix typos, adjust Makefiles to ensure the newly introduced option does not cause regressions.
Patch 08: Propagate -fzero-init-padding-bits=all to
EXTRA_CFLAGS and HOST_EXTRACFLAGS for the CC and HOSTCC compilers, respectively.
Patches 09 – 30: Apply EXTRA_CFLAGS and HOST_EXTRACFLAGS in
project Makefiles.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Through which tree do you expect these patch to be picked up ? Each maintainer picks the patches related to their subsystem ?

My common approach to this sort of thing (which works) is to put everything into mm.git, try to cc the whole world.

If Mark alerts us to the appearance of particular patches in linux-next via another tree, I drop the mm.git copy, so maintainers can simply cherrypick things and it's all automatic.

If maintainers send an ack (easiest!) then great.

Whatever is left over I upstream in the next merge window.

Quoted 8 lines
Quoted 2 lines

If that's the case it would be helpful to +CC the relevant subsystem lists on the patches that you expect to go that path.

I deliberately looped mainatiners but not CC'ed each subsystem lists, as it is a long list so I don't want to spam them.

If we conclude to go via subsystem trees, I will CC subsystem lists. Thanks for reminding.

Sure.

I can ensure that the appropriate maintainers are cc'ed on the relevant
mm.git commits. I generally don't auto-cc mailing lists - I already spray
out too many emails!

reviewer
#27 lore raw

Re: [PATCH v4 12/30] tools build: Append extra host cflags

In reply to Leo Yan
Quoted 14 lines · Leo Yan

Append HOST_EXTRACFLAGS to HOSTCFLAGS so that additional flags can be applied to the host compiler.

---
 tools/build/Makefile | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/build/Makefile +0 -0
index 3a5a3808ab2a1dedd40f35ea322913e8a0788130..a86ce3d99e2bb1c005942d9c32191e6eaa55cf50 100644

tools/build/Makefile endif · line 43

Quoted 4 lines
tools/build/Makefile +1 -0
@@ -40,14 +40,16 @@ endif
 FIXDEP         := $(OUTPUT)fixdep
 FIXDEP_IN      := $(OUTPUT)fixdep-in.o
 
+HOSTCFLAGS     := $(HOST_EXTRACFLAGS) $(KBUILD_HOSTCFLAGS)

Hi Leo, should HOST_EXTRACFLAGS appear after KBUILD_HOSTCFLAGS here? This allows the extra flags to override the build system's flags.

Thanks,
Ian

Quoted 19 lines
tools/build/Makefile +3 -3
+
 # To track fixdep's dependencies properly, fixdep needs to run on itself.
 # Build it twice the first time.
 $(FIXDEP_IN): FORCE
        $(Q)if [ ! -f $(FIXDEP) ]; then                                         \
-               $(MAKE) $(build)=fixdep HOSTCFLAGS="$(KBUILD_HOSTCFLAGS)";      \
+               $(MAKE) $(build)=fixdep HOSTCFLAGS="$(HOSTCFLAGS)";             \
                rm -f $(FIXDEP).o;                                              \
        fi
-       $(Q)$(MAKE) $(build)=fixdep HOSTCFLAGS="$(KBUILD_HOSTCFLAGS)"
+       $(Q)$(MAKE) $(build)=fixdep HOSTCFLAGS="$(HOSTCFLAGS)"
 
 
 $(FIXDEP): $(FIXDEP_IN)
 
--

2.34.1

reviewer
#9 lore raw

Re: [PATCH v4 05/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS

In reply to Leo Yan
Quoted 31 lines · Leo Yan

Prepare for future changes where EXTRA_CFLAGS may include flags not applicable to the host compiler.

Move the HOST_CFLAGS assignment before appending EXTRA_CFLAGS to CFLAGS so that HOST_CFLAGS does not inherit flags from EXTRA_CFLAGS.

---
 tools/bpf/bpftool/Makefile | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Makefile +6 -0
index 519ea5cb8ab1c0ee31acc67fc5f96b40e21005c2..3e7d8359e1b2a81a29a47544be8539e3b191a0e8 100644
@@ -81,6 +81,12 @@ CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
 ifneq ($(BPFTOOL_VERSION),)
 CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
 endif
+
+# This must be done before appending EXTRA_CFLAGS to CFLAGS to avoid
+# including flags that are not applicable to the host compiler.
+HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
 endif

tools/bpf/bpftool/Makefile ifneq ($(EXTRA_LDFLAGS),) · line 97

Quoted 6 lines
tools/bpf/bpftool/Makefile +0 -2
@@ -88,8 +94,6 @@ ifneq ($(EXTRA_LDFLAGS),)
 LDFLAGS += $(EXTRA_LDFLAGS)
 endif
 
-HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
-               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)

Should HOST_LDFLAGS be similarly moved? Otherwise, it seems target flags in EXTRA_LDFLAGS will be added for the host.

Thanks,
Ian

Quoted 5 lines
tools/bpf/bpftool/Makefile +0 -1
 INSTALL ?= install
 
--

2.34.1

reviewer
#44 lore raw

Re: [PATCH v4 27/30] selftests/bpf: Append extra cflags

In reply to Leo Yan
Quoted 18 lines · Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

urandom_read is built with clang. Introduce URANDOM_READ_CFLAGS to copy the compiler flags without appending EXTRA_CFLAGS, which may contain incompatible options with clang.

---
 tools/testing/selftests/bpf/Makefile | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +0 -0
index 72a9ba41f95ebb825d25799c07ba3f0311243e45..27112f08c9a5b6cabc41902207d00897070b41b4 100644

tools/testing/selftests/bpf/Makefile ifneq ($(LLVM),) · line 76

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +3 -0
@@ -71,6 +71,12 @@ ifneq ($(LLVM),)
 CFLAGS += -Wno-unused-command-line-argument
 endif
 
+# urandom_read is built with clang. Copy $(CFLAGS) before EXTRA_CFLAGS are
+# appended so that it does not inherit potentially incompatible options.
+URANDOM_READ_CFLAGS := $(CFLAGS)

Later in the Makefile, include ../lib.mk is executed and it appends CFLAGS like -D_GNU_SOURCE= which will be missing from URANDOM_READ_CFLAGS here. Perhaps move these assignments below the include?

Thanks,
Ian

Quoted 28 lines
tools/testing/selftests/bpf/Makefile +5 -3
+
+CFLAGS += $(EXTRA_CFLAGS)
+
 # Check whether bpf cpu=v4 is supported or not by clang
 ifneq ($(shell $(CLANG) --target=bpf -mcpu=help 2>&1 | grep 'v4'),)
 CLANG_CPUV4 := 1
@@ -255,7 +261,7 @@ endif
 $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
        $(call msg,LIB,,$@)
        $(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-                    $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
+                    $(filter-out -static,$(URANDOM_READ_CFLAGS) $(LDFLAGS)) \
                     $(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \
                     -Wno-unused-command-line-argument \
                     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
@@ -265,7 +271,7 @@ $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom
 $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
        $(call msg,BINARY,,$@)
        $(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-                    $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
+                    $(filter-out -static,$(URANDOM_READ_CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
                     -Wno-unused-command-line-argument \
                     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT) \
                     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
 
--

2.34.1

maintainer Acked-by on v4 8/30
#21 lore raw

Re: [PATCH v4 08/30] tools build: Append -fzero-init-padding-bits=all to extra cflags

In reply to Leo Yan
Quoted 22 lines · Leo Yan

GCC-15 release claims [1]:

{0} initializer in C or C++ for unions no longer guarantees clearing of the whole union (except for static storage duration initialization), it just initializes the first union member to zero. If initialization of the whole union including padding bits is desirable, use {} (valid in C23 or C++) or use -fzero-init-padding-bits=unions option to restore old GCC behavior.

As a result, this new behaviour might cause unexpected data when we initialize a union with using the '{ 0 }' initializer.

Since commit dce4aab8441d ("kbuild: Use -fzero-init-padding-bits=all"), the kernel has enabled -fzero-init-padding-bits=all to zero padding bits in unions and structures. This commit applies the same option for tools building.

The option is not supported by any version older than GCC 15, nor is it supported by LLVM. This patch adds the cc-option and host-cc-option functions to dynamically detect compiler option and append it to the EXTRA_CFLAGS and HOST_EXTRACFLAGS respectively.

Quoted 4 lines

[1] https://gcc.gnu.org/gcc-15/changes.html

Thanks,
Namhyung

Quoted 49 lines
---
 tools/scripts/Makefile.include | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/scripts/Makefile.include +30 -0
index b5ecf137febcae59f506e107a7f2e2ad72f4bef4..a2397ceae512c1bc54adb15cb1a111ff34e28e43 100644
@@ -137,6 +137,36 @@ else
 EXTRA_WARNINGS += -Wshadow
 endif
 
+# output directory for tests below
+TMPOUT = .tmp_$$$$
+
+# try-run
+# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
+# Exit code chooses option. "$$TMP" serves as a temporary file and is
+# automatically cleaned up.
+try-run = $(shell set -e;		\
+	TMP=$(TMPOUT)/tmp;		\
+	trap "rm -rf $(TMPOUT)" EXIT;	\
+	mkdir -p $(TMPOUT);		\
+	if ($(1)) >/dev/null 2>&1;	\
+	then echo "$(2)";		\
+	else echo "$(3)";		\
+	fi)
+
+# cc-option
+# Usage: CFLAGS += $(call cc-option,-march=winchip-c6,-march=i586)
+cc-option = $(call try-run, \
+	$(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+host-cc-option = $(call try-run, \
+	$(HOSTCC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+# Explicitly clear padding bits with the initializer '{ 0 }'
+FLAG_ZERO_INIT := $(call cc-option,-fzero-init-padding-bits=all)
+override EXTRA_CFLAGS += $(FLAG_ZERO_INIT)
+HOST_FLAG_ZERO_INIT := $(call host-cc-option,-fzero-init-padding-bits=all)
+override HOST_EXTRACFLAGS += $(HOST_FLAG_ZERO_INIT)
+
 ifneq ($(findstring $(MAKEFLAGS), w),w)
 PRINT_DIR = --no-print-directory
 else
Signature · 2 lines
-- 
2.34.1
maintainer Acked-by on v4 10/30
#24 lore raw

Re: [PATCH v4 10/30] perf build: Append extra host flags

In reply to Leo Yan
Quoted 4 lines · Leo Yan

Append HOST_EXTRACFLAGS to HOST_CFLAGS so that additional flags can be applied to the host compiler.

Thanks,
Namhyung

Quoted 21 lines
---
 tools/perf/Makefile.config | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/Makefile.config +1 -1
index a8dc72cfe48eec52bd9e2352c46c88f5d8047765..f4004ec148a19c784bfe3ee9b5cc3901a3db25a3 100644
@@ -17,7 +17,7 @@ detected     = $(shell echo "$(1)=y"       >> $(OUTPUT).config-detected)
 detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
-HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
+HOSTCFLAGS := $(HOST_EXTRACFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
 # This is required because the kernel is built with this and some of the code
 # borrowed from kernel headers depends on it, e.g. put_unaligned_*().
Signature · 2 lines
-- 
2.34.1
maintainer Acked-by on v4 28/30
#47 lore raw

Re: [PATCH v4 28/30] selftests/hid: Append extra cflags

In reply to Leo Yan
Quoted 13 lines · Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/testing/selftests/hid/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/hid/Makefile +0 -0
index 50ec9e0406abaf316fba232c5da1967e27d0fa9b..686c5f79ca592c2ce94e37b39ab5c3ebe8483f79 100644

tools/testing/selftests/hid/Makefile HOSTPKG_CONFIG := pkg-config · line 29

Quoted 4 lines
tools/testing/selftests/hid/Makefile +1 -0
@@ -26,6 +26,7 @@ HOSTPKG_CONFIG := pkg-config
 
 CFLAGS += -g -O0 -rdynamic -Wall -Werror -I$(OUTPUT)
 CFLAGS += -I$(OUTPUT)/tools/include
+CFLAGS += $(EXTRA_CFLAGS)

Cheers,
Benjamin

Quoted 8 lines
tools/testing/selftests/hid/Makefile +0 -0
 LDLIBS += -lelf -lz -lrt -lpthread
 
 
Signature · 1 line
2.34.1
author
#10 lore raw

Re: [PATCH v4 05/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS

In reply to Ian Rogers
Quoted 35 lines · Ian Rogers
Quoted 34 lines · Leo Yan

Prepare for future changes where EXTRA_CFLAGS may include flags not applicable to the host compiler.

Move the HOST_CFLAGS assignment before appending EXTRA_CFLAGS to CFLAGS so that HOST_CFLAGS does not inherit flags from EXTRA_CFLAGS.

---
 tools/bpf/bpftool/Makefile | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Makefile +6 -0
index 519ea5cb8ab1c0ee31acc67fc5f96b40e21005c2..3e7d8359e1b2a81a29a47544be8539e3b191a0e8 100644
@@ -81,6 +81,12 @@ CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
 ifneq ($(BPFTOOL_VERSION),)
 CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
 endif
+
+# This must be done before appending EXTRA_CFLAGS to CFLAGS to avoid
+# including flags that are not applicable to the host compiler.
+HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
 endif
@@ -88,8 +94,6 @@ ifneq ($(EXTRA_LDFLAGS),)
 LDFLAGS += $(EXTRA_LDFLAGS)
 endif
 
Quoted 6 lines

tools/bpf/bpftool/Makefile ifneq ($(EXTRA_LDFLAGS),) · line 97

Quoted 3 lines
tools/bpf/bpftool/Makefile +0 -2
-HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
-               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)

Should HOST_LDFLAGS be similarly moved? Otherwise, it seems target flags in EXTRA_LDFLAGS will be added for the host.

Though this series is irrelevant to LDFLAGS, I am fine to move HOST_LDFLAGS together.

Quentin, how about you think?

maintainer
#11 lore raw

Re: [PATCH v4 05/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS

2026-03-13 15:56 UTC+0000 ~ Leo Yan <[email protected]>

Quoted 43 lines · Ian Rogers
Quoted 35 lines · Leo Yan
Quoted 34 lines

Prepare for future changes where EXTRA_CFLAGS may include flags not applicable to the host compiler.

Move the HOST_CFLAGS assignment before appending EXTRA_CFLAGS to CFLAGS so that HOST_CFLAGS does not inherit flags from EXTRA_CFLAGS.

---
 tools/bpf/bpftool/Makefile | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Makefile +6 -0
index 519ea5cb8ab1c0ee31acc67fc5f96b40e21005c2..3e7d8359e1b2a81a29a47544be8539e3b191a0e8 100644
@@ -81,6 +81,12 @@ CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
 ifneq ($(BPFTOOL_VERSION),)
 CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
 endif
+
+# This must be done before appending EXTRA_CFLAGS to CFLAGS to avoid
+# including flags that are not applicable to the host compiler.
+HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
 endif
@@ -88,8 +94,6 @@ ifneq ($(EXTRA_LDFLAGS),)
 LDFLAGS += $(EXTRA_LDFLAGS)
 endif
 
Quoted 6 lines

tools/bpf/bpftool/Makefile ifneq ($(EXTRA_LDFLAGS),) · line 97

Quoted 3 lines
tools/bpf/bpftool/Makefile +0 -2
-HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
-               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)

Should HOST_LDFLAGS be similarly moved? Otherwise, it seems target flags in EXTRA_LDFLAGS will be added for the host.

Quoted 2 lines

Though this series is irrelevant to LDFLAGS, I am fine to move HOST_LDFLAGS together.

If we move the HOST_LDFLAGS earlier so that they no longer receive the EXTRA_LDFLAGS, then we lose the possibility to use any EXTRA variable to pass additional flags to HOST_LDFLAGS. So OK to move, but maybe add a "HOST_LDFLAGS += $(HOST_EXTRALDFLAGS)" to it for consistency with the processing of HOST_CFLAGS?

Quentin

author
#45 lore raw

Re: [PATCH v4 27/30] selftests/bpf: Append extra cflags

In reply to Ian Rogers
Quoted 22 lines · Ian Rogers
Quoted 21 lines · Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

urandom_read is built with clang. Introduce URANDOM_READ_CFLAGS to copy the compiler flags without appending EXTRA_CFLAGS, which may contain incompatible options with clang.

---
 tools/testing/selftests/bpf/Makefile | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +0 -0
index 72a9ba41f95ebb825d25799c07ba3f0311243e45..27112f08c9a5b6cabc41902207d00897070b41b4 100644
@@ -71,6 +71,12 @@ ifneq ($(LLVM),)
 CFLAGS += -Wno-unused-command-line-argument
 endif
 
Quoted 8 lines

tools/testing/selftests/bpf/Makefile ifneq ($(LLVM),) · line 76

Quoted 3 lines
tools/testing/selftests/bpf/Makefile +3 -0
+# urandom_read is built with clang. Copy $(CFLAGS) before EXTRA_CFLAGS are
+# appended so that it does not inherit potentially incompatible options.
+URANDOM_READ_CFLAGS := $(CFLAGS)

Later in the Makefile, include ../lib.mk is executed and it appends CFLAGS like -D_GNU_SOURCE= which will be missing from URANDOM_READ_CFLAGS here. Perhaps move these assignments below the include?

Will do, thanks!

author
#12 lore raw

Re: [PATCH v4 05/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS

In reply to Quentin Monnet

On Fri, Mar 13, 2026 at 04:07:27PM +0000, Quentin Monnet wrote:

[...]

Quoted 24 lines
Quoted 20 lines
Quoted 13 lines
Quoted 13 lines
tools/bpf/bpftool/Makefile +6 -0
+
+# This must be done before appending EXTRA_CFLAGS to CFLAGS to avoid
+# including flags that are not applicable to the host compiler.
+HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
 endif
@@ -88,8 +94,6 @@ ifneq ($(EXTRA_LDFLAGS),)
 LDFLAGS += $(EXTRA_LDFLAGS)
 endif
 
Quoted 6 lines

tools/bpf/bpftool/Makefile ifneq ($(EXTRA_LDFLAGS),) · line 97

Quoted 3 lines
tools/bpf/bpftool/Makefile +0 -2
-HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
-               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)

Should HOST_LDFLAGS be similarly moved? Otherwise, it seems target flags in EXTRA_LDFLAGS will be added for the host.

Quoted 2 lines

Though this series is irrelevant to LDFLAGS, I am fine to move HOST_LDFLAGS together.

Quoted 5 lines

If we move the HOST_LDFLAGS earlier so that they no longer receive the EXTRA_LDFLAGS, then we lose the possibility to use any EXTRA variable to pass additional flags to HOST_LDFLAGS. So OK to move, but maybe add a "HOST_LDFLAGS += $(HOST_EXTRALDFLAGS)" to it for consistency with the processing of HOST_CFLAGS?

Adding HOST_EXTRALDFLAGS seems a bit over designed to me, as there is no use case for it currently. To avoid complexity, let's keep this patch as it is, we can add HOST_EXTRALDFLAGS if later have requirement.

P.s. I saw that you already replied the same comment in the previous version. I should have confirmed at that time, sorry for the duplicate discussion.

maintainer
#13 lore raw

Re: [PATCH v4 05/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS

2026-03-13 16:24 UTC+0000 ~ Leo Yan <[email protected]>

Quoted 34 lines · Quentin Monnet

[...]

Quoted 24 lines
Quoted 20 lines
Quoted 13 lines
Quoted 13 lines
tools/bpf/bpftool/Makefile +6 -0
+
+# This must be done before appending EXTRA_CFLAGS to CFLAGS to avoid
+# including flags that are not applicable to the host compiler.
+HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
 endif
@@ -88,8 +94,6 @@ ifneq ($(EXTRA_LDFLAGS),)
 LDFLAGS += $(EXTRA_LDFLAGS)
 endif
 
Quoted 6 lines

tools/bpf/bpftool/Makefile ifneq ($(EXTRA_LDFLAGS),) · line 97

Quoted 3 lines
tools/bpf/bpftool/Makefile +0 -2
-HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
-               $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)

Should HOST_LDFLAGS be similarly moved? Otherwise, it seems target flags in EXTRA_LDFLAGS will be added for the host.

Quoted 2 lines

Though this series is irrelevant to LDFLAGS, I am fine to move HOST_LDFLAGS together.

Quoted 5 lines

If we move the HOST_LDFLAGS earlier so that they no longer receive the EXTRA_LDFLAGS, then we lose the possibility to use any EXTRA variable to pass additional flags to HOST_LDFLAGS. So OK to move, but maybe add a "HOST_LDFLAGS += $(HOST_EXTRALDFLAGS)" to it for consistency with the processing of HOST_CFLAGS?

Quoted 7 lines

Adding HOST_EXTRALDFLAGS seems a bit over designed to me, as there is no use case for it currently. To avoid complexity, let's keep this patch as it is, we can add HOST_EXTRALDFLAGS if later have requirement.

P.s. I saw that you already replied the same comment in the previous version. I should have confirmed at that time, sorry for the duplicate discussion.

That's fine :) - And agreed, it's probably best to leave it as-is for now. Thanks!

Quentin

author
#18 lore raw

Re: [PATCH v4 07/30] libbpf: Initialize CFLAGS before including Makefile.include

In reply to Leo Yan

Hi BPF maintainers,

Quoted 8 lines · Leo Yan

tools/scripts/Makefile.include may expand EXTRA_CFLAGS in a future change. This could alter the initialization of CFLAGS, as the default options "-g -O2" would never be set once EXTRA_CFLAGS is expanded.

Prepare for this by moving the CFLAGS initialization before including tools/scripts/Makefile.include, so it is not affected by the extended EXTRA_CFLAGS.

Quoted 5 lines

Append EXTRA_CFLAGS to CFLAGS only after including Makefile.include and place it last so that the extra flags propagate properly and can override the default options.

Before sending a new version, I'd check if you have a chance to review the libbpf patches. The libbpf change is crucial for this series.

Another reason for pinging because I received a bpf-ci report about test failures for this series [1]. The failures looks to be unrelated, as the tests fail starting from the first patch, which is irrelevant to BPF code [2]. I tried reaching [email protected] last week but haven't received a response yet.

If bpf-ci testing is mandatory for libbpf patches, could you advise me on the correct workflow (e.g., whether the patches should be based on a specific branch)?

Thanks.

[1] https://github.com/kernel-patches/bpf/actions/runs/22944268645
[2] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Quoted 49 lines
---
 tools/lib/bpf/Makefile | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
tools/lib/bpf/Makefile +9 -7
index b1fbd0211c5edde7be3d98630b16e53e028d9116..eca584fb061e16013e76827e4203f6be0477a73e 100644
@@ -49,6 +49,14 @@ man_dir_SQ = '$(subst ','\'',$(man_dir))'
 export man_dir man_dir_SQ INSTALL
 export DESTDIR DESTDIR_SQ
 
+# Defer assigning EXTRA_CFLAGS to CFLAGS until after including
+# tools/scripts/Makefile.include, as it may add flags to EXTRA_CFLAGS.
+ifdef EXTRA_CFLAGS
+  CFLAGS :=
+else
+  CFLAGS := -g -O2
+endif
+
 include $(srctree)/tools/scripts/Makefile.include
 
 # copy a bit from Linux kbuild
@@ -70,13 +78,6 @@ LIB_TARGET	= libbpf.a libbpf.so.$(LIBBPF_VERSION)
 LIB_FILE	= libbpf.a libbpf.so*
 PC_FILE		= libbpf.pc
 
-# Set compile option CFLAGS
-ifdef EXTRA_CFLAGS
-  CFLAGS := $(EXTRA_CFLAGS)
-else
-  CFLAGS := -g -O2
-endif
-
 # Append required CFLAGS
 override CFLAGS += -std=gnu89
 override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
@@ -84,6 +85,7 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
+override CFLAGS += $(EXTRA_CFLAGS)
 
 # flags specific for shared library
 SHLIB_FLAGS := -DSHARED -fPIC
Signature · 2 lines
-- 
2.34.1
Acked-by on v4 3/30
#6 lore raw

Re: [PATCH v4 03/30] tools: lib: thermal: Initialize CFLAGS before including Makefile.include

In reply to Leo Yan
Quoted 9 lines · Leo Yan

tools/scripts/Makefile.include may expand EXTRA_CFLAGS in a future change. This could alter the initialization of CFLAGS in the thermal Makefile, as the default options "-g -Wall" would never be set once EXTRA_CFLAGS is expanded.

Prepare for this by moving the CFLAGS initialization before including tools/scripts/Makefile.include, so it is not affected by the extended EXTRA_CFLAGS.

Quoted 6 lines

Append EXTRA_CFLAGS to CFLAGS only after including Makefile.include and place it last so that the extra flags propagate properly and can override the default options.

---

Thanks !

#19 lore raw

Re: [PATCH v4 07/30] libbpf: Initialize CFLAGS before including Makefile.include

In reply to Leo Yan
Quoted 27 lines · Leo Yan

Hi BPF maintainers,

Quoted 8 lines · Leo Yan

tools/scripts/Makefile.include may expand EXTRA_CFLAGS in a future change. This could alter the initialization of CFLAGS, as the default options "-g -O2" would never be set once EXTRA_CFLAGS is expanded.

Prepare for this by moving the CFLAGS initialization before including tools/scripts/Makefile.include, so it is not affected by the extended EXTRA_CFLAGS.

Quoted 5 lines

Append EXTRA_CFLAGS to CFLAGS only after including Makefile.include and place it last so that the extra flags propagate properly and can override the default options.

Before sending a new version, I'd check if you have a chance to review the libbpf patches. The libbpf change is crucial for this series.

Another reason for pinging because I received a bpf-ci report about test failures for this series [1]. The failures looks to be unrelated, as the tests fail starting from the first patch, which is irrelevant to BPF code [2]. I tried reaching [email protected] last week but haven't received a response yet.

Quoted 3 lines

If bpf-ci testing is mandatory for libbpf patches, could you advise me on the correct workflow (e.g., whether the patches should be based on a specific branch)?

yes. It is mandatory and libbpf patches must go through bpf-next tree, so pls send them separately.

#49 lore raw

Re: [PATCH v4 29/30] selftests/nolibc: Append extra cflags

In reply to Leo Yan
Quoted 22 lines · Leo Yan

Include tools/scripts/Makefile.include, which may provide additional flags in EXTRA_CFLAGS, and append to CFLAGS.

Since Makefile.include already defines cc-option, remove the redundant definition and include.

---
 tools/testing/selftests/nolibc/Makefile | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/testing/selftests/nolibc/Makefile +0 -3
index 0370489d938b540721a4fb814ebb68c463760e73..d1a9c1d5988bdc6a8778ef418aaa4c5ac1d26a10 100644
@@ -3,15 +3,13 @@
 TEST_GEN_PROGS := nolibc-test libc-test
 
 include ../lib.mk
-include $(top_srcdir)/scripts/Makefile.compiler
-
-cc-option = $(call __cc-option, $(CC),,$(1),$(2))

tools/testing/selftests/nolibc/Makefile line 14

Quoted 10 lines
tools/testing/selftests/nolibc/Makefile +2 -1
+include $(top_srcdir)/tools/scripts/Makefile.include
 
 include Makefile.include
 
 $(OUTPUT)/nolibc-test: CFLAGS = -nostdlib -nostdinc -static \
 	 -isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \
-	 $(CFLAGS_NOLIBC_TEST)
+	 $(CFLAGS_NOLIBC_TEST) $(EXTRA_CFLAGS)
 $(OUTPUT)/nolibc-test: LDLIBS = $(if $(LLVM),,-lgcc)
 $(OUTPUT)/nolibc-test: nolibc-test.c nolibc-test-linkage.c | headers

The rule for "libc-test", which uses the standard $(LINK.c) to invoke the compiler, does not respect $(EXTRA_CFLAGS) automatically. Is this intentional? It also affects all other users of lib.mk which don't have any custom cflags handling.

If so, there should be an override for the "libc-test" target, too. But if I am supposed to pick up this patch, I can fix it up when applying.

Thomas

author
#50 lore raw

Re: [PATCH v4 29/30] selftests/nolibc: Append extra cflags

Quoted 27 lines · Thomas Weißschuh
Quoted 26 lines · Leo Yan

Include tools/scripts/Makefile.include, which may provide additional flags in EXTRA_CFLAGS, and append to CFLAGS.

Since Makefile.include already defines cc-option, remove the redundant definition and include.

---
 tools/testing/selftests/nolibc/Makefile | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/testing/selftests/nolibc/Makefile +1 -3
index 0370489d938b540721a4fb814ebb68c463760e73..d1a9c1d5988bdc6a8778ef418aaa4c5ac1d26a10 100644
@@ -3,15 +3,13 @@
 TEST_GEN_PROGS := nolibc-test libc-test
 
 include ../lib.mk
-include $(top_srcdir)/scripts/Makefile.compiler
-
-cc-option = $(call __cc-option, $(CC),,$(1),$(2))
+include $(top_srcdir)/tools/scripts/Makefile.include
 
 include Makefile.include
 
Quoted 10 lines

tools/testing/selftests/nolibc/Makefile line 14

Quoted 6 lines
tools/testing/selftests/nolibc/Makefile +1 -1
 $(OUTPUT)/nolibc-test: CFLAGS = -nostdlib -nostdinc -static \
 	 -isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \
-	 $(CFLAGS_NOLIBC_TEST)
+	 $(CFLAGS_NOLIBC_TEST) $(EXTRA_CFLAGS)
 $(OUTPUT)/nolibc-test: LDLIBS = $(if $(LLVM),,-lgcc)
 $(OUTPUT)/nolibc-test: nolibc-test.c nolibc-test-linkage.c | headers

The rule for "libc-test", which uses the standard $(LINK.c) to invoke the compiler, does not respect $(EXTRA_CFLAGS) automatically. Is this intentional?

Thanks for the review. Is the change below OK with you? I can update it before sending a new series.

tools/testing/selftests/nolibc/Makefile +3 -4
index 0370489d938b..7b65e6f9840b 100644
@@ -3,18 +3,17 @@
 TEST_GEN_PROGS := nolibc-test libc-test
 
 include ../lib.mk
-include $(top_srcdir)/scripts/Makefile.compiler
-
-cc-option = $(call __cc-option, $(CC),,$(1),$(2))
+include $(top_srcdir)/tools/scripts/Makefile.include
 
 include Makefile.include
 
 $(OUTPUT)/nolibc-test: CFLAGS = -nostdlib -nostdinc -static \
         -isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \
-        $(CFLAGS_NOLIBC_TEST)
+        $(CFLAGS_NOLIBC_TEST) $(EXTRA_CFLAGS)
 $(OUTPUT)/nolibc-test: LDLIBS = $(if $(LLVM),,-lgcc)
 $(OUTPUT)/nolibc-test: nolibc-test.c nolibc-test-linkage.c | headers
 
+$(OUTPUT)/libc-test: CFLAGS += $(EXTRA_CFLAGS)
 $(OUTPUT)/libc-test: nolibc-test.c nolibc-test-linkage.c
        $(call msg,CC,,$@)
        $(Q)$(LINK.c) $^ -o $@
Quoted 2 lines

It also affects all other users of lib.mk which
don't have any custom cflags handling.

Many projects include lib.mk, but most do not include tools/scripts/Makefile.include. Without it, the appended EXTRA_CFLAGS cannot be inherited. This is why I did not touch those projects.

Appending EXTRA_CFLAGS in lib.mk may not be a good idea ? as such a global change could break some builds.

Thanks,
Leo

maintainer Acked-by on v4 18/30
#34 lore raw

Re: [PATCH v4 18/30] tools: iio: Append extra cflags

In reply to Leo Yan
Quoted 4 lines · On Wed, 11 Mar 2026 08:29:43 +0000 Leo Yan <[email protected]> wrote:

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

Given discussion leading to:
https://lore.kernel.org/all/[email protected]/
I'm assuming I don't need to do anything beyond.

Shout if otherwise.

Quoted 17 lines
---
 tools/iio/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/iio/Makefile +1 -0
index 3bcce0b7d10f42208765be83dac047faa2e0ceb1..f0428421792d1cbe5ff71b01b67f23eb908fa714 100644
@@ -13,6 +13,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := iio_event_monitor lsiio iio_generic_buffer
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))