Skip to content

Commit 40d0eb0

Browse files
committed
Merge branch 'selftests-bpf-use-pkg-config-to-determine-ld-flags'
Akihiko Odaki says: ==================== selftests/bpf: Use pkg-config to determine ld flags When linking statically, libraries may require other dependencies to be included to ld flags. In particular, libelf may require libzstd. Use pkg-config to determine such dependencies. V4 -> V5: Introduced variables LIBELF_CFLAGS and LIBELF_LIBS. (Daniel Borkmann) Added patch "selftests/bpf: Choose pkg-config for the target". V3 -> V4: Added "2> /dev/null". V2 -> V3: Added missing "echo". V1 -> V2: Implemented fallback, referring to HOSTPKG_CONFIG. ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents d4e7dd4 + 8998a47 commit 40d0eb0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ else
1818
GENDIR := $(abspath ../../../../include/generated)
1919
endif
2020
GENHDR := $(GENDIR)/autoconf.h
21-
HOSTPKG_CONFIG := pkg-config
21+
PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
2222

2323
ifneq ($(wildcard $(GENHDR)),)
2424
GENFLAGS := -DHAVE_GENHDR
@@ -29,13 +29,17 @@ SAN_CFLAGS ?=
2929
SAN_LDFLAGS ?= $(SAN_CFLAGS)
3030
RELEASE ?=
3131
OPT_FLAGS ?= $(if $(RELEASE),-O2,-O0)
32+
33+
LIBELF_CFLAGS := $(shell $(PKG_CONFIG) libelf --cflags 2>/dev/null)
34+
LIBELF_LIBS := $(shell $(PKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
35+
3236
CFLAGS += -g $(OPT_FLAGS) -rdynamic \
3337
-Wall -Werror \
34-
$(GENFLAGS) $(SAN_CFLAGS) \
38+
$(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS) \
3539
-I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
3640
-I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
3741
LDFLAGS += $(SAN_LDFLAGS)
38-
LDLIBS += -lelf -lz -lrt -lpthread
42+
LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread
3943

4044
ifneq ($(LLVM),)
4145
# Silence some warnings when compiled with clang
@@ -219,9 +223,9 @@ $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_r
219223

220224
$(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
221225
$(call msg,SIGN-FILE,,$@)
222-
$(Q)$(CC) $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null) \
226+
$(Q)$(CC) $(shell $(PKG_CONFIG) --cflags libcrypto 2> /dev/null) \
223227
$< -o $@ \
224-
$(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)
228+
$(shell $(PKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)
225229

226230
$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
227231
$(call msg,MOD,,$@)

tools/testing/selftests/bpf/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ In case of linker errors when running selftests, try using static linking:
7777

7878
.. code-block:: console
7979
80-
$ LDLIBS=-static vmtest.sh
80+
$ LDLIBS=-static PKG_CONFIG='pkg-config --static' vmtest.sh
8181
8282
.. note:: Some distros may not support static linking.
8383

0 commit comments

Comments
 (0)