Makefile 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. # Makefiles suck: This macro sets a default value of $(2) for the
  7. # variable named by $(1), unless the variable has been set by
  8. # environment or command line. This is necessary for CC and AR
  9. # because make sets default values, so the simpler ?= approach
  10. # won't work as expected.
  11. define allow-override
  12. $(if $(or $(findstring environment,$(origin $(1))),\
  13. $(findstring command line,$(origin $(1)))),,\
  14. $(eval $(1) = $(2)))
  15. endef
  16. # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
  17. $(call allow-override,CC,$(CROSS_COMPILE)gcc)
  18. $(call allow-override,AR,$(CROSS_COMPILE)ar)
  19. INSTALL = install
  20. # Use DESTDIR for installing into a different root directory.
  21. # This is useful for building a package. The program will be
  22. # installed in this directory as if it was the root directory.
  23. # Then the build tool can move it later.
  24. DESTDIR ?=
  25. DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
  26. LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
  27. ifeq ($(LP64), 1)
  28. libdir_relative = lib64
  29. else
  30. libdir_relative = lib
  31. endif
  32. prefix ?= /usr/local
  33. libdir = $(prefix)/$(libdir_relative)
  34. man_dir = $(prefix)/share/man
  35. man_dir_SQ = '$(subst ','\'',$(man_dir))'
  36. export man_dir man_dir_SQ INSTALL
  37. export DESTDIR DESTDIR_SQ
  38. include ../../scripts/Makefile.include
  39. # copy a bit from Linux kbuild
  40. ifeq ("$(origin V)", "command line")
  41. VERBOSE = $(V)
  42. endif
  43. ifndef VERBOSE
  44. VERBOSE = 0
  45. endif
  46. ifeq ($(srctree),)
  47. srctree := $(patsubst %/,%,$(dir $(shell pwd)))
  48. srctree := $(patsubst %/,%,$(dir $(srctree)))
  49. srctree := $(patsubst %/,%,$(dir $(srctree)))
  50. #$(info Determined 'srctree' to be $(srctree))
  51. endif
  52. FEATURE_DISPLAY = libelf libelf-getphdrnum libelf-mmap bpf
  53. FEATURE_TESTS = libelf bpf
  54. INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi
  55. FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
  56. include $(srctree)/tools/build/Makefile.feature
  57. export prefix libdir src obj
  58. # Shell quotes
  59. libdir_SQ = $(subst ','\'',$(libdir))
  60. libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
  61. plugin_dir_SQ = $(subst ','\'',$(plugin_dir))
  62. LIB_FILE = libbpf.a libbpf.so
  63. VERSION = $(BPF_VERSION)
  64. PATCHLEVEL = $(BPF_PATCHLEVEL)
  65. EXTRAVERSION = $(BPF_EXTRAVERSION)
  66. OBJ = $@
  67. N =
  68. LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
  69. # Set compile option CFLAGS
  70. ifdef EXTRA_CFLAGS
  71. CFLAGS := $(EXTRA_CFLAGS)
  72. else
  73. CFLAGS := -g -Wall
  74. endif
  75. ifeq ($(feature-libelf-mmap), 1)
  76. override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
  77. endif
  78. ifeq ($(feature-libelf-getphdrnum), 1)
  79. override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT
  80. endif
  81. # Append required CFLAGS
  82. override CFLAGS += $(EXTRA_WARNINGS)
  83. override CFLAGS += -Werror -Wall
  84. override CFLAGS += -fPIC
  85. override CFLAGS += $(INCLUDES)
  86. ifeq ($(VERBOSE),1)
  87. Q =
  88. else
  89. Q = @
  90. endif
  91. # Disable command line variables (CFLAGS) overide from top
  92. # level Makefile (perf), otherwise build Makefile will get
  93. # the same command line setup.
  94. MAKEOVERRIDES=
  95. export srctree OUTPUT CC LD CFLAGS V
  96. build := -f $(srctree)/tools/build/Makefile.build dir=. obj
  97. BPF_IN := $(OUTPUT)libbpf-in.o
  98. LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
  99. CMD_TARGETS = $(LIB_FILE)
  100. TARGETS = $(CMD_TARGETS)
  101. all: $(VERSION_FILES) all_cmd
  102. all_cmd: $(CMD_TARGETS)
  103. $(BPF_IN): force elfdep bpfdep
  104. $(Q)$(MAKE) $(build)=libbpf
  105. $(OUTPUT)libbpf.so: $(BPF_IN)
  106. $(QUIET_LINK)$(CC) --shared $^ -o $@
  107. $(OUTPUT)libbpf.a: $(BPF_IN)
  108. $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
  109. define update_dir
  110. (echo $1 > $@.tmp; \
  111. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  112. rm -f $@.tmp; \
  113. else \
  114. echo ' UPDATE $@'; \
  115. mv -f $@.tmp $@; \
  116. fi);
  117. endef
  118. define do_install
  119. if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
  120. $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
  121. fi; \
  122. $(INSTALL) $1 '$(DESTDIR_SQ)$2'
  123. endef
  124. install_lib: all_cmd
  125. $(call QUIET_INSTALL, $(LIB_FILE)) \
  126. $(call do_install,$(LIB_FILE),$(libdir_SQ))
  127. install: install_lib
  128. ### Cleaning rules
  129. config-clean:
  130. $(call QUIET_CLEAN, config)
  131. $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
  132. clean:
  133. $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \
  134. $(RM) LIBBPF-CFLAGS
  135. $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP
  136. PHONY += force elfdep bpfdep
  137. force:
  138. elfdep:
  139. @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi
  140. bpfdep:
  141. @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi
  142. # Declare the contents of the .PHONY variable as phony. We keep that
  143. # information in a variable so we can use it in if_changed and friends.
  144. .PHONY: $(PHONY)