2018-10-05 16:40:00 -07:00
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2015-07-01 02:13:51 +00:00
# Most of this file is copied from tools/lib/traceevent/Makefile
2020-08-18 21:27:58 -07:00
RM ?= rm
2024-03-06 19:42:22 +09:00
srctree := $( realpath $( srctree))
2020-08-18 21:27:58 -07:00
2021-08-15 00:06:00 -07:00
VERSION_SCRIPT := libbpf.map
2019-08-14 13:05:48 -07:00
LIBBPF_VERSION := $( shell \
2021-08-15 00:06:00 -07:00
grep -oE '^LIBBPF_([0-9.]+)' $( VERSION_SCRIPT) | \
2019-08-14 13:05:48 -07:00
sort -rV | head -n1 | cut -d'_' -f2)
2021-09-08 14:32:26 -07:00
LIBBPF_MAJOR_VERSION := $( word 1,$( subst ., ,$( LIBBPF_VERSION)))
LIBBPF_MINOR_VERSION := $( word 2,$( subst ., ,$( LIBBPF_VERSION)))
2015-07-01 02:13:51 +00:00
MAKEFLAGS += --no-print-directory
2019-09-26 19:13:44 -06:00
# 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
ifndef building_out_of_srctree
2016-11-22 09:30:26 +01:00
srctree := $( patsubst %/,%,$( dir $( CURDIR)))
2016-01-11 13:48:00 +00:00
srctree := $( patsubst %/,%,$( dir $( srctree)))
srctree := $( patsubst %/,%,$( dir $( srctree)))
#$(info Determined 'srctree' to be $(srctree))
endif
2015-07-01 02:13:51 +00:00
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))'
2016-01-11 13:48:00 +00:00
include $( srctree ) /tools/scripts/Makefile.arch
2015-07-01 02:13:51 +00:00
ifeq ( $( LP 64) , 1)
libdir_relative = lib64
else
libdir_relative = lib
endif
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
2019-01-15 13:13:46 -08:00
include $( srctree ) /tools/scripts/Makefile.include
2015-07-01 02:13:51 +00:00
# copy a bit from Linux kbuild
2022-02-11 14:14:11 +09:00
INCLUDES = -I$( or $( OUTPUT) ,.) \
2024-09-27 15:13:52 +02:00
-I$( srctree) /tools/include -I$( srctree) /tools/include/uapi \
-I$( srctree) /tools/arch/$( SRCARCH) /include
2015-07-01 02:13:51 +00:00
export prefix libdir src obj
# Shell quotes
libdir_SQ = $( subst ',' \' ' ,$( libdir))
libdir_relative_SQ = $( subst ',' \' ' ,$( libdir_relative))
OBJ = $@
N =
2019-03-23 01:49:10 +01:00
LIB_TARGET = libbpf.a libbpf.so.$( LIBBPF_VERSION)
LIB_FILE = libbpf.a libbpf.so*
2019-03-28 11:33:53 +00:00
PC_FILE = libbpf.pc
2015-07-01 02:13:51 +00:00
# Set compile option CFLAGS
ifdef EXTRA_CFLAGS
CFLAGS := $( EXTRA_CFLAGS)
else
2020-09-29 15:06:03 -07:00
CFLAGS := -g -O2
2015-07-01 02:13:51 +00:00
endif
# Append required CFLAGS
2021-11-06 05:12:40 +05:30
override CFLAGS += -std= gnu89
2020-08-18 14:33:50 -07:00
override CFLAGS += $( EXTRA_WARNINGS) -Wno-switch-enum
2015-07-01 02:13:51 +00:00
override CFLAGS += -Werror -Wall
override CFLAGS += $( INCLUDES)
2018-10-15 22:50:34 -07:00
override CFLAGS += -fvisibility= hidden
2019-08-15 15:13:54 +03:00
override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS= 64
2021-12-16 16:38:40 +00:00
override CFLAGS += $( CLANG_CROSS_FLAGS)
2015-07-01 02:13:51 +00:00
2019-09-30 14:02:03 -07:00
# flags specific for shared library
2020-09-29 15:06:04 -07:00
SHLIB_FLAGS := -DSHARED -fPIC
2019-09-30 14:02:03 -07:00
2017-03-09 16:16:33 -08:00
# Disable command line variables (CFLAGS) override from top
2015-07-01 02:13:51 +00:00
# level Makefile (perf), otherwise build Makefile will get
# the same command line setup.
MAKEOVERRIDES =
2015-09-23 12:34:02 +02:00
all :
2024-07-15 13:32:44 -07:00
OUTPUT ?= ./
OUTPUT := $( abspath $( OUTPUT)) /
2015-07-01 02:13:51 +00:00
export srctree OUTPUT CC LD CFLAGS V
2015-09-23 12:33:56 +02:00
include $( srctree ) /tools/build/Makefile.include
2015-07-01 02:13:51 +00:00
2019-09-30 14:02:03 -07:00
SHARED_OBJDIR := $( OUTPUT) sharedobjs/
STATIC_OBJDIR := $( OUTPUT) staticobjs/
BPF_IN_SHARED := $( SHARED_OBJDIR) libbpf-in.o
BPF_IN_STATIC := $( STATIC_OBJDIR) libbpf-in.o
2019-12-23 15:13:26 +09:00
BPF_HELPER_DEFS := $( OUTPUT) bpf_helper_defs.h
2021-09-13 15:23:09 -07:00
BPF_GENERATED := $( BPF_HELPER_DEFS)
2019-03-23 01:49:10 +01:00
LIB_TARGET := $( addprefix $( OUTPUT) ,$( LIB_TARGET))
LIB_FILE := $( addprefix $( OUTPUT) ,$( LIB_FILE))
2019-03-28 11:33:53 +00:00
PC_FILE := $( addprefix $( OUTPUT) ,$( PC_FILE))
2015-07-01 02:13:51 +00:00
2019-10-04 17:34:44 +02:00
TAGS_PROG := $(if $( shell which etags 2>/dev/null) ,etags,ctags)
2019-09-30 14:02:03 -07:00
GLOBAL_SYM_COUNT = $( shell readelf -s --wide $( BPF_IN_SHARED) | \
2019-08-27 02:25:27 +00:00
cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
2019-12-13 07:11:14 -03:00
sed 's/\[.*\]//' | \
2022-05-18 11:59:13 -07:00
awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
2019-08-27 02:25:27 +00:00
sort -u | wc -l)
2020-05-25 09:18:46 +03:00
VERSIONED_SYM_COUNT = $( shell readelf --dyn-syms --wide $( OUTPUT) libbpf.so | \
2020-11-18 22:13:50 +01:00
sed 's/\[.*\]//' | \
2022-02-04 13:43:55 -08:00
awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
2018-11-23 16:44:34 -08:00
grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
2019-12-02 13:59:31 -08:00
CMD_TARGETS = $( LIB_TARGET) $( PC_FILE)
2015-07-01 02:13:51 +00:00
2019-03-06 11:59:27 -08:00
all : fixdep
$( Q)$( MAKE) all_cmd
2015-07-01 02:13:51 +00:00
2018-11-23 16:44:34 -08:00
all_cmd : $( CMD_TARGETS ) check
2015-07-01 02:13:51 +00:00
2024-07-15 13:32:44 -07:00
$(SHARED_OBJDIR) $(STATIC_OBJDIR) :
$( Q) mkdir -p $@
$(BPF_IN_SHARED) : force $( BPF_GENERATED ) | $( SHARED_OBJDIR )
2017-08-15 11:33:30 -05:00
@( test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
2016-07-11 16:38:05 -03:00
( diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
2017-07-30 11:51:30 +02:00
echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >& 2 )) || true
2017-08-15 11:33:30 -05:00
@( test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
2016-07-11 16:38:05 -03:00
( diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
2017-07-30 11:51:30 +02:00
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
2019-02-21 10:21:26 +01:00
@( 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
2024-07-15 13:32:44 -07:00
$( SILENT_MAKE) -C $( srctree) /tools/build CFLAGS = LDFLAGS = OUTPUT = $( SHARED_OBJDIR) $( SHARED_OBJDIR) fixdep
2019-09-30 14:02:03 -07:00
$( Q)$( MAKE) $( build) = libbpf OUTPUT = $( SHARED_OBJDIR) CFLAGS = " $( CFLAGS) $( SHLIB_FLAGS) "
2024-07-15 13:32:44 -07:00
$(BPF_IN_STATIC) : force $( BPF_GENERATED ) | $( STATIC_OBJDIR )
$( SILENT_MAKE) -C $( srctree) /tools/build CFLAGS = LDFLAGS = OUTPUT = $( STATIC_OBJDIR) $( STATIC_OBJDIR) fixdep
2019-09-30 14:02:03 -07:00
$( Q)$( MAKE) $( build) = libbpf OUTPUT = $( STATIC_OBJDIR)
2015-07-01 02:13:51 +00:00
2019-12-23 15:13:26 +09:00
$(BPF_HELPER_DEFS) : $( srctree ) /tools /include /uapi /linux /bpf .h
2021-03-02 09:19:41 -08:00
$( QUIET_GEN)$( srctree) /scripts/bpf_doc.py --header \
2019-12-23 15:13:26 +09:00
--file $( srctree) /tools/include/uapi/linux/bpf.h > $( BPF_HELPER_DEFS)
2019-10-08 10:59:40 -07:00
2019-03-23 01:49:10 +01:00
$(OUTPUT)libbpf.so : $( OUTPUT ) libbpf .so .$( LIBBPF_VERSION )
2021-08-15 00:06:00 -07:00
$(OUTPUT)libbpf.so.$(LIBBPF_VERSION) : $( BPF_IN_SHARED ) $( VERSION_SCRIPT )
2021-12-16 16:38:40 +00:00
$( QUIET_LINK)$( CC) $( CFLAGS) $( LDFLAGS) \
2019-10-11 03:28:05 +03:00
--shared -Wl,-soname,libbpf.so.$( LIBBPF_MAJOR_VERSION) \
2021-08-15 00:06:00 -07:00
-Wl,--version-script= $( VERSION_SCRIPT) $< -lelf -lz -o $@
2019-03-23 01:49:10 +01:00
@ln -sf $( @F) $( OUTPUT) libbpf.so
2019-08-14 13:05:48 -07:00
@ln -sf $( @F) $( OUTPUT) libbpf.so.$( LIBBPF_MAJOR_VERSION)
2015-07-01 02:13:51 +00:00
2019-09-30 14:02:03 -07:00
$(OUTPUT)libbpf.a : $( BPF_IN_STATIC )
2020-08-18 21:27:58 -07:00
$( QUIET_LINK)$( RM) -f $@ ; $( AR) rcs $@ $^
2015-07-01 02:13:51 +00:00
2019-03-28 11:33:53 +00:00
$(OUTPUT)libbpf.pc :
$( QUIET_GEN) sed -e "s|@PREFIX@| $( prefix) |" \
-e "s|@LIBDIR@| $( libdir_SQ) |" \
-e "s|@VERSION@| $( LIBBPF_VERSION) |" \
< libbpf.pc.template > $@
2021-09-13 15:23:09 -07:00
check : check_abi check_version
2018-11-23 16:44:34 -08:00
2021-08-15 00:06:00 -07:00
check_abi : $( OUTPUT ) libbpf .so $( VERSION_SCRIPT )
2018-11-23 16:44:34 -08:00
@if [ " $( GLOBAL_SYM_COUNT) " != " $( VERSIONED_SYM_COUNT) " ] ; then \
2019-09-30 14:02:03 -07:00
echo "Warning: Num of global symbols in $( BPF_IN_SHARED) " \
2018-11-23 16:44:34 -08:00
"( $( 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; \
2019-11-27 12:01:34 -08:00
readelf -s --wide $( BPF_IN_SHARED) | \
2019-08-27 02:25:27 +00:00
cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
2019-12-13 07:11:14 -03:00
sed 's/\[.*\]//' | \
2019-12-01 20:57:28 +01:00
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}' | \
2019-05-22 10:51:28 -07:00
sort -u > $( OUTPUT) libbpf_global_syms.tmp; \
2020-05-25 09:18:46 +03:00
readelf --dyn-syms --wide $( OUTPUT) libbpf.so | \
2020-11-18 22:13:50 +01:00
sed 's/\[.*\]//' | \
2022-02-04 13:43:55 -08:00
awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
2019-05-22 10:51:28 -07:00
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; \
2018-11-23 16:44:34 -08:00
exit 1; \
fi
2021-10-06 12:10:49 +01:00
HDR_MAJ_VERSION := $( shell grep -oE '^$(pound)define LIBBPF_MAJOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
HDR_MIN_VERSION := $( shell grep -oE '^$(pound)define LIBBPF_MINOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
2021-09-13 15:23:09 -07:00
check_version : $( VERSION_SCRIPT ) libbpf_version .h
@if [ " $( HDR_MAJ_VERSION) " != " $( LIBBPF_MAJOR_VERSION) " ] ; then \
echo "Error: libbpf major version mismatch detected: " \
"' $( HDR_MAJ_VERSION) ' != ' $( LIBBPF_MAJOR_VERSION) '" >& 2; \
exit 1; \
fi
@if [ " $( HDR_MIN_VERSION) " != " $( LIBBPF_MINOR_VERSION) " ] ; then \
echo "Error: libbpf minor version mismatch detected: " \
"' $( HDR_MIN_VERSION) ' != ' $( LIBBPF_MINOR_VERSION) '" >& 2; \
exit 1; \
fi
2019-03-23 01:49:10 +01:00
define do_install_mkdir
if [ ! -d '$(DESTDIR_SQ)$1' ]; then \
$( INSTALL ) -d -m 755 '$(DESTDIR_SQ)$1' ; \
fi
endef
2015-07-01 02:13:51 +00:00
define do_install
if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
$( INSTALL ) -d -m 755 '$(DESTDIR_SQ)$2' ; \
fi; \
2021-03-08 10:30:38 -08:00
$( INSTALL ) $( if $ 3,-m $ 3,) $1 '$(DESTDIR_SQ)$2'
2015-07-01 02:13:51 +00:00
endef
install_lib : all_cmd
2019-03-23 01:49:10 +01:00
$( call QUIET_INSTALL, $( LIB_TARGET)) \
$( call do_install_mkdir,$( libdir_SQ)) ; \
cp -fpR $( LIB_FILE) $( DESTDIR)$( libdir_SQ)
2015-07-01 02:13:51 +00:00
2022-06-27 14:15:13 -07:00
SRC_HDRS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h \
2021-10-07 20:44:27 +01:00
bpf_helpers.h bpf_tracing.h bpf_endian.h bpf_core_read.h \
2022-04-04 16:41:56 -07:00
skel_internal.h libbpf_version.h usdt.bpf.h
2021-10-07 20:44:27 +01:00
GEN_HDRS := $( BPF_GENERATED)
2021-06-02 17:40:24 -07:00
2021-10-07 20:44:27 +01:00
INSTALL_PFX := $( DESTDIR)$( prefix) /include/bpf
INSTALL_SRC_HDRS := $( addprefix $( INSTALL_PFX) /, $( SRC_HDRS))
INSTALL_GEN_HDRS := $( addprefix $( INSTALL_PFX) /, $( notdir $( GEN_HDRS)))
$(INSTALL_SRC_HDRS) : $( INSTALL_PFX ) /%.h : %.h
$( call QUIET_INSTALL, $@ ) \
$( call do_install,$<,$( prefix) /include/bpf,644)
$(INSTALL_GEN_HDRS) : $( INSTALL_PFX ) /%.h : $( OUTPUT ) %.h
$( call QUIET_INSTALL, $@ ) \
$( call do_install,$<,$( prefix) /include/bpf,644)
install_headers : $( BPF_GENERATED ) $( INSTALL_SRC_HDRS ) $( INSTALL_GEN_HDRS )
2022-11-16 16:43:52 -08:00
$( call QUIET_INSTALL, libbpf_headers)
2017-07-25 11:17:11 -07:00
2019-03-28 11:33:53 +00:00
install_pkgconfig : $( PC_FILE )
$( call QUIET_INSTALL, $( PC_FILE)) \
$( call do_install,$( PC_FILE) ,$( libdir_SQ) /pkgconfig,644)
2020-05-26 20:46:12 +03:00
install : install_lib install_pkgconfig install_headers
2015-07-01 02:13:51 +00:00
2024-07-15 13:32:44 -07:00
clean : fixdep -clean
2020-01-09 21:17:14 -08:00
$( call QUIET_CLEAN, libbpf) $( RM) -rf $( CMD_TARGETS) \
2021-09-08 14:32:26 -07:00
*~ .*.d .*.cmd LIBBPF-CFLAGS $( BPF_GENERATED) \
2020-01-09 21:17:14 -08:00
$( SHARED_OBJDIR) $( STATIC_OBJDIR) \
$( addprefix $( OUTPUT) , \
*.o *.a *.so *.so.$( LIBBPF_MAJOR_VERSION) *.pc)
2015-07-01 02:13:51 +00:00
2021-09-13 15:23:09 -07:00
PHONY += force cscope tags check check_abi check_version
2015-07-01 02:13:51 +00:00
force :
2019-10-04 17:34:44 +02:00
cscope :
ls *.c *.h > cscope.files
cscope -b -q -I $( srctree) /include -f cscope.out
tags :
2020-08-18 21:27:58 -07:00
$( RM) -f TAGS tags
2019-10-04 17:34:44 +02:00
ls *.c *.h | xargs $( TAGS_PROG) -a
2015-07-01 02:13:51 +00:00
# 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 )
2019-10-20 13:23:44 +02:00
# Delete partially updated (corrupted) files on error
.DELETE_ON_ERROR :
2022-12-02 16:17:38 +08:00
help :
@echo 'libbpf common targets:'
@echo ' HINT: use "V=1" to enable verbose build'
@echo ' all - build libraries and pkgconfig'
@echo ' clean - remove all generated files'
2023-07-21 23:52:36 -07:00
@echo ' check - check ABI and version info'
2022-12-02 16:17:38 +08:00
@echo ''
@echo 'libbpf install targets:'
@echo ' HINT: use "prefix"(defaults to "/usr/local") or "DESTDIR" (defaults to "/")'
2023-07-21 23:52:36 -07:00
@echo ' to adjust target destination, e.g. "make prefix=/usr/local install"'
2022-12-02 16:17:38 +08:00
@echo ' install - build and install all headers, libraries and pkgconfig'
@echo ' install_headers - install only headers to include/bpf'
@echo ''
@echo 'libbpf make targets:'
@echo ' tags - use ctags to make tag information for source code browsing'
@echo ' cscope - use cscope to make interactive source code browsing database'