xref: /openbmc/linux/tools/lib/bpf/Makefile (revision 9b93eb47)
1# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2# Most of this file is copied from tools/lib/traceevent/Makefile
3
4BPF_VERSION = 0
5BPF_PATCHLEVEL = 0
6BPF_EXTRAVERSION = 3
7
8MAKEFLAGS += --no-print-directory
9
10ifeq ($(srctree),)
11srctree := $(patsubst %/,%,$(dir $(CURDIR)))
12srctree := $(patsubst %/,%,$(dir $(srctree)))
13srctree := $(patsubst %/,%,$(dir $(srctree)))
14#$(info Determined 'srctree' to be $(srctree))
15endif
16
17INSTALL = install
18
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.
23DESTDIR ?=
24DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
25
26include $(srctree)/tools/scripts/Makefile.arch
27
28ifeq ($(LP64), 1)
29  libdir_relative = lib64
30else
31  libdir_relative = lib
32endif
33
34prefix ?= /usr/local
35libdir = $(prefix)/$(libdir_relative)
36man_dir = $(prefix)/share/man
37man_dir_SQ = '$(subst ','\'',$(man_dir))'
38
39export man_dir man_dir_SQ INSTALL
40export DESTDIR DESTDIR_SQ
41
42include $(srctree)/tools/scripts/Makefile.include
43
44# copy a bit from Linux kbuild
45
46ifeq ("$(origin V)", "command line")
47  VERBOSE = $(V)
48endif
49ifndef VERBOSE
50  VERBOSE = 0
51endif
52
53FEATURE_USER = .libbpf
54FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx
55FEATURE_DISPLAY = libelf bpf
56
57INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
58FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
59
60check_feat := 1
61NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
62ifdef MAKECMDGOALS
63ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
64  check_feat := 0
65endif
66endif
67
68ifeq ($(check_feat),1)
69ifeq ($(FEATURES_DUMP),)
70include $(srctree)/tools/build/Makefile.feature
71else
72include $(FEATURES_DUMP)
73endif
74endif
75
76export prefix libdir src obj
77
78# Shell quotes
79libdir_SQ = $(subst ','\'',$(libdir))
80libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
81
82VERSION		= $(BPF_VERSION)
83PATCHLEVEL	= $(BPF_PATCHLEVEL)
84EXTRAVERSION	= $(BPF_EXTRAVERSION)
85
86OBJ		= $@
87N		=
88
89LIBBPF_VERSION	= $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
90
91LIB_TARGET	= libbpf.a libbpf.so.$(LIBBPF_VERSION)
92LIB_FILE	= libbpf.a libbpf.so*
93PC_FILE		= libbpf.pc
94
95# Set compile option CFLAGS
96ifdef EXTRA_CFLAGS
97  CFLAGS := $(EXTRA_CFLAGS)
98else
99  CFLAGS := -g -Wall
100endif
101
102ifeq ($(feature-libelf-mmap), 1)
103  override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
104endif
105
106ifeq ($(feature-reallocarray), 0)
107  override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
108endif
109
110# Append required CFLAGS
111override CFLAGS += $(EXTRA_WARNINGS)
112override CFLAGS += -Werror -Wall
113override CFLAGS += -fPIC
114override CFLAGS += $(INCLUDES)
115override CFLAGS += -fvisibility=hidden
116
117ifeq ($(VERBOSE),1)
118  Q =
119else
120  Q = @
121endif
122
123# Disable command line variables (CFLAGS) override from top
124# level Makefile (perf), otherwise build Makefile will get
125# the same command line setup.
126MAKEOVERRIDES=
127
128all:
129
130export srctree OUTPUT CC LD CFLAGS V
131include $(srctree)/tools/build/Makefile.include
132
133BPF_IN		:= $(OUTPUT)libbpf-in.o
134VERSION_SCRIPT	:= libbpf.map
135
136LIB_TARGET	:= $(addprefix $(OUTPUT),$(LIB_TARGET))
137LIB_FILE	:= $(addprefix $(OUTPUT),$(LIB_FILE))
138PC_FILE		:= $(addprefix $(OUTPUT),$(PC_FILE))
139
140GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
141			   awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}')
142VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
143			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
144
145CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
146
147CXX_TEST_TARGET = $(OUTPUT)test_libbpf
148
149ifeq ($(feature-cxx), 1)
150	CMD_TARGETS += $(CXX_TEST_TARGET)
151endif
152
153TARGETS = $(CMD_TARGETS)
154
155all: fixdep
156	$(Q)$(MAKE) all_cmd
157
158all_cmd: $(CMD_TARGETS) check
159
160$(BPF_IN): force elfdep bpfdep
161	@(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
162	(diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
163	echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
164	@(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
165	(diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
166	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
167	@(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \
168	(diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \
169	echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true
170	@(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \
171	(diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \
172	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
173	@(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
174	(diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
175	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
176	$(Q)$(MAKE) $(build)=libbpf
177
178$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
179
180$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN)
181	$(QUIET_LINK)$(CC) --shared -Wl,-soname,libbpf.so.$(VERSION) \
182				    -Wl,--version-script=$(VERSION_SCRIPT) $^ -lelf -o $@
183	@ln -sf $(@F) $(OUTPUT)libbpf.so
184	@ln -sf $(@F) $(OUTPUT)libbpf.so.$(VERSION)
185
186$(OUTPUT)libbpf.a: $(BPF_IN)
187	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
188
189$(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
190	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
191
192$(OUTPUT)libbpf.pc:
193	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
194		-e "s|@LIBDIR@|$(libdir_SQ)|" \
195		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
196		< libbpf.pc.template > $@
197
198check: check_abi
199
200check_abi: $(OUTPUT)libbpf.so
201	@if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then	 \
202		echo "Warning: Num of global symbols in $(BPF_IN)"	 \
203		     "($(GLOBAL_SYM_COUNT)) does NOT match with num of"	 \
204		     "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
205		     "Please make sure all LIBBPF_API symbols are"	 \
206		     "versioned in $(VERSION_SCRIPT)." >&2;		 \
207		exit 1;							 \
208	fi
209
210define do_install_mkdir
211	if [ ! -d '$(DESTDIR_SQ)$1' ]; then		\
212		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1';	\
213	fi
214endef
215
216define do_install
217	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
218		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
219	fi;						\
220	$(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
221endef
222
223install_lib: all_cmd
224	$(call QUIET_INSTALL, $(LIB_TARGET)) \
225		$(call do_install_mkdir,$(libdir_SQ)); \
226		cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
227
228install_headers:
229	$(call QUIET_INSTALL, headers) \
230		$(call do_install,bpf.h,$(prefix)/include/bpf,644); \
231		$(call do_install,libbpf.h,$(prefix)/include/bpf,644); \
232		$(call do_install,btf.h,$(prefix)/include/bpf,644); \
233		$(call do_install,libbpf_util.h,$(prefix)/include/bpf,644); \
234		$(call do_install,xsk.h,$(prefix)/include/bpf,644);
235
236install_pkgconfig: $(PC_FILE)
237	$(call QUIET_INSTALL, $(PC_FILE)) \
238		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
239
240install: install_lib install_pkgconfig
241
242### Cleaning rules
243
244config-clean:
245	$(call QUIET_CLEAN, config)
246	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
247
248clean:
249	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
250		*.o *~ *.a *.so *.so.$(VERSION) .*.d .*.cmd *.pc LIBBPF-CFLAGS
251	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
252
253
254
255PHONY += force elfdep bpfdep
256force:
257
258elfdep:
259	@if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
260
261bpfdep:
262	@if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
263
264# Declare the contents of the .PHONY variable as phony.  We keep that
265# information in a variable so we can use it in if_changed and friends.
266.PHONY: $(PHONY)
267