Skip to contentSkip to conversation

perf: build: Fix cross compilation

PATCH Superseded Leo Yan 2 participants 12 messages Jun 10, 2024 – Jun 25, 2024 1 message not in the archive perf events

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

v2 cover perf: build: Fix cross compilation

This patch series fixes cross compilation issues.

The first patch sets the package path if the package configuration path is not specified. This helps the compiler to find the architecture's package in a Multiarch system.

The patch 02 sets the Python configuration path and renames the .so to

The patches 03, 04 and 05 fix the static build failures.

The patch 06 adds document for how to cross compile.

This patch series is tested for building perf on x86_64 host for Arm64 target.

Changes from v1:
- Kept the cross-compile-pkg-config if it is available. (Namhyung)
- Removed the patch 02 for fixing pkg-config path for libtraceevent, as this will be resolved in Guilherme Amadio's patch "perf build: Use pkg-config for feature check for libtrace{event,fs}".
- Added patch 06 for document.

Leo Yan (6):
perf: build: Setup PKG_CONFIG_LIBDIR for cross compilation
perf: build: Set Python configuration for cross compilation
perf: build: Only link libebl.a for old libdw
perf: build: Link lib 'lzma' for static build
perf: build: Link lib 'zstd' for static build perf docs: Document cross compilation

 tools/build/feature/Makefile       | 54 ++++++++++++++++++++++++------
 tools/perf/Documentation/Build.txt | 37 ++++++++++++++++++++
 tools/perf/Makefile.config         | 20 ++++++++++-
 tools/perf/Makefile.perf           | 26 +++++++++++++-
 4 files changed, 125 insertions(+), 12 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/6 perf: build: Setup PKG_CONFIG_LIBDIR for cross compilation

On recent Linux distros like Ubuntu Noble and Debian Bookworm, the 'pkg-config-aarch64-linux-gnu' package is missing. As a result, the aarch64-linux-gnu-pkg-config command is not available, which causes build failures.

When a build passes the environment variables PKG_CONFIG_LIBDIR or PKG_CONFIG_PATH, like a user uses make command or a build system (like Yocto, Buildroot, etc) prepares the variables and passes to the Perf's Makefile, the commit keeps these variables for package configuration. Otherwise, this commit sets the PKG_CONFIG_LIBDIR variable to use the Multiarch libs for the cross compilation.

---
 tools/build/feature/Makefile | 26 +++++++++++++++++++++++++-
 tools/perf/Makefile.perf     | 26 +++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 2 deletions(-)
tools/build/feature/Makefile +25 -1
index ed54cef450f5..084f803093c3 100644
@@ -82,7 +82,31 @@ FILES=                                          \
 
 FILES := $(addprefix $(OUTPUT),$(FILES))
 
-PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
+# Some distros provide the command $(CROSS_COMPILE)pkg-config for
+# searching packges installed with Multiarch. Use it for cross
+# compilation if it is existed.
+ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
+  PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
+else
+  PKG_CONFIG ?= pkg-config
+
+  # PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR is required for the cross
+  # compilation. If both is not set, try to set the lib paths installed
+  # by multiarch.
+  ifdef CROSS_COMPILE
+    ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH),)
+      CROSS_ARCH = $(shell $(CC) -dumpmachine)
+      PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/
+      export PKG_CONFIG_LIBDIR
+      $(warning Missing PKG_CONFIG_LIBDIR and PKG_CONFIG_PATH for cross compilation,)
+      $(warning set PKG_CONFIG_LIBDIR=$(PKG_CONFIG_LIBDIR) for building with Multiarch libs.)
+    endif
+  endif
+endif
 
 all: $(FILES)
 
tools/perf/Makefile.perf +25 -1
index 5c35c0d89306..2c9e89415e48 100644
@@ -193,7 +193,31 @@ HOSTLD  ?= ld
 HOSTAR  ?= ar
 CLANG   ?= clang
 
-PKG_CONFIG = $(CROSS_COMPILE)pkg-config
+# Some distros provide the command $(CROSS_COMPILE)pkg-config for
+# searching packges installed with Multiarch. Use it for cross
+# compilation if it is existed.
+ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
+  PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
+else
+  PKG_CONFIG ?= pkg-config
+
+  # PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR is required for the cross
+  # compilation. If both is not set, try to set the lib paths installed
+  # by multiarch.
+  ifdef CROSS_COMPILE
+    ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH),)
+      CROSS_ARCH = $(shell $(CC) -dumpmachine)
+      PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/
+      export PKG_CONFIG_LIBDIR
+      $(warning Missing PKG_CONFIG_LIBDIR and PKG_CONFIG_PATH for cross compilation,)
+      $(warning set PKG_CONFIG_LIBDIR to $(PKG_CONFIG_LIBDIR) for using libs with Multiarch.)
+    endif
+  endif
+endif
 
 RM      = rm -f
 LN      = ln -f
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v2 1/6] perf: build: Setup PKG_CONFIG_LIBDIR for cross compilation

Hello,

Quoted 30 lines · Leo Yan

On recent Linux distros like Ubuntu Noble and Debian Bookworm, the 'pkg-config-aarch64-linux-gnu' package is missing. As a result, the aarch64-linux-gnu-pkg-config command is not available, which causes build failures.

When a build passes the environment variables PKG_CONFIG_LIBDIR or PKG_CONFIG_PATH, like a user uses make command or a build system (like Yocto, Buildroot, etc) prepares the variables and passes to the Perf's Makefile, the commit keeps these variables for package configuration. Otherwise, this commit sets the PKG_CONFIG_LIBDIR variable to use the Multiarch libs for the cross compilation.

---
 tools/build/feature/Makefile | 26 +++++++++++++++++++++++++-
 tools/perf/Makefile.perf     | 26 +++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 2 deletions(-)
tools/build/feature/Makefile +3 -1
index ed54cef450f5..084f803093c3 100644
@@ -82,7 +82,31 @@ FILES=                                          \
 
 FILES := $(addprefix $(OUTPUT),$(FILES))
 
-PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
+# Some distros provide the command $(CROSS_COMPILE)pkg-config for
+# searching packges installed with Multiarch. Use it for cross
+# compilation if it is existed.

tools/build/feature/Makefile FILES= \ · line 97

Quoted 10 lines
tools/build/feature/Makefile +10 -0
+ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
+  PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
+else
+  PKG_CONFIG ?= pkg-config
+
+  # PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR is required for the cross
+  # compilation. If both is not set, try to set the lib paths installed
+  # by multiarch.
+  ifdef CROSS_COMPILE
+    ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH),)

Maybe you want to check PKG_CONFIG_SYSROOT_DIR too.

tools/build/feature/Makefile FILES= \ · line 103

Quoted 6 lines
tools/build/feature/Makefile +6 -0
+      CROSS_ARCH = $(shell $(CC) -dumpmachine)
+      PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/

I'm not sure why this ordering.. don't you want to check CROSS_ARCH directories first and then /usr/local/share and /usr/share directory?

Quoted 27 lines
tools/build/feature/Makefile +6 -0
+      export PKG_CONFIG_LIBDIR
+      $(warning Missing PKG_CONFIG_LIBDIR and PKG_CONFIG_PATH for cross compilation,)
+      $(warning set PKG_CONFIG_LIBDIR=$(PKG_CONFIG_LIBDIR) for building with Multiarch libs.)
+    endif
+  endif
+endif
 
 all: $(FILES)
 
tools/perf/Makefile.perf +9 -1
index 5c35c0d89306..2c9e89415e48 100644
@@ -193,7 +193,31 @@ HOSTLD  ?= ld
 HOSTAR  ?= ar
 CLANG   ?= clang
 
-PKG_CONFIG = $(CROSS_COMPILE)pkg-config
+# Some distros provide the command $(CROSS_COMPILE)pkg-config for
+# searching packges installed with Multiarch. Use it for cross
+# compilation if it is existed.
+ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
+  PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
+else
+  PKG_CONFIG ?= pkg-config
+
+  # PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR is required for the cross

tools/perf/Makefile.perf HOSTLD ?= ld · line 214

Quoted 10 lines
tools/perf/Makefile.perf +10 -0
+  # compilation. If both is not set, try to set the lib paths installed
+  # by multiarch.
+  ifdef CROSS_COMPILE
+    ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH),)
+      CROSS_ARCH = $(shell $(CC) -dumpmachine)
+      PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/

Ditto.

Thanks,
Namhyung

Quoted 12 lines
tools/perf/Makefile.perf +6 -0
+      export PKG_CONFIG_LIBDIR
+      $(warning Missing PKG_CONFIG_LIBDIR and PKG_CONFIG_PATH for cross compilation,)
+      $(warning set PKG_CONFIG_LIBDIR to $(PKG_CONFIG_LIBDIR) for using libs with Multiarch.)
+    endif
+  endif
+endif
 
 RM      = rm -f
 LN      = ln -f
Signature · 1 line
2.34.1
author
#3 lore raw

Re: [PATCH v2 1/6] perf: build: Setup PKG_CONFIG_LIBDIR for cross compilation

Hi Namhyung,

Quoted 30 lines · Namhyung Kim

Hello,

Quoted 26 lines · Leo Yan

On recent Linux distros like Ubuntu Noble and Debian Bookworm, the 'pkg-config-aarch64-linux-gnu' package is missing. As a result, the aarch64-linux-gnu-pkg-config command is not available, which causes build failures.

When a build passes the environment variables PKG_CONFIG_LIBDIR or PKG_CONFIG_PATH, like a user uses make command or a build system (like Yocto, Buildroot, etc) prepares the variables and passes to the Perf's Makefile, the commit keeps these variables for package configuration. Otherwise, this commit sets the PKG_CONFIG_LIBDIR variable to use the Multiarch libs for the cross compilation.

---
  tools/build/feature/Makefile | 26 +++++++++++++++++++++++++-
  tools/perf/Makefile.perf     | 26 +++++++++++++++++++++++++-
  2 files changed, 50 insertions(+), 2 deletions(-)
tools/build/feature/Makefile +0 -0
index ed54cef450f5..084f803093c3 100644
@@ -82,7 +82,31 @@ FILES=                                          \
 
  FILES := $(addprefix $(OUTPUT),$(FILES))
 
Quoted 16 lines
Quoted 4 lines
tools/build/feature/Makefile +3 -1
-PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
+# Some distros provide the command $(CROSS_COMPILE)pkg-config for
+# searching packges installed with Multiarch. Use it for cross
+# compilation if it is existed.

tools/build/feature/Makefile FILES= \ · line 97

Quoted 10 lines
tools/build/feature/Makefile +10 -0
+ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
+  PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
+else
+  PKG_CONFIG ?= pkg-config
+
+  # PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR is required for the cross
+  # compilation. If both is not set, try to set the lib paths installed
+  # by multiarch.
+  ifdef CROSS_COMPILE
+    ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH),)

Maybe you want to check PKG_CONFIG_SYSROOT_DIR too.

IIRC, the manual says PKG_CONFIG_SYSROOT_DIR is used alongside PKG_CONFIG_LIBDIR or PKG_CONFIG_PATH for prepending prefix for the package paths.

I can add checking PKG_CONFIG_SYSROOT_DIR in the new patch set.

Quoted 10 lines

tools/build/feature/Makefile FILES= \ · line 103

Quoted 6 lines
tools/build/feature/Makefile +6 -0
+      CROSS_ARCH = $(shell $(CC) -dumpmachine)
+      PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
+      PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/

I'm not sure why this ordering.. don't you want to check CROSS_ARCH directories first and then /usr/local/share and /usr/share directory?

I mainly refered to the script [1] which is an obselete package ('aarch64-linux-gnu-pkg-config') on old Debian/Ubuntu distros.

Your suggestion makes sense to me. I will change order as:

PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/ PKG_CONFIG_LIBDIR :=
$(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
PKG_CONFIG_LIBDIR :=
$(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/ PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/

Thanks,
Leo

[1] https://gist.github.com/doug65536/ea9c52f9a65a655a2fd5cc4997e8443b

author
#4 lore raw

PATCH v2 2/6 perf: build: Set Python configuration for cross compilation

In reply to Leo Yan

Python configuration has dedicated folders for different architectures. For example, Python 3.11 has two folders as shown below, one for Arm64 and another for x86_64:

/usr/lib/python3.11/config-3.11-aarch64-linux-gnu/ /usr/lib/python3.11/config-3.11-x86_64-linux-gnu/

This commit updates the Python configuration path based on the compiler's machine type, guiding the compiler to find the correct path for Python libraries. It also renames the generated .so file name to match the machine name.

---
 tools/perf/Makefile.config | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/perf/Makefile.config +8 -0
index 7f1e016a9253..755fb78be76a 100644
@@ -303,6 +303,11 @@ endif
 
 ifdef PYTHON_CONFIG
   PYTHON_EMBED_LDOPTS := $(shell $(PYTHON_CONFIG_SQ) $(PYTHON_CONFIG_LDFLAGS) 2>/dev/null)
+  # Update the python flags for cross compilation
+  ifdef CROSS_COMPILE
+    PYTHON_NATIVE := $(shell echo $(PYTHON_EMBED_LDOPTS) | sed 's/\(-L.*\/\)\(.*-linux-gnu\).*/\2/')
+    PYTHON_EMBED_LDOPTS := $(subst $(PYTHON_NATIVE),$(shell $(CC) -dumpmachine),$(PYTHON_EMBED_LDOPTS))
+  endif
   PYTHON_EMBED_LDFLAGS := $(call strip-libs,$(PYTHON_EMBED_LDOPTS))
   PYTHON_EMBED_LIBADD := $(call grep-libs,$(PYTHON_EMBED_LDOPTS)) -lutil
   PYTHON_EMBED_CCOPTS := $(shell $(PYTHON_CONFIG_SQ) --includes 2>/dev/null)
@@ -904,6 +909,9 @@ else
          PYTHON_SETUPTOOLS_INSTALLED := $(shell $(PYTHON) -c 'import setuptools;' 2> /dev/null && echo "yes" || echo "no")
          ifeq ($(PYTHON_SETUPTOOLS_INSTALLED), yes)
            PYTHON_EXTENSION_SUFFIX := $(shell $(PYTHON) -c 'from importlib import machinery; print(machinery.EXTENSION_SUFFIXES[0])')
+           ifdef CROSS_COMPILE
+             PYTHON_EXTENSION_SUFFIX := $(subst $(PYTHON_NATIVE),$(shell $(CC) -dumpmachine),$(PYTHON_EXTENSION_SUFFIX))
+           endif
            LANG_BINDINGS += $(obj-perf)python/perf$(PYTHON_EXTENSION_SUFFIX)
 	 else
            $(warning Missing python setuptools, the python binding won't be built, please install python3-setuptools or equivalent)
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v2 3/6 perf: build: Only link libebl.a for old libdw

In reply to Leo Yan

Since libdw version 0.177, elfutils has merged libebl.a into libdw (see the commit "libebl: Don't install libebl.a, libebl.h and remove backends from spec." in the elfutils repository).

As a result, libebl.a does not exist on Debian Bullseye and newer releases, causing static perf builds to fail on these distributions.

This commit checks the libdw version and only links libebl.a if it detects that the libdw version is older than 0.177.

---
 tools/build/feature/Makefile | 12 +++++++++++-
 tools/perf/Makefile.config   | 12 +++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)
tools/build/feature/Makefile +11 -1
index 084f803093c3..b23b3e8ad5e4 100644
@@ -171,7 +171,17 @@ $(OUTPUT)test-libopencsd.bin:
 
 DWARFLIBS := -ldw
 ifeq ($(findstring -static,${LDFLAGS}),-static)
-DWARFLIBS += -lelf -lebl -lz -llzma -lbz2
+  DWARFLIBS += -lelf -lz -llzma -lbz2
+
+  LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
+  LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
+  LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION)))
+
+  # Elfutils merged libebl.a into libdw.a starting from version 0.177,
+  # Link libebl.a only if libdw is older than this version.
+  ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0)
+    DWARFLIBS += -lebl
+  endif
 endif
 
 $(OUTPUT)test-dwarf.bin:
tools/perf/Makefile.config +11 -1
index 755fb78be76a..db3bc460d4c2 100644
@@ -152,7 +152,17 @@ ifdef LIBDW_DIR
 endif
 DWARFLIBS := -ldw
 ifeq ($(findstring -static,${LDFLAGS}),-static)
-  DWARFLIBS += -lelf -lebl -ldl -lz -llzma -lbz2
+  DWARFLIBS += -lelf -ldl -lz -llzma -lbz2
+
+  LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
+  LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
+  LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION)))
+
+  # Elfutils merged libebl.a into libdw.a starting from version 0.177,
+  # Link libebl.a only if libdw is older than this version.
+  ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0)
+    DWARFLIBS += -lebl
+  endif
 endif
 FEATURE_CHECK_CFLAGS-libdw-dwarf-unwind := $(LIBDW_CFLAGS)
 FEATURE_CHECK_LDFLAGS-libdw-dwarf-unwind := $(LIBDW_LDFLAGS) $(DWARFLIBS)
Signature · 1 line
2.34.1
maintainer
#6 lore raw

Re: [PATCH v2 3/6] perf: build: Only link libebl.a for old libdw

Quoted 6 lines · Leo Yan

Since libdw version 0.177, elfutils has merged libebl.a into libdw (see the commit "libebl: Don't install libebl.a, libebl.h and remove backends from spec." in the elfutils repository).

As a result, libebl.a does not exist on Debian Bullseye and newer releases, causing static perf builds to fail on these distributions.

What about libebl.so? I'm curious why it's ok with dynamic build and causing a problem with static builds.

Quoted 3 lines

This commit checks the libdw version and only links libebl.a if it detects that the libdw version is older than 0.177.

Have you tested on the older versions too?

Quoted 16 lines
---
 tools/build/feature/Makefile | 12 +++++++++++-
 tools/perf/Makefile.config   | 12 +++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)
tools/build/feature/Makefile +0 -1
index 084f803093c3..b23b3e8ad5e4 100644
@@ -171,7 +171,17 @@ $(OUTPUT)test-libopencsd.bin:
 
 DWARFLIBS := -ldw
 ifeq ($(findstring -static,${LDFLAGS}),-static)
-DWARFLIBS += -lelf -lebl -lz -llzma -lbz2

tools/build/feature/Makefile $(OUTPUT)test-libopencsd.bin: · line 183

Quoted 10 lines
tools/build/feature/Makefile +10 -0
+  DWARFLIBS += -lelf -lz -llzma -lbz2
+
+  LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
+  LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
+  LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION)))
+
+  # Elfutils merged libebl.a into libdw.a starting from version 0.177,
+  # Link libebl.a only if libdw is older than this version.
+  ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0)
+    DWARFLIBS += -lebl

I'm not sure if it's ok to change the order as libebl might depend on later libraries like libz.

Thanks,
Namhyung

Quoted 30 lines
tools/build/feature/Makefile +1 -0
+  endif
 endif
 
 $(OUTPUT)test-dwarf.bin:
tools/perf/Makefile.config +11 -1
index 755fb78be76a..db3bc460d4c2 100644
@@ -152,7 +152,17 @@ ifdef LIBDW_DIR
 endif
 DWARFLIBS := -ldw
 ifeq ($(findstring -static,${LDFLAGS}),-static)
-  DWARFLIBS += -lelf -lebl -ldl -lz -llzma -lbz2
+  DWARFLIBS += -lelf -ldl -lz -llzma -lbz2
+
+  LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
+  LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
+  LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION)))
+
+  # Elfutils merged libebl.a into libdw.a starting from version 0.177,
+  # Link libebl.a only if libdw is older than this version.
+  ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0)
+    DWARFLIBS += -lebl
+  endif
 endif
 FEATURE_CHECK_CFLAGS-libdw-dwarf-unwind := $(LIBDW_CFLAGS)
 FEATURE_CHECK_LDFLAGS-libdw-dwarf-unwind := $(LIBDW_LDFLAGS) $(DWARFLIBS)
Signature · 1 line
2.34.1
author
#7 lore raw

Re: [PATCH v2 3/6] perf: build: Only link libebl.a for old libdw

Quoted 11 lines · Namhyung Kim
Quoted 6 lines · Leo Yan

Since libdw version 0.177, elfutils has merged libebl.a into libdw (see the commit "libebl: Don't install libebl.a, libebl.h and remove backends from spec." in the elfutils repository).

As a result, libebl.a does not exist on Debian Bullseye and newer releases, causing static perf builds to fail on these distributions.

What about libebl.so? I'm curious why it's ok with dynamic build and causing a problem with static builds.

For the new Debian / Ubuntu distros, libebl has been merged in libdw so merged into libdw, so libebl.so doesn't exist.

'-lebl' is only included only for static building (see below code piece, it detects '-static' in ${LDFLAGS}). This is why dynamic build has no issue.

Quoted 4 lines
Quoted 2 lines

This commit checks the libdw version and only links libebl.a if it detects that the libdw version is older than 0.177.

Have you tested on the older versions too?

IIRC, I did test on the Debian buster (which contains the libdw version is 0.176).

Quoted 12 lines
Quoted 12 lines
---
  tools/build/feature/Makefile | 12 +++++++++++-
  tools/perf/Makefile.config   | 12 +++++++++++-
  2 files changed, 22 insertions(+), 2 deletions(-)
tools/build/feature/Makefile +0 -0
index 084f803093c3..b23b3e8ad5e4 100644
@@ -171,7 +171,17 @@ $(OUTPUT)test-libopencsd.bin:
 
Quoted 16 lines

tools/build/feature/Makefile $(OUTPUT)test-libopencsd.bin: · line 183

Quoted 13 lines
tools/build/feature/Makefile +10 -1
  DWARFLIBS := -ldw
  ifeq ($(findstring -static,${LDFLAGS}),-static)
-DWARFLIBS += -lelf -lebl -lz -llzma -lbz2
+  DWARFLIBS += -lelf -lz -llzma -lbz2
+
+  LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
+  LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
+  LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION)))
+
+  # Elfutils merged libebl.a into libdw.a starting from version 0.177,
+  # Link libebl.a only if libdw is older than this version.
+  ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0)
+    DWARFLIBS += -lebl

I'm not sure if it's ok to change the order as libebl might depend on later libraries like libz.

I confirmed that this change works on Debian Buster (Debian 10) and Bookworm (Debian 12).

Thanks,
Leo

author
#8 lore raw

PATCH v2 4/6 perf: build: Link lib 'lzma' for static build

In reply to Leo Yan

The libunwind feature test failed with the static linkage. This is due to the 'lzma' lib is missed, so link it to dismiss building failure.

---
 tools/build/feature/Makefile | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/build/feature/Makefile +8 -8
index b23b3e8ad5e4..9e5663eefc01 100644
@@ -212,27 +212,27 @@ $(OUTPUT)test-numa_num_possible_cpus.bin:
 	$(BUILD) -lnuma
 
 $(OUTPUT)test-libunwind.bin:
-	$(BUILD) -lelf
+	$(BUILD) -lelf -llzma
 
 $(OUTPUT)test-libunwind-debug-frame.bin:
-	$(BUILD) -lelf
+	$(BUILD) -lelf -llzma
 $(OUTPUT)test-libunwind-x86.bin:
-	$(BUILD) -lelf -lunwind-x86
+	$(BUILD) -lelf -llzma -lunwind-x86
 
 $(OUTPUT)test-libunwind-x86_64.bin:
-	$(BUILD) -lelf -lunwind-x86_64
+	$(BUILD) -lelf -llzma -lunwind-x86_64
 
 $(OUTPUT)test-libunwind-arm.bin:
-	$(BUILD) -lelf -lunwind-arm
+	$(BUILD) -lelf -llzma -lunwind-arm
 
 $(OUTPUT)test-libunwind-aarch64.bin:
-	$(BUILD) -lelf -lunwind-aarch64
+	$(BUILD) -lelf -llzma -lunwind-aarch64
 
 $(OUTPUT)test-libunwind-debug-frame-arm.bin:
-	$(BUILD) -lelf -lunwind-arm
+	$(BUILD) -lelf -llzma -lunwind-arm
 
 $(OUTPUT)test-libunwind-debug-frame-aarch64.bin:
-	$(BUILD) -lelf -lunwind-aarch64
+	$(BUILD) -lelf -llzma -lunwind-aarch64
 
 $(OUTPUT)test-libaudit.bin:
 	$(BUILD) -laudit
Signature · 1 line
2.34.1
author
#9 lore raw

PATCH v2 5/6 perf: build: Link lib 'zstd' for static build

In reply to Leo Yan

When build static perf, Makefile reports the error:

Makefile.config:480: No libdw DWARF unwind found, Please install elfutils-devel/libdw-dev >= 0.158 and/or set LIBDW_DIR

The libdw has been installed on the system, but the build system fails to build the feature detecting binary 'test-libdw-dwarf-unwind'. The failure is caused by missing to link the lib 'zstd'.

Link lib 'zstd' for the static build, in the end, the dwarf feature can be enabled in the static perf.

---
 tools/build/feature/Makefile | 2 +-
 tools/perf/Makefile.config   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/build/feature/Makefile +1 -1
index 9e5663eefc01..991bb4fed774 100644
@@ -171,7 +171,7 @@ $(OUTPUT)test-libopencsd.bin:
 
 DWARFLIBS := -ldw
 ifeq ($(findstring -static,${LDFLAGS}),-static)
-  DWARFLIBS += -lelf -lz -llzma -lbz2
+  DWARFLIBS += -lelf -lz -llzma -lbz2 -lzstd
 
   LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
   LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
tools/perf/Makefile.config +1 -1
index db3bc460d4c2..0f918475d7b6 100644
@@ -152,7 +152,7 @@ ifdef LIBDW_DIR
 endif
 DWARFLIBS := -ldw
 ifeq ($(findstring -static,${LDFLAGS}),-static)
-  DWARFLIBS += -lelf -ldl -lz -llzma -lbz2
+  DWARFLIBS += -lelf -ldl -lz -llzma -lbz2 -lzstd
 
   LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
   LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH v2 6/6 perf docs: Document cross compilation

In reply to Leo Yan

Records the commands for cross compilation with two methods.

The first method relies on Multiarch. The second approach is to explicitly specify the PKG_CONFIG variables, which is widely used in build system (like Buildroot, Yocto, etc).

---
 tools/perf/Documentation/Build.txt | 37 ++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
tools/perf/Documentation/Build.txt +37 -0
index 3766886c4bca..2237ceee74ba 100644
@@ -71,3 +71,40 @@ supported by GCC. UBSan detects undefined behaviors of programs at runtime.
   $ UBSAN_OPTIONS=print_stacktrace=1 ./perf record -a
 
 If UBSan detects any problem at runtime, it outputs a “runtime error:” message.
+
+4) Cross compilation
+====================
+The perf tool can be cross compiled in below methods.
+
+As Multiarch is commonly supported in Linux distributions, we can install
+libraries for multiple architectures on the same system and then cross-compile
+Linux perf. For example, Aarch64 libraries and toolchains can be installed on
+an x86_64 machine, allowing us to compile perf for an Aarch64 target.
+
+Below is the command for building perf with dynamic linking:
+As Multiarch is commonly supported in Linux distros, therefore, we can
+install multiple architectures libs in the same system and then cross compile
+the Linux perf. For example, the Aarch64 libraries and toolchain can be
+installed on the x86_64 machine, based on it we can compile the perf for
+Aarch64 target.
+
+Below is the command for building the perf with dynamic linking.
+
+  $ cd /path/to/Linux
+  $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf
+
+For static linking, the option `LDFLAGS="-static"` is required.
+
+  $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \
+    LDFLAGS="-static" -C tools/perf
+
+In the embedded system world, a use case is to explicitly specify the package
+configuration paths for cross building:
+
+  $ PKG_CONFIG_SYSROOT_DIR="/path/to/cross/build/sysroot" \
+    PKG_CONFIG_LIBDIR="/usr/lib/:/usr/local/lib" \
+    make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf
+
+In this case, the variable PKG_CONFIG_SYSROOT_DIR can be used alongside the
+variable PKG_CONFIG_LIBDIR or PKG_CONFIG_PATH to prepend the sysroot path to
+the library paths for cross compilation.
Signature · 1 line
2.34.1

A message that is not in the archive (sent off-list or not yet received): CAP-5=fVqJWGWbrddyqAhUp6afG3AX61ekNr3Mpe6u=wYaeeCcg@mail.gmail.com

author
#12 lore raw

Re: [PATCH v2 6/6] perf docs: Document cross compilation

Quoted 9 lines · Ian Rogers

Records the commands for cross compilation with two methods.

The first method relies on Multiarch. The second approach is to explicitly
specify the PKG_CONFIG variables, which is widely used in build system (like Buildroot, Yocto, etc).

Quoted 5 lines

There is also:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/Documentation/android.txt?h=perf-tools-next
it looks very crufty, not least as it is referring to 32-bit builds

Could this be refreshed or deleted?

Yeah, the doc is quite old.

Actually, this patch series is to support static building, I assume a main usage case is to use the static building binary for Android.

I will give a try for building perf with Android NDK. I will base on my test result to update the file android.txt or remove it.

Thanks
Leo