Makefile 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Most of this file is copied from tools/lib/traceevent/Makefile
  3. BPF_VERSION = 0
  4. BPF_PATCHLEVEL = 0
  5. BPF_EXTRAVERSION = 1
  6. MAKEFLAGS += --no-print-directory
  7. ifeq ($(srctree),)
  8. srctree := $(patsubst %/,%,$(dir $(CURDIR)))
  9. srctree := $(patsubst %/,%,$(dir $(srctree)))
  10. srctree := $(patsubst %/,%,$(dir $(srctree)))
  11. #$(info Determined 'srctree' to be $(srctree))
  12. endif
  13. # Makefiles suck: This macro sets a default value of $(2) for the
  14. # variable named by $(1), unless the variable has been set by
  15. # environment or command line. This is necessary for CC and AR
  16. # because make sets default values, so the simpler ?= approach
  17. # won't work as expected.
  18. define allow-override
  19. $(if $(or $(findstring environment,$(origin $(1))),\
  20. $(findstring command line,$(origin $(1)))),,\
  21. $(eval $(1) = $(2)))
  22. endef
  23. # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
  24. $(call allow-override,CC,$(CROSS_COMPILE)gcc)
  25. $(call allow-override,AR,$(CROSS_COMPILE)ar)
  26. INSTALL = install
  27. # Use DESTDIR for installing into a different root directory.
  28. # This is useful for building a package. The program will be
  29. # installed in this directory as if it was the root directory.
  30. # Then the build tool can move it later.
  31. DESTDIR ?=
  32. DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
  33. include $(srctree)/tools/scripts/Makefile.arch
  34. ifeq ($(LP64), 1)
  35. libdir_relative = lib64
  36. else
  37. libdir_relative = lib
  38. endif
  39. prefix ?= /usr/local
  40. libdir = $(prefix)/$(libdir_relative)
  41. man_dir = $(prefix)/share/man
  42. man_dir_SQ = '$(subst ','\'',$(man_dir))'
  43. export man_dir man_dir_SQ INSTALL
  44. export DESTDIR DESTDIR_SQ
  45. include ../../scripts/Makefile.include
  46. # copy a bit from Linux kbuild
  47. ifeq ("$(origin V)", "command line")
  48. VERBOSE = $(V)
  49. endif
  50. ifndef VERBOSE
  51. VERBOSE = 0
  52. endif
  53. FEATURE_USER = .libbpf
  54. FEATURE_TESTS = libelf libelf-mmap bpf reallocarray
  55. FEATURE_DISPLAY = libelf bpf
  56. INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi -I$(srctree)/tools/perf
  57. FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
  58. check_feat := 1
  59. NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
  60. ifdef MAKECMDGOALS
  61. ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
  62. check_feat := 0
  63. endif
  64. endif
  65. ifeq ($(check_feat),1)
  66. ifeq ($(FEATURES_DUMP),)
  67. include $(srctree)/tools/build/Makefile.feature
  68. else
  69. include $(FEATURES_DUMP)
  70. endif
  71. endif
  72. export prefix libdir src obj
  73. # Shell quotes
  74. libdir_SQ = $(subst ','\'',$(libdir))
  75. libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
  76. LIB_FILE = libbpf.a libbpf.so
  77. VERSION = $(BPF_VERSION)
  78. PATCHLEVEL = $(BPF_PATCHLEVEL)
  79. EXTRAVERSION = $(BPF_EXTRAVERSION)
  80. OBJ = $@
  81. N =
  82. LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
  83. # Set compile option CFLAGS
  84. ifdef EXTRA_CFLAGS
  85. CFLAGS := $(EXTRA_CFLAGS)
  86. else
  87. CFLAGS := -g -Wall
  88. endif
  89. ifeq ($(feature-libelf-mmap), 1)
  90. override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
  91. endif
  92. ifeq ($(feature-reallocarray), 0)
  93. override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
  94. endif
  95. # Append required CFLAGS
  96. override CFLAGS += $(EXTRA_WARNINGS)
  97. override CFLAGS += -Werror -Wall
  98. override CFLAGS += -fPIC
  99. override CFLAGS += $(INCLUDES)
  100. ifeq ($(VERBOSE),1)
  101. Q =
  102. else
  103. Q = @
  104. endif
  105. # Disable command line variables (CFLAGS) override from top
  106. # level Makefile (perf), otherwise build Makefile will get
  107. # the same command line setup.
  108. MAKEOVERRIDES=
  109. all:
  110. export srctree OUTPUT CC LD CFLAGS V
  111. include $(srctree)/tools/build/Makefile.include
  112. BPF_IN := $(OUTPUT)libbpf-in.o
  113. LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
  114. CMD_TARGETS = $(LIB_FILE)
  115. TARGETS = $(CMD_TARGETS)
  116. all: fixdep
  117. $(Q)$(MAKE) all_cmd
  118. all_cmd: $(CMD_TARGETS)
  119. $(BPF_IN): force elfdep bpfdep
  120. @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
  121. (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
  122. echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
  123. @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
  124. (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
  125. 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
  126. @(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \
  127. (diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \
  128. echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true
  129. @(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \
  130. (diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \
  131. 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
  132. $(Q)$(MAKE) $(build)=libbpf
  133. $(OUTPUT)libbpf.so: $(BPF_IN)
  134. $(QUIET_LINK)$(CC) --shared $^ -o $@
  135. $(OUTPUT)libbpf.a: $(BPF_IN)
  136. $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
  137. define do_install
  138. if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
  139. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
  140. fi; \
  141. $(INSTALL) $(if $3,-m $3,) $1 '$(DESTDIR_SQ)$2'
  142. endef
  143. install_lib: all_cmd
  144. $(call QUIET_INSTALL, $(LIB_FILE)) \
  145. $(call do_install,$(LIB_FILE),$(libdir_SQ))
  146. install_headers:
  147. $(call QUIET_INSTALL, headers) \
  148. $(call do_install,bpf.h,$(prefix)/include/bpf,644); \
  149. $(call do_install,libbpf.h,$(prefix)/include/bpf,644);
  150. $(call do_install,btf.h,$(prefix)/include/bpf,644);
  151. install: install_lib
  152. ### Cleaning rules
  153. config-clean:
  154. $(call QUIET_CLEAN, config)
  155. $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
  156. clean:
  157. $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so .*.d .*.cmd \
  158. $(RM) LIBBPF-CFLAGS
  159. $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
  160. PHONY += force elfdep bpfdep
  161. force:
  162. elfdep:
  163. @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
  164. bpfdep:
  165. @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
  166. # Declare the contents of the .PHONY variable as phony. We keep that
  167. # information in a variable so we can use it in if_changed and friends.
  168. .PHONY: $(PHONY)