xref: /linux/tools/lib/bpf/Makefile (revision 9f5270d758d955506dcb114cb863a86b30a4c783)
1# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2# Most of this file is copied from tools/lib/traceevent/Makefile
3
4RM ?= rm
5srctree := $(realpath $(srctree))
6
7VERSION_SCRIPT := libbpf.map
8LIBBPF_VERSION := $(shell \
9	grep -oE '^LIBBPF_([0-9.]+)' $(VERSION_SCRIPT) | \
10	sort -rV | head -n1 | cut -d'_' -f2)
11LIBBPF_MAJOR_VERSION := $(word 1,$(subst ., ,$(LIBBPF_VERSION)))
12LIBBPF_MINOR_VERSION := $(word 2,$(subst ., ,$(LIBBPF_VERSION)))
13
14MAKEFLAGS += --no-print-directory
15
16# This will work when bpf is built in tools env. where srctree
17# isn't set and when invoked from selftests build, where srctree
18# is a ".". building_out_of_srctree is undefined for in srctree
19# builds
20ifndef building_out_of_srctree
21srctree := $(patsubst %/,%,$(dir $(CURDIR)))
22srctree := $(patsubst %/,%,$(dir $(srctree)))
23srctree := $(patsubst %/,%,$(dir $(srctree)))
24#$(info Determined 'srctree' to be $(srctree))
25endif
26
27INSTALL = install
28
29# Use DESTDIR for installing into a different root directory.
30# This is useful for building a package. The program will be
31# installed in this directory as if it was the root directory.
32# Then the build tool can move it later.
33DESTDIR ?=
34DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
35
36include $(srctree)/tools/scripts/Makefile.arch
37
38ifeq ($(LP64), 1)
39  libdir_relative = lib64
40else
41  libdir_relative = lib
42endif
43
44prefix ?= /usr/local
45libdir = $(prefix)/$(libdir_relative)
46man_dir = $(prefix)/share/man
47man_dir_SQ = '$(subst ','\'',$(man_dir))'
48
49export man_dir man_dir_SQ INSTALL
50export DESTDIR DESTDIR_SQ
51
52include $(srctree)/tools/scripts/Makefile.include
53
54# copy a bit from Linux kbuild
55
56INCLUDES = -I$(or $(OUTPUT),.) \
57	   -I$(srctree)/tools/include -I$(srctree)/tools/include/uapi \
58	   -I$(srctree)/tools/arch/$(SRCARCH)/include
59
60export prefix libdir src obj
61
62# Shell quotes
63libdir_SQ = $(subst ','\'',$(libdir))
64libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
65
66OBJ		= $@
67N		=
68
69LIB_TARGET	= libbpf.a libbpf.so.$(LIBBPF_VERSION)
70LIB_FILE	= libbpf.a libbpf.so*
71PC_FILE		= libbpf.pc
72
73# Set compile option CFLAGS
74ifdef EXTRA_CFLAGS
75  CFLAGS := $(EXTRA_CFLAGS)
76else
77  CFLAGS := -g -O2
78endif
79
80# Append required CFLAGS
81override CFLAGS += -std=gnu89
82override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
83override CFLAGS += -Werror -Wall
84override CFLAGS += $(INCLUDES)
85override CFLAGS += -fvisibility=hidden
86override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
87override CFLAGS += $(CLANG_CROSS_FLAGS)
88
89# flags specific for shared library
90SHLIB_FLAGS := -DSHARED -fPIC
91
92# Disable command line variables (CFLAGS) override from top
93# level Makefile (perf), otherwise build Makefile will get
94# the same command line setup.
95MAKEOVERRIDES=
96
97all:
98
99OUTPUT ?= ./
100OUTPUT := $(abspath $(OUTPUT))/
101export srctree OUTPUT CC LD CFLAGS V
102include $(srctree)/tools/build/Makefile.include
103
104SHARED_OBJDIR	:= $(OUTPUT)sharedobjs/
105STATIC_OBJDIR	:= $(OUTPUT)staticobjs/
106BPF_IN_SHARED	:= $(SHARED_OBJDIR)libbpf-in.o
107BPF_IN_STATIC	:= $(STATIC_OBJDIR)libbpf-in.o
108BPF_HELPER_DEFS	:= $(OUTPUT)bpf_helper_defs.h
109BPF_GENERATED	:= $(BPF_HELPER_DEFS)
110
111LIB_TARGET	:= $(addprefix $(OUTPUT),$(LIB_TARGET))
112LIB_FILE	:= $(addprefix $(OUTPUT),$(LIB_FILE))
113PC_FILE		:= $(addprefix $(OUTPUT),$(PC_FILE))
114
115TAGS_PROG := $(if $(shell which etags 2>/dev/null),etags,ctags)
116
117GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN_SHARED) | \
118			   cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
119			   sed 's/\[.*\]//' | \
120			   awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
121			   sort -u | wc -l)
122VERSIONED_SYM_COUNT = $(shell readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \
123			      sed 's/\[.*\]//' | \
124			      awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
125			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
126
127CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
128
129all: fixdep
130	$(Q)$(MAKE) all_cmd
131
132all_cmd: $(CMD_TARGETS) check
133
134$(SHARED_OBJDIR) $(STATIC_OBJDIR):
135	$(Q)mkdir -p $@
136
137$(BPF_IN_SHARED): force $(BPF_GENERATED) | $(SHARED_OBJDIR)
138	@(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
139	(diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
140	echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
141	@(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
142	(diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
143	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
144	@(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
145	(diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
146	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
147	$(SILENT_MAKE) -C $(srctree)/tools/build CFLAGS= LDFLAGS= OUTPUT=$(SHARED_OBJDIR) $(SHARED_OBJDIR)fixdep
148	$(Q)$(MAKE) $(build)=libbpf OUTPUT=$(SHARED_OBJDIR) CFLAGS="$(CFLAGS) $(SHLIB_FLAGS)"
149
150$(BPF_IN_STATIC): force $(BPF_GENERATED) | $(STATIC_OBJDIR)
151	$(SILENT_MAKE) -C $(srctree)/tools/build CFLAGS= LDFLAGS= OUTPUT=$(STATIC_OBJDIR) $(STATIC_OBJDIR)fixdep
152	$(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR)
153
154$(BPF_HELPER_DEFS): $(srctree)/tools/include/uapi/linux/bpf.h
155	$(QUIET_GEN)$(srctree)/scripts/bpf_doc.py --header \
156		--file $(srctree)/tools/include/uapi/linux/bpf.h > $(BPF_HELPER_DEFS)
157
158$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
159
160$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN_SHARED) $(VERSION_SCRIPT)
161	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) \
162		--shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
163		-Wl,--version-script=$(VERSION_SCRIPT) $< -lelf -lz -o $@
164	@ln -sf $(@F) $(OUTPUT)libbpf.so
165	@ln -sf $(@F) $(OUTPUT)libbpf.so.$(LIBBPF_MAJOR_VERSION)
166
167$(OUTPUT)libbpf.a: $(BPF_IN_STATIC)
168	$(QUIET_LINK)$(RM) -f $@; $(AR) rcs $@ $^
169
170$(OUTPUT)libbpf.pc:
171	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
172		-e "s|@LIBDIR@|$(libdir_SQ)|" \
173		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
174		< libbpf.pc.template > $@
175
176check: check_abi check_version
177
178check_abi: $(OUTPUT)libbpf.so $(VERSION_SCRIPT)
179	@if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then	 \
180		echo "Warning: Num of global symbols in $(BPF_IN_SHARED)"	 \
181		     "($(GLOBAL_SYM_COUNT)) does NOT match with num of"	 \
182		     "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
183		     "Please make sure all LIBBPF_API symbols are"	 \
184		     "versioned in $(VERSION_SCRIPT)." >&2;		 \
185		readelf -s --wide $(BPF_IN_SHARED) |			 \
186		    cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' |	 \
187		    sed 's/\[.*\]//' |					 \
188		    awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}'|  \
189		    sort -u > $(OUTPUT)libbpf_global_syms.tmp;		 \
190		readelf --dyn-syms --wide $(OUTPUT)libbpf.so |		 \
191		    sed 's/\[.*\]//' |					 \
192		    awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}'|  \
193		    grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 |		 \
194		    sort -u > $(OUTPUT)libbpf_versioned_syms.tmp; 	 \
195		diff -u $(OUTPUT)libbpf_global_syms.tmp			 \
196		     $(OUTPUT)libbpf_versioned_syms.tmp;		 \
197		rm $(OUTPUT)libbpf_global_syms.tmp			 \
198		   $(OUTPUT)libbpf_versioned_syms.tmp;			 \
199		exit 1;							 \
200	fi
201
202HDR_MAJ_VERSION := $(shell grep -oE '^$(pound)define LIBBPF_MAJOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
203HDR_MIN_VERSION := $(shell grep -oE '^$(pound)define LIBBPF_MINOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
204
205check_version: $(VERSION_SCRIPT) libbpf_version.h
206	@if [ "$(HDR_MAJ_VERSION)" != "$(LIBBPF_MAJOR_VERSION)" ]; then        \
207		echo "Error: libbpf major version mismatch detected: "	       \
208		     "'$(HDR_MAJ_VERSION)' != '$(LIBBPF_MAJOR_VERSION)'" >&2;  \
209		exit 1;							       \
210	fi
211	@if [ "$(HDR_MIN_VERSION)" != "$(LIBBPF_MINOR_VERSION)" ]; then	       \
212		echo "Error: libbpf minor version mismatch detected: "	       \
213		     "'$(HDR_MIN_VERSION)' != '$(LIBBPF_MINOR_VERSION)'" >&2;  \
214		exit 1;							       \
215	fi
216
217define do_install_mkdir
218	if [ ! -d '$(DESTDIR_SQ)$1' ]; then		\
219		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1';	\
220	fi
221endef
222
223define do_install
224	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
225		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
226	fi;						\
227	$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
228endef
229
230install_lib: all_cmd
231	$(call QUIET_INSTALL, $(LIB_TARGET)) \
232		$(call do_install_mkdir,$(libdir_SQ)); \
233		cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
234
235SRC_HDRS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h	     \
236	    bpf_helpers.h bpf_tracing.h bpf_endian.h bpf_core_read.h	     \
237	    skel_internal.h libbpf_version.h usdt.bpf.h
238GEN_HDRS := $(BPF_GENERATED)
239
240INSTALL_PFX := $(DESTDIR)$(prefix)/include/bpf
241INSTALL_SRC_HDRS := $(addprefix $(INSTALL_PFX)/, $(SRC_HDRS))
242INSTALL_GEN_HDRS := $(addprefix $(INSTALL_PFX)/, $(notdir $(GEN_HDRS)))
243
244$(INSTALL_SRC_HDRS): $(INSTALL_PFX)/%.h: %.h
245	$(call QUIET_INSTALL, $@) \
246		$(call do_install,$<,$(prefix)/include/bpf,644)
247
248$(INSTALL_GEN_HDRS): $(INSTALL_PFX)/%.h: $(OUTPUT)%.h
249	$(call QUIET_INSTALL, $@) \
250		$(call do_install,$<,$(prefix)/include/bpf,644)
251
252install_headers: $(BPF_GENERATED) $(INSTALL_SRC_HDRS) $(INSTALL_GEN_HDRS)
253	$(call QUIET_INSTALL, libbpf_headers)
254
255install_pkgconfig: $(PC_FILE)
256	$(call QUIET_INSTALL, $(PC_FILE)) \
257		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
258
259install: install_lib install_pkgconfig install_headers
260
261clean: fixdep-clean
262	$(call QUIET_CLEAN, libbpf) $(RM) -rf $(CMD_TARGETS)		     \
263		*~ .*.d .*.cmd LIBBPF-CFLAGS $(BPF_GENERATED)		     \
264		$(SHARED_OBJDIR) $(STATIC_OBJDIR)			     \
265		$(addprefix $(OUTPUT),					     \
266			    *.o *.a *.so *.so.$(LIBBPF_MAJOR_VERSION) *.pc)
267
268PHONY += force cscope tags check check_abi check_version
269force:
270
271cscope:
272	ls *.c *.h > cscope.files
273	cscope -b -q -I $(srctree)/include -f cscope.out
274
275tags:
276	$(RM) -f TAGS tags
277	ls *.c *.h | xargs $(TAGS_PROG) -a
278
279# Declare the contents of the .PHONY variable as phony.  We keep that
280# information in a variable so we can use it in if_changed and friends.
281.PHONY: $(PHONY)
282
283# Delete partially updated (corrupted) files on error
284.DELETE_ON_ERROR:
285
286help:
287	@echo 'libbpf common targets:'
288	@echo '  HINT: use "V=1" to enable verbose build'
289	@echo '  all     - build libraries and pkgconfig'
290	@echo '  clean   - remove all generated files'
291	@echo '  check   - check ABI and version info'
292	@echo ''
293	@echo 'libbpf install targets:'
294	@echo '  HINT: use "prefix"(defaults to "/usr/local") or "DESTDIR" (defaults to "/")'
295	@echo '        to adjust target destination, e.g. "make prefix=/usr/local install"'
296	@echo '  install          - build and install all headers, libraries and pkgconfig'
297	@echo '  install_headers  - install only headers to include/bpf'
298	@echo ''
299	@echo 'libbpf make targets:'
300	@echo '  tags    - use ctags to make tag information for source code browsing'
301	@echo '  cscope  - use cscope to make interactive source code browsing database'
302