Makefile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Most of this file is copied from tools/lib/traceevent/Makefile
  2. BPF_VERSION = 0
  3. BPF_PATCHLEVEL = 0
  4. BPF_EXTRAVERSION = 1
  5. MAKEFLAGS += --no-print-directory
  6. ifeq ($(srctree),)
  7. srctree := $(patsubst %/,%,$(dir $(shell pwd)))
  8. srctree := $(patsubst %/,%,$(dir $(srctree)))
  9. srctree := $(patsubst %/,%,$(dir $(srctree)))
  10. #$(info Determined 'srctree' to be $(srctree))
  11. endif
  12. # Makefiles suck: This macro sets a default value of $(2) for the
  13. # variable named by $(1), unless the variable has been set by
  14. # environment or command line. This is necessary for CC and AR
  15. # because make sets default values, so the simpler ?= approach
  16. # won't work as expected.
  17. define allow-override
  18. $(if $(or $(findstring environment,$(origin $(1))),\
  19. $(findstring command line,$(origin $(1)))),,\
  20. $(eval $(1) = $(2)))
  21. endef
  22. # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
  23. $(call allow-override,CC,$(CROSS_COMPILE)gcc)
  24. $(call allow-override,AR,$(CROSS_COMPILE)ar)
  25. INSTALL = install
  26. # Use DESTDIR for installing into a different root directory.
  27. # This is useful for building a package. The program will be
  28. # installed in this directory as if it was the root directory.
  29. # Then the build tool can move it later.
  30. DESTDIR ?=
  31. DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
  32. include $(srctree)/tools/scripts/Makefile.arch
  33. ifeq ($(LP64), 1)
  34. libdir_relative = lib64
  35. else
  36. libdir_relative = lib
  37. endif
  38. prefix ?= /usr/local
  39. libdir = $(prefix)/$(libdir_relative)
  40. man_dir = $(prefix)/share/man
  41. man_dir_SQ = '$(subst ','\'',$(man_dir))'
  42. export man_dir man_dir_SQ INSTALL
  43. export DESTDIR DESTDIR_SQ
  44. include ../../scripts/Makefile.include
  45. # copy a bit from Linux kbuild
  46. ifeq ("$(origin V)", "command line")
  47. VERBOSE = $(V)
  48. endif
  49. ifndef VERBOSE
  50. VERBOSE = 0
  51. endif
  52. FEATURE_USER = .libbpf
  53. FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf
  54. FEATURE_DISPLAY = libelf bpf
  55. INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi
  56. FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
  57. check_feat := 1
  58. NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
  59. ifdef MAKECMDGOALS
  60. ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
  61. check_feat := 0
  62. endif
  63. endif
  64. ifeq ($(check_feat),1)
  65. ifeq ($(FEATURES_DUMP),)
  66. include $(srctree)/tools/build/Makefile.feature
  67. else
  68. include $(FEATURES_DUMP)
  69. endif
  70. endif
  71. export prefix libdir src obj
  72. # Shell quotes
  73. libdir_SQ = $(subst ','\'',$(libdir))
  74. libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
  75. plugin_dir_SQ = $(subst ','\'',$(plugin_dir))
  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-libelf-getphdrnum), 1)
  93. override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT
  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) overide 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 $(VERSION_FILES) all_cmd
  117. all_cmd: $(CMD_TARGETS)
  118. $(BPF_IN): force elfdep bpfdep
  119. $(Q)$(MAKE) $(build)=libbpf
  120. $(OUTPUT)libbpf.so: $(BPF_IN)
  121. $(QUIET_LINK)$(CC) --shared $^ -o $@
  122. $(OUTPUT)libbpf.a: $(BPF_IN)
  123. $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
  124. define update_dir
  125. (echo $1 > $@.tmp; \
  126. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  127. rm -f $@.tmp; \
  128. else \
  129. echo ' UPDATE $@'; \
  130. mv -f $@.tmp $@; \
  131. fi);
  132. endef
  133. define do_install
  134. if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
  135. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
  136. fi; \
  137. $(INSTALL) $1 '$(DESTDIR_SQ)$2'
  138. endef
  139. install_lib: all_cmd
  140. $(call QUIET_INSTALL, $(LIB_FILE)) \
  141. $(call do_install,$(LIB_FILE),$(libdir_SQ))
  142. install: install_lib
  143. ### Cleaning rules
  144. config-clean:
  145. $(call QUIET_CLEAN, config)
  146. $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
  147. clean:
  148. $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd \
  149. $(RM) LIBBPF-CFLAGS
  150. $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
  151. PHONY += force elfdep bpfdep
  152. force:
  153. elfdep:
  154. @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi
  155. bpfdep:
  156. @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi
  157. # Declare the contents of the .PHONY variable as phony. We keep that
  158. # information in a variable so we can use it in if_changed and friends.
  159. .PHONY: $(PHONY)