Back to home page

OSCL-LXR

 
 

    


0001 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
0002 # Most of this file is copied from tools/lib/traceevent/Makefile
0003 
0004 RM ?= rm
0005 srctree = $(abs_srctree)
0006 
0007 VERSION_SCRIPT := libbpf.map
0008 LIBBPF_VERSION := $(shell \
0009         grep -oE '^LIBBPF_([0-9.]+)' $(VERSION_SCRIPT) | \
0010         sort -rV | head -n1 | cut -d'_' -f2)
0011 LIBBPF_MAJOR_VERSION := $(word 1,$(subst ., ,$(LIBBPF_VERSION)))
0012 LIBBPF_MINOR_VERSION := $(word 2,$(subst ., ,$(LIBBPF_VERSION)))
0013 
0014 MAKEFLAGS += --no-print-directory
0015 
0016 # This will work when bpf is built in tools env. where srctree
0017 # isn't set and when invoked from selftests build, where srctree
0018 # is a ".". building_out_of_srctree is undefined for in srctree
0019 # builds
0020 ifndef building_out_of_srctree
0021 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
0022 srctree := $(patsubst %/,%,$(dir $(srctree)))
0023 srctree := $(patsubst %/,%,$(dir $(srctree)))
0024 #$(info Determined 'srctree' to be $(srctree))
0025 endif
0026 
0027 INSTALL = install
0028 
0029 # Use DESTDIR for installing into a different root directory.
0030 # This is useful for building a package. The program will be
0031 # installed in this directory as if it was the root directory.
0032 # Then the build tool can move it later.
0033 DESTDIR ?=
0034 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
0035 
0036 include $(srctree)/tools/scripts/Makefile.arch
0037 
0038 ifeq ($(LP64), 1)
0039   libdir_relative = lib64
0040 else
0041   libdir_relative = lib
0042 endif
0043 
0044 prefix ?= /usr/local
0045 libdir = $(prefix)/$(libdir_relative)
0046 man_dir = $(prefix)/share/man
0047 man_dir_SQ = '$(subst ','\'',$(man_dir))'
0048 
0049 export man_dir man_dir_SQ INSTALL
0050 export DESTDIR DESTDIR_SQ
0051 
0052 include $(srctree)/tools/scripts/Makefile.include
0053 
0054 # copy a bit from Linux kbuild
0055 
0056 ifeq ("$(origin V)", "command line")
0057   VERBOSE = $(V)
0058 endif
0059 ifndef VERBOSE
0060   VERBOSE = 0
0061 endif
0062 
0063 INCLUDES = -I$(or $(OUTPUT),.) \
0064            -I$(srctree)/tools/include -I$(srctree)/tools/include/uapi
0065 
0066 export prefix libdir src obj
0067 
0068 # Shell quotes
0069 libdir_SQ = $(subst ','\'',$(libdir))
0070 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
0071 
0072 OBJ             = $@
0073 N               =
0074 
0075 LIB_TARGET      = libbpf.a libbpf.so.$(LIBBPF_VERSION)
0076 LIB_FILE        = libbpf.a libbpf.so*
0077 PC_FILE         = libbpf.pc
0078 
0079 # Set compile option CFLAGS
0080 ifdef EXTRA_CFLAGS
0081   CFLAGS := $(EXTRA_CFLAGS)
0082 else
0083   CFLAGS := -g -O2
0084 endif
0085 
0086 # Append required CFLAGS
0087 override CFLAGS += -std=gnu89
0088 override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
0089 override CFLAGS += -Werror -Wall
0090 override CFLAGS += $(INCLUDES)
0091 override CFLAGS += -fvisibility=hidden
0092 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
0093 override CFLAGS += $(CLANG_CROSS_FLAGS)
0094 
0095 # flags specific for shared library
0096 SHLIB_FLAGS := -DSHARED -fPIC
0097 
0098 ifeq ($(VERBOSE),1)
0099   Q =
0100 else
0101   Q = @
0102 endif
0103 
0104 # Disable command line variables (CFLAGS) override from top
0105 # level Makefile (perf), otherwise build Makefile will get
0106 # the same command line setup.
0107 MAKEOVERRIDES=
0108 
0109 all:
0110 
0111 export srctree OUTPUT CC LD CFLAGS V
0112 include $(srctree)/tools/build/Makefile.include
0113 
0114 SHARED_OBJDIR   := $(OUTPUT)sharedobjs/
0115 STATIC_OBJDIR   := $(OUTPUT)staticobjs/
0116 BPF_IN_SHARED   := $(SHARED_OBJDIR)libbpf-in.o
0117 BPF_IN_STATIC   := $(STATIC_OBJDIR)libbpf-in.o
0118 BPF_HELPER_DEFS := $(OUTPUT)bpf_helper_defs.h
0119 BPF_GENERATED   := $(BPF_HELPER_DEFS)
0120 
0121 LIB_TARGET      := $(addprefix $(OUTPUT),$(LIB_TARGET))
0122 LIB_FILE        := $(addprefix $(OUTPUT),$(LIB_FILE))
0123 PC_FILE         := $(addprefix $(OUTPUT),$(PC_FILE))
0124 
0125 TAGS_PROG := $(if $(shell which etags 2>/dev/null),etags,ctags)
0126 
0127 GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN_SHARED) | \
0128                            cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
0129                            sed 's/\[.*\]//' | \
0130                            awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
0131                            sort -u | wc -l)
0132 VERSIONED_SYM_COUNT = $(shell readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \
0133                               sed 's/\[.*\]//' | \
0134                               awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
0135                               grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
0136 
0137 CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
0138 
0139 all: fixdep
0140         $(Q)$(MAKE) all_cmd
0141 
0142 all_cmd: $(CMD_TARGETS) check
0143 
0144 $(BPF_IN_SHARED): force $(BPF_GENERATED)
0145         @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
0146         (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
0147         echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
0148         @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
0149         (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
0150         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
0151         @(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
0152         (diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
0153         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
0154         $(Q)$(MAKE) $(build)=libbpf OUTPUT=$(SHARED_OBJDIR) CFLAGS="$(CFLAGS) $(SHLIB_FLAGS)"
0155 
0156 $(BPF_IN_STATIC): force $(BPF_GENERATED)
0157         $(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR)
0158 
0159 $(BPF_HELPER_DEFS): $(srctree)/tools/include/uapi/linux/bpf.h
0160         $(QUIET_GEN)$(srctree)/scripts/bpf_doc.py --header \
0161                 --file $(srctree)/tools/include/uapi/linux/bpf.h > $(BPF_HELPER_DEFS)
0162 
0163 $(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
0164 
0165 $(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN_SHARED) $(VERSION_SCRIPT)
0166         $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) \
0167                 --shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
0168                 -Wl,--version-script=$(VERSION_SCRIPT) $< -lelf -lz -o $@
0169         @ln -sf $(@F) $(OUTPUT)libbpf.so
0170         @ln -sf $(@F) $(OUTPUT)libbpf.so.$(LIBBPF_MAJOR_VERSION)
0171 
0172 $(OUTPUT)libbpf.a: $(BPF_IN_STATIC)
0173         $(QUIET_LINK)$(RM) -f $@; $(AR) rcs $@ $^
0174 
0175 $(OUTPUT)libbpf.pc:
0176         $(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
0177                 -e "s|@LIBDIR@|$(libdir_SQ)|" \
0178                 -e "s|@VERSION@|$(LIBBPF_VERSION)|" \
0179                 < libbpf.pc.template > $@
0180 
0181 check: check_abi check_version
0182 
0183 check_abi: $(OUTPUT)libbpf.so $(VERSION_SCRIPT)
0184         @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then  \
0185                 echo "Warning: Num of global symbols in $(BPF_IN_SHARED)"        \
0186                      "($(GLOBAL_SYM_COUNT)) does NOT match with num of"  \
0187                      "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
0188                      "Please make sure all LIBBPF_API symbols are"       \
0189                      "versioned in $(VERSION_SCRIPT)." >&2;              \
0190                 readelf -s --wide $(BPF_IN_SHARED) |                     \
0191                     cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' |   \
0192                     sed 's/\[.*\]//' |                                   \
0193                     awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}'|  \
0194                     sort -u > $(OUTPUT)libbpf_global_syms.tmp;           \
0195                 readelf --dyn-syms --wide $(OUTPUT)libbpf.so |           \
0196                     sed 's/\[.*\]//' |                                   \
0197                     awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}'|  \
0198                     grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 |             \
0199                     sort -u > $(OUTPUT)libbpf_versioned_syms.tmp;        \
0200                 diff -u $(OUTPUT)libbpf_global_syms.tmp                  \
0201                      $(OUTPUT)libbpf_versioned_syms.tmp;                 \
0202                 rm $(OUTPUT)libbpf_global_syms.tmp                       \
0203                    $(OUTPUT)libbpf_versioned_syms.tmp;                   \
0204                 exit 1;                                                  \
0205         fi
0206 
0207 HDR_MAJ_VERSION := $(shell grep -oE '^$(pound)define LIBBPF_MAJOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
0208 HDR_MIN_VERSION := $(shell grep -oE '^$(pound)define LIBBPF_MINOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
0209 
0210 check_version: $(VERSION_SCRIPT) libbpf_version.h
0211         @if [ "$(HDR_MAJ_VERSION)" != "$(LIBBPF_MAJOR_VERSION)" ]; then        \
0212                 echo "Error: libbpf major version mismatch detected: "         \
0213                      "'$(HDR_MAJ_VERSION)' != '$(LIBBPF_MAJOR_VERSION)'" >&2;  \
0214                 exit 1;                                                        \
0215         fi
0216         @if [ "$(HDR_MIN_VERSION)" != "$(LIBBPF_MINOR_VERSION)" ]; then        \
0217                 echo "Error: libbpf minor version mismatch detected: "         \
0218                      "'$(HDR_MIN_VERSION)' != '$(LIBBPF_MINOR_VERSION)'" >&2;  \
0219                 exit 1;                                                        \
0220         fi
0221 
0222 define do_install_mkdir
0223         if [ ! -d '$(DESTDIR_SQ)$1' ]; then             \
0224                 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
0225         fi
0226 endef
0227 
0228 define do_install
0229         if [ ! -d '$(DESTDIR_SQ)$2' ]; then             \
0230                 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
0231         fi;                                             \
0232         $(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
0233 endef
0234 
0235 install_lib: all_cmd
0236         $(call QUIET_INSTALL, $(LIB_TARGET)) \
0237                 $(call do_install_mkdir,$(libdir_SQ)); \
0238                 cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
0239 
0240 SRC_HDRS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h             \
0241             bpf_helpers.h bpf_tracing.h bpf_endian.h bpf_core_read.h         \
0242             skel_internal.h libbpf_version.h usdt.bpf.h
0243 GEN_HDRS := $(BPF_GENERATED)
0244 
0245 INSTALL_PFX := $(DESTDIR)$(prefix)/include/bpf
0246 INSTALL_SRC_HDRS := $(addprefix $(INSTALL_PFX)/, $(SRC_HDRS))
0247 INSTALL_GEN_HDRS := $(addprefix $(INSTALL_PFX)/, $(notdir $(GEN_HDRS)))
0248 
0249 $(INSTALL_SRC_HDRS): $(INSTALL_PFX)/%.h: %.h
0250         $(call QUIET_INSTALL, $@) \
0251                 $(call do_install,$<,$(prefix)/include/bpf,644)
0252 
0253 $(INSTALL_GEN_HDRS): $(INSTALL_PFX)/%.h: $(OUTPUT)%.h
0254         $(call QUIET_INSTALL, $@) \
0255                 $(call do_install,$<,$(prefix)/include/bpf,644)
0256 
0257 install_headers: $(BPF_GENERATED) $(INSTALL_SRC_HDRS) $(INSTALL_GEN_HDRS)
0258 
0259 install_pkgconfig: $(PC_FILE)
0260         $(call QUIET_INSTALL, $(PC_FILE)) \
0261                 $(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
0262 
0263 install: install_lib install_pkgconfig install_headers
0264 
0265 clean:
0266         $(call QUIET_CLEAN, libbpf) $(RM) -rf $(CMD_TARGETS)                 \
0267                 *~ .*.d .*.cmd LIBBPF-CFLAGS $(BPF_GENERATED)                \
0268                 $(SHARED_OBJDIR) $(STATIC_OBJDIR)                            \
0269                 $(addprefix $(OUTPUT),                                       \
0270                             *.o *.a *.so *.so.$(LIBBPF_MAJOR_VERSION) *.pc)
0271 
0272 PHONY += force cscope tags check check_abi check_version
0273 force:
0274 
0275 cscope:
0276         ls *.c *.h > cscope.files
0277         cscope -b -q -I $(srctree)/include -f cscope.out 
0278 
0279 tags:
0280         $(RM) -f TAGS tags
0281         ls *.c *.h | xargs $(TAGS_PROG) -a
0282 
0283 # Declare the contents of the .PHONY variable as phony.  We keep that
0284 # information in a variable so we can use it in if_changed and friends.
0285 .PHONY: $(PHONY)
0286 
0287 # Delete partially updated (corrupted) files on error
0288 .DELETE_ON_ERROR: