2021-05-27 00:09:36 +05:00
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
# Most of this file is copied from tools/lib/traceevent/Makefile
RM ?= rm
srctree = $( abs_srctree)
LIBBPF_VERSION := $( shell \
grep -oE '^LIBBPF_([0-9.]+)' libbpf.map | \
sort -rV | head -n1 | cut -d'_' -f2)
LIBBPF_MAJOR_VERSION := $( firstword $( subst ., ,$( LIBBPF_VERSION) ) )
MAKEFLAGS += --no-print-directory
# This will work when bpf is built in tools env. where srctree
# isn't set and when invoked from selftests build, where srctree
# is a ".". building_out_of_srctree is undefined for in srctree
# builds
i f n d e f b u i l d i n g _ o u t _ o f _ s r c t r e e
srctree := $( patsubst %/,%,$( dir $( CURDIR) ) )
srctree := $( patsubst %/,%,$( dir $( srctree) ) )
srctree := $( patsubst %/,%,$( dir $( srctree) ) )
#$(info Determined 'srctree' to be $(srctree))
e n d i f
INSTALL = install
# Use DESTDIR for installing into a different root directory.
# This is useful for building a package. The program will be
# installed in this directory as if it was the root directory.
# Then the build tool can move it later.
DESTDIR ?=
DESTDIR_SQ = '$(subst ' ,'\' ',$(DESTDIR))'
i n c l u d e $( srctree ) / t o o l s / s c r i p t s / M a k e f i l e . a r c h
i f e q ( $( LP 64) , 1 )
libdir_relative = lib64
e l s e
libdir_relative = lib
e n d i f
prefix ?= /usr/local
libdir = $( prefix) /$( libdir_relative)
man_dir = $( prefix) /share/man
man_dir_SQ = '$(subst ' ,'\' ',$(man_dir))'
export man_dir man_dir_SQ INSTALL
export DESTDIR DESTDIR_SQ
i n c l u d e $( srctree ) / t o o l s / s c r i p t s / M a k e f i l e . i n c l u d e
# copy a bit from Linux kbuild
i f e q ( "$(origin V)" , "command line" )
VERBOSE = $( V)
e n d i f
i f n d e f V E R B O S E
VERBOSE = 0
e n d i f
INCLUDES = -I. -I$( srctree) /tools/include -I$( srctree) /tools/include/uapi
export prefix libdir src obj
# Shell quotes
libdir_SQ = $( subst ',' \' ' ,$( libdir) )
libdir_relative_SQ = $( subst ',' \' ' ,$( libdir_relative) )
OBJ = $@
N =
LIB_TARGET = libbpf.a libbpf.so.$( LIBBPF_VERSION)
LIB_FILE = libbpf.a libbpf.so*
PC_FILE = libbpf.pc
# Set compile option CFLAGS
i f d e f E X T R A _ C F L A G S
CFLAGS := $( EXTRA_CFLAGS)
e l s e
CFLAGS := -g -O2
e n d i f
# Append required CFLAGS
o v e r r i d e CFLAGS += $( EXTRA_WARNINGS) -Wno-switch-enum
o v e r r i d e CFLAGS += -Werror -Wall
o v e r r i d e CFLAGS += $( INCLUDES)
o v e r r i d e CFLAGS += -fvisibility= hidden
o v e r r i d e CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS= 64
# flags specific for shared library
SHLIB_FLAGS := -DSHARED -fPIC
i f e q ( $( VERBOSE ) , 1 )
Q =
e l s e
Q = @
e n d i f
# Disable command line variables (CFLAGS) override from top
# level Makefile (perf), otherwise build Makefile will get
# the same command line setup.
MAKEOVERRIDES =
all :
export srctree OUTPUT CC LD CFLAGS V
i n c l u d e $( srctree ) / t o o l s / b u i l d / M a k e f i l e . i n c l u d e
SHARED_OBJDIR := $( OUTPUT) sharedobjs/
STATIC_OBJDIR := $( OUTPUT) staticobjs/
BPF_IN_SHARED := $( SHARED_OBJDIR) libbpf-in.o
BPF_IN_STATIC := $( STATIC_OBJDIR) libbpf-in.o
VERSION_SCRIPT := libbpf.map
BPF_HELPER_DEFS := $( OUTPUT) bpf_helper_defs.h
LIB_TARGET := $( addprefix $( OUTPUT) ,$( LIB_TARGET) )
LIB_FILE := $( addprefix $( OUTPUT) ,$( LIB_FILE) )
PC_FILE := $( addprefix $( OUTPUT) ,$( PC_FILE) )
TAGS_PROG := $( if $( shell which etags 2>/dev/null) ,etags,ctags)
GLOBAL_SYM_COUNT = $( shell readelf -s --wide $( BPF_IN_SHARED) | \
cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
sed 's/\[.*\]//' | \
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}' | \
sort -u | wc -l)
VERSIONED_SYM_COUNT = $( shell readelf --dyn-syms --wide $( OUTPUT) libbpf.so | \
sed 's/\[.*\]//' | \
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}' | \
grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
CMD_TARGETS = $( LIB_TARGET) $( PC_FILE)
all : fixdep
$( Q) $( MAKE) all_cmd
all_cmd : $( CMD_TARGETS ) check
$(BPF_IN_SHARED) : force $( BPF_HELPER_DEFS )
@( test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
( diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >& 2 ) ) || true
@( test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
( diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
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
@( test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \
( diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \
echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >& 2 ) ) || true
@( test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \
( diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \
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
@( test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
( diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
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
$( Q) $( MAKE) $( build) = libbpf OUTPUT = $( SHARED_OBJDIR) CFLAGS = " $( CFLAGS) $( SHLIB_FLAGS) "
$(BPF_IN_STATIC) : force $( BPF_HELPER_DEFS )
$( Q) $( MAKE) $( build) = libbpf OUTPUT = $( STATIC_OBJDIR)
$(BPF_HELPER_DEFS) : $( srctree ) /tools /include /uapi /linux /bpf .h
2021-07-13 00:01:19 +05:00
$( QUIET_GEN) $( srctree) /scripts/bpf_doc.py --header \
2021-05-27 00:09:36 +05:00
--file $( srctree) /tools/include/uapi/linux/bpf.h > $( BPF_HELPER_DEFS)
$(OUTPUT)libbpf.so : $( OUTPUT ) libbpf .so .$( LIBBPF_VERSION )
$(OUTPUT)libbpf.so.$(LIBBPF_VERSION) : $( BPF_IN_SHARED )
$( QUIET_LINK) $( CC) $( LDFLAGS) \
--shared -Wl,-soname,libbpf.so.$( LIBBPF_MAJOR_VERSION) \
-Wl,--version-script= $( VERSION_SCRIPT) $^ -lelf -lz -o $@
@ln -sf $( @F) $( OUTPUT) libbpf.so
@ln -sf $( @F) $( OUTPUT) libbpf.so.$( LIBBPF_MAJOR_VERSION)
$(OUTPUT)libbpf.a : $( BPF_IN_STATIC )
$( QUIET_LINK) $( RM) -f $@ ; $( AR) rcs $@ $^
$(OUTPUT)libbpf.pc :
$( QUIET_GEN) sed -e " s|@PREFIX@| $( prefix) | " \
-e " s|@LIBDIR@| $( libdir_SQ) | " \
-e " s|@VERSION@| $( LIBBPF_VERSION) | " \
< libbpf.pc.template > $@
check : check_abi
check_abi : $( OUTPUT ) libbpf .so
@if [ " $( GLOBAL_SYM_COUNT) " != " $( VERSIONED_SYM_COUNT) " ] ; then \
echo " Warning: Num of global symbols in $( BPF_IN_SHARED) " \
" ( $( GLOBAL_SYM_COUNT) ) does NOT match with num of " \
" versioned symbols in $^ ( $( VERSIONED_SYM_COUNT) ). " \
"Please make sure all LIBBPF_API symbols are" \
" versioned in $( VERSION_SCRIPT) . " >& 2; \
readelf -s --wide $( BPF_IN_SHARED) | \
cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
sed 's/\[.*\]//' | \
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}' | \
sort -u > $( OUTPUT) libbpf_global_syms.tmp; \
readelf --dyn-syms --wide $( OUTPUT) libbpf.so | \
sed 's/\[.*\]//' | \
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}' | \
grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | \
sort -u > $( OUTPUT) libbpf_versioned_syms.tmp; \
diff -u $( OUTPUT) libbpf_global_syms.tmp \
$( OUTPUT) libbpf_versioned_syms.tmp; \
rm $( OUTPUT) libbpf_global_syms.tmp \
$( OUTPUT) libbpf_versioned_syms.tmp; \
exit 1; \
fi
d e f i n e d o _ i n s t a l l _ m k d i r
if [ ! -d '$(DESTDIR_SQ)$1' ] ; then \
$( INSTALL) -d -m 755 '$(DESTDIR_SQ)$1' ; \
fi
e n d e f
d e f i n e d o _ i n s t a l l
if [ ! -d '$(DESTDIR_SQ)$2' ] ; then \
$( INSTALL) -d -m 755 '$(DESTDIR_SQ)$2' ; \
fi ; \
$( INSTALL) $( if $3 ,-m $3 ,) $1 '$(DESTDIR_SQ)$2'
e n d e f
install_lib : all_cmd
$( call QUIET_INSTALL, $( LIB_TARGET) ) \
$( call do_install_mkdir,$( libdir_SQ) ) ; \
cp -fpR $( LIB_FILE) $( DESTDIR) $( libdir_SQ)
2021-07-22 21:18:54 +05:00
INSTALL_HEADERS = bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h xsk.h \
bpf_helpers.h $( BPF_HELPER_DEFS) bpf_tracing.h \
bpf_endian.h bpf_core_read.h skel_internal.h
2021-05-27 00:09:36 +05:00
install_headers : $( BPF_HELPER_DEFS )
2021-07-22 21:18:54 +05:00
$( call QUIET_INSTALL, headers) \
$( foreach hdr,$( INSTALL_HEADERS) , \
$( call do_install,$( hdr) ,$( prefix) /include/bpf,644) ; )
2021-05-27 00:09:36 +05:00
install_pkgconfig : $( PC_FILE )
$( call QUIET_INSTALL, $( PC_FILE) ) \
$( call do_install,$( PC_FILE) ,$( libdir_SQ) /pkgconfig,644)
install : install_lib install_pkgconfig install_headers
clean :
$( call QUIET_CLEAN, libbpf) $( RM) -rf $( CMD_TARGETS) \
*~ .*.d .*.cmd LIBBPF-CFLAGS $( BPF_HELPER_DEFS) \
$( SHARED_OBJDIR) $( STATIC_OBJDIR) \
$( addprefix $( OUTPUT) , \
*.o *.a *.so *.so.$( LIBBPF_MAJOR_VERSION) *.pc)
PHONY += force cscope tags
force :
cscope :
ls *.c *.h > cscope.files
cscope -b -q -I $( srctree) /include -f cscope.out
tags :
$( RM) -f TAGS tags
ls *.c *.h | xargs $( TAGS_PROG) -a
# Declare the contents of the .PHONY variable as phony. We keep that
# information in a variable so we can use it in if_changed and friends.
.PHONY : $( PHONY )
# Delete partially updated (corrupted) files on error
.DELETE_ON_ERROR :