1 # Most of this file is copied from tools/lib/traceevent/Makefile
7 MAKEFLAGS += --no-print-directory
10 # Makefiles suck: This macro sets a default value of $(2) for the
11 # variable named by $(1), unless the variable has been set by
12 # environment or command line. This is necessary for CC and AR
13 # because make sets default values, so the simpler ?= approach
14 # won't work as expected.
16 $(if $(or $(findstring environment,$(origin $(1))),\
17 $(findstring command line,$(origin $(1)))),,\
21 # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
22 $(call allow-override,CC,$(CROSS_COMPILE)gcc)
23 $(call allow-override,AR,$(CROSS_COMPILE)ar)
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.
32 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
34 LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
36 libdir_relative = lib64
42 libdir = $(prefix)/$(libdir_relative)
43 man_dir = $(prefix)/share/man
44 man_dir_SQ = '$(subst ','\'',$(man_dir))'
46 export man_dir man_dir_SQ INSTALL
47 export DESTDIR DESTDIR_SQ
49 include ../../scripts/Makefile.include
51 # copy a bit from Linux kbuild
53 ifeq ("$(origin V)", "command line")
61 srctree := $(patsubst %/,%,$(dir $(shell pwd)))
62 srctree := $(patsubst %/,%,$(dir $(srctree)))
63 srctree := $(patsubst %/,%,$(dir $(srctree)))
64 #$(info Determined 'srctree' to be $(srctree))
67 FEATURE_DISPLAY = libelf libelf-getphdrnum libelf-mmap bpf
68 FEATURE_TESTS = libelf bpf
70 INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi
71 FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
73 include $(srctree)/tools/build/Makefile.feature
75 export prefix libdir src obj
78 libdir_SQ = $(subst ','\'',$(libdir))
79 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
80 plugin_dir_SQ = $(subst ','\'',$(plugin_dir))
82 LIB_FILE = libbpf.a libbpf.so
84 VERSION = $(BPF_VERSION)
85 PATCHLEVEL = $(BPF_PATCHLEVEL)
86 EXTRAVERSION = $(BPF_EXTRAVERSION)
91 LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
93 # Set compile option CFLAGS
95 CFLAGS := $(EXTRA_CFLAGS)
100 ifeq ($(feature-libelf-mmap), 1)
101 override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
104 ifeq ($(feature-libelf-getphdrnum), 1)
105 override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT
108 # Append required CFLAGS
109 override CFLAGS += $(EXTRA_WARNINGS)
110 override CFLAGS += -Werror -Wall
111 override CFLAGS += -fPIC
112 override CFLAGS += $(INCLUDES)
120 # Disable command line variables (CFLAGS) overide from top
121 # level Makefile (perf), otherwise build Makefile will get
122 # the same command line setup.
125 export srctree OUTPUT CC LD CFLAGS V
126 build := -f $(srctree)/tools/build/Makefile.build dir=. obj
128 BPF_IN := $(OUTPUT)libbpf-in.o
129 LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
131 CMD_TARGETS = $(LIB_FILE)
133 TARGETS = $(CMD_TARGETS)
135 all: $(VERSION_FILES) all_cmd
137 all_cmd: $(CMD_TARGETS)
139 $(BPF_IN): force elfdep bpfdep
140 $(Q)$(MAKE) $(build)=libbpf
142 $(OUTPUT)libbpf.so: $(BPF_IN)
143 $(QUIET_LINK)$(CC) --shared $^ -o $@
145 $(OUTPUT)libbpf.a: $(BPF_IN)
146 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
150 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
159 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
160 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
162 $(INSTALL) $1 '$(DESTDIR_SQ)$2'
166 $(call QUIET_INSTALL, $(LIB_FILE)) \
167 $(call do_install,$(LIB_FILE),$(libdir_SQ))
174 $(call QUIET_CLEAN, config)
175 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
178 $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \
180 $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP
184 PHONY += force elfdep bpfdep
188 @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi
191 @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi
193 # Declare the contents of the .PHONY variable as phony. We keep that
194 # information in a variable so we can use it in if_changed and friends.