blob: 62d89d50fcbd0484e7c38883b9d3d512e627eaef [file] [log] [blame]
Wang Nan1b76c132015-07-01 02:13:51 +00001# Most of this file is copied from tools/lib/traceevent/Makefile
2
3BPF_VERSION = 0
4BPF_PATCHLEVEL = 0
5BPF_EXTRAVERSION = 1
6
7MAKEFLAGS += --no-print-directory
8
Naveen N. Raod5ef3142016-01-11 13:48:00 +00009ifeq ($(srctree),)
10srctree := $(patsubst %/,%,$(dir $(shell pwd)))
11srctree := $(patsubst %/,%,$(dir $(srctree)))
12srctree := $(patsubst %/,%,$(dir $(srctree)))
13#$(info Determined 'srctree' to be $(srctree))
14endif
Wang Nan1b76c132015-07-01 02:13:51 +000015
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.
21define allow-override
22 $(if $(or $(findstring environment,$(origin $(1))),\
23 $(findstring command line,$(origin $(1)))),,\
24 $(eval $(1) = $(2)))
25endef
26
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)
30
31INSTALL = install
32
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.
37DESTDIR ?=
38DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
39
Naveen N. Raod5ef3142016-01-11 13:48:00 +000040include $(srctree)/tools/scripts/Makefile.arch
41
Wang Nan1b76c132015-07-01 02:13:51 +000042ifeq ($(LP64), 1)
43 libdir_relative = lib64
44else
45 libdir_relative = lib
46endif
47
48prefix ?= /usr/local
49libdir = $(prefix)/$(libdir_relative)
50man_dir = $(prefix)/share/man
51man_dir_SQ = '$(subst ','\'',$(man_dir))'
52
53export man_dir man_dir_SQ INSTALL
54export DESTDIR DESTDIR_SQ
55
56include ../../scripts/Makefile.include
57
58# copy a bit from Linux kbuild
59
60ifeq ("$(origin V)", "command line")
61 VERBOSE = $(V)
62endif
63ifndef VERBOSE
64 VERBOSE = 0
65endif
66
Arnaldo Carvalho de Melo65f041b2015-09-21 17:25:27 -030067FEATURE_USER = .libbpf
Arnaldo Carvalho de Melo20517cd2015-09-21 17:00:27 -030068FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf
69FEATURE_DISPLAY = libelf bpf
Wang Nan1b76c132015-07-01 02:13:51 +000070
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030071INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
Wang Nan1b76c132015-07-01 02:13:51 +000072FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
73
Wang Nand8ad6a12015-11-24 13:36:07 +000074check_feat := 1
75NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help
76ifdef MAKECMDGOALS
77ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
78 check_feat := 0
79endif
80endif
81
82ifeq ($(check_feat),1)
Jiri Olsa58683602015-12-23 18:58:34 +010083ifeq ($(FEATURES_DUMP),)
Wang Nan1b76c132015-07-01 02:13:51 +000084include $(srctree)/tools/build/Makefile.feature
Jiri Olsa58683602015-12-23 18:58:34 +010085else
86include $(FEATURES_DUMP)
87endif
Wang Nand8ad6a12015-11-24 13:36:07 +000088endif
Wang Nan1b76c132015-07-01 02:13:51 +000089
90export prefix libdir src obj
91
92# Shell quotes
93libdir_SQ = $(subst ','\'',$(libdir))
94libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
95plugin_dir_SQ = $(subst ','\'',$(plugin_dir))
96
97LIB_FILE = libbpf.a libbpf.so
98
99VERSION = $(BPF_VERSION)
100PATCHLEVEL = $(BPF_PATCHLEVEL)
101EXTRAVERSION = $(BPF_EXTRAVERSION)
102
103OBJ = $@
104N =
105
106LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
107
108# Set compile option CFLAGS
109ifdef EXTRA_CFLAGS
110 CFLAGS := $(EXTRA_CFLAGS)
111else
112 CFLAGS := -g -Wall
113endif
114
115ifeq ($(feature-libelf-mmap), 1)
116 override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
117endif
118
119ifeq ($(feature-libelf-getphdrnum), 1)
120 override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT
121endif
122
123# Append required CFLAGS
124override CFLAGS += $(EXTRA_WARNINGS)
125override CFLAGS += -Werror -Wall
126override CFLAGS += -fPIC
127override CFLAGS += $(INCLUDES)
128
129ifeq ($(VERBOSE),1)
130 Q =
131else
132 Q = @
133endif
134
135# Disable command line variables (CFLAGS) overide from top
136# level Makefile (perf), otherwise build Makefile will get
137# the same command line setup.
138MAKEOVERRIDES=
139
Jiri Olsa7c422f52015-09-23 12:34:02 +0200140all:
141
Wang Nan1b76c132015-07-01 02:13:51 +0000142export srctree OUTPUT CC LD CFLAGS V
Jiri Olsaab6201d2015-09-23 12:33:56 +0200143include $(srctree)/tools/build/Makefile.include
Wang Nan1b76c132015-07-01 02:13:51 +0000144
145BPF_IN := $(OUTPUT)libbpf-in.o
146LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
147
148CMD_TARGETS = $(LIB_FILE)
149
150TARGETS = $(CMD_TARGETS)
151
Jiri Olsa7c422f52015-09-23 12:34:02 +0200152all: fixdep $(VERSION_FILES) all_cmd
Wang Nan1b76c132015-07-01 02:13:51 +0000153
154all_cmd: $(CMD_TARGETS)
155
156$(BPF_IN): force elfdep bpfdep
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300157 @(test -f ../../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
158 (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
159 echo "Warning: tools/include/uapi/linux/bpf.h differs from kernel" >&2 )) || true
160 @(test -f ../../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \
161 (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \
162 echo "Warning: tools/include/uapi/linux/bpf_common.h differs from kernel" >&2 )) || true
Wang Nan1b76c132015-07-01 02:13:51 +0000163 $(Q)$(MAKE) $(build)=libbpf
164
165$(OUTPUT)libbpf.so: $(BPF_IN)
166 $(QUIET_LINK)$(CC) --shared $^ -o $@
167
168$(OUTPUT)libbpf.a: $(BPF_IN)
169 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
170
171define update_dir
172 (echo $1 > $@.tmp; \
173 if [ -r $@ ] && cmp -s $@ $@.tmp; then \
174 rm -f $@.tmp; \
175 else \
176 echo ' UPDATE $@'; \
177 mv -f $@.tmp $@; \
178 fi);
179endef
180
181define do_install
182 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
183 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
184 fi; \
185 $(INSTALL) $1 '$(DESTDIR_SQ)$2'
186endef
187
188install_lib: all_cmd
189 $(call QUIET_INSTALL, $(LIB_FILE)) \
190 $(call do_install,$(LIB_FILE),$(libdir_SQ))
191
192install: install_lib
193
194### Cleaning rules
195
196config-clean:
197 $(call QUIET_CLEAN, config)
198 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
199
200clean:
Jiri Olsa22992a32016-01-11 11:54:49 +0100201 $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d .*.cmd \
Wang Nan1b76c132015-07-01 02:13:51 +0000202 $(RM) LIBBPF-CFLAGS
Yunlong Song0c77c042015-11-05 14:56:34 +0800203 $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
Wang Nan1b76c132015-07-01 02:13:51 +0000204
205
206
207PHONY += force elfdep bpfdep
208force:
209
210elfdep:
211 @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi
212
213bpfdep:
214 @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi
215
216# Declare the contents of the .PHONY variable as phony. We keep that
217# information in a variable so we can use it in if_changed and friends.
218.PHONY: $(PHONY)