[PATCH v3 01/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS

Leo Yan posted 30 patches 1 month ago
There is a newer version of this series
[PATCH v3 01/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS
Posted by Leo Yan 1 month ago
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.

Signed-off-by: Leo Yan <leo.yan@arm.com>
---
 tools/bpf/bpftool/Makefile | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
index 519ea5cb8ab1c0ee31acc67fc5f96b40e21005c2..3e7d8359e1b2a81a29a47544be8539e3b191a0e8 100644
--- a/tools/bpf/bpftool/Makefile
+++ b/tools/bpf/bpftool/Makefile
@@ -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

-- 
2.34.1
Re: [PATCH v3 01/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS
Posted by Ian Rogers 1 month ago
On Sun, Mar 8, 2026 at 9:46 AM Leo Yan <leo.yan@arm.com> wrote:
>
> 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.
>
> Signed-off-by: Leo Yan <leo.yan@arm.com>
> ---
>  tools/bpf/bpftool/Makefile | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
> index 519ea5cb8ab1c0ee31acc67fc5f96b40e21005c2..3e7d8359e1b2a81a29a47544be8539e3b191a0e8 100644
> --- a/tools/bpf/bpftool/Makefile
> +++ b/tools/bpf/bpftool/Makefile
> @@ -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)

This isn't an issue with the patch but AI code review is noting that
"LDFLAGS += $(EXTRA_LDFLAGS)" happens above, and then here,
"HOST_LDFLAGS := $(LDFLAGS)". This means something like
EXTRA_LDFLAGS="--sysroot=/my/target" in the build would cause the host
ld to pick up the --sysroot flag and probably break host builds which
likely use the host `bpftool` when cross-compiling (perf's build
should use the host `bpftool`). If the HOST_LDFLAGS were set to
LDFLAGS before EXTRA_LDFLAGS are appended, this would not be an issue.

Thanks,
Ian

>  INSTALL ?= install
>
> --
> 2.34.1
>
Re: [PATCH v3 01/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS
Posted by Quentin Monnet 1 month ago
2026-03-09 10:40 UTC-0700 ~ Ian Rogers <irogers@google.com>
> On Sun, Mar 8, 2026 at 9:46 AM Leo Yan <leo.yan@arm.com> wrote:
>>
>> 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.
>>
>> Signed-off-by: Leo Yan <leo.yan@arm.com>
>> ---
>>  tools/bpf/bpftool/Makefile | 8 ++++++--
>>  1 file changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
>> index 519ea5cb8ab1c0ee31acc67fc5f96b40e21005c2..3e7d8359e1b2a81a29a47544be8539e3b191a0e8 100644
>> --- a/tools/bpf/bpftool/Makefile
>> +++ b/tools/bpf/bpftool/Makefile
>> @@ -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)
> 
> This isn't an issue with the patch but AI code review is noting that
> "LDFLAGS += $(EXTRA_LDFLAGS)" happens above, and then here,
> "HOST_LDFLAGS := $(LDFLAGS)". This means something like
> EXTRA_LDFLAGS="--sysroot=/my/target" in the build would cause the host
> ld to pick up the --sysroot flag and probably break host builds which
> likely use the host `bpftool` when cross-compiling (perf's build
> should use the host `bpftool`). If the HOST_LDFLAGS were set to
> LDFLAGS before EXTRA_LDFLAGS are appended, this would not be an issue.


I noticed it when reviewing, but if the HOST_LDFLAGS were set before
EXTRA_LDFLAGS, we wouldn't be able to pass additional HOST_LDFLAGS via
any "EXTRA_..." variable, which would look inconsistent with how
bpftool's Makefile handles the HOST_CFLAGS. I suppose the right fix
would be to move HOST_LDFLAGS before EXTRA_LDFLAGS as you (and the AI)
suggest, but also to add a HOST_EXTRALDFLAGS to it? Although I've not
heard of any use case for passing different ld flags when
cross-compiling bpftool, yet.

Quentin
Re: [PATCH v3 01/30] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS
Posted by Quentin Monnet 1 month ago
2026-03-08 16:46 UTC+0000 ~ Leo Yan <leo.yan@arm.com>
> 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.
> 
> Signed-off-by: Leo Yan <leo.yan@arm.com>


Acked-by: Quentin Monnet <qmo@kernel.org>