From a613510bc023dfbf838a2127c94644a56252efcd Mon Sep 17 00:00:00 2001
From: John Crispin <john@phrozen.org>
Date: Mon, 11 Jul 2022 13:49:14 +0200
Subject: [PATCH] bpftools: update to latest HEAD

Signed-off-by: John Crispin <john@phrozen.org>
---
 ...pc-fix-EDEADLOCK-redefinition-errors.patch | 56 ------------
 package/{network => }/utils/bpftools/Makefile | 86 ++++++++-----------
 .../utils/bpftools/patches/001-cflags.patch   | 10 +++
 .../utils/bpftools/patches/002-includes.patch | 26 ++++++
 4 files changed, 70 insertions(+), 108 deletions(-)
 delete mode 100644 package/network/utils/bpftools/patches/005-tools-arch-powerpc-fix-EDEADLOCK-redefinition-errors.patch
 rename package/{network => }/utils/bpftools/Makefile (60%)
 create mode 100644 package/utils/bpftools/patches/001-cflags.patch
 create mode 100644 package/utils/bpftools/patches/002-includes.patch

diff --git a/package/network/utils/bpftools/patches/005-tools-arch-powerpc-fix-EDEADLOCK-redefinition-errors.patch b/package/network/utils/bpftools/patches/005-tools-arch-powerpc-fix-EDEADLOCK-redefinition-errors.patch
deleted file mode 100644
index 66c00a325f..0000000000
--- a/package/network/utils/bpftools/patches/005-tools-arch-powerpc-fix-EDEADLOCK-redefinition-errors.patch
+++ /dev/null
@@ -1,56 +0,0 @@
-From afe3f4c765b17ced23811fe652c7f7adf7a0c0cf Mon Sep 17 00:00:00 2001
-From: Tony Ambardar <Tony.Ambardar@gmail.com>
-Date: Mon, 14 Sep 2020 23:05:26 -0700
-Subject: [PATCH] tools/arch/powerpc: fix EDEADLOCK redefinition errors in
- errno.h
-
-A few archs like powerpc have different errno.h values for macros
-EDEADLOCK and EDEADLK. In code including both libc and linux versions of
-errno.h, this can result in multiple definitions of EDEADLOCK in the
-include chain. Definitions to the same value (e.g. seen with mips) do
-not raise warnings, but on powerpc there are redefinitions changing the
-value, which raise warnings and errors (with "-Werror").
-
-Guard against these redefinitions to avoid build errors like the following,
-first seen cross-compiling libbpf v5.8.9 for powerpc using GCC 8.4.0 with
-musl 1.1.24:
-
-  In file included from ../../arch/powerpc/include/uapi/asm/errno.h:5,
-                   from ../../include/linux/err.h:8,
-                   from libbpf.c:29:
-  ../../include/uapi/asm-generic/errno.h:40: error: "EDEADLOCK" redefined [-Werror]
-   #define EDEADLOCK EDEADLK
-
-  In file included from toolchain-powerpc_8540_gcc-8.4.0_musl/include/errno.h:10,
-                   from libbpf.c:26:
-  toolchain-powerpc_8540_gcc-8.4.0_musl/include/bits/errno.h:58: note: this is the location of the previous definition
-   #define EDEADLOCK       58
-
-  cc1: all warnings being treated as errors
-  make[5]: *** [target-powerpc_8540_musl/bpftools-5.8.9/tools/build/Makefile.build:97: /home/kodidev/openwrt-project/build_dir/target-powerpc_8540_musl/bpftools-minimal/bpftools-5.8.9//libbpf/staticobjs/libbpf.o] Error 1
-
-Fixes: 95f28190aa01 ("tools include arch: Grab a copy of errno.h for arch's
-                      supported by perf")
-Fixes: c3617f72036c ("UAPI: (Scripted) Disintegrate arch/powerpc/include/asm")
-
-Reported-by: Rosen Penev <rosenp@gmail.com>
-Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
----
- tools/arch/powerpc/include/uapi/asm/errno.h | 1 +
- 1 file changed, 1 insertion(+)
-
-diff --git a/tools/arch/powerpc/include/uapi/asm/errno.h b/tools/arch/powerpc/include/uapi/asm/errno.h
-index cc79856896a1..4ba87de32be0 100644
---- a/tools/arch/powerpc/include/uapi/asm/errno.h
-+++ b/tools/arch/powerpc/include/uapi/asm/errno.h
-@@ -2,6 +2,7 @@
- #ifndef _ASM_POWERPC_ERRNO_H
- #define _ASM_POWERPC_ERRNO_H
- 
-+#undef	EDEADLOCK
- #include <asm-generic/errno.h>
- 
- #undef	EDEADLOCK
--- 
-2.25.1
-
diff --git a/package/network/utils/bpftools/Makefile b/package/utils/bpftools/Makefile
similarity index 60%
rename from package/network/utils/bpftools/Makefile
rename to package/utils/bpftools/Makefile
index 82eb34e2dc..56422e7902 100644
--- a/package/network/utils/bpftools/Makefile
+++ b/package/utils/bpftools/Makefile
@@ -8,12 +8,14 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=bpftools
-PKG_VERSION:=5.10.10
-PKG_RELEASE:=2
+PKG_RELEASE:=1
 
-PKG_SOURCE:=linux-$(PKG_VERSION).tar.xz
-PKG_SOURCE_URL:=@KERNEL/linux/kernel/v5.x
-PKG_HASH:=60ed866fa951522a5255ea37ec3ac2006d3f3427d4783a13ef478464f37cdb19
+PKG_SOURCE_URL:=https://github.com/libbpf/bpftool
+PKG_SOURCE_PROTO:=git
+PKG_SOURCE_DATE:=2022-03-08
+PKG_SOURCE_VERSION:=04c465fd1f561f67796dc68bbfe1aa7cfa956c3c
+PKG_MIRROR_HASH:=e22a954cd186f43228a96586bbdc120b11e6c87360ab88ae96ba37afb9c7cb58
+PKG_ABI_VERSION:=$(call abi_version_str,$(PKG_SOURCE_DATE))
 
 PKG_MAINTAINER:=Tony Ambardar <itugrok@yahoo.com>
 
@@ -21,16 +23,6 @@ PKG_USE_MIPS16:=0
 PKG_BUILD_PARALLEL:=1
 PKG_INSTALL:=1
 
-LINUX_VERSION:=$(PKG_VERSION)
-LINUX_TLD:=linux-$(LINUX_VERSION)
-
-BPF_FILES:= \
-	kernel/bpf scripts tools/Makefile tools/bpf tools/perf/perf-sys.h \
-	tools/arch tools/build tools/include tools/lib tools/scripts
-TAR_OPTIONS+= \
-	--transform="s;$(LINUX_TLD)/;$(PKG_NAME)-$(PKG_VERSION)/;" \
-	$(addprefix $(LINUX_TLD)/,$(BPF_FILES))
-
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/nls.mk
 
@@ -77,7 +69,7 @@ define Package/libbpf
   TITLE:=libbpf - eBPF helper library
   VARIANT:=lib
   LICENSE:=LGPL-2.1 OR BSD-2-Clause
-  ABI_VERSION:=0
+  ABI_VERSION:=$(PKG_ABI_VERSION)
   URL:=http://www.kernel.org
   DEPENDS:=+libelf
 endef
@@ -93,53 +85,43 @@ ifneq ($(BUILD_VARIANT),lib)
   TARGET_LDFLAGS += -Wl,--gc-sections
 endif
 
-MAKE_FLAGS += \
+ifeq ($(BUILD_VARIANT),full)
+  full:=1
+else
+  full:=0
+endif
+
+MAKE_VARS = \
 	EXTRA_CFLAGS="$(TARGET_CFLAGS) $(TARGET_CPPFLAGS)" \
-	LDFLAGS="$(TARGET_LDFLAGS)" \
-	BPFTOOL_VERSION="$(LINUX_VERSION)" \
-	FEATURES_DUMP="$(PKG_BUILD_DIR)/FEATURE-DUMP.openwrt" \
+	LDFLAGS="$(TARGET_LDFLAGS)"
+
+MAKE_FLAGS += \
 	OUTPUT="$(PKG_BUILD_DIR)/" \
 	prefix="/usr" \
-	$(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='')
+	$(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='') \
+	LIBSUBDIR=lib \
+	check_feat=0 \
+	feature-clang-bpf-co-re=0 \
+	feature-reallocarray=1 \
+	feature-zlib=1 \
+	feature-libbfd=$(full) \
+	feature-libcap=0 \
+	feature-disassembler-four-args=$(full)
 
-ifeq ($(BUILD_VARIANT),full)
-  HAVE_LIBBFD:=1
-  HAVE_LIBCAP:=0
-  HAVE_CLANG:=0
-  MAKE_PATH:=tools/bpf/bpftool
-else ifeq ($(BUILD_VARIANT),minimal)
-  HAVE_LIBBFD:=0
-  HAVE_LIBCAP:=0
-  HAVE_CLANG:=0
-  MAKE_PATH:=tools/bpf/bpftool
-else ifeq ($(BUILD_VARIANT),lib)
-  HAVE_LIBBFD:=0
-  HAVE_LIBCAP:=0
-  HAVE_CLANG:=0
-  MAKE_PATH:=tools/lib/bpf
+ifeq ($(BUILD_VARIANT),lib)
+  MAKE_PATH = libbpf/src
+else
+  MAKE_PATH = src
 endif
 
-# Perform a "throw-away" make to create a FEATURE-DUMP.* file to edit later.
-# The "//" in the make target is actually needed, very unPOSIXly.
-define Build/Configure
-	+$(MAKE_VARS) $(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR)/tools/bpf/bpftool \
-		$(MAKE_FLAGS) FEATURES_DUMP= $(PKG_BUILD_DIR)//libbpf/libbpf.a
-	(cd $(PKG_BUILD_DIR); cat FEATURE-DUMP.bpftool libbpf/FEATURE-DUMP.libbpf \
-		| sort | uniq > FEATURE-DUMP.openwrt)
-	$(SED) 's/feature-libbfd=1/feature-libbfd=$(HAVE_LIBBFD)/' \
-		-e 's/feature-libcap=1/feature-libcap=$(HAVE_LIBCAP)/' \
-		-e 's/feature-clang-bpf-global-var=1/feature-clang-bpf-global-var=$(HAVE_CLANG)/' \
-		$(PKG_BUILD_DIR)/FEATURE-DUMP.openwrt
-endef
-
 define Build/InstallDev/libbpf
 	$(INSTALL_DIR) $(1)/usr/include/bpf
 	$(CP) $(PKG_INSTALL_DIR)/usr/include/bpf/*.h $(1)/usr/include/bpf/
 	$(INSTALL_DIR) $(1)/usr/lib
-	$(CP) $(PKG_INSTALL_DIR)/usr/lib$(LIB_SUFFIX)/libbpf.{a,so*} \
+	$(CP) $(PKG_INSTALL_DIR)/usr/lib/libbpf.{a,so*} \
 		$(1)/usr/lib/
 	$(INSTALL_DIR) $(1)/usr/lib/pkgconfig
-	$(CP) $(PKG_INSTALL_DIR)/usr/lib$(LIB_SUFFIX)/pkgconfig/libbpf.pc \
+	$(CP) $(PKG_INSTALL_DIR)/usr/lib/pkgconfig/libbpf.pc \
 		$(1)/usr/lib/pkgconfig/
 	$(SED) 's,/usr/include,$$$${prefix}/include,g' \
 		$(1)/usr/lib/pkgconfig/libbpf.pc
@@ -159,7 +141,7 @@ endef
 
 define Package/libbpf/install
 	$(INSTALL_DIR) $(1)/usr/lib
-	$(CP) $(PKG_INSTALL_DIR)/usr/lib$(LIB_SUFFIX)/libbpf.so.* $(1)/usr/lib/
+	$(CP) $(PKG_INSTALL_DIR)/usr/lib/libbpf.so.* $(1)/usr/lib/
 endef
 
 $(eval $(call BuildPackage,libbpf))
diff --git a/package/utils/bpftools/patches/001-cflags.patch b/package/utils/bpftools/patches/001-cflags.patch
new file mode 100644
index 0000000000..48617e302b
--- /dev/null
+++ b/package/utils/bpftools/patches/001-cflags.patch
@@ -0,0 +1,10 @@
+--- a/libbpf/src/Makefile
++++ b/libbpf/src/Makefile
+@@ -25,6 +25,7 @@ ALL_CFLAGS := $(INCLUDES)
+ 
+ SHARED_CFLAGS += -fPIC -fvisibility=hidden -DSHARED
+ 
++CFLAGS = $(EXTRA_CFLAGS)
+ CFLAGS ?= -g -O2 -Werror -Wall -std=gnu89
+ ALL_CFLAGS += $(CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
+ ALL_LDFLAGS += $(LDFLAGS)
diff --git a/package/utils/bpftools/patches/002-includes.patch b/package/utils/bpftools/patches/002-includes.patch
new file mode 100644
index 0000000000..589d71c31e
--- /dev/null
+++ b/package/utils/bpftools/patches/002-includes.patch
@@ -0,0 +1,26 @@
+--- a/libbpf/include/linux/list.h
++++ b/libbpf/include/linux/list.h
+@@ -3,6 +3,8 @@
+ #ifndef __LINUX_LIST_H
+ #define __LINUX_LIST_H
+ 
++#include <linux/types.h>
++
+ #define LIST_HEAD_INIT(name) { &(name), &(name) }
+ #define LIST_HEAD(name) \
+         struct list_head name = LIST_HEAD_INIT(name)
+--- a/src/Makefile
++++ b/src/Makefile
+@@ -73,10 +73,10 @@ CFLAGS += -W -Wall -Wextra -Wno-unused-p
+ CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
+ CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
+ 	-I$(if $(OUTPUT),$(OUTPUT),.) \
+-	-I$(LIBBPF_INCLUDE) \
+ 	-I$(srctree)/src/kernel/bpf/ \
+ 	-I$(srctree)/include \
+-	-I$(srctree)/include/uapi
++	-I$(srctree)/include/uapi \
++	-I$(LIBBPF_INCLUDE)
+ ifneq ($(BPFTOOL_VERSION),)
+ CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
+ endif
-- 
2.25.1

