xref: /linux/tools/lib/bpf/Makefile (revision 20517cd9c5930c9ef580082d23073842e92249ad)
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
91b76c13eSWang Nan
101b76c13eSWang Nan# Makefiles suck: This macro sets a default value of $(2) for the
111b76c13eSWang Nan# variable named by $(1), unless the variable has been set by
121b76c13eSWang Nan# environment or command line. This is necessary for CC and AR
131b76c13eSWang Nan# because make sets default values, so the simpler ?= approach
141b76c13eSWang Nan# won't work as expected.
151b76c13eSWang Nandefine allow-override
161b76c13eSWang Nan  $(if $(or $(findstring environment,$(origin $(1))),\
171b76c13eSWang Nan            $(findstring command line,$(origin $(1)))),,\
181b76c13eSWang Nan    $(eval $(1) = $(2)))
191b76c13eSWang Nanendef
201b76c13eSWang Nan
211b76c13eSWang Nan# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
221b76c13eSWang Nan$(call allow-override,CC,$(CROSS_COMPILE)gcc)
231b76c13eSWang Nan$(call allow-override,AR,$(CROSS_COMPILE)ar)
241b76c13eSWang Nan
251b76c13eSWang NanINSTALL = install
261b76c13eSWang Nan
271b76c13eSWang Nan# Use DESTDIR for installing into a different root directory.
281b76c13eSWang Nan# This is useful for building a package. The program will be
291b76c13eSWang Nan# installed in this directory as if it was the root directory.
301b76c13eSWang Nan# Then the build tool can move it later.
311b76c13eSWang NanDESTDIR ?=
321b76c13eSWang NanDESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
331b76c13eSWang Nan
341b76c13eSWang NanLP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
351b76c13eSWang Nanifeq ($(LP64), 1)
361b76c13eSWang Nan  libdir_relative = lib64
371b76c13eSWang Nanelse
381b76c13eSWang Nan  libdir_relative = lib
391b76c13eSWang Nanendif
401b76c13eSWang Nan
411b76c13eSWang Nanprefix ?= /usr/local
421b76c13eSWang Nanlibdir = $(prefix)/$(libdir_relative)
431b76c13eSWang Nanman_dir = $(prefix)/share/man
441b76c13eSWang Nanman_dir_SQ = '$(subst ','\'',$(man_dir))'
451b76c13eSWang Nan
461b76c13eSWang Nanexport man_dir man_dir_SQ INSTALL
471b76c13eSWang Nanexport DESTDIR DESTDIR_SQ
481b76c13eSWang Nan
491b76c13eSWang Naninclude ../../scripts/Makefile.include
501b76c13eSWang Nan
511b76c13eSWang Nan# copy a bit from Linux kbuild
521b76c13eSWang Nan
531b76c13eSWang Nanifeq ("$(origin V)", "command line")
541b76c13eSWang Nan  VERBOSE = $(V)
551b76c13eSWang Nanendif
561b76c13eSWang Nanifndef VERBOSE
571b76c13eSWang Nan  VERBOSE = 0
581b76c13eSWang Nanendif
591b76c13eSWang Nan
601b76c13eSWang Nanifeq ($(srctree),)
611b76c13eSWang Nansrctree := $(patsubst %/,%,$(dir $(shell pwd)))
621b76c13eSWang Nansrctree := $(patsubst %/,%,$(dir $(srctree)))
631b76c13eSWang Nansrctree := $(patsubst %/,%,$(dir $(srctree)))
641b76c13eSWang Nan#$(info Determined 'srctree' to be $(srctree))
651b76c13eSWang Nanendif
661b76c13eSWang Nan
67*20517cd9SArnaldo Carvalho de MeloFEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf
68*20517cd9SArnaldo Carvalho de MeloFEATURE_DISPLAY = libelf bpf
691b76c13eSWang Nan
701b76c13eSWang NanINCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi
711b76c13eSWang NanFEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
721b76c13eSWang Nan
731b76c13eSWang Naninclude $(srctree)/tools/build/Makefile.feature
741b76c13eSWang Nan
751b76c13eSWang Nanexport prefix libdir src obj
761b76c13eSWang Nan
771b76c13eSWang Nan# Shell quotes
781b76c13eSWang Nanlibdir_SQ = $(subst ','\'',$(libdir))
791b76c13eSWang Nanlibdir_relative_SQ = $(subst ','\'',$(libdir_relative))
801b76c13eSWang Nanplugin_dir_SQ = $(subst ','\'',$(plugin_dir))
811b76c13eSWang Nan
821b76c13eSWang NanLIB_FILE = libbpf.a libbpf.so
831b76c13eSWang Nan
841b76c13eSWang NanVERSION		= $(BPF_VERSION)
851b76c13eSWang NanPATCHLEVEL	= $(BPF_PATCHLEVEL)
861b76c13eSWang NanEXTRAVERSION	= $(BPF_EXTRAVERSION)
871b76c13eSWang Nan
881b76c13eSWang NanOBJ		= $@
891b76c13eSWang NanN		=
901b76c13eSWang Nan
911b76c13eSWang NanLIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
921b76c13eSWang Nan
931b76c13eSWang Nan# Set compile option CFLAGS
941b76c13eSWang Nanifdef EXTRA_CFLAGS
951b76c13eSWang Nan  CFLAGS := $(EXTRA_CFLAGS)
961b76c13eSWang Nanelse
971b76c13eSWang Nan  CFLAGS := -g -Wall
981b76c13eSWang Nanendif
991b76c13eSWang Nan
1001b76c13eSWang Nanifeq ($(feature-libelf-mmap), 1)
1011b76c13eSWang Nan  override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
1021b76c13eSWang Nanendif
1031b76c13eSWang Nan
1041b76c13eSWang Nanifeq ($(feature-libelf-getphdrnum), 1)
1051b76c13eSWang Nan  override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT
1061b76c13eSWang Nanendif
1071b76c13eSWang Nan
1081b76c13eSWang Nan# Append required CFLAGS
1091b76c13eSWang Nanoverride CFLAGS += $(EXTRA_WARNINGS)
1101b76c13eSWang Nanoverride CFLAGS += -Werror -Wall
1111b76c13eSWang Nanoverride CFLAGS += -fPIC
1121b76c13eSWang Nanoverride CFLAGS += $(INCLUDES)
1131b76c13eSWang Nan
1141b76c13eSWang Nanifeq ($(VERBOSE),1)
1151b76c13eSWang Nan  Q =
1161b76c13eSWang Nanelse
1171b76c13eSWang Nan  Q = @
1181b76c13eSWang Nanendif
1191b76c13eSWang Nan
1201b76c13eSWang Nan# Disable command line variables (CFLAGS) overide from top
1211b76c13eSWang Nan# level Makefile (perf), otherwise build Makefile will get
1221b76c13eSWang Nan# the same command line setup.
1231b76c13eSWang NanMAKEOVERRIDES=
1241b76c13eSWang Nan
1251b76c13eSWang Nanexport srctree OUTPUT CC LD CFLAGS V
1261b76c13eSWang Nanbuild := -f $(srctree)/tools/build/Makefile.build dir=. obj
1271b76c13eSWang Nan
1281b76c13eSWang NanBPF_IN    := $(OUTPUT)libbpf-in.o
1291b76c13eSWang NanLIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
1301b76c13eSWang Nan
1311b76c13eSWang NanCMD_TARGETS = $(LIB_FILE)
1321b76c13eSWang Nan
1331b76c13eSWang NanTARGETS = $(CMD_TARGETS)
1341b76c13eSWang Nan
1351b76c13eSWang Nanall: $(VERSION_FILES) all_cmd
1361b76c13eSWang Nan
1371b76c13eSWang Nanall_cmd: $(CMD_TARGETS)
1381b76c13eSWang Nan
1391b76c13eSWang Nan$(BPF_IN): force elfdep bpfdep
1401b76c13eSWang Nan	$(Q)$(MAKE) $(build)=libbpf
1411b76c13eSWang Nan
1421b76c13eSWang Nan$(OUTPUT)libbpf.so: $(BPF_IN)
1431b76c13eSWang Nan	$(QUIET_LINK)$(CC) --shared $^ -o $@
1441b76c13eSWang Nan
1451b76c13eSWang Nan$(OUTPUT)libbpf.a: $(BPF_IN)
1461b76c13eSWang Nan	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
1471b76c13eSWang Nan
1481b76c13eSWang Nandefine update_dir
1491b76c13eSWang Nan  (echo $1 > $@.tmp;				\
1501b76c13eSWang Nan   if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
1511b76c13eSWang Nan     rm -f $@.tmp;				\
1521b76c13eSWang Nan   else						\
1531b76c13eSWang Nan     echo '  UPDATE                 $@';	\
1541b76c13eSWang Nan     mv -f $@.tmp $@;				\
1551b76c13eSWang Nan   fi);
1561b76c13eSWang Nanendef
1571b76c13eSWang Nan
1581b76c13eSWang Nandefine do_install
1591b76c13eSWang Nan	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
1601b76c13eSWang Nan		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
1611b76c13eSWang Nan	fi;						\
1621b76c13eSWang Nan	$(INSTALL) $1 '$(DESTDIR_SQ)$2'
1631b76c13eSWang Nanendef
1641b76c13eSWang Nan
1651b76c13eSWang Naninstall_lib: all_cmd
1661b76c13eSWang Nan	$(call QUIET_INSTALL, $(LIB_FILE)) \
1671b76c13eSWang Nan		$(call do_install,$(LIB_FILE),$(libdir_SQ))
1681b76c13eSWang Nan
1691b76c13eSWang Naninstall: install_lib
1701b76c13eSWang Nan
1711b76c13eSWang Nan### Cleaning rules
1721b76c13eSWang Nan
1731b76c13eSWang Nanconfig-clean:
1741b76c13eSWang Nan	$(call QUIET_CLEAN, config)
1751b76c13eSWang Nan	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
1761b76c13eSWang Nan
1771b76c13eSWang Nanclean:
1781b76c13eSWang Nan	$(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \
1791b76c13eSWang Nan		$(RM) LIBBPF-CFLAGS
1801b76c13eSWang Nan	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP
1811b76c13eSWang Nan
1821b76c13eSWang Nan
1831b76c13eSWang Nan
1841b76c13eSWang NanPHONY += force elfdep bpfdep
1851b76c13eSWang Nanforce:
1861b76c13eSWang Nan
1871b76c13eSWang Nanelfdep:
1881b76c13eSWang Nan	@if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi
1891b76c13eSWang Nan
1901b76c13eSWang Nanbpfdep:
1911b76c13eSWang Nan	@if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi
1921b76c13eSWang Nan
1931b76c13eSWang Nan# Declare the contents of the .PHONY variable as phony.  We keep that
1941b76c13eSWang Nan# information in a variable so we can use it in if_changed and friends.
1951b76c13eSWang Nan.PHONY: $(PHONY)
196