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