11b76c13eSWang Nan# Most of this file is copied from tools/lib/traceevent/Makefile 21b76c13eSWang Nan 31b76c13eSWang NanBPF_VERSION = 0 41b76c13eSWang NanBPF_PATCHLEVEL = 0 51b76c13eSWang NanBPF_EXTRAVERSION = 1 61b76c13eSWang Nan 71b76c13eSWang NanMAKEFLAGS += --no-print-directory 81b76c13eSWang Nan 9d5ef3140SNaveen N. Raoifeq ($(srctree),) 10e19b7ceeSUwe Kleine-Königsrctree := $(patsubst %/,%,$(dir $(CURDIR))) 11d5ef3140SNaveen N. Raosrctree := $(patsubst %/,%,$(dir $(srctree))) 12d5ef3140SNaveen N. Raosrctree := $(patsubst %/,%,$(dir $(srctree))) 13d5ef3140SNaveen N. Rao#$(info Determined 'srctree' to be $(srctree)) 14d5ef3140SNaveen N. Raoendif 151b76c13eSWang Nan 161b76c13eSWang Nan# Makefiles suck: This macro sets a default value of $(2) for the 171b76c13eSWang Nan# variable named by $(1), unless the variable has been set by 181b76c13eSWang Nan# environment or command line. This is necessary for CC and AR 191b76c13eSWang Nan# because make sets default values, so the simpler ?= approach 201b76c13eSWang Nan# won't work as expected. 211b76c13eSWang Nandefine allow-override 221b76c13eSWang Nan $(if $(or $(findstring environment,$(origin $(1))),\ 231b76c13eSWang Nan $(findstring command line,$(origin $(1)))),,\ 241b76c13eSWang Nan $(eval $(1) = $(2))) 251b76c13eSWang Nanendef 261b76c13eSWang Nan 271b76c13eSWang Nan# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix. 281b76c13eSWang Nan$(call allow-override,CC,$(CROSS_COMPILE)gcc) 291b76c13eSWang Nan$(call allow-override,AR,$(CROSS_COMPILE)ar) 301b76c13eSWang Nan 311b76c13eSWang NanINSTALL = install 321b76c13eSWang Nan 331b76c13eSWang Nan# Use DESTDIR for installing into a different root directory. 341b76c13eSWang Nan# This is useful for building a package. The program will be 351b76c13eSWang Nan# installed in this directory as if it was the root directory. 361b76c13eSWang Nan# Then the build tool can move it later. 371b76c13eSWang NanDESTDIR ?= 381b76c13eSWang NanDESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' 391b76c13eSWang Nan 40d5ef3140SNaveen N. Raoinclude $(srctree)/tools/scripts/Makefile.arch 41d5ef3140SNaveen N. Rao 421b76c13eSWang Nanifeq ($(LP64), 1) 431b76c13eSWang Nan libdir_relative = lib64 441b76c13eSWang Nanelse 451b76c13eSWang Nan libdir_relative = lib 461b76c13eSWang Nanendif 471b76c13eSWang Nan 481b76c13eSWang Nanprefix ?= /usr/local 491b76c13eSWang Nanlibdir = $(prefix)/$(libdir_relative) 501b76c13eSWang Nanman_dir = $(prefix)/share/man 511b76c13eSWang Nanman_dir_SQ = '$(subst ','\'',$(man_dir))' 521b76c13eSWang Nan 531b76c13eSWang Nanexport man_dir man_dir_SQ INSTALL 541b76c13eSWang Nanexport DESTDIR DESTDIR_SQ 551b76c13eSWang Nan 561b76c13eSWang Naninclude ../../scripts/Makefile.include 571b76c13eSWang Nan 581b76c13eSWang Nan# copy a bit from Linux kbuild 591b76c13eSWang Nan 601b76c13eSWang Nanifeq ("$(origin V)", "command line") 611b76c13eSWang Nan VERBOSE = $(V) 621b76c13eSWang Nanendif 631b76c13eSWang Nanifndef VERBOSE 641b76c13eSWang Nan VERBOSE = 0 651b76c13eSWang Nanendif 661b76c13eSWang Nan 6765f041beSArnaldo Carvalho de MeloFEATURE_USER = .libbpf 6820517cd9SArnaldo Carvalho de MeloFEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf 6920517cd9SArnaldo Carvalho de MeloFEATURE_DISPLAY = libelf bpf 701b76c13eSWang Nan 71971e827bSArnaldo Carvalho de MeloINCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi 721b76c13eSWang NanFEATURE_CHECK_CFLAGS-bpf = $(INCLUDES) 731b76c13eSWang Nan 74d8ad6a15SWang Nancheck_feat := 1 75d8ad6a15SWang NanNON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help 76d8ad6a15SWang Nanifdef MAKECMDGOALS 77d8ad6a15SWang Nanifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) 78d8ad6a15SWang Nan check_feat := 0 79d8ad6a15SWang Nanendif 80d8ad6a15SWang Nanendif 81d8ad6a15SWang Nan 82d8ad6a15SWang Nanifeq ($(check_feat),1) 8358683600SJiri Olsaifeq ($(FEATURES_DUMP),) 841b76c13eSWang Naninclude $(srctree)/tools/build/Makefile.feature 8558683600SJiri Olsaelse 8658683600SJiri Olsainclude $(FEATURES_DUMP) 8758683600SJiri Olsaendif 88d8ad6a15SWang Nanendif 891b76c13eSWang Nan 901b76c13eSWang Nanexport prefix libdir src obj 911b76c13eSWang Nan 921b76c13eSWang Nan# Shell quotes 931b76c13eSWang Nanlibdir_SQ = $(subst ','\'',$(libdir)) 941b76c13eSWang Nanlibdir_relative_SQ = $(subst ','\'',$(libdir_relative)) 951b76c13eSWang Nanplugin_dir_SQ = $(subst ','\'',$(plugin_dir)) 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 1191b76c13eSWang Nanifeq ($(feature-libelf-getphdrnum), 1) 1201b76c13eSWang Nan override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT 1211b76c13eSWang Nanendif 1221b76c13eSWang Nan 1231b76c13eSWang Nan# Append required CFLAGS 1241b76c13eSWang Nanoverride CFLAGS += $(EXTRA_WARNINGS) 1251b76c13eSWang Nanoverride CFLAGS += -Werror -Wall 1261b76c13eSWang Nanoverride CFLAGS += -fPIC 1271b76c13eSWang Nanoverride CFLAGS += $(INCLUDES) 1281b76c13eSWang Nan 1291b76c13eSWang Nanifeq ($(VERBOSE),1) 1301b76c13eSWang Nan Q = 1311b76c13eSWang Nanelse 1321b76c13eSWang Nan Q = @ 1331b76c13eSWang Nanendif 1341b76c13eSWang Nan 135505d3085SMasahiro Yamada# Disable command line variables (CFLAGS) override from top 1361b76c13eSWang Nan# level Makefile (perf), otherwise build Makefile will get 1371b76c13eSWang Nan# the same command line setup. 1381b76c13eSWang NanMAKEOVERRIDES= 1391b76c13eSWang Nan 1407c422f55SJiri Olsaall: 1417c422f55SJiri Olsa 1421b76c13eSWang Nanexport srctree OUTPUT CC LD CFLAGS V 143ab6201d0SJiri Olsainclude $(srctree)/tools/build/Makefile.include 1441b76c13eSWang Nan 1451b76c13eSWang NanBPF_IN := $(OUTPUT)libbpf-in.o 1461b76c13eSWang NanLIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) 1471b76c13eSWang Nan 1481b76c13eSWang NanCMD_TARGETS = $(LIB_FILE) 1491b76c13eSWang Nan 1501b76c13eSWang NanTARGETS = $(CMD_TARGETS) 1511b76c13eSWang Nan 1527c422f55SJiri Olsaall: fixdep $(VERSION_FILES) all_cmd 1531b76c13eSWang Nan 1541b76c13eSWang Nanall_cmd: $(CMD_TARGETS) 1551b76c13eSWang Nan 1561b76c13eSWang Nan$(BPF_IN): force elfdep bpfdep 157971e827bSArnaldo Carvalho de Melo @(test -f ../../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \ 158971e827bSArnaldo Carvalho de Melo (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \ 159971e827bSArnaldo Carvalho de Melo echo "Warning: tools/include/uapi/linux/bpf.h differs from kernel" >&2 )) || true 160971e827bSArnaldo Carvalho de Melo @(test -f ../../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \ 161971e827bSArnaldo Carvalho de Melo (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \ 162971e827bSArnaldo Carvalho de Melo echo "Warning: tools/include/uapi/linux/bpf_common.h differs from kernel" >&2 )) || true 1631b76c13eSWang Nan $(Q)$(MAKE) $(build)=libbpf 1641b76c13eSWang Nan 1651b76c13eSWang Nan$(OUTPUT)libbpf.so: $(BPF_IN) 1661b76c13eSWang Nan $(QUIET_LINK)$(CC) --shared $^ -o $@ 1671b76c13eSWang Nan 1681b76c13eSWang Nan$(OUTPUT)libbpf.a: $(BPF_IN) 1691b76c13eSWang Nan $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ 1701b76c13eSWang Nan 1711b76c13eSWang Nandefine update_dir 1721b76c13eSWang Nan (echo $1 > $@.tmp; \ 1731b76c13eSWang Nan if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 1741b76c13eSWang Nan rm -f $@.tmp; \ 1751b76c13eSWang Nan else \ 1761b76c13eSWang Nan echo ' UPDATE $@'; \ 1771b76c13eSWang Nan mv -f $@.tmp $@; \ 1781b76c13eSWang Nan fi); 1791b76c13eSWang Nanendef 1801b76c13eSWang Nan 1811b76c13eSWang Nandefine do_install 1821b76c13eSWang Nan if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ 1831b76c13eSWang Nan $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ 1841b76c13eSWang Nan fi; \ 1851b76c13eSWang Nan $(INSTALL) $1 '$(DESTDIR_SQ)$2' 1861b76c13eSWang Nanendef 1871b76c13eSWang Nan 1881b76c13eSWang Naninstall_lib: all_cmd 1891b76c13eSWang Nan $(call QUIET_INSTALL, $(LIB_FILE)) \ 1901b76c13eSWang Nan $(call do_install,$(LIB_FILE),$(libdir_SQ)) 1911b76c13eSWang Nan 192*eb54e522SJakub Kicinskiinstall_headers: 193*eb54e522SJakub Kicinski $(call QUIET_INSTALL, headers) \ 194*eb54e522SJakub Kicinski $(call do_install,bpf.h,$(prefix)/include/bpf,644) 195*eb54e522SJakub Kicinski 1961b76c13eSWang Naninstall: install_lib 1971b76c13eSWang Nan 1981b76c13eSWang Nan### Cleaning rules 1991b76c13eSWang Nan 2001b76c13eSWang Nanconfig-clean: 2011b76c13eSWang Nan $(call QUIET_CLEAN, config) 2021b76c13eSWang Nan $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 2031b76c13eSWang Nan 2041b76c13eSWang Nanclean: 20522992a32SJiri Olsa $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd \ 2061b76c13eSWang Nan $(RM) LIBBPF-CFLAGS 2070c77c04aSYunlong Song $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf 2081b76c13eSWang Nan 2091b76c13eSWang Nan 2101b76c13eSWang Nan 2111b76c13eSWang NanPHONY += force elfdep bpfdep 2121b76c13eSWang Nanforce: 2131b76c13eSWang Nan 2141b76c13eSWang Nanelfdep: 2151b76c13eSWang Nan @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi 2161b76c13eSWang Nan 2171b76c13eSWang Nanbpfdep: 2181b76c13eSWang Nan @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi 2191b76c13eSWang Nan 2201b76c13eSWang Nan# Declare the contents of the .PHONY variable as phony. We keep that 2211b76c13eSWang Nan# information in a variable so we can use it in if_changed and friends. 2221b76c13eSWang Nan.PHONY: $(PHONY) 223