bpftools: add utility and library packages supporting eBPF usage
[openwrt/openwrt.git] / package / network / utils / bpftools / Makefile
1 #
2 # Copyright (C) 2020 Tony Ambardar <itugrok@yahoo.com>
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 include $(TOPDIR)/rules.mk
9
10 PKG_NAME:=bpftools
11 PKG_VERSION:=5.8.3
12 PKG_RELEASE:=1
13
14 PKG_SOURCE:=linux-$(PKG_VERSION).tar.xz
15 PKG_SOURCE_URL:=@KERNEL/linux/kernel/v5.x
16 PKG_HASH:=8ea27e1ed38981f772cc6d5f16ab8dbb8052c3800ba0494bd798fbb95dcc1779
17
18 PKG_MAINTAINER:=Tony Ambardar <itugrok@yahoo.com>
19
20 PKG_USE_MIPS16:=0
21 PKG_BUILD_PARALLEL:=1
22 PKG_INSTALL:=1
23
24 LINUX_VERSION:=$(PKG_VERSION)
25 LINUX_TLD:=linux-$(LINUX_VERSION)
26
27 BPF_FILES:= \
28 kernel/bpf scripts tools/Makefile tools/bpf tools/perf/perf-sys.h \
29 tools/arch tools/build tools/include tools/lib tools/scripts
30 TAR_OPTIONS+= \
31 --transform="s;$(LINUX_TLD)/;$(PKG_NAME)-$(PKG_VERSION)/;" \
32 $(addprefix $(LINUX_TLD)/,$(BPF_FILES))
33
34 include $(INCLUDE_DIR)/package.mk
35
36 define Package/bpftool/Default
37 SECTION:=net
38 CATEGORY:=Network
39 TITLE:=bpftool - eBPF subsystem utility
40 LICENSE:=GPL-2.0-only OR BSD-2-Clause
41 URL:=http://www.kernel.org
42 DEPENDS:=+libelf
43 endef
44
45 define Package/bpftool-minimal
46 $(call Package/bpftool/Default)
47 TITLE+= (Minimal)
48 VARIANT:=minimal
49 DEFAULT_VARIANT:=1
50 PROVIDES:=bpftool
51 ALTERNATIVES:=200:/usr/sbin/bpftool:/usr/libexec/bpftool-minimal
52 endef
53
54 define Package/bpftool-full
55 $(call Package/bpftool/Default)
56 TITLE+= (Full)
57 VARIANT:=full
58 PROVIDES:=bpftool
59 ALTERNATIVES:=300:/usr/sbin/bpftool:/usr/libexec/bpftool-full
60 DEPENDS+= +libbfd +libopcodes
61 endef
62
63 define Package/bpftool-minimal/description
64 A tool for inspection and simple manipulation of eBPF programs and maps.
65 endef
66
67 define Package/bpftool-full/description
68 A tool for inspection and simple manipulation of eBPF programs and maps.
69 This full version uses libbfd and libopcodes to support disassembly of
70 eBPF programs and jited code.
71 endef
72
73 define Package/libbpf
74 SECTION:=libs
75 CATEGORY:=Libraries
76 TITLE:=libbpf - eBPF helper library
77 VARIANT:=lib
78 LICENSE:=LGPL-2.1 OR BSD-2-Clause
79 ABI_VERSION:=0
80 URL:=http://www.kernel.org
81 DEPENDS:=+libelf
82 endef
83
84 define Package/libbpf/description
85 libbpf is a library for loading eBPF programs and reading and manipulating eBPF objects from user-space.
86 endef
87
88
89 # LTO not compatible with DSO using PIC
90 ifneq ($(BUILD_VARIANT),lib)
91 TARGET_CFLAGS += -ffunction-sections -fdata-sections -flto
92 TARGET_LDFLAGS += -Wl,--gc-sections
93 endif
94
95 MAKE_FLAGS += \
96 EXTRA_CFLAGS="$(TARGET_CFLAGS) $(TARGET_CPPFLAGS)" \
97 LDFLAGS="$(TARGET_LDFLAGS)" \
98 BPFTOOL_VERSION="$(LINUX_VERSION)" \
99 FEATURES_DUMP="$(PKG_BUILD_DIR)/FEATURE-DUMP.openwrt" \
100 OUTPUT="$(PKG_BUILD_DIR)/" \
101 prefix="/usr" \
102 $(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='')
103
104 ifeq ($(BUILD_VARIANT),full)
105 HAVE_LIBBFD:=1
106 HAVE_LIBCAP:=0
107 HAVE_CLANG:=0
108 MAKE_PATH:=tools/bpf/bpftool
109 else ifeq ($(BUILD_VARIANT),minimal)
110 HAVE_LIBBFD:=0
111 HAVE_LIBCAP:=0
112 HAVE_CLANG:=0
113 MAKE_PATH:=tools/bpf/bpftool
114 else ifeq ($(BUILD_VARIANT),lib)
115 HAVE_LIBBFD:=0
116 HAVE_LIBCAP:=0
117 HAVE_CLANG:=0
118 MAKE_PATH:=tools/lib/bpf
119 endif
120
121 # Perform a "throw-away" make to create a FEATURE-DUMP.* file to edit later.
122 # The "//" in the make target is actually needed, very unPOSIXly.
123 define Build/Configure
124 +$(MAKE_VARS) $(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR)/tools/bpf/bpftool \
125 $(MAKE_FLAGS) FEATURES_DUMP= $(PKG_BUILD_DIR)//libbpf/libbpf.a
126 (cd $(PKG_BUILD_DIR); cat FEATURE-DUMP.bpftool libbpf/FEATURE-DUMP.libbpf \
127 | sort | uniq > FEATURE-DUMP.openwrt)
128 $(SED) 's/feature-libbfd=[01]/feature-libbfd=$(HAVE_LIBBFD)/' \
129 -e 's/feature-libcap=[01]/feature-libcap=$(HAVE_LIBCAP)/' \
130 -e 's/feature-clang-bpf-global-var=[01]/feature-clang-bpf-global-var=$(HAVE_CLANG)/' \
131 $(PKG_BUILD_DIR)/FEATURE-DUMP.openwrt
132 endef
133
134 define Build/InstallDev/libbpf
135 $(INSTALL_DIR) $(1)/usr/include/bpf
136 $(CP) $(PKG_INSTALL_DIR)/usr/include/bpf/*.h $(1)/usr/include/bpf/
137 $(INSTALL_DIR) $(1)/usr/lib
138 $(CP) $(PKG_INSTALL_DIR)/usr/lib$(LIB_SUFFIX)/libbpf.{a,so*} \
139 $(1)/usr/lib/
140 $(INSTALL_DIR) $(1)/usr/lib/pkgconfig
141 $(CP) $(PKG_INSTALL_DIR)/usr/lib$(LIB_SUFFIX)/pkgconfig/libbpf.pc \
142 $(1)/usr/lib/pkgconfig/
143 endef
144
145 ifeq ($(BUILD_VARIANT),lib)
146 Build/InstallDev=$(Build/InstallDev/libbpf)
147 endif
148
149 define Package/bpftool-$(BUILD_VARIANT)/install
150 $(INSTALL_DIR) $(1)/usr/libexec
151 $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/bpftool \
152 $(1)/usr/libexec/bpftool-$(BUILD_VARIANT)
153 endef
154
155 define Package/libbpf/install
156 $(INSTALL_DIR) $(1)/usr/lib
157 $(CP) $(PKG_INSTALL_DIR)/usr/lib$(LIB_SUFFIX)/libbpf.so.* $(1)/usr/lib/
158 endef
159
160 $(eval $(call BuildPackage,bpftool-full))
161 $(eval $(call BuildPackage,bpftool-minimal))
162 $(eval $(call BuildPackage,libbpf))