]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/lib/bpf/Makefile
powerpc/mm: Define MAX_PHYSMEM_BITS for all 64-bit configs
[linux.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 = 1
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 LIB_FILE = libbpf.a libbpf.so
83
84 VERSION         = $(BPF_VERSION)
85 PATCHLEVEL      = $(BPF_PATCHLEVEL)
86 EXTRAVERSION    = $(BPF_EXTRAVERSION)
87
88 OBJ             = $@
89 N               =
90
91 LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
92
93 # Set compile option CFLAGS
94 ifdef EXTRA_CFLAGS
95   CFLAGS := $(EXTRA_CFLAGS)
96 else
97   CFLAGS := -g -Wall
98 endif
99
100 ifeq ($(feature-libelf-mmap), 1)
101   override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
102 endif
103
104 ifeq ($(feature-reallocarray), 0)
105   override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
106 endif
107
108 # Append required CFLAGS
109 override CFLAGS += $(EXTRA_WARNINGS)
110 override CFLAGS += -Werror -Wall
111 override CFLAGS += -fPIC
112 override CFLAGS += $(INCLUDES)
113 override CFLAGS += -fvisibility=hidden
114
115 ifeq ($(VERBOSE),1)
116   Q =
117 else
118   Q = @
119 endif
120
121 # Disable command line variables (CFLAGS) override from top
122 # level Makefile (perf), otherwise build Makefile will get
123 # the same command line setup.
124 MAKEOVERRIDES=
125
126 all:
127
128 export srctree OUTPUT CC LD CFLAGS V
129 include $(srctree)/tools/build/Makefile.include
130
131 BPF_IN    := $(OUTPUT)libbpf-in.o
132 LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
133 VERSION_SCRIPT := libbpf.map
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_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: $(BPF_IN)
174         $(QUIET_LINK)$(CC) --shared -Wl,--version-script=$(VERSION_SCRIPT) \
175                 $^ -o $@
176
177 $(OUTPUT)libbpf.a: $(BPF_IN)
178         $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
179
180 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
181         $(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
182
183 check: check_abi
184
185 check_abi: $(OUTPUT)libbpf.so
186         @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then  \
187                 echo "Warning: Num of global symbols in $(BPF_IN)"       \
188                      "($(GLOBAL_SYM_COUNT)) does NOT match with num of"  \
189                      "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
190                      "Please make sure all LIBBPF_API symbols are"       \
191                      "versioned in $(VERSION_SCRIPT)." >&2;              \
192                 exit 1;                                                  \
193         fi
194
195 define do_install
196         if [ ! -d '$(DESTDIR_SQ)$2' ]; then             \
197                 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
198         fi;                                             \
199         $(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
200 endef
201
202 install_lib: all_cmd
203         $(call QUIET_INSTALL, $(LIB_FILE)) \
204                 $(call do_install,$(LIB_FILE),$(libdir_SQ))
205
206 install_headers:
207         $(call QUIET_INSTALL, headers) \
208                 $(call do_install,bpf.h,$(prefix)/include/bpf,644); \
209                 $(call do_install,libbpf.h,$(prefix)/include/bpf,644);
210                 $(call do_install,btf.h,$(prefix)/include/bpf,644);
211
212 install: install_lib
213
214 ### Cleaning rules
215
216 config-clean:
217         $(call QUIET_CLEAN, config)
218         $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
219
220 clean:
221         $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
222                 *.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
223         $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
224
225
226
227 PHONY += force elfdep bpfdep
228 force:
229
230 elfdep:
231         @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
232
233 bpfdep:
234         @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
235
236 # Declare the contents of the .PHONY variable as phony.  We keep that
237 # information in a variable so we can use it in if_changed and friends.
238 .PHONY: $(PHONY)