Merge tag 'irqchip-fixes-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/maz...
[linux-2.6-microblaze.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 BPF_VERSION = 0
5 BPF_PATCHLEVEL = 0
6 BPF_EXTRAVERSION = 4
7
8 MAKEFLAGS += --no-print-directory
9
10 ifeq ($(srctree),)
11 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
12 srctree := $(patsubst %/,%,$(dir $(srctree)))
13 srctree := $(patsubst %/,%,$(dir $(srctree)))
14 #$(info Determined 'srctree' to be $(srctree))
15 endif
16
17 INSTALL = 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.
23 DESTDIR ?=
24 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
25
26 include $(srctree)/tools/scripts/Makefile.arch
27
28 ifeq ($(LP64), 1)
29   libdir_relative = lib64
30 else
31   libdir_relative = lib
32 endif
33
34 prefix ?= /usr/local
35 libdir = $(prefix)/$(libdir_relative)
36 man_dir = $(prefix)/share/man
37 man_dir_SQ = '$(subst ','\'',$(man_dir))'
38
39 export man_dir man_dir_SQ INSTALL
40 export DESTDIR DESTDIR_SQ
41
42 include $(srctree)/tools/scripts/Makefile.include
43
44 # copy a bit from Linux kbuild
45
46 ifeq ("$(origin V)", "command line")
47   VERBOSE = $(V)
48 endif
49 ifndef VERBOSE
50   VERBOSE = 0
51 endif
52
53 FEATURE_USER = .libbpf
54 FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx
55 FEATURE_DISPLAY = libelf bpf
56
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)
59
60 check_feat := 1
61 NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
62 ifdef MAKECMDGOALS
63 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
64   check_feat := 0
65 endif
66 endif
67
68 ifeq ($(check_feat),1)
69 ifeq ($(FEATURES_DUMP),)
70 include $(srctree)/tools/build/Makefile.feature
71 else
72 include $(FEATURES_DUMP)
73 endif
74 endif
75
76 export prefix libdir src obj
77
78 # Shell quotes
79 libdir_SQ = $(subst ','\'',$(libdir))
80 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
81
82 VERSION         = $(BPF_VERSION)
83 PATCHLEVEL      = $(BPF_PATCHLEVEL)
84 EXTRAVERSION    = $(BPF_EXTRAVERSION)
85
86 OBJ             = $@
87 N               =
88
89 LIBBPF_VERSION  = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
90
91 LIB_TARGET      = libbpf.a libbpf.so.$(LIBBPF_VERSION)
92 LIB_FILE        = libbpf.a libbpf.so*
93 PC_FILE         = libbpf.pc
94
95 # Set compile option CFLAGS
96 ifdef EXTRA_CFLAGS
97   CFLAGS := $(EXTRA_CFLAGS)
98 else
99   CFLAGS := -g -Wall
100 endif
101
102 ifeq ($(feature-libelf-mmap), 1)
103   override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
104 endif
105
106 ifeq ($(feature-reallocarray), 0)
107   override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
108 endif
109
110 # Append required CFLAGS
111 override CFLAGS += $(EXTRA_WARNINGS)
112 override CFLAGS += -Werror -Wall
113 override CFLAGS += -fPIC
114 override CFLAGS += $(INCLUDES)
115 override CFLAGS += -fvisibility=hidden
116
117 ifeq ($(VERBOSE),1)
118   Q =
119 else
120   Q = @
121 endif
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.
126 MAKEOVERRIDES=
127
128 all:
129
130 export srctree OUTPUT CC LD CFLAGS V
131 include $(srctree)/tools/build/Makefile.include
132
133 BPF_IN          := $(OUTPUT)libbpf-in.o
134 VERSION_SCRIPT  := libbpf.map
135
136 LIB_TARGET      := $(addprefix $(OUTPUT),$(LIB_TARGET))
137 LIB_FILE        := $(addprefix $(OUTPUT),$(LIB_FILE))
138 PC_FILE         := $(addprefix $(OUTPUT),$(PC_FILE))
139
140 GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
141                            awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}')
142 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
143                               grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
144
145 CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
146
147 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
148
149 ifeq ($(feature-cxx), 1)
150         CMD_TARGETS += $(CXX_TEST_TARGET)
151 endif
152
153 TARGETS = $(CMD_TARGETS)
154
155 all: fixdep
156         $(Q)$(MAKE) all_cmd
157
158 all_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
198 check: check_abi
199
200 check_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                 readelf -s --wide $(OUTPUT)libbpf-in.o |                 \
208                     awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$8}'|   \
209                     sort -u > $(OUTPUT)libbpf_global_syms.tmp;           \
210                 readelf -s --wide $(OUTPUT)libbpf.so |                   \
211                     grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 |             \
212                     sort -u > $(OUTPUT)libbpf_versioned_syms.tmp;        \
213                 diff -u $(OUTPUT)libbpf_global_syms.tmp                  \
214                      $(OUTPUT)libbpf_versioned_syms.tmp;                 \
215                 rm $(OUTPUT)libbpf_global_syms.tmp                       \
216                    $(OUTPUT)libbpf_versioned_syms.tmp;                   \
217                 exit 1;                                                  \
218         fi
219
220 define do_install_mkdir
221         if [ ! -d '$(DESTDIR_SQ)$1' ]; then             \
222                 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
223         fi
224 endef
225
226 define do_install
227         if [ ! -d '$(DESTDIR_SQ)$2' ]; then             \
228                 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
229         fi;                                             \
230         $(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
231 endef
232
233 install_lib: all_cmd
234         $(call QUIET_INSTALL, $(LIB_TARGET)) \
235                 $(call do_install_mkdir,$(libdir_SQ)); \
236                 cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
237
238 install_headers:
239         $(call QUIET_INSTALL, headers) \
240                 $(call do_install,bpf.h,$(prefix)/include/bpf,644); \
241                 $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \
242                 $(call do_install,btf.h,$(prefix)/include/bpf,644); \
243                 $(call do_install,libbpf_util.h,$(prefix)/include/bpf,644); \
244                 $(call do_install,xsk.h,$(prefix)/include/bpf,644);
245
246 install_pkgconfig: $(PC_FILE)
247         $(call QUIET_INSTALL, $(PC_FILE)) \
248                 $(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
249
250 install: install_lib install_pkgconfig
251
252 ### Cleaning rules
253
254 config-clean:
255         $(call QUIET_CLEAN, config)
256         $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
257
258 clean:
259         $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
260                 *.o *~ *.a *.so *.so.$(VERSION) .*.d .*.cmd *.pc LIBBPF-CFLAGS
261         $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
262
263
264
265 PHONY += force elfdep bpfdep
266 force:
267
268 elfdep:
269         @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
270
271 bpfdep:
272         @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
273
274 # Declare the contents of the .PHONY variable as phony.  We keep that
275 # information in a variable so we can use it in if_changed and friends.
276 .PHONY: $(PHONY)