Makefile 6.6 KB
Newer Older
1
# SPDX-License-Identifier: GPL-2.0-only
J
Jakub Kicinski 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
include ../../scripts/Makefile.include
include ../../scripts/utilities.mak

ifeq ($(srctree),)
srctree := $(patsubst %/,%,$(dir $(CURDIR)))
srctree := $(patsubst %/,%,$(dir $(srctree)))
srctree := $(patsubst %/,%,$(dir $(srctree)))
endif

ifeq ($(V),1)
  Q =
else
  Q = @
endif

17
BPF_DIR = $(srctree)/tools/lib/bpf/
J
Jakub Kicinski 已提交
18 19

ifneq ($(OUTPUT),)
20 21
  LIBBPF_OUTPUT = $(OUTPUT)/libbpf/
  LIBBPF_PATH = $(LIBBPF_OUTPUT)
22
  BOOTSTRAP_OUTPUT = $(OUTPUT)/bootstrap/
J
Jakub Kicinski 已提交
23
else
24
  LIBBPF_OUTPUT =
25
  LIBBPF_PATH = $(BPF_DIR)
26
  BOOTSTRAP_OUTPUT = $(CURDIR)/bootstrap/
J
Jakub Kicinski 已提交
27 28
endif

29
LIBBPF = $(LIBBPF_PATH)libbpf.a
30 31
LIBBPF_BOOTSTRAP_OUTPUT = $(BOOTSTRAP_OUTPUT)libbpf/
LIBBPF_BOOTSTRAP = $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a
J
Jakub Kicinski 已提交
32

33 34 35
ifeq ($(BPFTOOL_VERSION),)
BPFTOOL_VERSION := $(shell make -rR --no-print-directory -sC ../../.. kernelversion)
endif
36

37
$(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT):
38 39 40
	$(QUIET_MKDIR)mkdir -p $@

$(LIBBPF): FORCE | $(LIBBPF_OUTPUT)
41
	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a
J
Jakub Kicinski 已提交
42

43 44 45 46
$(LIBBPF_BOOTSTRAP): FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT)
	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
		ARCH= CC=$(HOSTCC) LD=$(HOSTLD) $@

47
$(LIBBPF)-clean: $(LIBBPF_OUTPUT)
J
Jakub Kicinski 已提交
48
	$(call QUIET_CLEAN, libbpf)
49
	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
J
Jakub Kicinski 已提交
50

51 52 53 54
$(LIBBPF_BOOTSTRAP)-clean: $(LIBBPF_BOOTSTRAP_OUTPUT)
	$(call QUIET_CLEAN, libbpf-bootstrap)
	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) clean >/dev/null

55
prefix ?= /usr/local
56
bash_compdir ?= /usr/share/bash-completion/completions
J
Jakub Kicinski 已提交
57 58

CFLAGS += -O2
59
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
60
CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
61
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
62
	-I$(if $(OUTPUT),$(OUTPUT),.) \
63 64 65
	-I$(srctree)/kernel/bpf/ \
	-I$(srctree)/tools/include \
	-I$(srctree)/tools/include/uapi \
66
	-I$(srctree)/tools/lib \
67
	-I$(srctree)/tools/perf
68
CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
69 70 71
ifneq ($(EXTRA_CFLAGS),)
CFLAGS += $(EXTRA_CFLAGS)
endif
72 73 74
ifneq ($(EXTRA_LDFLAGS),)
LDFLAGS += $(EXTRA_LDFLAGS)
endif
75

76 77
INSTALL ?= install
RM ?= rm -f
78
CLANG ?= clang
79
LLVM_STRIP ?= llvm-strip
80

81
FEATURE_USER = .bpftool
82
FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
83
	clang-bpf-co-re
84
FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
85
	clang-bpf-co-re
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

check_feat := 1
NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
ifdef MAKECMDGOALS
ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
  check_feat := 0
endif
endif

ifeq ($(check_feat),1)
ifeq ($(FEATURES_DUMP),)
include $(srctree)/tools/build/Makefile.feature
else
include $(FEATURES_DUMP)
endif
endif

ifeq ($(feature-disassembler-four-args), 1)
CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
endif

107 108 109 110
ifeq ($(feature-reallocarray), 0)
CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
endif

111
LIBS = $(LIBBPF) -lelf -lz
112
LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
113 114 115 116 117
ifeq ($(feature-libcap), 1)
CFLAGS += -DUSE_LIBCAP
LIBS += -lcap
endif

118
include $(wildcard $(OUTPUT)*.d)
J
Jakub Kicinski 已提交
119 120 121

all: $(OUTPUT)bpftool

S
Stanislav Fomichev 已提交
122 123 124 125 126
BFD_SRCS = jit_disasm.c

SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))

ifeq ($(feature-libbfd),1)
127 128 129 130 131 132 133 134
  LIBS += -lbfd -ldl -lopcodes
else ifeq ($(feature-libbfd-liberty),1)
  LIBS += -lbfd -ldl -lopcodes -liberty
else ifeq ($(feature-libbfd-liberty-z),1)
  LIBS += -lbfd -ldl -lopcodes -liberty -lz
endif

ifneq ($(filter -lbfd,$(LIBS)),)
S
Stanislav Fomichev 已提交
135 136 137 138
CFLAGS += -DHAVE_LIBBFD_SUPPORT
SRCS += $(BFD_SRCS)
endif

139
BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
140

141
BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o)
142
OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
143

144
VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
145 146 147 148
		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
		     ../../../vmlinux					\
		     /sys/kernel/btf/vmlinux				\
		     /boot/vmlinux-$(shell uname -r)
149
VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
150

151
ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
152 153 154 155 156
ifeq ($(feature-clang-bpf-co-re),1)

BUILD_BPF_SKELS := 1

$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
157
ifeq ($(VMLINUX_H),)
158
	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
159 160 161
else
	$(Q)cp "$(VMLINUX_H)" $@
endif
162

163
$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
164
	$(QUIET_CLANG)$(CLANG) \
165
		-I$(if $(OUTPUT),$(OUTPUT),.) \
166
		-I$(srctree)/tools/include/uapi/ \
167 168
		-I$(LIBBPF_PATH) \
		-I$(srctree)/tools/lib \
169
		-g -O2 -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@
170

171 172 173 174 175
$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@

$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h

176 177
$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h

178 179
endif
endif
180

181
CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
182 183

$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
184
	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
J
Jakub Kicinski 已提交
185

186 187
$(OUTPUT)feature.o: | zdep

188 189 190
$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
	$(QUIET_LINK)$(HOSTCC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) \
		$(LIBS_BOOTSTRAP)
191 192 193

$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
J
Jakub Kicinski 已提交
194

195 196 197
$(BOOTSTRAP_OUTPUT)%.o: %.c | $(BOOTSTRAP_OUTPUT)
	$(QUIET_CC)$(HOSTCC) $(CFLAGS) -c -MMD -o $@ $<

J
Jakub Kicinski 已提交
198
$(OUTPUT)%.o: %.c
199
	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
J
Jakub Kicinski 已提交
200

201 202 203 204
feature-detect-clean:
	$(call QUIET_CLEAN, feature-detect)
	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null

205
clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
J
Jakub Kicinski 已提交
206
	$(call QUIET_CLEAN, bpftool)
207
	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
208 209
	$(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
	$(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
210
	$(call QUIET_CLEAN, core-gen)
211 212
	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
	$(Q)$(RM) -r -- $(OUTPUT)feature/
J
Jakub Kicinski 已提交
213

214 215 216 217 218 219
install: $(OUTPUT)bpftool
	$(call QUIET_INSTALL, bpftool)
	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
J
Jakub Kicinski 已提交
220

221 222
uninstall:
	$(call QUIET_UNINST, bpftool)
223 224
	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
225

J
Jakub Kicinski 已提交
226
doc:
227 228 229 230
	$(call descend,Documentation)

doc-clean:
	$(call descend,Documentation,clean)
J
Jakub Kicinski 已提交
231 232

doc-install:
233
	$(call descend,Documentation,install)
J
Jakub Kicinski 已提交
234

235 236 237
doc-uninstall:
	$(call descend,Documentation,uninstall)

J
Jakub Kicinski 已提交
238 239
FORCE:

240 241 242
zdep:
	@if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi

243
.SECONDARY:
244
.PHONY: all FORCE clean install uninstall zdep
245
.PHONY: doc doc-clean doc-install doc-uninstall
J
Jakub Kicinski 已提交
246
.DEFAULT_GOAL := all