1# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2# Most of this file is copied from tools/lib/traceevent/Makefile 3 4LIBBPF_VERSION := $(shell \ 5 grep -oE '^LIBBPF_([0-9.]+)' libbpf.map | \ 6 sort -rV | head -n1 | cut -d'_' -f2) 7LIBBPF_MAJOR_VERSION := $(firstword $(subst ., ,$(LIBBPF_VERSION))) 8 9MAKEFLAGS += --no-print-directory 10 11ifeq ($(srctree),) 12srctree := $(patsubst %/,%,$(dir $(CURDIR))) 13srctree := $(patsubst %/,%,$(dir $(srctree))) 14srctree := $(patsubst %/,%,$(dir $(srctree))) 15#$(info Determined 'srctree' to be $(srctree)) 16endif 17 18INSTALL = install 19 20# Use DESTDIR for installing into a different root directory. 21# This is useful for building a package. The program will be 22# installed in this directory as if it was the root directory. 23# Then the build tool can move it later. 24DESTDIR ?= 25DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' 26 27include $(srctree)/tools/scripts/Makefile.arch 28 29ifeq ($(LP64), 1) 30 libdir_relative = lib64 31else 32 libdir_relative = lib 33endif 34 35prefix ?= /usr/local 36libdir = $(prefix)/$(libdir_relative) 37man_dir = $(prefix)/share/man 38man_dir_SQ = '$(subst ','\'',$(man_dir))' 39 40export man_dir man_dir_SQ INSTALL 41export DESTDIR DESTDIR_SQ 42 43include $(srctree)/tools/scripts/Makefile.include 44 45# copy a bit from Linux kbuild 46 47ifeq ("$(origin V)", "command line") 48 VERBOSE = $(V) 49endif 50ifndef VERBOSE 51 VERBOSE = 0 52endif 53 54FEATURE_USER = .libbpf 55FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx 56FEATURE_DISPLAY = libelf bpf 57 58INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi 59FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES) 60 61check_feat := 1 62NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help 63ifdef MAKECMDGOALS 64ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) 65 check_feat := 0 66endif 67endif 68 69ifeq ($(check_feat),1) 70ifeq ($(FEATURES_DUMP),) 71include $(srctree)/tools/build/Makefile.feature 72else 73include $(FEATURES_DUMP) 74endif 75endif 76 77export prefix libdir src obj 78 79# Shell quotes 80libdir_SQ = $(subst ','\'',$(libdir)) 81libdir_relative_SQ = $(subst ','\'',$(libdir_relative)) 82 83OBJ = $@ 84N = 85 86LIB_TARGET = libbpf.a libbpf.so.$(LIBBPF_VERSION) 87LIB_FILE = libbpf.a libbpf.so* 88PC_FILE = libbpf.pc 89 90# Set compile option CFLAGS 91ifdef EXTRA_CFLAGS 92 CFLAGS := $(EXTRA_CFLAGS) 93else 94 CFLAGS := -g -Wall 95endif 96 97ifeq ($(feature-libelf-mmap), 1) 98 override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT 99endif 100 101ifeq ($(feature-reallocarray), 0) 102 override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY 103endif 104 105# Append required CFLAGS 106override CFLAGS += $(EXTRA_WARNINGS) 107override CFLAGS += -Werror -Wall 108override CFLAGS += -fPIC 109override CFLAGS += $(INCLUDES) 110override CFLAGS += -fvisibility=hidden 111override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 112 113ifeq ($(VERBOSE),1) 114 Q = 115else 116 Q = @ 117endif 118 119# Disable command line variables (CFLAGS) override from top 120# level Makefile (perf), otherwise build Makefile will get 121# the same command line setup. 122MAKEOVERRIDES= 123 124all: 125 126export srctree OUTPUT CC LD CFLAGS V 127include $(srctree)/tools/build/Makefile.include 128 129BPF_IN := $(OUTPUT)libbpf-in.o 130VERSION_SCRIPT := libbpf.map 131 132LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET)) 133LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) 134PC_FILE := $(addprefix $(OUTPUT),$(PC_FILE)) 135 136GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \ 137 cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \ 138 awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$8}' | \ 139 sort -u | wc -l) 140VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \ 141 grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l) 142 143CMD_TARGETS = $(LIB_TARGET) $(PC_FILE) 144 145CXX_TEST_TARGET = $(OUTPUT)test_libbpf 146 147ifeq ($(feature-cxx), 1) 148 CMD_TARGETS += $(CXX_TEST_TARGET) 149endif 150 151TARGETS = $(CMD_TARGETS) 152 153all: fixdep 154 $(Q)$(MAKE) all_cmd 155 156all_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 @(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \ 166 (diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \ 167 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true 168 @(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \ 169 (diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \ 170 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 @(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \ 172 (diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \ 173 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 $(Q)$(MAKE) $(build)=libbpf 175 176$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION) 177 178$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN) 179 $(QUIET_LINK)$(CC) --shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \ 180 -Wl,--version-script=$(VERSION_SCRIPT) $^ -lelf -o $@ 181 @ln -sf $(@F) $(OUTPUT)libbpf.so 182 @ln -sf $(@F) $(OUTPUT)libbpf.so.$(LIBBPF_MAJOR_VERSION) 183 184$(OUTPUT)libbpf.a: $(BPF_IN) 185 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ 186 187$(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a 188 $(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@ 189 190$(OUTPUT)libbpf.pc: 191 $(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \ 192 -e "s|@LIBDIR@|$(libdir_SQ)|" \ 193 -e "s|@VERSION@|$(LIBBPF_VERSION)|" \ 194 < libbpf.pc.template > $@ 195 196check: check_abi 197 198check_abi: $(OUTPUT)libbpf.so 199 @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then \ 200 echo "Warning: Num of global symbols in $(BPF_IN)" \ 201 "($(GLOBAL_SYM_COUNT)) does NOT match with num of" \ 202 "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \ 203 "Please make sure all LIBBPF_API symbols are" \ 204 "versioned in $(VERSION_SCRIPT)." >&2; \ 205 readelf -s --wide $(OUTPUT)libbpf-in.o | \ 206 cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \ 207 awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$8}'| \ 208 sort -u > $(OUTPUT)libbpf_global_syms.tmp; \ 209 readelf -s --wide $(OUTPUT)libbpf.so | \ 210 grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | \ 211 sort -u > $(OUTPUT)libbpf_versioned_syms.tmp; \ 212 diff -u $(OUTPUT)libbpf_global_syms.tmp \ 213 $(OUTPUT)libbpf_versioned_syms.tmp; \ 214 rm $(OUTPUT)libbpf_global_syms.tmp \ 215 $(OUTPUT)libbpf_versioned_syms.tmp; \ 216 exit 1; \ 217 fi 218 219define do_install_mkdir 220 if [ ! -d '$(DESTDIR_SQ)$1' ]; then \ 221 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \ 222 fi 223endef 224 225define do_install 226 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ 227 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ 228 fi; \ 229 $(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2' 230endef 231 232install_lib: all_cmd 233 $(call QUIET_INSTALL, $(LIB_TARGET)) \ 234 $(call do_install_mkdir,$(libdir_SQ)); \ 235 cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ) 236 237install_headers: 238 $(call QUIET_INSTALL, headers) \ 239 $(call do_install,bpf.h,$(prefix)/include/bpf,644); \ 240 $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \ 241 $(call do_install,btf.h,$(prefix)/include/bpf,644); \ 242 $(call do_install,libbpf_util.h,$(prefix)/include/bpf,644); \ 243 $(call do_install,xsk.h,$(prefix)/include/bpf,644); 244 245install_pkgconfig: $(PC_FILE) 246 $(call QUIET_INSTALL, $(PC_FILE)) \ 247 $(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644) 248 249install: install_lib install_pkgconfig 250 251### Cleaning rules 252 253config-clean: 254 $(call QUIET_CLEAN, config) 255 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 256 257clean: 258 $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \ 259 *.o *~ *.a *.so *.so.$(LIBBPF_MAJOR_VERSION) .*.d .*.cmd \ 260 *.pc LIBBPF-CFLAGS 261 $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf 262 263 264 265PHONY += force elfdep bpfdep 266force: 267 268elfdep: 269 @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi 270 271bpfdep: 272 @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi 273 274# Declare the contents of the .PHONY variable as phony. We keep that 275# information in a variable so we can use it in if_changed and friends. 276.PHONY: $(PHONY) 277