Makefile 7.6 KB
Newer Older
1
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 3 4 5
# Most of this file is copied from tools/lib/traceevent/Makefile

BPF_VERSION = 0
BPF_PATCHLEVEL = 0
6
BPF_EXTRAVERSION = 2
7 8 9

MAKEFLAGS += --no-print-directory

10
ifeq ($(srctree),)
11
srctree := $(patsubst %/,%,$(dir $(CURDIR)))
12 13 14 15
srctree := $(patsubst %/,%,$(dir $(srctree)))
srctree := $(patsubst %/,%,$(dir $(srctree)))
#$(info Determined 'srctree' to be $(srctree))
endif
16 17 18 19 20 21 22 23 24 25

INSTALL = install

# Use DESTDIR for installing into a different root directory.
# This is useful for building a package. The program will be
# installed in this directory as if it was the root directory.
# Then the build tool can move it later.
DESTDIR ?=
DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'

26 27
include $(srctree)/tools/scripts/Makefile.arch

28 29 30 31 32 33 34 35 36 37 38 39 40 41
ifeq ($(LP64), 1)
  libdir_relative = lib64
else
  libdir_relative = lib
endif

prefix ?= /usr/local
libdir = $(prefix)/$(libdir_relative)
man_dir = $(prefix)/share/man
man_dir_SQ = '$(subst ','\'',$(man_dir))'

export man_dir man_dir_SQ INSTALL
export DESTDIR DESTDIR_SQ

42
include $(srctree)/tools/scripts/Makefile.include
43 44 45 46 47 48 49 50 51 52

# copy a bit from Linux kbuild

ifeq ("$(origin V)", "command line")
  VERBOSE = $(V)
endif
ifndef VERBOSE
  VERBOSE = 0
endif

53
FEATURE_USER = .libbpf
54
FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx
55
FEATURE_DISPLAY = libelf bpf
56

57
INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
58 59
FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)

60 61 62 63 64 65 66 67 68
check_feat := 1
NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
ifdef MAKECMDGOALS
ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
  check_feat := 0
endif
endif

ifeq ($(check_feat),1)
69
ifeq ($(FEATURES_DUMP),)
70
include $(srctree)/tools/build/Makefile.feature
71 72 73
else
include $(FEATURES_DUMP)
endif
74
endif
75 76 77 78 79 80 81 82 83 84 85 86 87 88

export prefix libdir src obj

# Shell quotes
libdir_SQ = $(subst ','\'',$(libdir))
libdir_relative_SQ = $(subst ','\'',$(libdir_relative))

VERSION		= $(BPF_VERSION)
PATCHLEVEL	= $(BPF_PATCHLEVEL)
EXTRAVERSION	= $(BPF_EXTRAVERSION)

OBJ		= $@
N		=

89 90 91 92
LIBBPF_VERSION	= $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)

LIB_TARGET	= libbpf.a libbpf.so.$(LIBBPF_VERSION)
LIB_FILE	= libbpf.a libbpf.so*
93 94 95 96 97 98 99 100 101 102 103 104

# Set compile option CFLAGS
ifdef EXTRA_CFLAGS
  CFLAGS := $(EXTRA_CFLAGS)
else
  CFLAGS := -g -Wall
endif

ifeq ($(feature-libelf-mmap), 1)
  override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
endif

105 106 107 108
ifeq ($(feature-reallocarray), 0)
  override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
endif

109 110 111 112 113
# Append required CFLAGS
override CFLAGS += $(EXTRA_WARNINGS)
override CFLAGS += -Werror -Wall
override CFLAGS += -fPIC
override CFLAGS += $(INCLUDES)
114
override CFLAGS += -fvisibility=hidden
115 116 117 118 119 120 121

ifeq ($(VERBOSE),1)
  Q =
else
  Q = @
endif

122
# Disable command line variables (CFLAGS) override from top
123 124 125 126
# level Makefile (perf), otherwise build Makefile will get
# the same command line setup.
MAKEOVERRIDES=

127 128
all:

129
export srctree OUTPUT CC LD CFLAGS V
J
Jiri Olsa 已提交
130
include $(srctree)/tools/build/Makefile.include
131

132 133 134 135 136
BPF_IN		:= $(OUTPUT)libbpf-in.o
VERSION_SCRIPT	:= libbpf.map

LIB_TARGET	:= $(addprefix $(OUTPUT),$(LIB_TARGET))
LIB_FILE	:= $(addprefix $(OUTPUT),$(LIB_FILE))
137

138
GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
A
Andrey Ignatov 已提交
139
			   awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}')
140
VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
A
Andrey Ignatov 已提交
141 142
			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)

143
CMD_TARGETS = $(LIB_TARGET)
144

145 146 147 148 149 150
CXX_TEST_TARGET = $(OUTPUT)test_libbpf

ifeq ($(feature-cxx), 1)
	CMD_TARGETS += $(CXX_TEST_TARGET)
endif

151 152
TARGETS = $(CMD_TARGETS)

153 154
all: fixdep
	$(Q)$(MAKE) all_cmd
155

A
Andrey Ignatov 已提交
156
all_cmd: $(CMD_TARGETS) check
157 158

$(BPF_IN): force elfdep bpfdep
159
	@(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
160
	(diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
161
	echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
162
	@(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
163
	(diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
164
	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
165 166 167 168 169 170
	@(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \
	(diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \
	echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true
	@(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \
	(diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \
	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
171 172 173
	@(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
	(diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
	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
174 175
	$(Q)$(MAKE) $(build)=libbpf

176 177 178 179 180 181 182
$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)

$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN)
	$(QUIET_LINK)$(CC) --shared -Wl,-soname,libbpf.so.$(VERSION) \
				    -Wl,--version-script=$(VERSION_SCRIPT) $^ -o $@
	@ln -sf $(@F) $(OUTPUT)libbpf.so
	@ln -sf $(@F) $(OUTPUT)libbpf.so.$(VERSION)
183 184 185 186

$(OUTPUT)libbpf.a: $(BPF_IN)
	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^

187
$(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
188
	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
189

A
Andrey Ignatov 已提交
190 191 192 193 194 195 196 197 198 199 200 201
check: check_abi

check_abi: $(OUTPUT)libbpf.so
	@if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then	 \
		echo "Warning: Num of global symbols in $(BPF_IN)"	 \
		     "($(GLOBAL_SYM_COUNT)) does NOT match with num of"	 \
		     "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
		     "Please make sure all LIBBPF_API symbols are"	 \
		     "versioned in $(VERSION_SCRIPT)." >&2;		 \
		exit 1;							 \
	fi

202 203 204 205 206 207
define do_install_mkdir
	if [ ! -d '$(DESTDIR_SQ)$1' ]; then		\
		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1';	\
	fi
endef

208 209 210 211
define do_install
	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
	fi;						\
212
	$(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
213 214 215
endef

install_lib: all_cmd
216 217 218
	$(call QUIET_INSTALL, $(LIB_TARGET)) \
		$(call do_install_mkdir,$(libdir_SQ)); \
		cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
219

220 221
install_headers:
	$(call QUIET_INSTALL, headers) \
222 223
		$(call do_install,bpf.h,$(prefix)/include/bpf,644); \
		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
224
		$(call do_install,btf.h,$(prefix)/include/bpf,644);
225
		$(call do_install,xsk.h,$(prefix)/include/bpf,644);
226

227 228 229 230 231 232 233 234 235
install: install_lib

### Cleaning rules

config-clean:
	$(call QUIET_CLEAN, config)
	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null

clean:
236
	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
237
		*.o *~ *.a *.so *.so.$(VERSION) .*.d .*.cmd LIBBPF-CFLAGS
238
	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
239 240 241 242 243 244 245



PHONY += force elfdep bpfdep
force:

elfdep:
246
	@if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
247 248

bpfdep:
249
	@if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
250 251 252 253

# Declare the contents of the .PHONY variable as phony.  We keep that
# information in a variable so we can use it in if_changed and friends.
.PHONY: $(PHONY)