Makefile 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. # Most of this file is copied from tools/lib/traceevent/Makefile
  3. RM ?= rm
  4. srctree = $(abs_srctree)
  5. VERSION_SCRIPT := libbpf.map
  6. LIBBPF_VERSION := $(shell \
  7. grep -oE '^LIBBPF_([0-9.]+)' $(VERSION_SCRIPT) | \
  8. sort -rV | head -n1 | cut -d'_' -f2)
  9. LIBBPF_MAJOR_VERSION := $(word 1,$(subst ., ,$(LIBBPF_VERSION)))
  10. LIBBPF_MINOR_VERSION := $(word 2,$(subst ., ,$(LIBBPF_VERSION)))
  11. MAKEFLAGS += --no-print-directory
  12. # This will work when bpf is built in tools env. where srctree
  13. # isn't set and when invoked from selftests build, where srctree
  14. # is a ".". building_out_of_srctree is undefined for in srctree
  15. # builds
  16. ifndef building_out_of_srctree
  17. srctree := $(patsubst %/,%,$(dir $(CURDIR)))
  18. srctree := $(patsubst %/,%,$(dir $(srctree)))
  19. srctree := $(patsubst %/,%,$(dir $(srctree)))
  20. #$(info Determined 'srctree' to be $(srctree))
  21. endif
  22. INSTALL = install
  23. # Use DESTDIR for installing into a different root directory.
  24. # This is useful for building a package. The program will be
  25. # installed in this directory as if it was the root directory.
  26. # Then the build tool can move it later.
  27. DESTDIR ?=
  28. DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
  29. include $(srctree)/tools/scripts/Makefile.arch
  30. ifeq ($(LP64), 1)
  31. libdir_relative = lib64
  32. else
  33. libdir_relative = lib
  34. endif
  35. prefix ?= /usr/local
  36. libdir = $(prefix)/$(libdir_relative)
  37. man_dir = $(prefix)/share/man
  38. man_dir_SQ = '$(subst ','\'',$(man_dir))'
  39. export man_dir man_dir_SQ INSTALL
  40. export DESTDIR DESTDIR_SQ
  41. include $(srctree)/tools/scripts/Makefile.include
  42. # copy a bit from Linux kbuild
  43. ifeq ("$(origin V)", "command line")
  44. VERBOSE = $(V)
  45. endif
  46. ifndef VERBOSE
  47. VERBOSE = 0
  48. endif
  49. INCLUDES = -I$(or $(OUTPUT),.) \
  50. -I$(srctree)/tools/include -I$(srctree)/tools/include/uapi
  51. export prefix libdir src obj
  52. # Shell quotes
  53. libdir_SQ = $(subst ','\'',$(libdir))
  54. libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
  55. OBJ = $@
  56. N =
  57. LIB_TARGET = libbpf.a libbpf.so.$(LIBBPF_VERSION)
  58. LIB_FILE = libbpf.a libbpf.so*
  59. PC_FILE = libbpf.pc
  60. # Set compile option CFLAGS
  61. ifdef EXTRA_CFLAGS
  62. CFLAGS := $(EXTRA_CFLAGS)
  63. else
  64. CFLAGS := -g -O2
  65. endif
  66. # Append required CFLAGS
  67. override CFLAGS += -std=gnu89
  68. override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
  69. override CFLAGS += -Werror -Wall
  70. override CFLAGS += $(INCLUDES)
  71. override CFLAGS += -fvisibility=hidden
  72. override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
  73. override CFLAGS += $(CLANG_CROSS_FLAGS)
  74. # flags specific for shared library
  75. SHLIB_FLAGS := -DSHARED -fPIC
  76. ifeq ($(VERBOSE),1)
  77. Q =
  78. else
  79. Q = @
  80. endif
  81. # Disable command line variables (CFLAGS) override from top
  82. # level Makefile (perf), otherwise build Makefile will get
  83. # the same command line setup.
  84. MAKEOVERRIDES=
  85. all:
  86. export srctree OUTPUT CC LD CFLAGS V
  87. include $(srctree)/tools/build/Makefile.include
  88. SHARED_OBJDIR := $(OUTPUT)sharedobjs/
  89. STATIC_OBJDIR := $(OUTPUT)staticobjs/
  90. BPF_IN_SHARED := $(SHARED_OBJDIR)libbpf-in.o
  91. BPF_IN_STATIC := $(STATIC_OBJDIR)libbpf-in.o
  92. BPF_HELPER_DEFS := $(OUTPUT)bpf_helper_defs.h
  93. BPF_GENERATED := $(BPF_HELPER_DEFS)
  94. LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET))
  95. LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
  96. PC_FILE := $(addprefix $(OUTPUT),$(PC_FILE))
  97. TAGS_PROG := $(if $(shell which etags 2>/dev/null),etags,ctags)
  98. GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN_SHARED) | \
  99. cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
  100. sed 's/\[.*\]//' | \
  101. awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
  102. sort -u | wc -l)
  103. VERSIONED_SYM_COUNT = $(shell readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \
  104. sed 's/\[.*\]//' | \
  105. awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
  106. grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
  107. CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
  108. all: fixdep
  109. $(Q)$(MAKE) all_cmd
  110. all_cmd: $(CMD_TARGETS) check
  111. $(BPF_IN_SHARED): force $(BPF_GENERATED)
  112. @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
  113. (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
  114. echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
  115. @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
  116. (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
  117. 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
  118. @(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
  119. (diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
  120. 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
  121. $(Q)$(MAKE) $(build)=libbpf OUTPUT=$(SHARED_OBJDIR) CFLAGS="$(CFLAGS) $(SHLIB_FLAGS)"
  122. $(BPF_IN_STATIC): force $(BPF_GENERATED)
  123. $(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR)
  124. $(BPF_HELPER_DEFS): $(srctree)/tools/include/uapi/linux/bpf.h
  125. $(QUIET_GEN)$(srctree)/scripts/bpf_doc.py --header \
  126. --file $(srctree)/tools/include/uapi/linux/bpf.h > $(BPF_HELPER_DEFS)
  127. $(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
  128. $(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN_SHARED) $(VERSION_SCRIPT)
  129. $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) \
  130. --shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
  131. -Wl,--version-script=$(VERSION_SCRIPT) $< -lelf -lz -o $@
  132. @ln -sf $(@F) $(OUTPUT)libbpf.so
  133. @ln -sf $(@F) $(OUTPUT)libbpf.so.$(LIBBPF_MAJOR_VERSION)
  134. $(OUTPUT)libbpf.a: $(BPF_IN_STATIC)
  135. $(QUIET_LINK)$(RM) -f $@; $(AR) rcs $@ $^
  136. $(OUTPUT)libbpf.pc:
  137. $(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
  138. -e "s|@LIBDIR@|$(libdir_SQ)|" \
  139. -e "s|@VERSION@|$(LIBBPF_VERSION)|" \
  140. < libbpf.pc.template > $@
  141. check: check_abi check_version
  142. check_abi: $(OUTPUT)libbpf.so $(VERSION_SCRIPT)
  143. @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then \
  144. echo "Warning: Num of global symbols in $(BPF_IN_SHARED)" \
  145. "($(GLOBAL_SYM_COUNT)) does NOT match with num of" \
  146. "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
  147. "Please make sure all LIBBPF_API symbols are" \
  148. "versioned in $(VERSION_SCRIPT)." >&2; \
  149. readelf -s --wide $(BPF_IN_SHARED) | \
  150. cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
  151. sed 's/\[.*\]//' | \
  152. awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}'| \
  153. sort -u > $(OUTPUT)libbpf_global_syms.tmp; \
  154. readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \
  155. sed 's/\[.*\]//' | \
  156. awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}'| \
  157. grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | \
  158. sort -u > $(OUTPUT)libbpf_versioned_syms.tmp; \
  159. diff -u $(OUTPUT)libbpf_global_syms.tmp \
  160. $(OUTPUT)libbpf_versioned_syms.tmp; \
  161. rm $(OUTPUT)libbpf_global_syms.tmp \
  162. $(OUTPUT)libbpf_versioned_syms.tmp; \
  163. exit 1; \
  164. fi
  165. HDR_MAJ_VERSION := $(shell grep -oE '^$(pound)define LIBBPF_MAJOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
  166. HDR_MIN_VERSION := $(shell grep -oE '^$(pound)define LIBBPF_MINOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
  167. check_version: $(VERSION_SCRIPT) libbpf_version.h
  168. @if [ "$(HDR_MAJ_VERSION)" != "$(LIBBPF_MAJOR_VERSION)" ]; then \
  169. echo "Error: libbpf major version mismatch detected: " \
  170. "'$(HDR_MAJ_VERSION)' != '$(LIBBPF_MAJOR_VERSION)'" >&2; \
  171. exit 1; \
  172. fi
  173. @if [ "$(HDR_MIN_VERSION)" != "$(LIBBPF_MINOR_VERSION)" ]; then \
  174. echo "Error: libbpf minor version mismatch detected: " \
  175. "'$(HDR_MIN_VERSION)' != '$(LIBBPF_MINOR_VERSION)'" >&2; \
  176. exit 1; \
  177. fi
  178. define do_install_mkdir
  179. if [ ! -d '$(DESTDIR_SQ)$1' ]; then \
  180. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
  181. fi
  182. endef
  183. define do_install
  184. if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
  185. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
  186. fi; \
  187. $(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
  188. endef
  189. install_lib: all_cmd
  190. $(call QUIET_INSTALL, $(LIB_TARGET)) \
  191. $(call do_install_mkdir,$(libdir_SQ)); \
  192. cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
  193. SRC_HDRS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h \
  194. bpf_helpers.h bpf_tracing.h bpf_endian.h bpf_core_read.h \
  195. skel_internal.h libbpf_version.h usdt.bpf.h
  196. GEN_HDRS := $(BPF_GENERATED)
  197. INSTALL_PFX := $(DESTDIR)$(prefix)/include/bpf
  198. INSTALL_SRC_HDRS := $(addprefix $(INSTALL_PFX)/, $(SRC_HDRS))
  199. INSTALL_GEN_HDRS := $(addprefix $(INSTALL_PFX)/, $(notdir $(GEN_HDRS)))
  200. $(INSTALL_SRC_HDRS): $(INSTALL_PFX)/%.h: %.h
  201. $(call QUIET_INSTALL, $@) \
  202. $(call do_install,$<,$(prefix)/include/bpf,644)
  203. $(INSTALL_GEN_HDRS): $(INSTALL_PFX)/%.h: $(OUTPUT)%.h
  204. $(call QUIET_INSTALL, $@) \
  205. $(call do_install,$<,$(prefix)/include/bpf,644)
  206. install_headers: $(BPF_GENERATED) $(INSTALL_SRC_HDRS) $(INSTALL_GEN_HDRS)
  207. install_pkgconfig: $(PC_FILE)
  208. $(call QUIET_INSTALL, $(PC_FILE)) \
  209. $(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
  210. install: install_lib install_pkgconfig install_headers
  211. clean:
  212. $(call QUIET_CLEAN, libbpf) $(RM) -rf $(CMD_TARGETS) \
  213. *~ .*.d .*.cmd LIBBPF-CFLAGS $(BPF_GENERATED) \
  214. $(SHARED_OBJDIR) $(STATIC_OBJDIR) \
  215. $(addprefix $(OUTPUT), \
  216. *.o *.a *.so *.so.$(LIBBPF_MAJOR_VERSION) *.pc)
  217. PHONY += force cscope tags check check_abi check_version
  218. force:
  219. cscope:
  220. ls *.c *.h > cscope.files
  221. cscope -b -q -I $(srctree)/include -f cscope.out
  222. tags:
  223. $(RM) -f TAGS tags
  224. ls *.c *.h | xargs $(TAGS_PROG) -a
  225. # Declare the contents of the .PHONY variable as phony. We keep that
  226. # information in a variable so we can use it in if_changed and friends.
  227. .PHONY: $(PHONY)
  228. # Delete partially updated (corrupted) files on error
  229. .DELETE_ON_ERROR: