1 # Most of this file is copied from tools/lib/traceevent/Makefile
7 MAKEFLAGS += --no-print-directory
10 srctree := $(patsubst %/,%,$(dir $(shell pwd)))
11 srctree := $(patsubst %/,%,$(dir $(srctree)))
12 srctree := $(patsubst %/,%,$(dir $(srctree)))
13 #$(info Determined 'srctree' to be $(srctree))
16 # Makefiles suck: This macro sets a default value of $(2) for the
17 # variable named by $(1), unless the variable has been set by
18 # environment or command line. This is necessary for CC and AR
19 # because make sets default values, so the simpler ?= approach
20 # won't work as expected.
22 $(if $(or $(findstring environment,$(origin $(1))),\
23 $(findstring command line,$(origin $(1)))),,\
27 # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
28 $(call allow-override,CC,$(CROSS_COMPILE)gcc)
29 $(call allow-override,AR,$(CROSS_COMPILE)ar)
33 # Use DESTDIR for installing into a different root directory.
34 # This is useful for building a package. The program will be
35 # installed in this directory as if it was the root directory.
36 # Then the build tool can move it later.
38 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
40 include $(srctree)/tools/scripts/Makefile.arch
43 libdir_relative = lib64
49 libdir = $(prefix)/$(libdir_relative)
50 man_dir = $(prefix)/share/man
51 man_dir_SQ = '$(subst ','\'',$(man_dir))'
53 export man_dir man_dir_SQ INSTALL
54 export DESTDIR DESTDIR_SQ
56 include ../../scripts/Makefile.include
58 # copy a bit from Linux kbuild
60 ifeq ("$(origin V)", "command line")
67 FEATURE_USER = .libbpf
68 FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf
69 FEATURE_DISPLAY = libelf bpf
71 INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi
72 FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
75 NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
77 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
82 ifeq ($(check_feat),1)
83 ifeq ($(FEATURES_DUMP),)
84 include $(srctree)/tools/build/Makefile.feature
86 include $(FEATURES_DUMP)
90 export prefix libdir src obj
93 libdir_SQ = $(subst ','\'',$(libdir))
94 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
95 plugin_dir_SQ = $(subst ','\'',$(plugin_dir))
97 LIB_FILE = libbpf.a libbpf.so
99 VERSION = $(BPF_VERSION)
100 PATCHLEVEL = $(BPF_PATCHLEVEL)
101 EXTRAVERSION = $(BPF_EXTRAVERSION)
106 LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
108 # Set compile option CFLAGS
110 CFLAGS := $(EXTRA_CFLAGS)
115 ifeq ($(feature-libelf-mmap), 1)
116 override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
119 ifeq ($(feature-libelf-getphdrnum), 1)
120 override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT
123 # Append required CFLAGS
124 override CFLAGS += $(EXTRA_WARNINGS)
125 override CFLAGS += -Werror -Wall
126 override CFLAGS += -fPIC
127 override CFLAGS += $(INCLUDES)
135 # Disable command line variables (CFLAGS) overide from top
136 # level Makefile (perf), otherwise build Makefile will get
137 # the same command line setup.
142 export srctree OUTPUT CC LD CFLAGS V
143 include $(srctree)/tools/build/Makefile.include
145 BPF_IN := $(OUTPUT)libbpf-in.o
146 LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
148 CMD_TARGETS = $(LIB_FILE)
150 TARGETS = $(CMD_TARGETS)
152 all: fixdep $(VERSION_FILES) all_cmd
154 all_cmd: $(CMD_TARGETS)
156 $(BPF_IN): force elfdep bpfdep
157 $(Q)$(MAKE) $(build)=libbpf
159 $(OUTPUT)libbpf.so: $(BPF_IN)
160 $(QUIET_LINK)$(CC) --shared $^ -o $@
162 $(OUTPUT)libbpf.a: $(BPF_IN)
163 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
167 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
176 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
177 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
179 $(INSTALL) $1 '$(DESTDIR_SQ)$2'
183 $(call QUIET_INSTALL, $(LIB_FILE)) \
184 $(call do_install,$(LIB_FILE),$(libdir_SQ))
191 $(call QUIET_CLEAN, config)
192 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
195 $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd \
197 $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
201 PHONY += force elfdep bpfdep
205 @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi
208 @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi
210 # Declare the contents of the .PHONY variable as phony. We keep that
211 # information in a variable so we can use it in if_changed and friends.