xref: /aosp_15_r20/external/libbpf/src/Makefile (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker
3*f7c14bbaSAndroid Build Coastguard Workerifeq ($(V),1)
4*f7c14bbaSAndroid Build Coastguard Worker	Q =
5*f7c14bbaSAndroid Build Coastguard Worker	msg =
6*f7c14bbaSAndroid Build Coastguard Workerelse
7*f7c14bbaSAndroid Build Coastguard Worker	Q = @
8*f7c14bbaSAndroid Build Coastguard Worker	msg = @printf '  %-8s %s%s\n' "$(1)" "$(2)" "$(if $(3), $(3))";
9*f7c14bbaSAndroid Build Coastguard Workerendif
10*f7c14bbaSAndroid Build Coastguard Worker
11*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_MAJOR_VERSION := 1
12*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_MINOR_VERSION := 4
13*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_PATCH_VERSION := 5
14*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_VERSION := $(LIBBPF_MAJOR_VERSION).$(LIBBPF_MINOR_VERSION).$(LIBBPF_PATCH_VERSION)
15*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_MAJMIN_VERSION := $(LIBBPF_MAJOR_VERSION).$(LIBBPF_MINOR_VERSION).0
16*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_MAP_VERSION := $(shell grep -oE '^LIBBPF_([0-9.]+)' libbpf.map | sort -rV | head -n1 | cut -d'_' -f2)
17*f7c14bbaSAndroid Build Coastguard Workerifneq ($(LIBBPF_MAJMIN_VERSION), $(LIBBPF_MAP_VERSION))
18*f7c14bbaSAndroid Build Coastguard Worker$(error Libbpf release ($(LIBBPF_VERSION)) and map ($(LIBBPF_MAP_VERSION)) versions are out of sync!)
19*f7c14bbaSAndroid Build Coastguard Workerendif
20*f7c14bbaSAndroid Build Coastguard Worker
21*f7c14bbaSAndroid Build Coastguard Workerdefine allow-override
22*f7c14bbaSAndroid Build Coastguard Worker  $(if $(or $(findstring environment,$(origin $(1))),\
23*f7c14bbaSAndroid Build Coastguard Worker            $(findstring command line,$(origin $(1)))),,\
24*f7c14bbaSAndroid Build Coastguard Worker    $(eval $(1) = $(2)))
25*f7c14bbaSAndroid Build Coastguard Workerendef
26*f7c14bbaSAndroid Build Coastguard Worker
27*f7c14bbaSAndroid Build Coastguard Worker$(call allow-override,CC,$(CROSS_COMPILE)cc)
28*f7c14bbaSAndroid Build Coastguard Worker$(call allow-override,LD,$(CROSS_COMPILE)ld)
29*f7c14bbaSAndroid Build Coastguard Worker
30*f7c14bbaSAndroid Build Coastguard WorkerTOPDIR = ..
31*f7c14bbaSAndroid Build Coastguard Worker
32*f7c14bbaSAndroid Build Coastguard WorkerINCLUDES := -I. -I$(TOPDIR)/include -I$(TOPDIR)/include/uapi
33*f7c14bbaSAndroid Build Coastguard WorkerALL_CFLAGS := $(INCLUDES)
34*f7c14bbaSAndroid Build Coastguard Worker
35*f7c14bbaSAndroid Build Coastguard WorkerSHARED_CFLAGS += -fPIC -fvisibility=hidden -DSHARED
36*f7c14bbaSAndroid Build Coastguard Worker
37*f7c14bbaSAndroid Build Coastguard WorkerCFLAGS ?= -g -O2 -Werror -Wall -std=gnu89
38*f7c14bbaSAndroid Build Coastguard WorkerALL_CFLAGS += $(CFLAGS) 						\
39*f7c14bbaSAndroid Build Coastguard Worker	      -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64		\
40*f7c14bbaSAndroid Build Coastguard Worker	      -Wno-unknown-warning-option -Wno-format-overflow		\
41*f7c14bbaSAndroid Build Coastguard Worker	      $(EXTRA_CFLAGS)
42*f7c14bbaSAndroid Build Coastguard WorkerALL_LDFLAGS += $(LDFLAGS) $(EXTRA_LDFLAGS)
43*f7c14bbaSAndroid Build Coastguard Worker
44*f7c14bbaSAndroid Build Coastguard Workerifdef NO_PKG_CONFIG
45*f7c14bbaSAndroid Build Coastguard Worker	ALL_LDFLAGS += -lelf -lz
46*f7c14bbaSAndroid Build Coastguard Workerelse
47*f7c14bbaSAndroid Build Coastguard Worker	PKG_CONFIG ?= pkg-config
48*f7c14bbaSAndroid Build Coastguard Worker	ALL_CFLAGS += $(shell $(PKG_CONFIG) --cflags libelf zlib)
49*f7c14bbaSAndroid Build Coastguard Worker	ALL_LDFLAGS += $(shell $(PKG_CONFIG) --libs libelf zlib)
50*f7c14bbaSAndroid Build Coastguard Workerendif
51*f7c14bbaSAndroid Build Coastguard Worker
52*f7c14bbaSAndroid Build Coastguard WorkerOBJDIR ?= .
53*f7c14bbaSAndroid Build Coastguard WorkerSHARED_OBJDIR := $(OBJDIR)/sharedobjs
54*f7c14bbaSAndroid Build Coastguard WorkerSTATIC_OBJDIR := $(OBJDIR)/staticobjs
55*f7c14bbaSAndroid Build Coastguard WorkerOBJS := bpf.o btf.o libbpf.o libbpf_errno.o netlink.o \
56*f7c14bbaSAndroid Build Coastguard Worker	nlattr.o str_error.o libbpf_probes.o bpf_prog_linfo.o \
57*f7c14bbaSAndroid Build Coastguard Worker	btf_dump.o hashmap.o ringbuf.o strset.o linker.o gen_loader.o \
58*f7c14bbaSAndroid Build Coastguard Worker	relo_core.o usdt.o zip.o elf.o features.o
59*f7c14bbaSAndroid Build Coastguard WorkerSHARED_OBJS := $(addprefix $(SHARED_OBJDIR)/,$(OBJS))
60*f7c14bbaSAndroid Build Coastguard WorkerSTATIC_OBJS := $(addprefix $(STATIC_OBJDIR)/,$(OBJS))
61*f7c14bbaSAndroid Build Coastguard Worker
62*f7c14bbaSAndroid Build Coastguard WorkerSTATIC_LIBS := $(OBJDIR)/libbpf.a
63*f7c14bbaSAndroid Build Coastguard Workerifndef BUILD_STATIC_ONLY
64*f7c14bbaSAndroid Build Coastguard Worker	SHARED_LIBS := $(OBJDIR)/libbpf.so \
65*f7c14bbaSAndroid Build Coastguard Worker		       $(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION) \
66*f7c14bbaSAndroid Build Coastguard Worker		       $(OBJDIR)/libbpf.so.$(LIBBPF_VERSION)
67*f7c14bbaSAndroid Build Coastguard Worker	VERSION_SCRIPT := libbpf.map
68*f7c14bbaSAndroid Build Coastguard Workerendif
69*f7c14bbaSAndroid Build Coastguard Worker
70*f7c14bbaSAndroid Build Coastguard WorkerHEADERS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h \
71*f7c14bbaSAndroid Build Coastguard Worker	   bpf_helpers.h bpf_helper_defs.h bpf_tracing.h \
72*f7c14bbaSAndroid Build Coastguard Worker	   bpf_endian.h bpf_core_read.h skel_internal.h libbpf_version.h \
73*f7c14bbaSAndroid Build Coastguard Worker	   usdt.bpf.h
74*f7c14bbaSAndroid Build Coastguard WorkerUAPI_HEADERS := $(addprefix $(TOPDIR)/include/uapi/linux/,\
75*f7c14bbaSAndroid Build Coastguard Worker			    bpf.h bpf_common.h btf.h)
76*f7c14bbaSAndroid Build Coastguard Worker
77*f7c14bbaSAndroid Build Coastguard WorkerPC_FILE := $(OBJDIR)/libbpf.pc
78*f7c14bbaSAndroid Build Coastguard Worker
79*f7c14bbaSAndroid Build Coastguard WorkerINSTALL = install
80*f7c14bbaSAndroid Build Coastguard Worker
81*f7c14bbaSAndroid Build Coastguard WorkerDESTDIR ?=
82*f7c14bbaSAndroid Build Coastguard Worker
83*f7c14bbaSAndroid Build Coastguard WorkerHOSTARCH = $(firstword $(subst -, ,$(shell $(CC) -dumpmachine)))
84*f7c14bbaSAndroid Build Coastguard Workerifeq ($(filter-out %64 %64be %64eb %64le %64el s390x, $(HOSTARCH)),)
85*f7c14bbaSAndroid Build Coastguard Worker	LIBSUBDIR := lib64
86*f7c14bbaSAndroid Build Coastguard Workerelse
87*f7c14bbaSAndroid Build Coastguard Worker	LIBSUBDIR := lib
88*f7c14bbaSAndroid Build Coastguard Workerendif
89*f7c14bbaSAndroid Build Coastguard Worker
90*f7c14bbaSAndroid Build Coastguard Worker# By default let the pc file itself use ${prefix} in includedir/libdir so that
91*f7c14bbaSAndroid Build Coastguard Worker# the prefix can be overridden at runtime (eg: --define-prefix)
92*f7c14bbaSAndroid Build Coastguard Workerifndef LIBDIR
93*f7c14bbaSAndroid Build Coastguard Worker	LIBDIR_PC := $$\{prefix\}/$(LIBSUBDIR)
94*f7c14bbaSAndroid Build Coastguard Workerelse
95*f7c14bbaSAndroid Build Coastguard Worker	LIBDIR_PC := $(LIBDIR)
96*f7c14bbaSAndroid Build Coastguard Workerendif
97*f7c14bbaSAndroid Build Coastguard WorkerPREFIX ?= /usr
98*f7c14bbaSAndroid Build Coastguard WorkerLIBDIR ?= $(PREFIX)/$(LIBSUBDIR)
99*f7c14bbaSAndroid Build Coastguard WorkerINCLUDEDIR ?= $(PREFIX)/include
100*f7c14bbaSAndroid Build Coastguard WorkerUAPIDIR ?= $(PREFIX)/include
101*f7c14bbaSAndroid Build Coastguard Worker
102*f7c14bbaSAndroid Build Coastguard WorkerTAGS_PROG := $(if $(shell which etags 2>/dev/null),etags,ctags)
103*f7c14bbaSAndroid Build Coastguard Worker
104*f7c14bbaSAndroid Build Coastguard Workerall: $(STATIC_LIBS) $(SHARED_LIBS) $(PC_FILE)
105*f7c14bbaSAndroid Build Coastguard Worker
106*f7c14bbaSAndroid Build Coastguard Worker$(OBJDIR)/libbpf.a: $(STATIC_OBJS)
107*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,AR,$@)
108*f7c14bbaSAndroid Build Coastguard Worker	$(Q)$(AR) rcs $@ $^
109*f7c14bbaSAndroid Build Coastguard Worker
110*f7c14bbaSAndroid Build Coastguard Worker$(OBJDIR)/libbpf.so: $(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION)
111*f7c14bbaSAndroid Build Coastguard Worker	$(Q)ln -sf $(^F) $@
112*f7c14bbaSAndroid Build Coastguard Worker
113*f7c14bbaSAndroid Build Coastguard Worker$(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION): $(OBJDIR)/libbpf.so.$(LIBBPF_VERSION)
114*f7c14bbaSAndroid Build Coastguard Worker	$(Q)ln -sf $(^F) $@
115*f7c14bbaSAndroid Build Coastguard Worker
116*f7c14bbaSAndroid Build Coastguard Worker$(OBJDIR)/libbpf.so.$(LIBBPF_VERSION): $(SHARED_OBJS)
117*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,CC,$@)
118*f7c14bbaSAndroid Build Coastguard Worker	$(Q)$(CC) -shared -Wl,--version-script=$(VERSION_SCRIPT) \
119*f7c14bbaSAndroid Build Coastguard Worker		  -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
120*f7c14bbaSAndroid Build Coastguard Worker		  $^ $(ALL_LDFLAGS) -o $@
121*f7c14bbaSAndroid Build Coastguard Worker
122*f7c14bbaSAndroid Build Coastguard Worker$(OBJDIR)/libbpf.pc: force
123*f7c14bbaSAndroid Build Coastguard Worker	$(Q)sed -e "s|@PREFIX@|$(PREFIX)|" \
124*f7c14bbaSAndroid Build Coastguard Worker		-e "s|@LIBDIR@|$(LIBDIR_PC)|" \
125*f7c14bbaSAndroid Build Coastguard Worker		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
126*f7c14bbaSAndroid Build Coastguard Worker		< libbpf.pc.template > $@
127*f7c14bbaSAndroid Build Coastguard Worker
128*f7c14bbaSAndroid Build Coastguard Worker$(STATIC_OBJDIR) $(SHARED_OBJDIR):
129*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,MKDIR,$@)
130*f7c14bbaSAndroid Build Coastguard Worker	$(Q)mkdir -p $@
131*f7c14bbaSAndroid Build Coastguard Worker
132*f7c14bbaSAndroid Build Coastguard Worker$(STATIC_OBJDIR)/%.o: %.c | $(STATIC_OBJDIR)
133*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,CC,$@)
134*f7c14bbaSAndroid Build Coastguard Worker	$(Q)$(CC) $(ALL_CFLAGS) $(CPPFLAGS) -c $< -o $@
135*f7c14bbaSAndroid Build Coastguard Worker
136*f7c14bbaSAndroid Build Coastguard Worker$(SHARED_OBJDIR)/%.o: %.c | $(SHARED_OBJDIR)
137*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,CC,$@)
138*f7c14bbaSAndroid Build Coastguard Worker	$(Q)$(CC) $(ALL_CFLAGS) $(SHARED_CFLAGS) $(CPPFLAGS) -c $< -o $@
139*f7c14bbaSAndroid Build Coastguard Worker
140*f7c14bbaSAndroid Build Coastguard Workerdefine do_install
141*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,INSTALL,$1)
142*f7c14bbaSAndroid Build Coastguard Worker	$(Q)if [ ! -d '$(DESTDIR)$2' ]; then		\
143*f7c14bbaSAndroid Build Coastguard Worker		$(INSTALL) -d -m 755 '$(DESTDIR)$2';	\
144*f7c14bbaSAndroid Build Coastguard Worker	fi;
145*f7c14bbaSAndroid Build Coastguard Worker	$(Q)$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR)$2'
146*f7c14bbaSAndroid Build Coastguard Workerendef
147*f7c14bbaSAndroid Build Coastguard Worker
148*f7c14bbaSAndroid Build Coastguard Worker# Preserve symlinks at installation.
149*f7c14bbaSAndroid Build Coastguard Workerdefine do_s_install
150*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,INSTALL,$1)
151*f7c14bbaSAndroid Build Coastguard Worker	$(Q)if [ ! -d '$(DESTDIR)$2' ]; then		\
152*f7c14bbaSAndroid Build Coastguard Worker		$(INSTALL) -d -m 755 '$(DESTDIR)$2';	\
153*f7c14bbaSAndroid Build Coastguard Worker	fi;
154*f7c14bbaSAndroid Build Coastguard Worker	$(Q)cp -fR $1 '$(DESTDIR)$2'
155*f7c14bbaSAndroid Build Coastguard Workerendef
156*f7c14bbaSAndroid Build Coastguard Worker
157*f7c14bbaSAndroid Build Coastguard Workerinstall: all install_headers install_pkgconfig
158*f7c14bbaSAndroid Build Coastguard Worker	$(call do_s_install,$(STATIC_LIBS) $(SHARED_LIBS),$(LIBDIR))
159*f7c14bbaSAndroid Build Coastguard Worker
160*f7c14bbaSAndroid Build Coastguard Workerinstall_headers:
161*f7c14bbaSAndroid Build Coastguard Worker	$(call do_install,$(HEADERS),$(INCLUDEDIR)/bpf,644)
162*f7c14bbaSAndroid Build Coastguard Worker
163*f7c14bbaSAndroid Build Coastguard Worker# UAPI headers can be installed by a different package so they're not installed
164*f7c14bbaSAndroid Build Coastguard Worker# in by install rule.
165*f7c14bbaSAndroid Build Coastguard Workerinstall_uapi_headers:
166*f7c14bbaSAndroid Build Coastguard Worker	$(call do_install,$(UAPI_HEADERS),$(UAPIDIR)/linux,644)
167*f7c14bbaSAndroid Build Coastguard Worker
168*f7c14bbaSAndroid Build Coastguard Workerinstall_pkgconfig: $(PC_FILE)
169*f7c14bbaSAndroid Build Coastguard Worker	$(call do_install,$(PC_FILE),$(LIBDIR)/pkgconfig,644)
170*f7c14bbaSAndroid Build Coastguard Worker
171*f7c14bbaSAndroid Build Coastguard Workerclean:
172*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,CLEAN)
173*f7c14bbaSAndroid Build Coastguard Worker	$(Q)rm -rf *.o *.a *.so *.so.* *.pc $(SHARED_OBJDIR) $(STATIC_OBJDIR)
174*f7c14bbaSAndroid Build Coastguard Worker
175*f7c14bbaSAndroid Build Coastguard Worker.PHONY: cscope tags force
176*f7c14bbaSAndroid Build Coastguard Workercscope:
177*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,CSCOPE)
178*f7c14bbaSAndroid Build Coastguard Worker	$(Q)ls *.c *.h > cscope.files
179*f7c14bbaSAndroid Build Coastguard Worker	$(Q)cscope -b -q -f cscope.out
180*f7c14bbaSAndroid Build Coastguard Worker
181*f7c14bbaSAndroid Build Coastguard Workertags:
182*f7c14bbaSAndroid Build Coastguard Worker	$(call msg,CTAGS)
183*f7c14bbaSAndroid Build Coastguard Worker	$(Q)rm -f TAGS tags
184*f7c14bbaSAndroid Build Coastguard Worker	$(Q)ls *.c *.h | xargs $(TAGS_PROG) -a
185*f7c14bbaSAndroid Build Coastguard Worker
186*f7c14bbaSAndroid Build Coastguard Workerforce:
187