17c2aad20Sopenharmony_ci# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
27c2aad20Sopenharmony_ci
37c2aad20Sopenharmony_ciifeq ($(V),1)
47c2aad20Sopenharmony_ci	Q =
57c2aad20Sopenharmony_ci	msg =
67c2aad20Sopenharmony_cielse
77c2aad20Sopenharmony_ci	Q = @
87c2aad20Sopenharmony_ci	msg = @printf '  %-8s %s%s\n' "$(1)" "$(2)" "$(if $(3), $(3))";
97c2aad20Sopenharmony_ciendif
107c2aad20Sopenharmony_ci
117c2aad20Sopenharmony_ciLIBBPF_MAJOR_VERSION := 1
127c2aad20Sopenharmony_ciLIBBPF_MINOR_VERSION := 3
137c2aad20Sopenharmony_ciLIBBPF_PATCH_VERSION := 0
147c2aad20Sopenharmony_ciLIBBPF_VERSION := $(LIBBPF_MAJOR_VERSION).$(LIBBPF_MINOR_VERSION).$(LIBBPF_PATCH_VERSION)
157c2aad20Sopenharmony_ciLIBBPF_MAJMIN_VERSION := $(LIBBPF_MAJOR_VERSION).$(LIBBPF_MINOR_VERSION).0
167c2aad20Sopenharmony_ciLIBBPF_MAP_VERSION := $(shell grep -oE '^LIBBPF_([0-9.]+)' libbpf.map | sort -rV | head -n1 | cut -d'_' -f2)
177c2aad20Sopenharmony_ciifneq ($(LIBBPF_MAJMIN_VERSION), $(LIBBPF_MAP_VERSION))
187c2aad20Sopenharmony_ci$(error Libbpf release ($(LIBBPF_VERSION)) and map ($(LIBBPF_MAP_VERSION)) versions are out of sync!)
197c2aad20Sopenharmony_ciendif
207c2aad20Sopenharmony_ci
217c2aad20Sopenharmony_cidefine allow-override
227c2aad20Sopenharmony_ci  $(if $(or $(findstring environment,$(origin $(1))),\
237c2aad20Sopenharmony_ci            $(findstring command line,$(origin $(1)))),,\
247c2aad20Sopenharmony_ci    $(eval $(1) = $(2)))
257c2aad20Sopenharmony_ciendef
267c2aad20Sopenharmony_ci
277c2aad20Sopenharmony_ci$(call allow-override,CC,$(CROSS_COMPILE)cc)
287c2aad20Sopenharmony_ci$(call allow-override,LD,$(CROSS_COMPILE)ld)
297c2aad20Sopenharmony_ci
307c2aad20Sopenharmony_ciTOPDIR = ..
317c2aad20Sopenharmony_ci
327c2aad20Sopenharmony_ciINCLUDES := -I. -I$(TOPDIR)/include -I$(TOPDIR)/include/uapi
337c2aad20Sopenharmony_ciALL_CFLAGS := $(INCLUDES)
347c2aad20Sopenharmony_ci
357c2aad20Sopenharmony_ciSHARED_CFLAGS += -fPIC -fvisibility=hidden -DSHARED
367c2aad20Sopenharmony_ci
377c2aad20Sopenharmony_ciCFLAGS ?= -g -O2 -Werror -Wall -std=gnu89
387c2aad20Sopenharmony_ciALL_CFLAGS += $(CFLAGS) 						\
397c2aad20Sopenharmony_ci	      -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64		\
407c2aad20Sopenharmony_ci	      -Wno-unknown-warning-option -Wno-format-overflow		\
417c2aad20Sopenharmony_ci	      $(EXTRA_CFLAGS)
427c2aad20Sopenharmony_ciALL_LDFLAGS += $(LDFLAGS) $(EXTRA_LDFLAGS)
437c2aad20Sopenharmony_ci
447c2aad20Sopenharmony_ciifdef NO_PKG_CONFIG
457c2aad20Sopenharmony_ci	ALL_LDFLAGS += -lelf -lz
467c2aad20Sopenharmony_cielse
477c2aad20Sopenharmony_ci	PKG_CONFIG ?= pkg-config
487c2aad20Sopenharmony_ci	ALL_CFLAGS += $(shell $(PKG_CONFIG) --cflags libelf zlib)
497c2aad20Sopenharmony_ci	ALL_LDFLAGS += $(shell $(PKG_CONFIG) --libs libelf zlib)
507c2aad20Sopenharmony_ciendif
517c2aad20Sopenharmony_ci
527c2aad20Sopenharmony_ciOBJDIR ?= .
537c2aad20Sopenharmony_ciSHARED_OBJDIR := $(OBJDIR)/sharedobjs
547c2aad20Sopenharmony_ciSTATIC_OBJDIR := $(OBJDIR)/staticobjs
557c2aad20Sopenharmony_ciOBJS := bpf.o btf.o libbpf.o libbpf_errno.o netlink.o \
567c2aad20Sopenharmony_ci	nlattr.o str_error.o libbpf_probes.o bpf_prog_linfo.o \
577c2aad20Sopenharmony_ci	btf_dump.o hashmap.o ringbuf.o strset.o linker.o gen_loader.o \
587c2aad20Sopenharmony_ci	relo_core.o usdt.o zip.o elf.o
597c2aad20Sopenharmony_ciSHARED_OBJS := $(addprefix $(SHARED_OBJDIR)/,$(OBJS))
607c2aad20Sopenharmony_ciSTATIC_OBJS := $(addprefix $(STATIC_OBJDIR)/,$(OBJS))
617c2aad20Sopenharmony_ci
627c2aad20Sopenharmony_ciSTATIC_LIBS := $(OBJDIR)/libbpf.a
637c2aad20Sopenharmony_ciifndef BUILD_STATIC_ONLY
647c2aad20Sopenharmony_ci	SHARED_LIBS := $(OBJDIR)/libbpf.so \
657c2aad20Sopenharmony_ci		       $(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION) \
667c2aad20Sopenharmony_ci		       $(OBJDIR)/libbpf.so.$(LIBBPF_VERSION)
677c2aad20Sopenharmony_ci	VERSION_SCRIPT := libbpf.map
687c2aad20Sopenharmony_ciendif
697c2aad20Sopenharmony_ci
707c2aad20Sopenharmony_ciHEADERS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h \
717c2aad20Sopenharmony_ci	   bpf_helpers.h bpf_helper_defs.h bpf_tracing.h \
727c2aad20Sopenharmony_ci	   bpf_endian.h bpf_core_read.h skel_internal.h libbpf_version.h \
737c2aad20Sopenharmony_ci	   usdt.bpf.h
747c2aad20Sopenharmony_ciUAPI_HEADERS := $(addprefix $(TOPDIR)/include/uapi/linux/,\
757c2aad20Sopenharmony_ci			    bpf.h bpf_common.h btf.h)
767c2aad20Sopenharmony_ci
777c2aad20Sopenharmony_ciPC_FILE := $(OBJDIR)/libbpf.pc
787c2aad20Sopenharmony_ci
797c2aad20Sopenharmony_ciINSTALL = install
807c2aad20Sopenharmony_ci
817c2aad20Sopenharmony_ciDESTDIR ?=
827c2aad20Sopenharmony_ci
837c2aad20Sopenharmony_ciHOSTARCH = $(firstword $(subst -, ,$(shell $(CC) -dumpmachine)))
847c2aad20Sopenharmony_ciifeq ($(filter-out %64 %64be %64eb %64le %64el s390x, $(HOSTARCH)),)
857c2aad20Sopenharmony_ci	LIBSUBDIR := lib64
867c2aad20Sopenharmony_cielse
877c2aad20Sopenharmony_ci	LIBSUBDIR := lib
887c2aad20Sopenharmony_ciendif
897c2aad20Sopenharmony_ci
907c2aad20Sopenharmony_ci# By default let the pc file itself use ${prefix} in includedir/libdir so that
917c2aad20Sopenharmony_ci# the prefix can be overridden at runtime (eg: --define-prefix)
927c2aad20Sopenharmony_ciifndef LIBDIR
937c2aad20Sopenharmony_ci	LIBDIR_PC := $$\{prefix\}/$(LIBSUBDIR)
947c2aad20Sopenharmony_cielse
957c2aad20Sopenharmony_ci	LIBDIR_PC := $(LIBDIR)
967c2aad20Sopenharmony_ciendif
977c2aad20Sopenharmony_ciPREFIX ?= /usr
987c2aad20Sopenharmony_ciLIBDIR ?= $(PREFIX)/$(LIBSUBDIR)
997c2aad20Sopenharmony_ciINCLUDEDIR ?= $(PREFIX)/include
1007c2aad20Sopenharmony_ciUAPIDIR ?= $(PREFIX)/include
1017c2aad20Sopenharmony_ci
1027c2aad20Sopenharmony_ciTAGS_PROG := $(if $(shell which etags 2>/dev/null),etags,ctags)
1037c2aad20Sopenharmony_ci
1047c2aad20Sopenharmony_ciall: $(STATIC_LIBS) $(SHARED_LIBS) $(PC_FILE)
1057c2aad20Sopenharmony_ci
1067c2aad20Sopenharmony_ci$(OBJDIR)/libbpf.a: $(STATIC_OBJS)
1077c2aad20Sopenharmony_ci	$(call msg,AR,$@)
1087c2aad20Sopenharmony_ci	$(Q)$(AR) rcs $@ $^
1097c2aad20Sopenharmony_ci
1107c2aad20Sopenharmony_ci$(OBJDIR)/libbpf.so: $(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION)
1117c2aad20Sopenharmony_ci	$(Q)ln -sf $(^F) $@
1127c2aad20Sopenharmony_ci
1137c2aad20Sopenharmony_ci$(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION): $(OBJDIR)/libbpf.so.$(LIBBPF_VERSION)
1147c2aad20Sopenharmony_ci	$(Q)ln -sf $(^F) $@
1157c2aad20Sopenharmony_ci
1167c2aad20Sopenharmony_ci$(OBJDIR)/libbpf.so.$(LIBBPF_VERSION): $(SHARED_OBJS)
1177c2aad20Sopenharmony_ci	$(call msg,CC,$@)
1187c2aad20Sopenharmony_ci	$(Q)$(CC) -shared -Wl,--version-script=$(VERSION_SCRIPT) \
1197c2aad20Sopenharmony_ci		  -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
1207c2aad20Sopenharmony_ci		  $^ $(ALL_LDFLAGS) -o $@
1217c2aad20Sopenharmony_ci
1227c2aad20Sopenharmony_ci$(OBJDIR)/libbpf.pc: force
1237c2aad20Sopenharmony_ci	$(Q)sed -e "s|@PREFIX@|$(PREFIX)|" \
1247c2aad20Sopenharmony_ci		-e "s|@LIBDIR@|$(LIBDIR_PC)|" \
1257c2aad20Sopenharmony_ci		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
1267c2aad20Sopenharmony_ci		< libbpf.pc.template > $@
1277c2aad20Sopenharmony_ci
1287c2aad20Sopenharmony_ci$(STATIC_OBJDIR) $(SHARED_OBJDIR):
1297c2aad20Sopenharmony_ci	$(call msg,MKDIR,$@)
1307c2aad20Sopenharmony_ci	$(Q)mkdir -p $@
1317c2aad20Sopenharmony_ci
1327c2aad20Sopenharmony_ci$(STATIC_OBJDIR)/%.o: %.c | $(STATIC_OBJDIR)
1337c2aad20Sopenharmony_ci	$(call msg,CC,$@)
1347c2aad20Sopenharmony_ci	$(Q)$(CC) $(ALL_CFLAGS) $(CPPFLAGS) -c $< -o $@
1357c2aad20Sopenharmony_ci
1367c2aad20Sopenharmony_ci$(SHARED_OBJDIR)/%.o: %.c | $(SHARED_OBJDIR)
1377c2aad20Sopenharmony_ci	$(call msg,CC,$@)
1387c2aad20Sopenharmony_ci	$(Q)$(CC) $(ALL_CFLAGS) $(SHARED_CFLAGS) $(CPPFLAGS) -c $< -o $@
1397c2aad20Sopenharmony_ci
1407c2aad20Sopenharmony_cidefine do_install
1417c2aad20Sopenharmony_ci	$(call msg,INSTALL,$1)
1427c2aad20Sopenharmony_ci	$(Q)if [ ! -d '$(DESTDIR)$2' ]; then		\
1437c2aad20Sopenharmony_ci		$(INSTALL) -d -m 755 '$(DESTDIR)$2';	\
1447c2aad20Sopenharmony_ci	fi;
1457c2aad20Sopenharmony_ci	$(Q)$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR)$2'
1467c2aad20Sopenharmony_ciendef
1477c2aad20Sopenharmony_ci
1487c2aad20Sopenharmony_ci# Preserve symlinks at installation.
1497c2aad20Sopenharmony_cidefine do_s_install
1507c2aad20Sopenharmony_ci	$(call msg,INSTALL,$1)
1517c2aad20Sopenharmony_ci	$(Q)if [ ! -d '$(DESTDIR)$2' ]; then		\
1527c2aad20Sopenharmony_ci		$(INSTALL) -d -m 755 '$(DESTDIR)$2';	\
1537c2aad20Sopenharmony_ci	fi;
1547c2aad20Sopenharmony_ci	$(Q)cp -fR $1 '$(DESTDIR)$2'
1557c2aad20Sopenharmony_ciendef
1567c2aad20Sopenharmony_ci
1577c2aad20Sopenharmony_ciinstall: all install_headers install_pkgconfig
1587c2aad20Sopenharmony_ci	$(call do_s_install,$(STATIC_LIBS) $(SHARED_LIBS),$(LIBDIR))
1597c2aad20Sopenharmony_ci
1607c2aad20Sopenharmony_ciinstall_headers:
1617c2aad20Sopenharmony_ci	$(call do_install,$(HEADERS),$(INCLUDEDIR)/bpf,644)
1627c2aad20Sopenharmony_ci
1637c2aad20Sopenharmony_ci# UAPI headers can be installed by a different package so they're not installed
1647c2aad20Sopenharmony_ci# in by install rule.
1657c2aad20Sopenharmony_ciinstall_uapi_headers:
1667c2aad20Sopenharmony_ci	$(call do_install,$(UAPI_HEADERS),$(UAPIDIR)/linux,644)
1677c2aad20Sopenharmony_ci
1687c2aad20Sopenharmony_ciinstall_pkgconfig: $(PC_FILE)
1697c2aad20Sopenharmony_ci	$(call do_install,$(PC_FILE),$(LIBDIR)/pkgconfig,644)
1707c2aad20Sopenharmony_ci
1717c2aad20Sopenharmony_ciclean:
1727c2aad20Sopenharmony_ci	$(call msg,CLEAN)
1737c2aad20Sopenharmony_ci	$(Q)rm -rf *.o *.a *.so *.so.* *.pc $(SHARED_OBJDIR) $(STATIC_OBJDIR)
1747c2aad20Sopenharmony_ci
1757c2aad20Sopenharmony_ci.PHONY: cscope tags force
1767c2aad20Sopenharmony_cicscope:
1777c2aad20Sopenharmony_ci	$(call msg,CSCOPE)
1787c2aad20Sopenharmony_ci	$(Q)ls *.c *.h > cscope.files
1797c2aad20Sopenharmony_ci	$(Q)cscope -b -q -f cscope.out
1807c2aad20Sopenharmony_ci
1817c2aad20Sopenharmony_citags:
1827c2aad20Sopenharmony_ci	$(call msg,CTAGS)
1837c2aad20Sopenharmony_ci	$(Q)rm -f TAGS tags
1847c2aad20Sopenharmony_ci	$(Q)ls *.c *.h | xargs $(TAGS_PROG) -a
1857c2aad20Sopenharmony_ci
1867c2aad20Sopenharmony_ciforce:
187