• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3ifeq ($(V),1)
4	Q =
5	msg =
6else
7	Q = @
8	msg = @printf '  %-8s %s%s\n' "$(1)" "$(2)" "$(if $(3), $(3))";
9endif
10
11LIBBPF_VERSION := $(shell \
12	grep -oE '^LIBBPF_([0-9.]+)' libbpf.map | \
13	sort -rV | head -n1 | cut -d'_' -f2)
14LIBBPF_MAJOR_VERSION := $(firstword $(subst ., ,$(LIBBPF_VERSION)))
15
16TOPDIR = ..
17
18INCLUDES := -I. -I$(TOPDIR)/include -I$(TOPDIR)/include/uapi
19ALL_CFLAGS := $(INCLUDES)
20
21SHARED_CFLAGS += -fPIC -fvisibility=hidden -DSHARED
22
23CFLAGS ?= -g -O2 -Werror -Wall -std=gnu89
24ALL_CFLAGS += $(CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
25ALL_LDFLAGS += $(LDFLAGS)
26ifdef NO_PKG_CONFIG
27	ALL_LDFLAGS += -lelf -lz
28else
29	PKG_CONFIG ?= pkg-config
30	ALL_CFLAGS += $(shell $(PKG_CONFIG) --cflags libelf zlib)
31	ALL_LDFLAGS += $(shell $(PKG_CONFIG) --libs libelf zlib)
32endif
33
34OBJDIR ?= .
35SHARED_OBJDIR := $(OBJDIR)/sharedobjs
36STATIC_OBJDIR := $(OBJDIR)/staticobjs
37OBJS := bpf.o btf.o libbpf.o libbpf_errno.o netlink.o \
38	nlattr.o str_error.o libbpf_probes.o bpf_prog_linfo.o xsk.o \
39	btf_dump.o hashmap.o ringbuf.o strset.o linker.o gen_loader.o \
40	relo_core.o
41SHARED_OBJS := $(addprefix $(SHARED_OBJDIR)/,$(OBJS))
42STATIC_OBJS := $(addprefix $(STATIC_OBJDIR)/,$(OBJS))
43
44STATIC_LIBS := $(OBJDIR)/libbpf.a
45ifndef BUILD_STATIC_ONLY
46	SHARED_LIBS := $(OBJDIR)/libbpf.so \
47		       $(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION) \
48		       $(OBJDIR)/libbpf.so.$(LIBBPF_VERSION)
49	VERSION_SCRIPT := libbpf.map
50endif
51
52HEADERS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h xsk.h \
53	   bpf_helpers.h bpf_helper_defs.h bpf_tracing.h \
54	   bpf_endian.h bpf_core_read.h skel_internal.h libbpf_version.h
55UAPI_HEADERS := $(addprefix $(TOPDIR)/include/uapi/linux/,\
56			    bpf.h bpf_common.h btf.h)
57
58PC_FILE := $(OBJDIR)/libbpf.pc
59
60INSTALL = install
61
62DESTDIR ?=
63
64ifeq ($(filter-out %64 %64be %64eb %64le %64el s390x, $(shell uname -m)),)
65	LIBSUBDIR := lib64
66else
67	LIBSUBDIR := lib
68endif
69
70# By default let the pc file itself use ${prefix} in includedir/libdir so that
71# the prefix can be overridden at runtime (eg: --define-prefix)
72ifndef LIBDIR
73	LIBDIR_PC := $$\{prefix\}/$(LIBSUBDIR)
74else
75	LIBDIR_PC := $(LIBDIR)
76endif
77PREFIX ?= /usr
78LIBDIR ?= $(PREFIX)/$(LIBSUBDIR)
79INCLUDEDIR ?= $(PREFIX)/include
80UAPIDIR ?= $(PREFIX)/include
81
82TAGS_PROG := $(if $(shell which etags 2>/dev/null),etags,ctags)
83
84all: $(STATIC_LIBS) $(SHARED_LIBS) $(PC_FILE)
85
86$(OBJDIR)/libbpf.a: $(STATIC_OBJS)
87	$(call msg,AR,$@)
88	$(Q)$(AR) rcs $@ $^
89
90$(OBJDIR)/libbpf.so: $(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION)
91	$(Q)ln -sf $(^F) $@
92
93$(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION): $(OBJDIR)/libbpf.so.$(LIBBPF_VERSION)
94	$(Q)ln -sf $(^F) $@
95
96$(OBJDIR)/libbpf.so.$(LIBBPF_VERSION): $(SHARED_OBJS)
97	$(call msg,CC,$@)
98	$(Q)$(CC) -shared -Wl,--version-script=$(VERSION_SCRIPT) \
99		  -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
100		  $^ $(ALL_LDFLAGS) -o $@
101
102$(OBJDIR)/libbpf.pc:
103	$(Q)sed -e "s|@PREFIX@|$(PREFIX)|" \
104		-e "s|@LIBDIR@|$(LIBDIR_PC)|" \
105		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
106		< libbpf.pc.template > $@
107
108$(STATIC_OBJDIR) $(SHARED_OBJDIR):
109	$(call msg,MKDIR,$@)
110	$(Q)mkdir -p $@
111
112$(STATIC_OBJDIR)/%.o: %.c | $(STATIC_OBJDIR)
113	$(call msg,CC,$@)
114	$(Q)$(CC) $(ALL_CFLAGS) $(CPPFLAGS) -c $< -o $@
115
116$(SHARED_OBJDIR)/%.o: %.c | $(SHARED_OBJDIR)
117	$(call msg,CC,$@)
118	$(Q)$(CC) $(ALL_CFLAGS) $(SHARED_CFLAGS) $(CPPFLAGS) -c $< -o $@
119
120define do_install
121	$(call msg,INSTALL,$1)
122	$(Q)if [ ! -d '$(DESTDIR)$2' ]; then		\
123		$(INSTALL) -d -m 755 '$(DESTDIR)$2';	\
124	fi;
125	$(Q)$(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR)$2'
126endef
127
128# Preserve symlinks at installation.
129define do_s_install
130	$(call msg,INSTALL,$1)
131	$(Q)if [ ! -d '$(DESTDIR)$2' ]; then		\
132		$(INSTALL) -d -m 755 '$(DESTDIR)$2';	\
133	fi;
134	$(Q)cp -fR $1 '$(DESTDIR)$2'
135endef
136
137install: all install_headers install_pkgconfig
138	$(call do_s_install,$(STATIC_LIBS) $(SHARED_LIBS),$(LIBDIR))
139
140install_headers:
141	$(call do_install,$(HEADERS),$(INCLUDEDIR)/bpf,644)
142
143# UAPI headers can be installed by a different package so they're not installed
144# in by install rule.
145install_uapi_headers:
146	$(call do_install,$(UAPI_HEADERS),$(UAPIDIR)/linux,644)
147
148install_pkgconfig: $(PC_FILE)
149	$(call do_install,$(PC_FILE),$(LIBDIR)/pkgconfig,644)
150
151clean:
152	$(call msg,CLEAN)
153	$(Q)rm -rf *.o *.a *.so *.so.* *.pc $(SHARED_OBJDIR) $(STATIC_OBJDIR)
154
155.PHONY: cscope tags
156cscope:
157	$(call msg,CSCOPE)
158	$(Q)ls *.c *.h > cscope.files
159	$(Q)cscope -b -q -f cscope.out
160
161tags:
162	$(call msg,CTAGS)
163	$(Q)rm -f TAGS tags
164	$(Q)ls *.c *.h | xargs $(TAGS_PROG) -a
165