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 111 112ifeq ($(VERBOSE),1) 113 Q = 114else 115 Q = @ 116endif 117 118# Disable command line variables (CFLAGS) override from top 119# level Makefile (perf), otherwise build Makefile will get 120# the same command line setup. 121MAKEOVERRIDES= 122 123all: 124 125export srctree OUTPUT CC LD CFLAGS V 126include $(srctree)/tools/build/Makefile.include 127 128BPF_IN := $(OUTPUT)libbpf-in.o 129VERSION_SCRIPT := libbpf.map 130 131LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET)) 132LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) 133PC_FILE := $(addprefix $(OUTPUT),$(PC_FILE)) 134 135GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \ 136 awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}') 137VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \ 138 grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l) 139 140CMD_TARGETS = $(LIB_TARGET) $(PC_FILE) 141 142CXX_TEST_TARGET = $(OUTPUT)test_libbpf 143 144ifeq ($(feature-cxx), 1) 145 CMD_TARGETS += $(CXX_TEST_TARGET) 146endif 147 148TARGETS = $(CMD_TARGETS) 149 150all: fixdep 151 $(Q)$(MAKE) all_cmd 152 153all_cmd: $(CMD_TARGETS) check 154 155$(BPF_IN): force elfdep bpfdep 156 @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \ 157 (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \ 158 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true 159 @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \ 160 (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \ 161 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 162 @(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \ 163 (diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \ 164 echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true 165 @(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \ 166 (diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \ 167 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 168 @(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \ 169 (diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \ 170 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 171 $(Q)$(MAKE) $(build)=libbpf 172 173$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION) 174 175$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN) 176 $(QUIET_LINK)$(CC) --shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \ 177 -Wl,--version-script=$(VERSION_SCRIPT) $^ -lelf -o $@ 178 @ln -sf $(@F) $(OUTPUT)libbpf.so 179 @ln -sf $(@F) $(OUTPUT)libbpf.so.$(LIBBPF_MAJOR_VERSION) 180 181$(OUTPUT)libbpf.a: $(BPF_IN) 182 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ 183 184$(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a 185 $(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@ 186 187$(OUTPUT)libbpf.pc: 188 $(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \ 189 -e "s|@LIBDIR@|$(libdir_SQ)|" \ 190 -e "s|@VERSION@|$(LIBBPF_VERSION)|" \ 191 < libbpf.pc.template > $@ 192 193check: check_abi 194 195check_abi: $(OUTPUT)libbpf.so 196 @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then \ 197 echo "Warning: Num of global symbols in $(BPF_IN)" \ 198 "($(GLOBAL_SYM_COUNT)) does NOT match with num of" \ 199 "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \ 200 "Please make sure all LIBBPF_API symbols are" \ 201 "versioned in $(VERSION_SCRIPT)." >&2; \ 202 readelf -s --wide $(OUTPUT)libbpf-in.o | \ 203 awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$8}'| \ 204 sort -u > $(OUTPUT)libbpf_global_syms.tmp; \ 205 readelf -s --wide $(OUTPUT)libbpf.so | \ 206 grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | \ 207 sort -u > $(OUTPUT)libbpf_versioned_syms.tmp; \ 208 diff -u $(OUTPUT)libbpf_global_syms.tmp \ 209 $(OUTPUT)libbpf_versioned_syms.tmp; \ 210 rm $(OUTPUT)libbpf_global_syms.tmp \ 211 $(OUTPUT)libbpf_versioned_syms.tmp; \ 212 exit 1; \ 213 fi 214 215define do_install_mkdir 216 if [ ! -d '$(DESTDIR_SQ)$1' ]; then \ 217 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \ 218 fi 219endef 220 221define do_install 222 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ 223 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ 224 fi; \ 225 $(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2' 226endef 227 228install_lib: all_cmd 229 $(call QUIET_INSTALL, $(LIB_TARGET)) \ 230 $(call do_install_mkdir,$(libdir_SQ)); \ 231 cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ) 232 233install_headers: 234 $(call QUIET_INSTALL, headers) \ 235 $(call do_install,bpf.h,$(prefix)/include/bpf,644); \ 236 $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \ 237 $(call do_install,btf.h,$(prefix)/include/bpf,644); \ 238 $(call do_install,libbpf_util.h,$(prefix)/include/bpf,644); \ 239 $(call do_install,xsk.h,$(prefix)/include/bpf,644); 240 241install_pkgconfig: $(PC_FILE) 242 $(call QUIET_INSTALL, $(PC_FILE)) \ 243 $(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644) 244 245install: install_lib install_pkgconfig 246 247### Cleaning rules 248 249config-clean: 250 $(call QUIET_CLEAN, config) 251 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 252 253clean: 254 $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \ 255 *.o *~ *.a *.so *.so.$(LIBBPF_MAJOR_VERSION) .*.d .*.cmd \ 256 *.pc LIBBPF-CFLAGS 257 $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf 258 259 260 261PHONY += force elfdep bpfdep 262force: 263 264elfdep: 265 @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi 266 267bpfdep: 268 @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi 269 270# Declare the contents of the .PHONY variable as phony. We keep that 271# information in a variable so we can use it in if_changed and friends. 272.PHONY: $(PHONY) 273