11bc38b8fSAlexei Starovoitov# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 21b76c13eSWang Nan# Most of this file is copied from tools/lib/traceevent/Makefile 31b76c13eSWang Nan 41b76c13eSWang NanBPF_VERSION = 0 51b76c13eSWang NanBPF_PATCHLEVEL = 0 61b76c13eSWang NanBPF_EXTRAVERSION = 1 71b76c13eSWang Nan 81b76c13eSWang NanMAKEFLAGS += --no-print-directory 91b76c13eSWang Nan 10d5ef3140SNaveen N. Raoifeq ($(srctree),) 11e19b7ceeSUwe Kleine-Königsrctree := $(patsubst %/,%,$(dir $(CURDIR))) 12d5ef3140SNaveen N. Raosrctree := $(patsubst %/,%,$(dir $(srctree))) 13d5ef3140SNaveen N. Raosrctree := $(patsubst %/,%,$(dir $(srctree))) 14d5ef3140SNaveen N. Rao#$(info Determined 'srctree' to be $(srctree)) 15d5ef3140SNaveen N. Raoendif 161b76c13eSWang Nan 171b76c13eSWang Nan# Makefiles suck: This macro sets a default value of $(2) for the 181b76c13eSWang Nan# variable named by $(1), unless the variable has been set by 191b76c13eSWang Nan# environment or command line. This is necessary for CC and AR 201b76c13eSWang Nan# because make sets default values, so the simpler ?= approach 211b76c13eSWang Nan# won't work as expected. 221b76c13eSWang Nandefine allow-override 231b76c13eSWang Nan $(if $(or $(findstring environment,$(origin $(1))),\ 241b76c13eSWang Nan $(findstring command line,$(origin $(1)))),,\ 251b76c13eSWang Nan $(eval $(1) = $(2))) 261b76c13eSWang Nanendef 271b76c13eSWang Nan 281b76c13eSWang Nan# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix. 291b76c13eSWang Nan$(call allow-override,CC,$(CROSS_COMPILE)gcc) 301b76c13eSWang Nan$(call allow-override,AR,$(CROSS_COMPILE)ar) 311b76c13eSWang Nan 321b76c13eSWang NanINSTALL = install 331b76c13eSWang Nan 341b76c13eSWang Nan# Use DESTDIR for installing into a different root directory. 351b76c13eSWang Nan# This is useful for building a package. The program will be 361b76c13eSWang Nan# installed in this directory as if it was the root directory. 371b76c13eSWang Nan# Then the build tool can move it later. 381b76c13eSWang NanDESTDIR ?= 391b76c13eSWang NanDESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' 401b76c13eSWang Nan 41d5ef3140SNaveen N. Raoinclude $(srctree)/tools/scripts/Makefile.arch 42d5ef3140SNaveen N. Rao 431b76c13eSWang Nanifeq ($(LP64), 1) 441b76c13eSWang Nan libdir_relative = lib64 451b76c13eSWang Nanelse 461b76c13eSWang Nan libdir_relative = lib 471b76c13eSWang Nanendif 481b76c13eSWang Nan 491b76c13eSWang Nanprefix ?= /usr/local 501b76c13eSWang Nanlibdir = $(prefix)/$(libdir_relative) 511b76c13eSWang Nanman_dir = $(prefix)/share/man 521b76c13eSWang Nanman_dir_SQ = '$(subst ','\'',$(man_dir))' 531b76c13eSWang Nan 541b76c13eSWang Nanexport man_dir man_dir_SQ INSTALL 551b76c13eSWang Nanexport DESTDIR DESTDIR_SQ 561b76c13eSWang Nan 571b76c13eSWang Naninclude ../../scripts/Makefile.include 581b76c13eSWang Nan 591b76c13eSWang Nan# copy a bit from Linux kbuild 601b76c13eSWang Nan 611b76c13eSWang Nanifeq ("$(origin V)", "command line") 621b76c13eSWang Nan VERBOSE = $(V) 631b76c13eSWang Nanendif 641b76c13eSWang Nanifndef VERBOSE 651b76c13eSWang Nan VERBOSE = 0 661b76c13eSWang Nanendif 671b76c13eSWang Nan 6865f041beSArnaldo Carvalho de MeloFEATURE_USER = .libbpf 698c4905b9SStanislav FomichevFEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx 7020517cd9SArnaldo Carvalho de MeloFEATURE_DISPLAY = libelf bpf 711b76c13eSWang Nan 72438363c0SYonghong SongINCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi 731b76c13eSWang NanFEATURE_CHECK_CFLAGS-bpf = $(INCLUDES) 741b76c13eSWang Nan 75d8ad6a15SWang Nancheck_feat := 1 76d8ad6a15SWang NanNON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help 77d8ad6a15SWang Nanifdef MAKECMDGOALS 78d8ad6a15SWang Nanifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) 79d8ad6a15SWang Nan check_feat := 0 80d8ad6a15SWang Nanendif 81d8ad6a15SWang Nanendif 82d8ad6a15SWang Nan 83d8ad6a15SWang Nanifeq ($(check_feat),1) 8458683600SJiri Olsaifeq ($(FEATURES_DUMP),) 851b76c13eSWang Naninclude $(srctree)/tools/build/Makefile.feature 8658683600SJiri Olsaelse 8758683600SJiri Olsainclude $(FEATURES_DUMP) 8858683600SJiri Olsaendif 89d8ad6a15SWang Nanendif 901b76c13eSWang Nan 911b76c13eSWang Nanexport prefix libdir src obj 921b76c13eSWang Nan 931b76c13eSWang Nan# Shell quotes 941b76c13eSWang Nanlibdir_SQ = $(subst ','\'',$(libdir)) 951b76c13eSWang Nanlibdir_relative_SQ = $(subst ','\'',$(libdir_relative)) 961b76c13eSWang Nan 971b76c13eSWang NanLIB_FILE = libbpf.a libbpf.so 981b76c13eSWang Nan 991b76c13eSWang NanVERSION = $(BPF_VERSION) 1001b76c13eSWang NanPATCHLEVEL = $(BPF_PATCHLEVEL) 1011b76c13eSWang NanEXTRAVERSION = $(BPF_EXTRAVERSION) 1021b76c13eSWang Nan 1031b76c13eSWang NanOBJ = $@ 1041b76c13eSWang NanN = 1051b76c13eSWang Nan 1061b76c13eSWang NanLIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION) 1071b76c13eSWang Nan 1081b76c13eSWang Nan# Set compile option CFLAGS 1091b76c13eSWang Nanifdef EXTRA_CFLAGS 1101b76c13eSWang Nan CFLAGS := $(EXTRA_CFLAGS) 1111b76c13eSWang Nanelse 1121b76c13eSWang Nan CFLAGS := -g -Wall 1131b76c13eSWang Nanendif 1141b76c13eSWang Nan 1151b76c13eSWang Nanifeq ($(feature-libelf-mmap), 1) 1161b76c13eSWang Nan override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT 1171b76c13eSWang Nanendif 1181b76c13eSWang Nan 119531b014eSJakub Kicinskiifeq ($(feature-reallocarray), 0) 120531b014eSJakub Kicinski override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY 121531b014eSJakub Kicinskiendif 122531b014eSJakub Kicinski 1231b76c13eSWang Nan# Append required CFLAGS 1241b76c13eSWang Nanoverride CFLAGS += $(EXTRA_WARNINGS) 1251b76c13eSWang Nanoverride CFLAGS += -Werror -Wall 1261b76c13eSWang Nanoverride CFLAGS += -fPIC 1271b76c13eSWang Nanoverride CFLAGS += $(INCLUDES) 128ab9e0848SAndrey Ignatovoverride CFLAGS += -fvisibility=hidden 1291b76c13eSWang Nan 1301b76c13eSWang Nanifeq ($(VERBOSE),1) 1311b76c13eSWang Nan Q = 1321b76c13eSWang Nanelse 1331b76c13eSWang Nan Q = @ 1341b76c13eSWang Nanendif 1351b76c13eSWang Nan 136505d3085SMasahiro Yamada# Disable command line variables (CFLAGS) override from top 1371b76c13eSWang Nan# level Makefile (perf), otherwise build Makefile will get 1381b76c13eSWang Nan# the same command line setup. 1391b76c13eSWang NanMAKEOVERRIDES= 1401b76c13eSWang Nan 1417c422f55SJiri Olsaall: 1427c422f55SJiri Olsa 1431b76c13eSWang Nanexport srctree OUTPUT CC LD CFLAGS V 144ab6201d0SJiri Olsainclude $(srctree)/tools/build/Makefile.include 1451b76c13eSWang Nan 1461b76c13eSWang NanBPF_IN := $(OUTPUT)libbpf-in.o 1471b76c13eSWang NanLIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) 14816192a77SAndrey IgnatovVERSION_SCRIPT := libbpf.map 1491b76c13eSWang Nan 150*306b267cSAndrey IgnatovGLOBAL_SYM_COUNT = $(shell readelf -s $(BPF_IN) | \ 151*306b267cSAndrey Ignatov awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}') 152*306b267cSAndrey IgnatovVERSIONED_SYM_COUNT = $(shell readelf -s $(OUTPUT)libbpf.so | \ 153*306b267cSAndrey Ignatov grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l) 154*306b267cSAndrey Ignatov 1551b76c13eSWang NanCMD_TARGETS = $(LIB_FILE) 1561b76c13eSWang Nan 1578c4905b9SStanislav FomichevCXX_TEST_TARGET = $(OUTPUT)test_libbpf 1588c4905b9SStanislav Fomichev 1598c4905b9SStanislav Fomichevifeq ($(feature-cxx), 1) 1608c4905b9SStanislav Fomichev CMD_TARGETS += $(CXX_TEST_TARGET) 1618c4905b9SStanislav Fomichevendif 1628c4905b9SStanislav Fomichev 1631b76c13eSWang NanTARGETS = $(CMD_TARGETS) 1641b76c13eSWang Nan 16563c85910SJesper Dangaard Brouerall: fixdep all_cmd 1661b76c13eSWang Nan 167*306b267cSAndrey Ignatovall_cmd: $(CMD_TARGETS) check 1681b76c13eSWang Nan 1691b76c13eSWang Nan$(BPF_IN): force elfdep bpfdep 170f15d5e6dSDaniel Díaz @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \ 171971e827bSArnaldo Carvalho de Melo (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \ 1728255e1efSIngo Molnar echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true 173f15d5e6dSDaniel Díaz @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \ 174971e827bSArnaldo Carvalho de Melo (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \ 1758255e1efSIngo Molnar 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 176dc2b9f19SEric Leblond @(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \ 177dc2b9f19SEric Leblond (diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \ 178dc2b9f19SEric Leblond echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true 179dc2b9f19SEric Leblond @(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \ 180dc2b9f19SEric Leblond (diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \ 181dc2b9f19SEric Leblond 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 1821b76c13eSWang Nan $(Q)$(MAKE) $(build)=libbpf 1831b76c13eSWang Nan 1841b76c13eSWang Nan$(OUTPUT)libbpf.so: $(BPF_IN) 18516192a77SAndrey Ignatov $(QUIET_LINK)$(CC) --shared -Wl,--version-script=$(VERSION_SCRIPT) \ 18616192a77SAndrey Ignatov $^ -o $@ 1871b76c13eSWang Nan 1881b76c13eSWang Nan$(OUTPUT)libbpf.a: $(BPF_IN) 1891b76c13eSWang Nan $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ 1901b76c13eSWang Nan 1918c4905b9SStanislav Fomichev$(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a 1928c4905b9SStanislav Fomichev $(QUIET_LINK)$(CXX) $^ -lelf -o $@ 1938c4905b9SStanislav Fomichev 194*306b267cSAndrey Ignatovcheck: check_abi 195*306b267cSAndrey Ignatov 196*306b267cSAndrey Ignatovcheck_abi: $(OUTPUT)libbpf.so 197*306b267cSAndrey Ignatov @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then \ 198*306b267cSAndrey Ignatov echo "Warning: Num of global symbols in $(BPF_IN)" \ 199*306b267cSAndrey Ignatov "($(GLOBAL_SYM_COUNT)) does NOT match with num of" \ 200*306b267cSAndrey Ignatov "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \ 201*306b267cSAndrey Ignatov "Please make sure all LIBBPF_API symbols are" \ 202*306b267cSAndrey Ignatov "versioned in $(VERSION_SCRIPT)." >&2; \ 203*306b267cSAndrey Ignatov exit 1; \ 204*306b267cSAndrey Ignatov fi 205*306b267cSAndrey Ignatov 2061b76c13eSWang Nandefine do_install 2071b76c13eSWang Nan if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ 2081b76c13eSWang Nan $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ 2091b76c13eSWang Nan fi; \ 2107110d80dSJesper Dangaard Brouer $(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2' 2111b76c13eSWang Nanendef 2121b76c13eSWang Nan 2131b76c13eSWang Naninstall_lib: all_cmd 2141b76c13eSWang Nan $(call QUIET_INSTALL, $(LIB_FILE)) \ 2151b76c13eSWang Nan $(call do_install,$(LIB_FILE),$(libdir_SQ)) 2161b76c13eSWang Nan 217eb54e522SJakub Kicinskiinstall_headers: 218eb54e522SJakub Kicinski $(call QUIET_INSTALL, headers) \ 2197d386c62SJesper Dangaard Brouer $(call do_install,bpf.h,$(prefix)/include/bpf,644); \ 2207d386c62SJesper Dangaard Brouer $(call do_install,libbpf.h,$(prefix)/include/bpf,644); 221a493f5f9SAndrey Ignatov $(call do_install,btf.h,$(prefix)/include/bpf,644); 222eb54e522SJakub Kicinski 2231b76c13eSWang Naninstall: install_lib 2241b76c13eSWang Nan 2251b76c13eSWang Nan### Cleaning rules 2261b76c13eSWang Nan 2271b76c13eSWang Nanconfig-clean: 2281b76c13eSWang Nan $(call QUIET_CLEAN, config) 2291b76c13eSWang Nan $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 2301b76c13eSWang Nan 2311b76c13eSWang Nanclean: 2328c4905b9SStanislav Fomichev $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \ 2338c4905b9SStanislav Fomichev *.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS 2340c77c04aSYunlong Song $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf 2351b76c13eSWang Nan 2361b76c13eSWang Nan 2371b76c13eSWang Nan 2381b76c13eSWang NanPHONY += force elfdep bpfdep 2391b76c13eSWang Nanforce: 2401b76c13eSWang Nan 2411b76c13eSWang Nanelfdep: 24221567edeSJakub Kicinski @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi 2431b76c13eSWang Nan 2441b76c13eSWang Nanbpfdep: 24521567edeSJakub Kicinski @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi 2461b76c13eSWang Nan 2471b76c13eSWang Nan# Declare the contents of the .PHONY variable as phony. We keep that 2481b76c13eSWang Nan# information in a variable so we can use it in if_changed and friends. 2491b76c13eSWang Nan.PHONY: $(PHONY) 250