]>
Commit | Line | Data |
---|---|---|
1bc38b8f | 1 | # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
1b76c13e WN |
2 | # Most of this file is copied from tools/lib/traceevent/Makefile |
3 | ||
4 | BPF_VERSION = 0 | |
5 | BPF_PATCHLEVEL = 0 | |
1d382264 | 6 | BPF_EXTRAVERSION = 2 |
1b76c13e WN |
7 | |
8 | MAKEFLAGS += --no-print-directory | |
9 | ||
d5ef3140 | 10 | ifeq ($(srctree),) |
e19b7cee | 11 | srctree := $(patsubst %/,%,$(dir $(CURDIR))) |
d5ef3140 NR |
12 | srctree := $(patsubst %/,%,$(dir $(srctree))) |
13 | srctree := $(patsubst %/,%,$(dir $(srctree))) | |
14 | #$(info Determined 'srctree' to be $(srctree)) | |
15 | endif | |
1b76c13e | 16 | |
1b76c13e WN |
17 | INSTALL = install |
18 | ||
19 | # Use DESTDIR for installing into a different root directory. | |
20 | # This is useful for building a package. The program will be | |
21 | # installed in this directory as if it was the root directory. | |
22 | # Then the build tool can move it later. | |
23 | DESTDIR ?= | |
24 | DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' | |
25 | ||
d5ef3140 NR |
26 | include $(srctree)/tools/scripts/Makefile.arch |
27 | ||
1b76c13e WN |
28 | ifeq ($(LP64), 1) |
29 | libdir_relative = lib64 | |
30 | else | |
31 | libdir_relative = lib | |
32 | endif | |
33 | ||
34 | prefix ?= /usr/local | |
35 | libdir = $(prefix)/$(libdir_relative) | |
36 | man_dir = $(prefix)/share/man | |
37 | man_dir_SQ = '$(subst ','\'',$(man_dir))' | |
38 | ||
39 | export man_dir man_dir_SQ INSTALL | |
40 | export DESTDIR DESTDIR_SQ | |
41 | ||
eeedd352 | 42 | include $(srctree)/tools/scripts/Makefile.include |
1b76c13e WN |
43 | |
44 | # copy a bit from Linux kbuild | |
45 | ||
46 | ifeq ("$(origin V)", "command line") | |
47 | VERBOSE = $(V) | |
48 | endif | |
49 | ifndef VERBOSE | |
50 | VERBOSE = 0 | |
51 | endif | |
52 | ||
65f041be | 53 | FEATURE_USER = .libbpf |
8c4905b9 | 54 | FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx |
20517cd9 | 55 | FEATURE_DISPLAY = libelf bpf |
1b76c13e | 56 | |
438363c0 | 57 | INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi |
1b76c13e WN |
58 | FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES) |
59 | ||
d8ad6a15 WN |
60 | check_feat := 1 |
61 | NON_CHECK_FEAT_TARGETS := clean TAGS tags cscope help | |
62 | ifdef MAKECMDGOALS | |
63 | ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) | |
64 | check_feat := 0 | |
65 | endif | |
66 | endif | |
67 | ||
68 | ifeq ($(check_feat),1) | |
58683600 | 69 | ifeq ($(FEATURES_DUMP),) |
1b76c13e | 70 | include $(srctree)/tools/build/Makefile.feature |
58683600 JO |
71 | else |
72 | include $(FEATURES_DUMP) | |
73 | endif | |
d8ad6a15 | 74 | endif |
1b76c13e WN |
75 | |
76 | export prefix libdir src obj | |
77 | ||
78 | # Shell quotes | |
79 | libdir_SQ = $(subst ','\'',$(libdir)) | |
80 | libdir_relative_SQ = $(subst ','\'',$(libdir_relative)) | |
1b76c13e | 81 | |
1b76c13e WN |
82 | VERSION = $(BPF_VERSION) |
83 | PATCHLEVEL = $(BPF_PATCHLEVEL) | |
84 | EXTRAVERSION = $(BPF_EXTRAVERSION) | |
85 | ||
86 | OBJ = $@ | |
87 | N = | |
88 | ||
1d382264 DB |
89 | LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION) |
90 | ||
91 | LIB_TARGET = libbpf.a libbpf.so.$(LIBBPF_VERSION) | |
92 | LIB_FILE = libbpf.a libbpf.so* | |
1b76c13e WN |
93 | |
94 | # Set compile option CFLAGS | |
95 | ifdef EXTRA_CFLAGS | |
96 | CFLAGS := $(EXTRA_CFLAGS) | |
97 | else | |
98 | CFLAGS := -g -Wall | |
99 | endif | |
100 | ||
101 | ifeq ($(feature-libelf-mmap), 1) | |
102 | override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT | |
103 | endif | |
104 | ||
531b014e JK |
105 | ifeq ($(feature-reallocarray), 0) |
106 | override CFLAGS += -DCOMPAT_NEED_REALLOCARRAY | |
107 | endif | |
108 | ||
1b76c13e WN |
109 | # Append required CFLAGS |
110 | override CFLAGS += $(EXTRA_WARNINGS) | |
111 | override CFLAGS += -Werror -Wall | |
112 | override CFLAGS += -fPIC | |
113 | override CFLAGS += $(INCLUDES) | |
ab9e0848 | 114 | override CFLAGS += -fvisibility=hidden |
1b76c13e WN |
115 | |
116 | ifeq ($(VERBOSE),1) | |
117 | Q = | |
118 | else | |
119 | Q = @ | |
120 | endif | |
121 | ||
505d3085 | 122 | # Disable command line variables (CFLAGS) override from top |
1b76c13e WN |
123 | # level Makefile (perf), otherwise build Makefile will get |
124 | # the same command line setup. | |
125 | MAKEOVERRIDES= | |
126 | ||
7c422f55 JO |
127 | all: |
128 | ||
1b76c13e | 129 | export srctree OUTPUT CC LD CFLAGS V |
ab6201d0 | 130 | include $(srctree)/tools/build/Makefile.include |
1b76c13e | 131 | |
1d382264 DB |
132 | BPF_IN := $(OUTPUT)libbpf-in.o |
133 | VERSION_SCRIPT := libbpf.map | |
134 | ||
135 | LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET)) | |
136 | LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) | |
1b76c13e | 137 | |
771744f9 | 138 | GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \ |
306b267c | 139 | awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}') |
771744f9 | 140 | VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \ |
306b267c AI |
141 | grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l) |
142 | ||
1d382264 | 143 | CMD_TARGETS = $(LIB_TARGET) |
1b76c13e | 144 | |
8c4905b9 SF |
145 | CXX_TEST_TARGET = $(OUTPUT)test_libbpf |
146 | ||
147 | ifeq ($(feature-cxx), 1) | |
148 | CMD_TARGETS += $(CXX_TEST_TARGET) | |
149 | endif | |
150 | ||
1b76c13e WN |
151 | TARGETS = $(CMD_TARGETS) |
152 | ||
8e268887 SF |
153 | all: fixdep |
154 | $(Q)$(MAKE) all_cmd | |
1b76c13e | 155 | |
306b267c | 156 | all_cmd: $(CMD_TARGETS) check |
1b76c13e WN |
157 | |
158 | $(BPF_IN): force elfdep bpfdep | |
f15d5e6d | 159 | @(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \ |
971e827b | 160 | (diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \ |
8255e1ef | 161 | echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true |
f15d5e6d | 162 | @(test -f ../../include/uapi/linux/bpf_common.h -a -f ../../../include/uapi/linux/bpf_common.h && ( \ |
971e827b | 163 | (diff -B ../../include/uapi/linux/bpf_common.h ../../../include/uapi/linux/bpf_common.h >/dev/null) || \ |
8255e1ef | 164 | echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf_common.h' differs from latest version at 'include/uapi/linux/bpf_common.h'" >&2 )) || true |
dc2b9f19 EL |
165 | @(test -f ../../include/uapi/linux/netlink.h -a -f ../../../include/uapi/linux/netlink.h && ( \ |
166 | (diff -B ../../include/uapi/linux/netlink.h ../../../include/uapi/linux/netlink.h >/dev/null) || \ | |
167 | echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/netlink.h' differs from latest version at 'include/uapi/linux/netlink.h'" >&2 )) || true | |
168 | @(test -f ../../include/uapi/linux/if_link.h -a -f ../../../include/uapi/linux/if_link.h && ( \ | |
169 | (diff -B ../../include/uapi/linux/if_link.h ../../../include/uapi/linux/if_link.h >/dev/null) || \ | |
170 | echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/if_link.h' differs from latest version at 'include/uapi/linux/if_link.h'" >&2 )) || true | |
1cad0788 MK |
171 | @(test -f ../../include/uapi/linux/if_xdp.h -a -f ../../../include/uapi/linux/if_xdp.h && ( \ |
172 | (diff -B ../../include/uapi/linux/if_xdp.h ../../../include/uapi/linux/if_xdp.h >/dev/null) || \ | |
173 | echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/if_xdp.h' differs from latest version at 'include/uapi/linux/if_xdp.h'" >&2 )) || true | |
1b76c13e WN |
174 | $(Q)$(MAKE) $(build)=libbpf |
175 | ||
1d382264 DB |
176 | $(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION) |
177 | ||
178 | $(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN) | |
179 | $(QUIET_LINK)$(CC) --shared -Wl,-soname,libbpf.so.$(VERSION) \ | |
89dedaef | 180 | -Wl,--version-script=$(VERSION_SCRIPT) $^ -lelf -o $@ |
1d382264 DB |
181 | @ln -sf $(@F) $(OUTPUT)libbpf.so |
182 | @ln -sf $(@F) $(OUTPUT)libbpf.so.$(VERSION) | |
1b76c13e WN |
183 | |
184 | $(OUTPUT)libbpf.a: $(BPF_IN) | |
185 | $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ | |
186 | ||
8c4905b9 | 187 | $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a |
1cad0788 | 188 | $(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@ |
8c4905b9 | 189 | |
306b267c AI |
190 | check: check_abi |
191 | ||
192 | check_abi: $(OUTPUT)libbpf.so | |
193 | @if [ "$(GLOBAL_SYM_COUNT)" != "$(VERSIONED_SYM_COUNT)" ]; then \ | |
194 | echo "Warning: Num of global symbols in $(BPF_IN)" \ | |
195 | "($(GLOBAL_SYM_COUNT)) does NOT match with num of" \ | |
196 | "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \ | |
197 | "Please make sure all LIBBPF_API symbols are" \ | |
198 | "versioned in $(VERSION_SCRIPT)." >&2; \ | |
199 | exit 1; \ | |
200 | fi | |
201 | ||
1d382264 DB |
202 | define do_install_mkdir |
203 | if [ ! -d '$(DESTDIR_SQ)$1' ]; then \ | |
204 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \ | |
205 | fi | |
206 | endef | |
207 | ||
1b76c13e WN |
208 | define do_install |
209 | if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ | |
210 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ | |
211 | fi; \ | |
7110d80d | 212 | $(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2' |
1b76c13e WN |
213 | endef |
214 | ||
215 | install_lib: all_cmd | |
1d382264 DB |
216 | $(call QUIET_INSTALL, $(LIB_TARGET)) \ |
217 | $(call do_install_mkdir,$(libdir_SQ)); \ | |
218 | cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ) | |
1b76c13e | 219 | |
eb54e522 JK |
220 | install_headers: |
221 | $(call QUIET_INSTALL, headers) \ | |
7d386c62 | 222 | $(call do_install,bpf.h,$(prefix)/include/bpf,644); \ |
8543e437 DB |
223 | $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \ |
224 | $(call do_install,btf.h,$(prefix)/include/bpf,644); \ | |
379e2014 | 225 | $(call do_install,xsk.h,$(prefix)/include/bpf,644); |
eb54e522 | 226 | |
1b76c13e WN |
227 | install: install_lib |
228 | ||
229 | ### Cleaning rules | |
230 | ||
231 | config-clean: | |
232 | $(call QUIET_CLEAN, config) | |
233 | $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null | |
234 | ||
235 | clean: | |
8c4905b9 | 236 | $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \ |
1d382264 | 237 | *.o *~ *.a *.so *.so.$(VERSION) .*.d .*.cmd LIBBPF-CFLAGS |
0c77c04a | 238 | $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf |
1b76c13e WN |
239 | |
240 | ||
241 | ||
242 | PHONY += force elfdep bpfdep | |
243 | force: | |
244 | ||
245 | elfdep: | |
21567ede | 246 | @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi |
1b76c13e WN |
247 | |
248 | bpfdep: | |
21567ede | 249 | @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi |
1b76c13e WN |
250 | |
251 | # Declare the contents of the .PHONY variable as phony. We keep that | |
252 | # information in a variable so we can use it in if_changed and friends. | |
253 | .PHONY: $(PHONY) |