History

resolve_btfids: Fix linker flags detection

The "|| echo -lzstd" default makes zstd an unconditional link dependency of resolve_btfids. On systems where libzstd-dev is not installed and pkg-config fails, the linker fails: ld: cannot find -lzstd: No such file or directory libzstd is a transitive dependency of libelf, so the -lzstd flag is strictly necessary only for static builds [1]. Remove ZSTD_LIBS variable, and instead set LIBELF_LIBS depending on whether the build is static or not. Use $(HOSTPKG_CONFIG) as primary source of the flags list. Also add a default value for HOSTPKG_CONFIG in case it's not built via the toplevel Makefile. Pass it from selftests/bpf too. [1] https://lore.kernel.org/bpf/4ff82800-2daa-4b9f-95a9-6f512859ee70@linux.dev/

Ihor Solodrai committed 8 months ago

Commit b0dcdcb1 parent 325d1ba2 files · +8 −2Browse files

Unified · Split

2 files
  1. tools/bpf/resolve_btfids/Makefile +7 −2
  2. tools/testing/selftests/bpf/Makefile +1 −0
tools/bpf/resolve_btfids/Makefile+7−2
@@ -23,6 +23,7 @@ RM ?= rm · 22 unmodified lines
2323 HOSTCC ?= gcc
2424 HOSTLD ?= ld
2525 HOSTAR ?= ar
26+HOSTPKG_CONFIG ?= pkg-config
2627 CROSS_COMPILE =
2728
2829 OUTPUT ?= $(srctree)/tools/bpf/resolve_btfids/
@@ -63,10 +64,14 @@ $(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU · 34 unmodified lines
6364 $(abspath $@) install_headers
6465
6566 LIBELF_FLAGS := $(shell $(HOSTPKG_CONFIG) libelf --cflags 2>/dev/null)
67+
68+ifneq ($(filter -static,$(EXTRA_LDFLAGS)),)
69+LIBELF_LIBS := $(shell $(HOSTPKG_CONFIG) libelf --libs --static 2>/dev/null || echo -lelf -lzstd)
70+else
6671 LIBELF_LIBS := $(shell $(HOSTPKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
72+endif
6773
6874 ZLIB_LIBS := $(shell $(HOSTPKG_CONFIG) zlib --libs 2>/dev/null || echo -lz)
69−ZSTD_LIBS := $(shell $(HOSTPKG_CONFIG) libzstd --libs 2>/dev/null || echo -lzstd)
7075
7176 HOSTCFLAGS_resolve_btfids += -g \
7277 -I$(srctree)/tools/include \
@@ -76,7 +81,7 @@ HOSTCFLAGS_resolve_btfids += -g \ · 3 unmodified lines
7681 $(LIBELF_FLAGS) \
7782 -Wall -Werror
7883
79−LIBS = $(LIBELF_LIBS) $(ZLIB_LIBS) $(ZSTD_LIBS)
84+LIBS = $(LIBELF_LIBS) $(ZLIB_LIBS)
8085
8186 export srctree OUTPUT HOSTCFLAGS_resolve_btfids Q HOSTCC HOSTLD HOSTAR
8287 include $(srctree)/tools/build/Makefile.include
tools/testing/selftests/bpf/Makefile+1−0
@@ -409,6 +409,7 @@ $(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids \ · 408 unmodified lines
409409 CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \
410410 LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
411411 EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \
412+ HOSTPKG_CONFIG=$(PKG_CONFIG) \
412413 OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
413414
414415 # Get Clang's default includes on this system, as opposed to those seen by