1 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 # Most of this file is copied from tools/lib/traceevent/Makefile
8 MAKEFLAGS
+= --no-print-directory
11 srctree
:= $(patsubst %/,%,$(dir $(CURDIR
)))
12 srctree
:= $(patsubst %/,%,$(dir $(srctree
)))
13 srctree
:= $(patsubst %/,%,$(dir $(srctree
)))
14 #$(info Determined 'srctree' to be $(srctree))
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.
24 DESTDIR_SQ
= '$(subst ','\'',$(DESTDIR
))'
26 include $(srctree)/tools/scripts/Makefile.arch
29 libdir_relative = lib64
35 libdir = $(prefix)/$(libdir_relative)
36 man_dir = $(prefix)/share/man
37 man_dir_SQ = '$(subst ','\'',$(man_dir))'
39 export man_dir man_dir_SQ INSTALL
40 export DESTDIR DESTDIR_SQ
42 include $(srctree
)/tools
/scripts
/Makefile.
include
44 # copy a bit from Linux kbuild
46 ifeq ("$(origin V)", "command line")
53 FEATURE_USER
= .libbpf
54 FEATURE_TESTS
= libelf libelf-mmap bpf reallocarray
cxx
55 FEATURE_DISPLAY
= libelf bpf
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
)
61 NON_CHECK_FEAT_TARGETS
:= clean TAGS
tags cscope help
63 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS
),$(MAKECMDGOALS
)),)
68 ifeq ($(check_feat
),1)
69 ifeq ($(FEATURES_DUMP
),)
70 include $(srctree
)/tools
/build
/Makefile.feature
72 include $(FEATURES_DUMP
)
76 export prefix libdir src obj
79 libdir_SQ
= $(subst ','\'',$(libdir))
80 libdir_relative_SQ = $(subst ','\'',$(libdir_relative
))
82 VERSION
= $(BPF_VERSION
)
83 PATCHLEVEL
= $(BPF_PATCHLEVEL
)
84 EXTRAVERSION
= $(BPF_EXTRAVERSION
)
89 LIBBPF_VERSION
= $(BPF_VERSION
).
$(BPF_PATCHLEVEL
).
$(BPF_EXTRAVERSION
)
91 LIB_TARGET
= libbpf.a libbpf.so.
$(LIBBPF_VERSION
)
92 LIB_FILE
= libbpf.a libbpf.so
*
95 # Set compile option CFLAGS
97 CFLAGS
:= $(EXTRA_CFLAGS
)
102 ifeq ($(feature-libelf-mmap
), 1)
103 override CFLAGS
+= -DHAVE_LIBELF_MMAP_SUPPORT
106 ifeq ($(feature-reallocarray
), 0)
107 override CFLAGS
+= -DCOMPAT_NEED_REALLOCARRAY
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
123 # Disable command line variables (CFLAGS) override from top
124 # level Makefile (perf), otherwise build Makefile will get
125 # the same command line setup.
130 export srctree OUTPUT CC LD CFLAGS V
131 include $(srctree
)/tools
/build
/Makefile.
include
133 BPF_IN
:= $(OUTPUT
)libbpf-in.o
134 VERSION_SCRIPT
:= libbpf.map
136 LIB_TARGET
:= $(addprefix $(OUTPUT
),$(LIB_TARGET
))
137 LIB_FILE
:= $(addprefix $(OUTPUT
),$(LIB_FILE
))
138 PC_FILE
:= $(addprefix $(OUTPUT
),$(PC_FILE
))
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
)
145 CMD_TARGETS
= $(LIB_TARGET
) $(PC_FILE
)
147 CXX_TEST_TARGET
= $(OUTPUT
)test_libbpf
149 ifeq ($(feature-cxx
), 1)
150 CMD_TARGETS
+= $(CXX_TEST_TARGET
)
153 TARGETS
= $(CMD_TARGETS
)
158 all_cmd
: $(CMD_TARGETS
) check
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
178 $(OUTPUT
)libbpf.so
: $(OUTPUT
)libbpf.so.
$(LIBBPF_VERSION
)
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
)
186 $(OUTPUT
)libbpf.a
: $(BPF_IN
)
187 $(QUIET_LINK
)$(RM
) $@
; $(AR
) rcs
$@
$^
189 $(OUTPUT
)test_libbpf
: test_libbpf.
cpp $(OUTPUT
)libbpf.a
190 $(QUIET_LINK
)$(CXX
) $(INCLUDES
) $^
-lelf
-o
$@
193 $(QUIET_GEN
)sed
-e
"s|@PREFIX@|$(prefix)|" \
194 -e
"s|@LIBDIR@|$(libdir_SQ)|" \
195 -e
"s|@VERSION@|$(LIBBPF_VERSION)|" \
196 < libbpf.
pc.template
> $@
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; \
210 define do_install_mkdir
211 if
[ ! -d
'$(DESTDIR_SQ)$1' ]; then \
212 $(INSTALL
) -d
-m
755 '$(DESTDIR_SQ)$1'; \
217 if
[ ! -d
'$(DESTDIR_SQ)$2' ]; then \
218 $(INSTALL
) -d
-m
755 '$(DESTDIR_SQ)$2'; \
220 $(INSTALL
) $1 $(if
$3,-m
$3,) '$(DESTDIR_SQ)$2'
224 $(call QUIET_INSTALL
, $(LIB_TARGET
)) \
225 $(call do_install_mkdir
,$(libdir_SQ
)); \
226 cp
-fpR
$(LIB_FILE
) $(DESTDIR
)$(libdir_SQ
)
229 $(call QUIET_INSTALL
, headers
) \
230 $(call do_install
,bpf.h
,$(prefix)/include/bpf
,644); \
231 $(call do_install
,libbpf.h
,$(prefix)/include/bpf
,644); \
232 $(call do_install
,btf.h
,$(prefix)/include/bpf
,644); \
233 $(call do_install
,libbpf_util.h
,$(prefix)/include/bpf
,644); \
234 $(call do_install
,xsk.h
,$(prefix)/include/bpf
,644);
236 install_pkgconfig
: $(PC_FILE
)
237 $(call QUIET_INSTALL
, $(PC_FILE
)) \
238 $(call do_install
,$(PC_FILE
),$(libdir_SQ
)/pkgconfig
,644)
240 install: install_lib install_pkgconfig
245 $(call QUIET_CLEAN
, config
)
246 $(Q
)$(MAKE
) -C
$(srctree
)/tools
/build
/feature
/ clean >/dev
/null
249 $(call QUIET_CLEAN
, libbpf
) $(RM
) $(TARGETS
) $(CXX_TEST_TARGET
) \
250 *.o
*~
*.a
*.so
*.so.
$(VERSION
) .
*.d .
*.cmd
*.
pc LIBBPF-CFLAGS
251 $(call QUIET_CLEAN
, core-gen
) $(RM
) $(OUTPUT
)FEATURE-DUMP.libbpf
255 PHONY
+= force elfdep bpfdep
259 @if
[ "$(feature-libelf)" != "1" ]; then echo
"No libelf found"; exit
1 ; fi
262 @if
[ "$(feature-bpf)" != "1" ]; then echo
"BPF API too old"; exit
1 ; fi
264 # Declare the contents of the .PHONY variable as phony. We keep that
265 # information in a variable so we can use it in if_changed and friends.