Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 := $(realpath $(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. -I$(srctree)/tools/arch/$(SRCARCH)/include
  52. export prefix libdir src obj
  53. # Shell quotes
  54. libdir_SQ = $(subst ','\'',$(libdir))
  55. libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
  56. OBJ = $@
  57. N =
  58. LIB_TARGET = libbpf.a libbpf.so.$(LIBBPF_VERSION)
  59. LIB_FILE = libbpf.a libbpf.so*
  60. PC_FILE = libbpf.pc
  61. # Set compile option CFLAGS
  62. ifdef EXTRA_CFLAGS
  63. CFLAGS := $(EXTRA_CFLAGS)
  64. else
  65. CFLAGS := -g -O2
  66. endif
  67. # Append required CFLAGS
  68. override CFLAGS += -std=gnu89
  69. override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
  70. override CFLAGS += -Werror -Wall
  71. override CFLAGS += $(INCLUDES)
  72. override CFLAGS += -fvisibility=hidden
  73. override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
  74. override CFLAGS += $(CLANG_CROSS_FLAGS)
  75. # flags specific for shared library
  76. SHLIB_FLAGS := -DSHARED -fPIC
  77. ifeq ($(VERBOSE),1)
  78. Q =
  79. else
  80. Q = @
  81. endif
  82. # Disable command line variables (CFLAGS) override from top
  83. # level Makefile (perf), otherwise build Makefile will get
  84. # the same command line setup.
  85. MAKEOVERRIDES=
  86. all:
  87. OUTPUT ?= ./
  88. OUTPUT := $(abspath $(OUTPUT))/
  89. export srctree OUTPUT CC LD CFLAGS V
  90. include $(srctree)/tools/build/Makefile.include
  91. SHARED_OBJDIR := $(OUTPUT)sharedobjs/
  92. STATIC_OBJDIR := $(OUTPUT)staticobjs/
  93. BPF_IN_SHARED := $(SHARED_OBJDIR)libbpf-in.o
  94. BPF_IN_STATIC := $(STATIC_OBJDIR)libbpf-in.o
  95. BPF_HELPER_DEFS := $(OUTPUT)bpf_helper_defs.h
  96. BPF_GENERATED := $(BPF_HELPER_DEFS)
  97. LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET))
  98. LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
  99. PC_FILE := $(addprefix $(OUTPUT),$(PC_FILE))
  100. TAGS_PROG := $(if $(shell which etags 2>/dev/null),etags,ctags)
  101. GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN_SHARED) | \
  102. cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
  103. sed 's/\[.*\]//' | \
  104. awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
  105. sort -u | wc -l)
  106. VERSIONED_SYM_COUNT = $(shell readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \
  107. sed 's/\[.*\]//' | \
  108. awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \
  109. grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
  110. CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
  111. all: fixdep
  112. $(Q)$(MAKE) all_cmd
  113. all_cmd: $(CMD_TARGETS) check
  114. $(SHARED_OBJDIR) $(STATIC_OBJDIR):
  115. $(Q)mkdir -p $@
  116. $(BPF_IN_SHARED): force $(BPF_GENERATED) | $(SHARED_OBJDIR)
  117. @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
  118. (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
  119. echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
  120. @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
  121. (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
  122. 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
  123. @(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \
  124. (diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \
  125. 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
  126. $(SILENT_MAKE) -C $(srctree)/tools/build CFLAGS= LDFLAGS= OUTPUT=$(SHARED_OBJDIR) $(SHARED_OBJDIR)fixdep
  127. $(Q)$(MAKE) $(build)=libbpf OUTPUT=$(SHARED_OBJDIR) CFLAGS="$(CFLAGS) $(SHLIB_FLAGS)"
  128. $(BPF_IN_STATIC): force $(BPF_GENERATED) | $(STATIC_OBJDIR)
  129. $(SILENT_MAKE) -C $(srctree)/tools/build CFLAGS= LDFLAGS= OUTPUT=$(STATIC_OBJDIR) $(STATIC_OBJDIR)fixdep
  130. $(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR)
  131. $(BPF_HELPER_DEFS): $(srctree)/tools/include/uapi/linux/bpf.h
  132. $(QUIET_GEN)$(srctree)/scripts/bpf_doc.py --header \
  133. --file $(srctree)/tools/include/uapi/linux/bpf.h > $(BPF_HELPER_DEFS)
  134. $(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
  135. $(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN_SHARED) $(VERSION_SCRIPT)
  136. $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) \
  137. --shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
  138. -Wl,--version-script=$(VERSION_SCRIPT) $< -lelf -lz -o $@
  139. @ln -sf $(@F) $(OUTPUT)libbpf.so
  140. @ln -sf $(@F) $(OUTPUT)libbpf.so.$(LIBBPF_MAJOR_VERSION)
  141. $(OUTPUT)libbpf.a: $(BPF_IN_STATIC)
  142. $(QUIET_LINK)$(RM) -f $@; $(AR) rcs $@ $^
  143. $(OUTPUT)libbpf.pc:
  144. $(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
  145. -e "s|@LIBDIR@|$(libdir_SQ)|" \
  146. -e "s|@VERSION@|$(LIBBPF_VERSION)|" \
  147. < libbpf.pc.template > $@
  148. check: check_abi check_version
  149. check_abi: $(OUTPUT)libbpf.so $(VERSION_SCRIPT)
  150. @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then \
  151. echo "Warning: Num of global symbols in $(BPF_IN_SHARED)" \
  152. "($(GLOBAL_SYM_COUNT)) does NOT match with num of" \
  153. "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \
  154. "Please make sure all LIBBPF_API symbols are" \
  155. "versioned in $(VERSION_SCRIPT)." >&2; \
  156. readelf -s --wide $(BPF_IN_SHARED) | \
  157. cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \
  158. sed 's/\[.*\]//' | \
  159. awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}'| \
  160. sort -u > $(OUTPUT)libbpf_global_syms.tmp; \
  161. readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \
  162. sed 's/\[.*\]//' | \
  163. awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}'| \
  164. grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | \
  165. sort -u > $(OUTPUT)libbpf_versioned_syms.tmp; \
  166. diff -u $(OUTPUT)libbpf_global_syms.tmp \
  167. $(OUTPUT)libbpf_versioned_syms.tmp; \
  168. rm $(OUTPUT)libbpf_global_syms.tmp \
  169. $(OUTPUT)libbpf_versioned_syms.tmp; \
  170. exit 1; \
  171. fi
  172. HDR_MAJ_VERSION := $(shell grep -oE '^$(pound)define LIBBPF_MAJOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
  173. HDR_MIN_VERSION := $(shell grep -oE '^$(pound)define LIBBPF_MINOR_VERSION ([0-9]+)$$' libbpf_version.h | cut -d' ' -f3)
  174. check_version: $(VERSION_SCRIPT) libbpf_version.h
  175. @if [ "$(HDR_MAJ_VERSION)" != "$(LIBBPF_MAJOR_VERSION)" ]; then \
  176. echo "Error: libbpf major version mismatch detected: " \
  177. "'$(HDR_MAJ_VERSION)' != '$(LIBBPF_MAJOR_VERSION)'" >&2; \
  178. exit 1; \
  179. fi
  180. @if [ "$(HDR_MIN_VERSION)" != "$(LIBBPF_MINOR_VERSION)" ]; then \
  181. echo "Error: libbpf minor version mismatch detected: " \
  182. "'$(HDR_MIN_VERSION)' != '$(LIBBPF_MINOR_VERSION)'" >&2; \
  183. exit 1; \
  184. fi
  185. define do_install_mkdir
  186. if [ ! -d '$(DESTDIR_SQ)$1' ]; then \
  187. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
  188. fi
  189. endef
  190. define do_install
  191. if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
  192. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
  193. fi; \
  194. $(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
  195. endef
  196. install_lib: all_cmd
  197. $(call QUIET_INSTALL, $(LIB_TARGET)) \
  198. $(call do_install_mkdir,$(libdir_SQ)); \
  199. cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
  200. SRC_HDRS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h \
  201. bpf_helpers.h bpf_tracing.h bpf_endian.h bpf_core_read.h \
  202. skel_internal.h libbpf_version.h usdt.bpf.h
  203. GEN_HDRS := $(BPF_GENERATED)
  204. INSTALL_PFX := $(DESTDIR)$(prefix)/include/bpf
  205. INSTALL_SRC_HDRS := $(addprefix $(INSTALL_PFX)/, $(SRC_HDRS))
  206. INSTALL_GEN_HDRS := $(addprefix $(INSTALL_PFX)/, $(notdir $(GEN_HDRS)))
  207. $(INSTALL_SRC_HDRS): $(INSTALL_PFX)/%.h: %.h
  208. $(call QUIET_INSTALL, $@) \
  209. $(call do_install,$<,$(prefix)/include/bpf,644)
  210. $(INSTALL_GEN_HDRS): $(INSTALL_PFX)/%.h: $(OUTPUT)%.h
  211. $(call QUIET_INSTALL, $@) \
  212. $(call do_install,$<,$(prefix)/include/bpf,644)
  213. install_headers: $(BPF_GENERATED) $(INSTALL_SRC_HDRS) $(INSTALL_GEN_HDRS)
  214. $(call QUIET_INSTALL, libbpf_headers)
  215. install_pkgconfig: $(PC_FILE)
  216. $(call QUIET_INSTALL, $(PC_FILE)) \
  217. $(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
  218. install: install_lib install_pkgconfig install_headers
  219. clean: fixdep-clean
  220. $(call QUIET_CLEAN, libbpf) $(RM) -rf $(CMD_TARGETS) \
  221. *~ .*.d .*.cmd LIBBPF-CFLAGS $(BPF_GENERATED) \
  222. $(SHARED_OBJDIR) $(STATIC_OBJDIR) \
  223. $(addprefix $(OUTPUT), \
  224. *.o *.a *.so *.so.$(LIBBPF_MAJOR_VERSION) *.pc)
  225. PHONY += force cscope tags check check_abi check_version
  226. force:
  227. cscope:
  228. ls *.c *.h > cscope.files
  229. cscope -b -q -I $(srctree)/include -f cscope.out
  230. tags:
  231. $(RM) -f TAGS tags
  232. ls *.c *.h | xargs $(TAGS_PROG) -a
  233. # Declare the contents of the .PHONY variable as phony. We keep that
  234. # information in a variable so we can use it in if_changed and friends.
  235. .PHONY: $(PHONY)
  236. # Delete partially updated (corrupted) files on error
  237. .DELETE_ON_ERROR:
  238. help:
  239. @echo 'libbpf common targets:'
  240. @echo ' HINT: use "V=1" to enable verbose build'
  241. @echo ' all - build libraries and pkgconfig'
  242. @echo ' clean - remove all generated files'
  243. @echo ' check - check ABI and version info'
  244. @echo ''
  245. @echo 'libbpf install targets:'
  246. @echo ' HINT: use "prefix"(defaults to "/usr/local") or "DESTDIR" (defaults to "/")'
  247. @echo ' to adjust target destination, e.g. "make prefix=/usr/local install"'
  248. @echo ' install - build and install all headers, libraries and pkgconfig'
  249. @echo ' install_headers - install only headers to include/bpf'
  250. @echo ''
  251. @echo 'libbpf make targets:'
  252. @echo ' tags - use ctags to make tag information for source code browsing'
  253. @echo ' cscope - use cscope to make interactive source code browsing database'