xref: /openbmc/linux/tools/lib/bpf/Makefile (revision 9efc7794496d531593751a3fb008030d5f9ba87c)
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
6d859900cSDaniel BorkmannBPF_EXTRAVERSION = 3
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 NanINSTALL = install
181b76c13eSWang Nan
191b76c13eSWang Nan# Use DESTDIR for installing into a different root directory.
201b76c13eSWang Nan# This is useful for building a package. The program will be
211b76c13eSWang Nan# installed in this directory as if it was the root directory.
221b76c13eSWang Nan# Then the build tool can move it later.
231b76c13eSWang NanDESTDIR ?=
241b76c13eSWang NanDESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
251b76c13eSWang Nan
26d5ef3140SNaveen N. Raoinclude $(srctree)/tools/scripts/Makefile.arch
27d5ef3140SNaveen N. Rao
281b76c13eSWang Nanifeq ($(LP64), 1)
291b76c13eSWang Nan  libdir_relative = lib64
301b76c13eSWang Nanelse
311b76c13eSWang Nan  libdir_relative = lib
321b76c13eSWang Nanendif
331b76c13eSWang Nan
341b76c13eSWang Nanprefix ?= /usr/local
351b76c13eSWang Nanlibdir = $(prefix)/$(libdir_relative)
361b76c13eSWang Nanman_dir = $(prefix)/share/man
371b76c13eSWang Nanman_dir_SQ = '$(subst ','\'',$(man_dir))'
381b76c13eSWang Nan
391b76c13eSWang Nanexport man_dir man_dir_SQ INSTALL
401b76c13eSWang Nanexport DESTDIR DESTDIR_SQ
411b76c13eSWang Nan
42eeedd352SStanislav Fomichevinclude $(srctree)/tools/scripts/Makefile.include
431b76c13eSWang Nan
441b76c13eSWang Nan# copy a bit from Linux kbuild
451b76c13eSWang Nan
461b76c13eSWang Nanifeq ("$(origin V)", "command line")
471b76c13eSWang Nan  VERBOSE = $(V)
481b76c13eSWang Nanendif
491b76c13eSWang Nanifndef VERBOSE
501b76c13eSWang Nan  VERBOSE = 0
511b76c13eSWang Nanendif
521b76c13eSWang Nan
5365f041beSArnaldo Carvalho de MeloFEATURE_USER = .libbpf
548c4905b9SStanislav FomichevFEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx
5520517cd9SArnaldo Carvalho de MeloFEATURE_DISPLAY = libelf bpf
561b76c13eSWang Nan
57438363c0SYonghong SongINCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
581b76c13eSWang NanFEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
591b76c13eSWang Nan
60d8ad6a15SWang Nancheck_feat := 1
61d8ad6a15SWang NanNON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
62d8ad6a15SWang Nanifdef MAKECMDGOALS
63d8ad6a15SWang Nanifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
64d8ad6a15SWang Nan  check_feat := 0
65d8ad6a15SWang Nanendif
66d8ad6a15SWang Nanendif
67d8ad6a15SWang Nan
68d8ad6a15SWang Nanifeq ($(check_feat),1)
6958683600SJiri Olsaifeq ($(FEATURES_DUMP),)
701b76c13eSWang Naninclude $(srctree)/tools/build/Makefile.feature
7158683600SJiri Olsaelse
7258683600SJiri Olsainclude $(FEATURES_DUMP)
7358683600SJiri Olsaendif
74d8ad6a15SWang Nanendif
751b76c13eSWang Nan
761b76c13eSWang Nanexport prefix libdir src obj
771b76c13eSWang Nan
781b76c13eSWang Nan# Shell quotes
791b76c13eSWang Nanlibdir_SQ = $(subst ','\'',$(libdir))
801b76c13eSWang Nanlibdir_relative_SQ = $(subst ','\'',$(libdir_relative))
811b76c13eSWang Nan
821b76c13eSWang NanVERSION		= $(BPF_VERSION)
831b76c13eSWang NanPATCHLEVEL	= $(BPF_PATCHLEVEL)
841b76c13eSWang NanEXTRAVERSION	= $(BPF_EXTRAVERSION)
851b76c13eSWang Nan
861b76c13eSWang NanOBJ		= $@
871b76c13eSWang NanN		=
881b76c13eSWang Nan
891b76c13eSWang NanLIBBPF_VERSION	= $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
901b76c13eSWang Nan
911d382264SDaniel BorkmannLIB_TARGET	= libbpf.a libbpf.so.$(LIBBPF_VERSION)
921d382264SDaniel BorkmannLIB_FILE	= libbpf.a libbpf.so*
93dd399ac9SLuca BoccassiPC_FILE		= libbpf.pc
941d382264SDaniel Borkmann
951b76c13eSWang Nan# Set compile option CFLAGS
961b76c13eSWang Nanifdef EXTRA_CFLAGS
971b76c13eSWang Nan  CFLAGS := $(EXTRA_CFLAGS)
981b76c13eSWang Nanelse
991b76c13eSWang Nan  CFLAGS := -g -Wall
1001b76c13eSWang Nanendif
1011b76c13eSWang Nan
1021b76c13eSWang Nanifeq ($(feature-libelf-mmap), 1)
1031b76c13eSWang Nan  override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
1041b76c13eSWang Nanendif
1051b76c13eSWang Nan
106531b014eSJakub Kicinskiifeq ($(feature-reallocarray), 0)
107531b014eSJakub Kicinski  override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
108531b014eSJakub Kicinskiendif
109531b014eSJakub Kicinski
1101b76c13eSWang Nan# Append required CFLAGS
1111b76c13eSWang Nanoverride CFLAGS += $(EXTRA_WARNINGS)
1121b76c13eSWang Nanoverride CFLAGS += -Werror -Wall
1131b76c13eSWang Nanoverride CFLAGS += -fPIC
1141b76c13eSWang Nanoverride CFLAGS += $(INCLUDES)
115ab9e0848SAndrey Ignatovoverride CFLAGS += -fvisibility=hidden
1161b76c13eSWang Nan
1171b76c13eSWang Nanifeq ($(VERBOSE),1)
1181b76c13eSWang Nan  Q =
1191b76c13eSWang Nanelse
1201b76c13eSWang Nan  Q = @
1211b76c13eSWang Nanendif
1221b76c13eSWang Nan
123505d3085SMasahiro Yamada# Disable command line variables (CFLAGS) override from top
1241b76c13eSWang Nan# level Makefile (perf), otherwise build Makefile will get
1251b76c13eSWang Nan# the same command line setup.
1261b76c13eSWang NanMAKEOVERRIDES=
1271b76c13eSWang Nan
1287c422f55SJiri Olsaall:
1297c422f55SJiri Olsa
1301b76c13eSWang Nanexport srctree OUTPUT CC LD CFLAGS V
131ab6201d0SJiri Olsainclude $(srctree)/tools/build/Makefile.include
1321b76c13eSWang Nan
1331b76c13eSWang NanBPF_IN		:= $(OUTPUT)libbpf-in.o
13416192a77SAndrey IgnatovVERSION_SCRIPT	:= libbpf.map
1351b76c13eSWang Nan
1361d382264SDaniel BorkmannLIB_TARGET	:= $(addprefix $(OUTPUT),$(LIB_TARGET))
1371d382264SDaniel BorkmannLIB_FILE	:= $(addprefix $(OUTPUT),$(LIB_FILE))
138dd399ac9SLuca BoccassiPC_FILE		:= $(addprefix $(OUTPUT),$(PC_FILE))
1391d382264SDaniel Borkmann
140771744f9SJakub KicinskiGLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
141306b267cSAndrey Ignatov			   awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}')
142771744f9SJakub KicinskiVERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
143306b267cSAndrey Ignatov			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
144306b267cSAndrey Ignatov
145dd399ac9SLuca BoccassiCMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
1461b76c13eSWang Nan
1478c4905b9SStanislav FomichevCXX_TEST_TARGET = $(OUTPUT)test_libbpf
1488c4905b9SStanislav Fomichev
1498c4905b9SStanislav Fomichevifeq ($(feature-cxx), 1)
1508c4905b9SStanislav Fomichev	CMD_TARGETS += $(CXX_TEST_TARGET)
1518c4905b9SStanislav Fomichevendif
1528c4905b9SStanislav Fomichev
1531b76c13eSWang NanTARGETS = $(CMD_TARGETS)
1541b76c13eSWang Nan
1558e268887SStanislav Fomichevall: fixdep
1568e268887SStanislav Fomichev	$(Q)$(MAKE) all_cmd
1571b76c13eSWang Nan
158306b267cSAndrey Ignatovall_cmd: $(CMD_TARGETS) check
1591b76c13eSWang Nan
1601b76c13eSWang Nan$(BPF_IN): force elfdep bpfdep
161f15d5e6dSDaniel Díaz	@(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
162971e827bSArnaldo Carvalho de Melo	(diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
1638255e1efSIngo 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
164f15d5e6dSDaniel Díaz	@(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
165971e827bSArnaldo Carvalho de Melo	(diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
1668255e1efSIngo 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
167dc2b9f19SEric Leblond	@(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \
168dc2b9f19SEric Leblond	(diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \
169dc2b9f19SEric 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
170dc2b9f19SEric Leblond	@(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \
171dc2b9f19SEric Leblond	(diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \
172dc2b9f19SEric 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
1731cad0788SMagnus Karlsson	@(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
1741cad0788SMagnus Karlsson	(diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
1751cad0788SMagnus Karlsson	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
1761b76c13eSWang Nan	$(Q)$(MAKE) $(build)=libbpf
1771b76c13eSWang Nan
1781d382264SDaniel Borkmann$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
1791d382264SDaniel Borkmann
1801d382264SDaniel Borkmann$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN)
1811d382264SDaniel Borkmann	$(QUIET_LINK)$(CC) --shared -Wl,-soname,libbpf.so.$(VERSION) \
18289dedaefSBjörn Töpel				    -Wl,--version-script=$(VERSION_SCRIPT) $^ -lelf -o $@
1831d382264SDaniel Borkmann	@ln -sf $(@F) $(OUTPUT)libbpf.so
1841d382264SDaniel Borkmann	@ln -sf $(@F) $(OUTPUT)libbpf.so.$(VERSION)
1851b76c13eSWang Nan
1861b76c13eSWang Nan$(OUTPUT)libbpf.a: $(BPF_IN)
1871b76c13eSWang Nan	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
1881b76c13eSWang Nan
1898c4905b9SStanislav Fomichev$(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
1901cad0788SMagnus Karlsson	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
1918c4905b9SStanislav Fomichev
192dd399ac9SLuca Boccassi$(OUTPUT)libbpf.pc:
193dd399ac9SLuca Boccassi	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
194dd399ac9SLuca Boccassi		-e "s|@LIBDIR@|$(libdir_SQ)|" \
195dd399ac9SLuca Boccassi		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
196dd399ac9SLuca Boccassi		< libbpf.pc.template > $@
197dd399ac9SLuca Boccassi
198306b267cSAndrey Ignatovcheck: check_abi
199306b267cSAndrey Ignatov
200306b267cSAndrey Ignatovcheck_abi: $(OUTPUT)libbpf.so
201306b267cSAndrey Ignatov	@if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then	 \
202306b267cSAndrey Ignatov		echo "Warning: Num of global symbols in $(BPF_IN)"	 \
203306b267cSAndrey Ignatov		     "($(GLOBAL_SYM_COUNT)) does NOT match with num of"	 \
204306b267cSAndrey Ignatov		     "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
205306b267cSAndrey Ignatov		     "Please make sure all LIBBPF_API symbols are"	 \
206306b267cSAndrey Ignatov		     "versioned in $(VERSION_SCRIPT)." >&2;		 \
207*9efc7794SAndrii Nakryiko		readelf -s --wide $(OUTPUT)libbpf-in.o |		 \
208*9efc7794SAndrii Nakryiko		    awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$8}'|   \
209*9efc7794SAndrii Nakryiko		    sort -u > $(OUTPUT)libbpf_global_syms.tmp;		 \
210*9efc7794SAndrii Nakryiko		readelf -s --wide $(OUTPUT)libbpf.so |			 \
211*9efc7794SAndrii Nakryiko		    grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 |		 \
212*9efc7794SAndrii Nakryiko		    sort -u > $(OUTPUT)libbpf_versioned_syms.tmp; 	 \
213*9efc7794SAndrii Nakryiko		diff -u $(OUTPUT)libbpf_global_syms.tmp			 \
214*9efc7794SAndrii Nakryiko		     $(OUTPUT)libbpf_versioned_syms.tmp;		 \
215*9efc7794SAndrii Nakryiko		rm $(OUTPUT)libbpf_global_syms.tmp			 \
216*9efc7794SAndrii Nakryiko		   $(OUTPUT)libbpf_versioned_syms.tmp;			 \
217306b267cSAndrey Ignatov		exit 1;							 \
218306b267cSAndrey Ignatov	fi
219306b267cSAndrey Ignatov
2201d382264SDaniel Borkmanndefine do_install_mkdir
2211d382264SDaniel Borkmann	if [ ! -d '$(DESTDIR_SQ)$1' ]; then		\
2221d382264SDaniel Borkmann		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1';	\
2231d382264SDaniel Borkmann	fi
2241d382264SDaniel Borkmannendef
2251d382264SDaniel Borkmann
2261b76c13eSWang Nandefine do_install
2271b76c13eSWang Nan	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
2281b76c13eSWang Nan		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
2291b76c13eSWang Nan	fi;						\
2307110d80dSJesper Dangaard Brouer	$(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
2311b76c13eSWang Nanendef
2321b76c13eSWang Nan
2331b76c13eSWang Naninstall_lib: all_cmd
2341d382264SDaniel Borkmann	$(call QUIET_INSTALL, $(LIB_TARGET)) \
2351d382264SDaniel Borkmann		$(call do_install_mkdir,$(libdir_SQ)); \
2361d382264SDaniel Borkmann		cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
2371b76c13eSWang Nan
238eb54e522SJakub Kicinskiinstall_headers:
239eb54e522SJakub Kicinski	$(call QUIET_INSTALL, headers) \
2407d386c62SJesper Dangaard Brouer		$(call do_install,bpf.h,$(prefix)/include/bpf,644); \
2418543e437SDaniel Borkmann		$(call do_install,libbpf.h,$(prefix)/include/bpf,644); \
2428543e437SDaniel Borkmann		$(call do_install,btf.h,$(prefix)/include/bpf,644); \
2437080da89SWilliam Tu		$(call do_install,libbpf_util.h,$(prefix)/include/bpf,644); \
244379e2014SBjörn Töpel		$(call do_install,xsk.h,$(prefix)/include/bpf,644);
245eb54e522SJakub Kicinski
246dd399ac9SLuca Boccassiinstall_pkgconfig: $(PC_FILE)
247dd399ac9SLuca Boccassi	$(call QUIET_INSTALL, $(PC_FILE)) \
248dd399ac9SLuca Boccassi		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
249dd399ac9SLuca Boccassi
250dd399ac9SLuca Boccassiinstall: install_lib install_pkgconfig
2511b76c13eSWang Nan
2521b76c13eSWang Nan### Cleaning rules
2531b76c13eSWang Nan
2541b76c13eSWang Nanconfig-clean:
2551b76c13eSWang Nan	$(call QUIET_CLEAN, config)
2561b76c13eSWang Nan	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
2571b76c13eSWang Nan
2581b76c13eSWang Nanclean:
2598c4905b9SStanislav Fomichev	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
260dd399ac9SLuca Boccassi		*.o *~ *.a *.so *.so.$(VERSION) .*.d .*.cmd *.pc LIBBPF-CFLAGS
2610c77c04aSYunlong Song	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
2621b76c13eSWang Nan
2631b76c13eSWang Nan
2641b76c13eSWang Nan
2651b76c13eSWang NanPHONY += force elfdep bpfdep
2661b76c13eSWang Nanforce:
2671b76c13eSWang Nan
2681b76c13eSWang Nanelfdep:
26921567edeSJakub Kicinski	@if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
2701b76c13eSWang Nan
2711b76c13eSWang Nanbpfdep:
27221567edeSJakub Kicinski	@if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
2731b76c13eSWang Nan
2741b76c13eSWang Nan# Declare the contents of the .PHONY variable as phony.  We keep that
2751b76c13eSWang Nan# information in a variable so we can use it in if_changed and friends.
2761b76c13eSWang Nan.PHONY: $(PHONY)
277