linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC] perf build: Allow passing extra Clang flags via EXTRA_CLANG_FLAGS
@ 2025-10-13  8:06 hupu
  2025-10-13  8:07 ` hupu
  0 siblings, 1 reply; 6+ messages in thread
From: hupu @ 2025-10-13  8:06 UTC (permalink / raw)
  To: namhyung, irogers, peterz, mingo, acme, mark.rutland,
	alexander.shishkin, jolsa, adrian.hunter, nathan,
	nick.desaulniers+lkml, morbo, justinstitt, linux-kernel,
	linux-perf-users
  Cc: hupu.gm

When cross-compiling perf with the BPF option enabled, Clang is used
during the build process. Some environments may require additional
compiler options such as `--sysroot` or custom include paths. This patch
adds a new make variable, `EXTRA_CLANG_FLAGS`, which appends user-specified
options to `CLANG_OPTIONS` during BPF skeleton builds.

Example:

  EXTRA_CLANG_FLAGS="--sysroot=$SYSROOT"
  make perf ARCH=arm64 EXTRA_CLANG_FLAGS="$EXTRA_CLANG_FLAGS"

This enables more flexible cross-compilation setups without modifying
Makefile.perf directly.

Signed-off-by: hupu <hupu.gm@gmail.com>
---
 tools/perf/Makefile.perf | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 47c906b807ef..b0da7314a0d8 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -1249,6 +1249,11 @@ else
 	$(Q)cp "$(VMLINUX_H)" $@
 endif
 
+# Allow users to specify additional Clang options (e.g. --sysroot)
+# when cross-compiling BPF skeletons, enabling more flexible
+# build configurations.
+CLANG_OPTIONS += $(EXTRA_CLANG_FLAGS)
+
 $(SKEL_TMP_OUT)/%.bpf.o: $(OUTPUT)PERF-VERSION-FILE util/bpf_skel/perf_version.h | $(SKEL_TMP_OUT)
 $(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(LIBBPF) $(SKEL_OUT)/vmlinux.h
 	$(QUIET_CLANG)$(CLANG) -g -O2 -fno-stack-protector --target=bpf \
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC] perf build: Allow passing extra Clang flags via EXTRA_CLANG_FLAGS
  2025-10-13  8:06 [RFC] perf build: Allow passing extra Clang flags via EXTRA_CLANG_FLAGS hupu
@ 2025-10-13  8:07 ` hupu
  2025-10-13 15:46   ` Ian Rogers
  0 siblings, 1 reply; 6+ messages in thread
From: hupu @ 2025-10-13  8:07 UTC (permalink / raw)
  To: namhyung, irogers, peterz, mingo, acme, mark.rutland,
	alexander.shishkin, jolsa, adrian.hunter, nathan,
	nick.desaulniers+lkml, morbo, justinstitt, linux-kernel,
	linux-perf-users

Hi Maintainer,

I encountered some issues while cross-compiling perf (ARCH=arm64),
particularly when enabling the BPF option. During the build, Clang
fails to compile BPF programs due to missing header files. The
relevant error messages are as follows:


---------------------------------------
/usr/bin/ld: skipping incompatible
/home/hupu/work/tools/crosstool-ng/output/aarch64-dumpstack-linux-gnu/bin/../aarch64-dumpstack-linux-gnu/sysroot/usr/lib/aarch64-linux-gnu/libzstd.a
when searching for -lzstd
/usr/bin/ld: skipping incompatible
/home/hupu/work/tools/crosstool-ng/output/aarch64-dumpstack-linux-gnu/bin/../aarch64-dumpstack-linux-gnu/sysroot/usr/lib/libc.a
when searching for -lc
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libcrypto.a(libcrypto-lib-dso_dlfcn.o):
in function `dlfcn_globallookup':
(.text+0x1f): warning: Using 'dlopen' in statically linked
applications requires at runtime the shared libraries from the glibc
version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libcrypto.a(libcrypto-lib-bio_addr.o):
in function `BIO_lookup_ex':
(.text+0xdbc): warning: Using 'getaddrinfo' in statically linked
applications requires at runtime the shared libraries from the glibc
version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libcrypto.a(libcrypto-lib-bio_sock.o):
in function `BIO_gethostbyname':
(.text+0x85): warning: Using 'gethostbyname' in statically linked
applications requires at runtime the shared libraries from the glibc
version used for linking
  CLANG   /home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bpf_prog_profiler.bpf.o
  CLANG   /home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_leader.bpf.o
  CLANG   /home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_follower.bpf.o
  CLANG   /home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_cgroup.bpf.o
In file included from <built-in>:2:
In file included from ./util/bpf_skel/perf_version.h:6:
In file included from <built-in>:2:
In file included from ./util/bpf_skel/perf_version.h:6:
In file included from
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/../vmlinux.h:7:
In file included from
/home/hupu/work/code/explore/linux-mainline/tools/include/uapi/linux/perf_event.h:19:
/usr/include/linux/ioctl.h:5:10: fatal error: 'asm/ioctl.h' file not found
    In file included from
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/../vmlinux.h:5
| #include <asm/ioctl.h>
      |          ^~~~~~~~~~~~~
7:
In file included from
/home/hupu/work/code/explore/linux-mainline/tools/include/uapi/linux/perf_event.h:19:
In file included from <built-in>:2:
In file included from ./util/bpf_skel/perf_version.h:6:
In file included from
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/../vmlinux.h:7:
In file included from
/home/hupu/work/code/explore/linux-mainline/tools/include/uapi/linux/perf_event.h:19:
/usr/include/linux/ioctl.h:5:10: fatal error: 'asm/ioctl.h' file not found
    5 | #include <asm/ioctl.h>
      |          ^~~~~~~~~~~~~
/usr/include/linux/ioctl.h:5:10: fatal error: 'asm/ioctl.h' file not found
    5 | #include <asm/ioctl.h>
      |          ^~~~~~~~~~~~~
1 error generated.
make[3]: *** [Makefile.perf:1259:
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_leader.bpf.o]
Error 1
make[3]: *** Waiting for unfinished jobs....
In file included from <built-in>:2:
In file included from ./util/bpf_skel/perf_version.h:6:
In file included from
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/../vmlinux.h:7:
In file included from
/home/hupu/work/code/explore/linux-mainline/tools/include/uapi/linux/perf_event.h:19:
/usr/include/linux/ioctl.h:5:10: fatal error: 'asm/ioctl.h' file not found
    5 | #include <asm/ioctl.h>
      |          ^~~~~~~~~~~~~
1 error generated.
make[3]: *** [Makefile.perf:1259:
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_cgroup.bpf.o]
Error 1
1 error generated.
1 error generated.
make[3]: *** [Makefile.perf:1259:
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bpf_prog_profiler.bpf.o]
Error 1
make[3]: *** [Makefile.perf:1259:
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_follower.bpf.o]
Error 1
make[2]: *** [Makefile.perf:289: sub-make] Error 2
make[1]: *** [Makefile:76: all] Error 2
make: *** [Makefile:93: perf] Error 2
---------------------------------------


This happens because the eBPF part of perf is compiled with Clang,
which by default searches only the host /usr/include/ paths for
headers, not the cross-compilation sysroot. As a result, it cannot
find the ARM64-specific asm/ioctl.h.

To address this issue, I wrote a patch that introduces a new make
variable, EXTRA_CLANG_FLAGS. This allows passing custom Clang options
via the command line during the BPF skeleton build, for example
--sysroot or custom include paths, without modifying Makefile.perf
itself.

Example usage:
  EXTRA_CLANG_FLAGS="--sysroot=$SYSROOT"
  make perf ARCH=arm64 EXTRA_CLANG_FLAGS="$EXTRA_CLANG_FLAGS"

I have tested this approach and it works. However, I am not sure if
this is the optimal solution, so I would like to discuss this further
with you and get your feedback.

Thanks,
hupu

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] perf build: Allow passing extra Clang flags via EXTRA_CLANG_FLAGS
  2025-10-13  8:07 ` hupu
@ 2025-10-13 15:46   ` Ian Rogers
  2025-10-13 16:14     ` Leo Yan
  2025-10-14  1:57     ` hupu
  0 siblings, 2 replies; 6+ messages in thread
From: Ian Rogers @ 2025-10-13 15:46 UTC (permalink / raw)
  To: hupu, Leo Yan
  Cc: namhyung, peterz, mingo, acme, mark.rutland, alexander.shishkin,
	jolsa, adrian.hunter, nathan, nick.desaulniers+lkml, morbo,
	justinstitt, linux-kernel, linux-perf-users

On Mon, Oct 13, 2025 at 1:08 AM hupu <hupu.gm@gmail.com> wrote:
>
> Hi Maintainer,
>
> I encountered some issues while cross-compiling perf (ARCH=arm64),
> particularly when enabling the BPF option. During the build, Clang
> fails to compile BPF programs due to missing header files. The
> relevant error messages are as follows:

Thanks for reporting this issue! There has been some recent work in
this area by Leo:
https://lore.kernel.org/lkml/20251006-perf_build_android_ndk-v3-0-4305590795b2@arm.com/
The patches are in Linux v6.18-rc1. Perhaps you could try repeating
your build with this tree and reporting issues?

Thanks,
Ian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] perf build: Allow passing extra Clang flags via EXTRA_CLANG_FLAGS
  2025-10-13 15:46   ` Ian Rogers
@ 2025-10-13 16:14     ` Leo Yan
  2025-10-14  2:31       ` hupu
  2025-10-14  1:57     ` hupu
  1 sibling, 1 reply; 6+ messages in thread
From: Leo Yan @ 2025-10-13 16:14 UTC (permalink / raw)
  To: Ian Rogers
  Cc: hupu, namhyung, peterz, mingo, acme, mark.rutland,
	alexander.shishkin, jolsa, adrian.hunter, nathan,
	nick.desaulniers+lkml, morbo, justinstitt, linux-kernel,
	linux-perf-users

On Mon, Oct 13, 2025 at 08:46:01AM -0700, Ian Rogers wrote:
> On Mon, Oct 13, 2025 at 1:08 AM hupu <hupu.gm@gmail.com> wrote:
> >
> > Hi Maintainer,
> >
> > I encountered some issues while cross-compiling perf (ARCH=arm64),
> > particularly when enabling the BPF option. During the build, Clang
> > fails to compile BPF programs due to missing header files. The
> > relevant error messages are as follows:
> 
> Thanks for reporting this issue! There has been some recent work in
> this area by Leo:
> https://lore.kernel.org/lkml/20251006-perf_build_android_ndk-v3-0-4305590795b2@arm.com/
> The patches are in Linux v6.18-rc1. Perhaps you could try repeating
> your build with this tree and reporting issues?

I am not 100% sure, could you execute install kernel headers and then
build perf ?

  make headers_install

Thanks,
Leo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] perf build: Allow passing extra Clang flags via EXTRA_CLANG_FLAGS
  2025-10-13 15:46   ` Ian Rogers
  2025-10-13 16:14     ` Leo Yan
@ 2025-10-14  1:57     ` hupu
  1 sibling, 0 replies; 6+ messages in thread
From: hupu @ 2025-10-14  1:57 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Leo Yan, namhyung, peterz, mingo, acme, mark.rutland,
	alexander.shishkin, jolsa, adrian.hunter, nathan,
	nick.desaulniers+lkml, morbo, justinstitt, linux-kernel,
	linux-perf-users

Hi lan,
Thank you very much for your reply.

On Mon, Oct 13, 2025 at 11:46 PM Ian Rogers <irogers@google.com> wrote:
>
> On Mon, Oct 13, 2025 at 1:08 AM hupu <hupu.gm@gmail.com> wrote:
> >
> > Hi Maintainer,
> >
> > I encountered some issues while cross-compiling perf (ARCH=arm64),
> > particularly when enabling the BPF option. During the build, Clang
> > fails to compile BPF programs due to missing header files. The
> > relevant error messages are as follows:
>
> Thanks for reporting this issue! There has been some recent work in
> this area by Leo:
> https://lore.kernel.org/lkml/20251006-perf_build_android_ndk-v3-0-4305590795b2@arm.com/
> The patches are in Linux v6.18-rc1. Perhaps you could try repeating
> your build with this tree and reporting issues?
>

My local code is already updated to the latest version, and the
current commit is exactly at tag: v6.18-rc1:

commit 3a8660878839faadb4f1a6dd72c3179c1df56787 (tag: v6.18-rc1,
origin/master, origin/HEAD)
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Sun Oct 12 13:42:36 2025 -0700

    Linux 6.18-rc1

However, building with this version still gives the same error as
before — when Clang tries to compile the BPF programs, it fails due to
a missing asm/ioctl.h header file.

----------------------------------------------------------
  INSTALL libsubcmd_headers
  INSTALL libsymbol_headers
  SYSHDR  /home/hupu/work/code/explore/output/build-mainline/tools/perf/libperf/arch/arm64/include/generated/uapi/asm/unistd_64.h
  INSTALL libapi_headers
  INSTALL libperf_headers
  CC      /home/hupu/work/code/explore/output/build-mainline/tools/perf/libperf/evsel.o
  INSTALL libbpf_headers
  CLANG   /home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bpf_prog_profiler.bpf.o
  CLANG   /home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_leader.bpf.o
  CLANG   /home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_follower.bpf.o
In file included from <built-in>:2:
In file included from ./util/bpf_skel/perf_version.h:6:
In file included from
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/../vmlinux.h:7:
In file included from
/home/hupu/work/code/explore/linux-mainline/tools/include/uapi/linux/perf_event.h:19:
/usr/include/linux/ioctl.h:5:10: fatal error: 'asm/ioctl.h' file not found
    5 | #include <asm/ioctl.h>
      |          ^~~~~~~~~~~~~
In file included from <built-in>:2:
In file included from ./util/bpf_skel/perf_version.h:6:
In file included from
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/../vmlinux.h:7:
In file included from
/home/hupu/work/code/explore/linux-mainline/tools/include/uapi/linux/perf_event.h:19:
/usr/include/linux/ioctl.h:5:10: fatal error: 'asm/ioctl.h' file not found
    5 | #include <asm/ioctl.h>
      |          ^~~~~~~~~~~~~
In file included from <built-in>:2:
In file included from ./util/bpf_skel/perf_version.h:6:
In file included from
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/../vmlinux.h:7:
In file included from
/home/hupu/work/code/explore/linux-mainline/tools/include/uapi/linux/perf_event.h:19:
/usr/include/linux/ioctl.h:5:10: fatal error: 'asm/ioctl.h' file not found
    5 | #include <asm/ioctl.h>
      |          ^~~~~~~~~~~~~
11 error generated.
 error generated.
make[3]: *** [Makefile.perf:1259:
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_leader.bpf.o]
Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: *** [Makefile.perf:1259:
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bpf_prog_profiler.bpf.o]
Error 1
1 error generated.
make[3]: *** [Makefile.perf:1259:
/home/hupu/work/code/explore/output/build-mainline/tools/perf/util/bpf_skel/.tmp/bperf_follower.bpf.o]
Error 1
  LD      /home/hupu/work/code/explore/output/build-mainline/tools/perf/libperf/libperf-in.o
  AR      /home/hupu/work/code/explore/output/build-mainline/tools/perf/libperf/libperf.a
make[2]: *** [Makefile.perf:289: sub-make] Error 2
make[1]: *** [Makefile:76: all] Error 2
make: *** [Makefile:93: perf] Error 2
----------------------------------------------------------

However, when I apply the previous PATCH and explicitly specify the
sysroot of the cross toolchain via the EXTRA_CLANG_FLAGS setting, the
build succeeds:

    SYSROOT=$(${CROSS_COMPILE}gcc --print-sysroot)
    EXTRA_CLANG_FLAGS="--sysroot=$SYSROOT"
    make perf ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE
EXTRA_CLANG_FLAGS="$EXTRA_CLANG_FLAGS"

Please let me know if you need any more details or testing.

Thanks,
hupu

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] perf build: Allow passing extra Clang flags via EXTRA_CLANG_FLAGS
  2025-10-13 16:14     ` Leo Yan
@ 2025-10-14  2:31       ` hupu
  0 siblings, 0 replies; 6+ messages in thread
From: hupu @ 2025-10-14  2:31 UTC (permalink / raw)
  To: Leo Yan
  Cc: Ian Rogers, namhyung, peterz, mingo, acme, mark.rutland,
	alexander.shishkin, jolsa, adrian.hunter, nathan,
	nick.desaulniers+lkml, morbo, justinstitt, linux-kernel,
	linux-perf-users

Hi Leo,
Thank you for your reply.

On Tue, Oct 14, 2025 at 12:14 AM Leo Yan <leo.yan@arm.com> wrote:
>
> On Mon, Oct 13, 2025 at 08:46:01AM -0700, Ian Rogers wrote:
> > On Mon, Oct 13, 2025 at 1:08 AM hupu <hupu.gm@gmail.com> wrote:
> > >
> > > Hi Maintainer,
> > >
> > > I encountered some issues while cross-compiling perf (ARCH=arm64),
> > > particularly when enabling the BPF option. During the build, Clang
> > > fails to compile BPF programs due to missing header files. The
> > > relevant error messages are as follows:
> >
> > Thanks for reporting this issue! There has been some recent work in
> > this area by Leo:
> > https://lore.kernel.org/lkml/20251006-perf_build_android_ndk-v3-0-4305590795b2@arm.com/
> > The patches are in Linux v6.18-rc1. Perhaps you could try repeating
> > your build with this tree and reporting issues?
>
> I am not 100% sure, could you execute install kernel headers and then
> build perf ?
>
>   make headers_install
>

I am currently building perf for arm64 in an Ubuntu environment using
a cross toolchain, rather than compiling the entire perf directly with
Clang. Clang is only invoked during the build process when the BPF
option is enabled — as shown below where bpf is detected as on:

Auto-detecting system features:
...                                   libdw: [ on  ]
...                                   glibc: [ on  ]
...                                  libelf: [ on  ]
...                                 libnuma: [ on  ]
...                  numa_num_possible_cpus: [ on  ]
...                               libpython: [ OFF ]
...                             libcapstone: [ on  ]
...                               llvm-perf: [ OFF ]
...                                    zlib: [ on  ]
...                                    lzma: [ on  ]
...                               get_cpuid: [ OFF ]
...                                     bpf: [ on  ]
...                                  libaio: [ on  ]
...                                 libzstd: [ on  ]


I have already run the following command and successfully installed
the related headers, but the same error still occurs when building
perf. I am not sure if I also need to specify additional parameters
during the build process to ensure these headers can be found
correctly?

    make ARCH=arm64 headers_install

Please let me know if you need any more details or testing.


Thanks,
hupu

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-10-14  2:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-13  8:06 [RFC] perf build: Allow passing extra Clang flags via EXTRA_CLANG_FLAGS hupu
2025-10-13  8:07 ` hupu
2025-10-13 15:46   ` Ian Rogers
2025-10-13 16:14     ` Leo Yan
2025-10-14  2:31       ` hupu
2025-10-14  1:57     ` hupu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).