1 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 # Most of this file is copied from tools/lib/traceevent/Makefile
4 LIBBPF_VERSION := $(shell \
5 grep -oE '^LIBBPF_([0-9.]+)' libbpf.map | \
6 sort -rV | head -n1 | cut -d'_' -f2)
7 LIBBPF_MAJOR_VERSION := $(firstword $(subst ., ,$(LIBBPF_VERSION)))
9 MAKEFLAGS += --no-print-directory
12 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
13 srctree := $(patsubst %/,%,$(dir $(srctree)))
14 srctree := $(patsubst %/,%,$(dir $(srctree)))
15 #$(info Determined 'srctree' to be $(srctree))
20 # Use DESTDIR for installing into a different root directory.
21 # This is useful for building a package. The program will be
22 # installed in this directory as if it was the root directory.
23 # Then the build tool can move it later.
25 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
27 include $(srctree)/tools/scripts/Makefile.arch
30 libdir_relative = lib64
36 libdir = $(prefix)/$(libdir_relative)
37 man_dir = $(prefix)/share/man
38 man_dir_SQ = '$(subst ','\'',$(man_dir))'
40 export man_dir man_dir_SQ INSTALL
41 export DESTDIR DESTDIR_SQ
43 include $(srctree)/tools/scripts/Makefile.include
45 # copy a bit from Linux kbuild
47 ifeq ("$(origin V)", "command line")
54 FEATURE_USER = .libbpf
55 FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx
56 FEATURE_DISPLAY = libelf bpf
58 INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
59 FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
62 NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
64 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
69 ifeq ($(check_feat),1)
70 ifeq ($(FEATURES_DUMP),)
71 include $(srctree)/tools/build/Makefile.feature
73 include $(FEATURES_DUMP)
77 export prefix libdir src obj
80 libdir_SQ = $(subst ','\'',$(libdir))
81 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
86 LIB_TARGET = libbpf.a libbpf.so.$(LIBBPF_VERSION)
87 LIB_FILE = libbpf.a libbpf.so*
90 # Set compile option CFLAGS
92 CFLAGS := $(EXTRA_CFLAGS)
97 ifeq ($(feature-libelf-mmap), 1)
98 override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
101 ifeq ($(feature-reallocarray), 0)
102 override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
105 # Append required CFLAGS
106 override CFLAGS += $(EXTRA_WARNINGS)
107 override CFLAGS += -Werror -Wall
108 override CFLAGS += -fPIC
109 override CFLAGS += $(INCLUDES)
110 override CFLAGS += -fvisibility=hidden
111 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
119 # Disable command line variables (CFLAGS) override from top
120 # level Makefile (perf), otherwise build Makefile will get
121 # the same command line setup.
126 export srctree OUTPUT CC LD CFLAGS V
127 include $(srctree)/tools/build/Makefile.include
129 BPF_IN := $(OUTPUT)libbpf-in.o
130 VERSION_SCRIPT := libbpf.map
132 LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET))
133 LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
134 PC_FILE := $(addprefix $(OUTPUT),$(PC_FILE))
136 GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
137 cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
138 awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$8}' | \
140 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
141 grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
143 CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
145 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
147 ifeq ($(feature-cxx), 1)
148 CMD_TARGETS += $(CXX_TEST_TARGET)
151 TARGETS = $(CMD_TARGETS)
156 all_cmd: $(CMD_TARGETS) check
158 $(BPF_IN): force elfdep bpfdep
159 @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
160 (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
161 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
162 @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
163 (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
164 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf_common.h' differs from latest version at 'include/uapi/linux/bpf_common.h'" >&2 )) || true
165 @(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \
166 (diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \
167 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true
168 @(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \
169 (diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \
170 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/if_link.h' differs from latest version at 'include/uapi/linux/if_link.h'" >&2 )) || true
171 @(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
172 (diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
173 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/if_xdp.h' differs from latest version at 'include/uapi/linux/if_xdp.h'" >&2 )) || true
174 $(Q)$(MAKE) $(build)=libbpf
176 $(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
178 $(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN)
179 $(QUIET_LINK)$(CC) --shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
180 -Wl,--version-script=$(VERSION_SCRIPT) $^ -lelf -o $@
181 @ln -sf $(@F) $(OUTPUT)libbpf.so
182 @ln -sf $(@F) $(OUTPUT)libbpf.so.$(LIBBPF_MAJOR_VERSION)
184 $(OUTPUT)libbpf.a: $(BPF_IN)
185 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
187 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
188 $(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
191 $(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
192 -e "s|@LIBDIR@|$(libdir_SQ)|" \
193 -e "s|@VERSION@|$(LIBBPF_VERSION)|" \
194 < libbpf.pc.template > $@
198 check_abi: $(OUTPUT)libbpf.so
199 @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then \
200 echo "Warning: Num of global symbols in $(BPF_IN)" \
201 "($(GLOBAL_SYM_COUNT)) does NOT match with num of" \
202 "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
203 "Please make sure all LIBBPF_API symbols are" \
204 "versioned in $(VERSION_SCRIPT)." >&2; \
205 readelf -s --wide $(OUTPUT)libbpf-in.o | \
206 cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
207 awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$8}'| \
208 sort -u > $(OUTPUT)libbpf_global_syms.tmp; \
209 readelf -s --wide $(OUTPUT)libbpf.so | \
210 grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | \
211 sort -u > $(OUTPUT)libbpf_versioned_syms.tmp; \
212 diff -u $(OUTPUT)libbpf_global_syms.tmp \
213 $(OUTPUT)libbpf_versioned_syms.tmp; \
214 rm $(OUTPUT)libbpf_global_syms.tmp \
215 $(OUTPUT)libbpf_versioned_syms.tmp; \
219 define do_install_mkdir
220 if [ ! -d '$(DESTDIR_SQ)$1' ]; then \
221 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
226 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
227 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
229 $(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
233 $(call QUIET_INSTALL, $(LIB_TARGET)) \
234 $(call do_install_mkdir,$(libdir_SQ)); \
235 cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
238 $(call QUIET_INSTALL, headers) \
239 $(call do_install,bpf.h,$(prefix)/include/bpf,644); \
240 $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \
241 $(call do_install,btf.h,$(prefix)/include/bpf,644); \
242 $(call do_install,libbpf_util.h,$(prefix)/include/bpf,644); \
243 $(call do_install,xsk.h,$(prefix)/include/bpf,644);
245 install_pkgconfig: $(PC_FILE)
246 $(call QUIET_INSTALL, $(PC_FILE)) \
247 $(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
249 install: install_lib install_pkgconfig
254 $(call QUIET_CLEAN, config)
255 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
258 $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
259 *.o *~ *.a *.so *.so.$(LIBBPF_MAJOR_VERSION) .*.d .*.cmd \
261 $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
265 PHONY += force elfdep bpfdep
269 @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
272 @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
274 # Declare the contents of the .PHONY variable as phony. We keep that
275 # information in a variable so we can use it in if_changed and friends.