xref: /openbmc/linux/tools/lib/bpf/Makefile (revision be709d48)
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 = 2
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*
93
94# Set compile option CFLAGS
95ifdef EXTRA_CFLAGS
96  CFLAGS := $(EXTRA_CFLAGS)
97else
98  CFLAGS := -g -Wall
99endif
100
101ifeq ($(feature-libelf-mmap), 1)
102  override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
103endif
104
105ifeq ($(feature-reallocarray), 0)
106  override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
107endif
108
109# Append required CFLAGS
110override CFLAGS += $(EXTRA_WARNINGS)
111override CFLAGS += -Werror -Wall
112override CFLAGS += -fPIC
113override CFLAGS += $(INCLUDES)
114override CFLAGS += -fvisibility=hidden
115
116ifeq ($(VERBOSE),1)
117  Q =
118else
119  Q = @
120endif
121
122# Disable command line variables (CFLAGS) override from top
123# level Makefile (perf), otherwise build Makefile will get
124# the same command line setup.
125MAKEOVERRIDES=
126
127all:
128
129export srctree OUTPUT CC LD CFLAGS V
130include $(srctree)/tools/build/Makefile.include
131
132BPF_IN		:= $(OUTPUT)libbpf-in.o
133VERSION_SCRIPT	:= libbpf.map
134
135LIB_TARGET	:= $(addprefix $(OUTPUT),$(LIB_TARGET))
136LIB_FILE	:= $(addprefix $(OUTPUT),$(LIB_FILE))
137
138GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
139			   awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}')
140VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
141			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
142
143CMD_TARGETS = $(LIB_TARGET)
144
145CXX_TEST_TARGET = $(OUTPUT)test_libbpf
146
147ifeq ($(feature-cxx), 1)
148	CMD_TARGETS += $(CXX_TEST_TARGET)
149endif
150
151TARGETS = $(CMD_TARGETS)
152
153all: fixdep
154	$(Q)$(MAKE) all_cmd
155
156all_cmd: $(CMD_TARGETS) check
157
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
175
176$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
177
178$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN)
179	$(QUIET_LINK)$(CC) --shared -Wl,-soname,libbpf.so.$(VERSION) \
180				    -Wl,--version-script=$(VERSION_SCRIPT) $^ -o $@
181	@ln -sf $(@F) $(OUTPUT)libbpf.so
182	@ln -sf $(@F) $(OUTPUT)libbpf.so.$(VERSION)
183
184$(OUTPUT)libbpf.a: $(BPF_IN)
185	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
186
187$(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
188	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
189
190check: check_abi
191
192check_abi: $(OUTPUT)libbpf.so
193	@if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then	 \
194		echo "Warning: Num of global symbols in $(BPF_IN)"	 \
195		     "($(GLOBAL_SYM_COUNT)) does NOT match with num of"	 \
196		     "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
197		     "Please make sure all LIBBPF_API symbols are"	 \
198		     "versioned in $(VERSION_SCRIPT)." >&2;		 \
199		exit 1;							 \
200	fi
201
202define do_install_mkdir
203	if [ ! -d '$(DESTDIR_SQ)$1' ]; then		\
204		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1';	\
205	fi
206endef
207
208define do_install
209	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
210		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
211	fi;						\
212	$(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
213endef
214
215install_lib: all_cmd
216	$(call QUIET_INSTALL, $(LIB_TARGET)) \
217		$(call do_install_mkdir,$(libdir_SQ)); \
218		cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
219
220install_headers:
221	$(call QUIET_INSTALL, headers) \
222		$(call do_install,bpf.h,$(prefix)/include/bpf,644); \
223		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
224		$(call do_install,btf.h,$(prefix)/include/bpf,644);
225
226install: install_lib
227
228### Cleaning rules
229
230config-clean:
231	$(call QUIET_CLEAN, config)
232	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
233
234clean:
235	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
236		*.o *~ *.a *.so *.so.$(VERSION) .*.d .*.cmd LIBBPF-CFLAGS
237	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
238
239
240
241PHONY += force elfdep bpfdep
242force:
243
244elfdep:
245	@if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
246
247bpfdep:
248	@if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
249
250# Declare the contents of the .PHONY variable as phony.  We keep that
251# information in a variable so we can use it in if_changed and friends.
252.PHONY: $(PHONY)
253