1 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 # Most of this file is copied from tools/lib/traceevent/Makefile
8 MAKEFLAGS += --no-print-directory
11 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
12 srctree := $(patsubst %/,%,$(dir $(srctree)))
13 srctree := $(patsubst %/,%,$(dir $(srctree)))
14 #$(info Determined 'srctree' to be $(srctree))
19 # Use DESTDIR for installing into a different root directory.
20 # This is useful for building a package. The program will be
21 # installed in this directory as if it was the root directory.
22 # Then the build tool can move it later.
24 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
26 include $(srctree)/tools/scripts/Makefile.arch
29 libdir_relative = lib64
35 libdir = $(prefix)/$(libdir_relative)
36 man_dir = $(prefix)/share/man
37 man_dir_SQ = '$(subst ','\'',$(man_dir))'
39 export man_dir man_dir_SQ INSTALL
40 export DESTDIR DESTDIR_SQ
42 include $(srctree)/tools/scripts/Makefile.include
44 # copy a bit from Linux kbuild
46 ifeq ("$(origin V)", "command line")
53 FEATURE_USER = .libbpf
54 FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx
55 FEATURE_DISPLAY = libelf bpf
57 INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
58 FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
61 NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
63 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
68 ifeq ($(check_feat),1)
69 ifeq ($(FEATURES_DUMP),)
70 include $(srctree)/tools/build/Makefile.feature
72 include $(FEATURES_DUMP)
76 export prefix libdir src obj
79 libdir_SQ = $(subst ','\'',$(libdir))
80 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
82 LIB_FILE = libbpf.a libbpf.so
84 VERSION = $(BPF_VERSION)
85 PATCHLEVEL = $(BPF_PATCHLEVEL)
86 EXTRAVERSION = $(BPF_EXTRAVERSION)
91 LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
93 # Set compile option CFLAGS
95 CFLAGS := $(EXTRA_CFLAGS)
100 ifeq ($(feature-libelf-mmap), 1)
101 override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
104 ifeq ($(feature-reallocarray), 0)
105 override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
108 # Append required CFLAGS
109 override CFLAGS += $(EXTRA_WARNINGS)
110 override CFLAGS += -Werror -Wall
111 override CFLAGS += -fPIC
112 override CFLAGS += $(INCLUDES)
113 override CFLAGS += -fvisibility=hidden
121 # Disable command line variables (CFLAGS) override from top
122 # level Makefile (perf), otherwise build Makefile will get
123 # the same command line setup.
128 export srctree OUTPUT CC LD CFLAGS V
129 include $(srctree)/tools/build/Makefile.include
131 BPF_IN := $(OUTPUT)libbpf-in.o
132 LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
133 VERSION_SCRIPT := libbpf.map
135 GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
136 awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}')
137 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
138 grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
140 CMD_TARGETS = $(LIB_FILE)
142 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
144 ifeq ($(feature-cxx), 1)
145 CMD_TARGETS += $(CXX_TEST_TARGET)
148 TARGETS = $(CMD_TARGETS)
153 all_cmd: $(CMD_TARGETS) check
155 $(BPF_IN): force elfdep bpfdep
156 @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
157 (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
158 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
159 @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
160 (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
161 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
162 @(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \
163 (diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \
164 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true
165 @(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \
166 (diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \
167 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
168 @(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
169 (diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
170 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
171 $(Q)$(MAKE) $(build)=libbpf
173 $(OUTPUT)libbpf.so: $(BPF_IN)
174 $(QUIET_LINK)$(CC) --shared -Wl,--version-script=$(VERSION_SCRIPT) \
177 $(OUTPUT)libbpf.a: $(BPF_IN)
178 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
180 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
181 $(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
185 check_abi: $(OUTPUT)libbpf.so
186 @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then \
187 echo "Warning: Num of global symbols in $(BPF_IN)" \
188 "($(GLOBAL_SYM_COUNT)) does NOT match with num of" \
189 "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
190 "Please make sure all LIBBPF_API symbols are" \
191 "versioned in $(VERSION_SCRIPT)." >&2; \
196 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
197 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
199 $(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
203 $(call QUIET_INSTALL, $(LIB_FILE)) \
204 $(call do_install,$(LIB_FILE),$(libdir_SQ))
207 $(call QUIET_INSTALL, headers) \
208 $(call do_install,bpf.h,$(prefix)/include/bpf,644); \
209 $(call do_install,libbpf.h,$(prefix)/include/bpf,644);
210 $(call do_install,btf.h,$(prefix)/include/bpf,644);
217 $(call QUIET_CLEAN, config)
218 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
221 $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
222 *.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
223 $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
227 PHONY += force elfdep bpfdep
231 @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
234 @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
236 # Declare the contents of the .PHONY variable as phony. We keep that
237 # information in a variable so we can use it in if_changed and friends.