xref: /linux/tools/lib/bpf/Makefile (revision 64af4e0da419ef9e9db0d34a3b5836adbf90a5e8)
1# 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
9
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.
15define allow-override
16  $(if $(or $(findstring environment,$(origin $(1))),\
17            $(findstring command line,$(origin $(1)))),,\
18    $(eval $(1) = $(2)))
19endef
20
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)
24
25INSTALL = install
26
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.
31DESTDIR ?=
32DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
33
34LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
35ifeq ($(LP64), 1)
36  libdir_relative = lib64
37else
38  libdir_relative = lib
39endif
40
41prefix ?= /usr/local
42libdir = $(prefix)/$(libdir_relative)
43man_dir = $(prefix)/share/man
44man_dir_SQ = '$(subst ','\'',$(man_dir))'
45
46export man_dir man_dir_SQ INSTALL
47export DESTDIR DESTDIR_SQ
48
49include ../../scripts/Makefile.include
50
51# copy a bit from Linux kbuild
52
53ifeq ("$(origin V)", "command line")
54  VERBOSE = $(V)
55endif
56ifndef VERBOSE
57  VERBOSE = 0
58endif
59
60ifeq ($(srctree),)
61srctree := $(patsubst %/,%,$(dir $(shell pwd)))
62srctree := $(patsubst %/,%,$(dir $(srctree)))
63srctree := $(patsubst %/,%,$(dir $(srctree)))
64#$(info Determined 'srctree' to be $(srctree))
65endif
66
67FEATURE_USER = .libbpf
68FEATURE_TESTS = libelf libelf-getphdrnum libelf-mmap bpf
69FEATURE_DISPLAY = libelf bpf
70
71INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi
72FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
73
74check_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)
83ifeq ($(FEATURES_DUMP),)
84include $(srctree)/tools/build/Makefile.feature
85else
86include $(FEATURES_DUMP)
87endif
88endif
89
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
140all:
141
142export srctree OUTPUT CC LD CFLAGS V
143include $(srctree)/tools/build/Makefile.include
144
145BPF_IN    := $(OUTPUT)libbpf-in.o
146LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
147
148CMD_TARGETS = $(LIB_FILE)
149
150TARGETS = $(CMD_TARGETS)
151
152all: fixdep $(VERSION_FILES) all_cmd
153
154all_cmd: $(CMD_TARGETS)
155
156$(BPF_IN): force elfdep bpfdep
157	$(Q)$(MAKE) $(build)=libbpf
158
159$(OUTPUT)libbpf.so: $(BPF_IN)
160	$(QUIET_LINK)$(CC) --shared $^ -o $@
161
162$(OUTPUT)libbpf.a: $(BPF_IN)
163	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
164
165define update_dir
166  (echo $1 > $@.tmp;				\
167   if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
168     rm -f $@.tmp;				\
169   else						\
170     echo '  UPDATE                 $@';	\
171     mv -f $@.tmp $@;				\
172   fi);
173endef
174
175define do_install
176	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\
177		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\
178	fi;						\
179	$(INSTALL) $1 '$(DESTDIR_SQ)$2'
180endef
181
182install_lib: all_cmd
183	$(call QUIET_INSTALL, $(LIB_FILE)) \
184		$(call do_install,$(LIB_FILE),$(libdir_SQ))
185
186install: install_lib
187
188### Cleaning rules
189
190config-clean:
191	$(call QUIET_CLEAN, config)
192	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
193
194clean:
195	$(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \
196		$(RM) LIBBPF-CFLAGS
197	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
198
199
200
201PHONY += force elfdep bpfdep
202force:
203
204elfdep:
205	@if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi
206
207bpfdep:
208	@if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi
209
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.
212.PHONY: $(PHONY)
213