Makefile 5.8 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)
J
Jakub Kicinski 已提交
22
else
23
  LIBBPF_PATH = $(BPF_DIR)
J
Jakub Kicinski 已提交
24 25
endif

26
LIBBPF = $(LIBBPF_PATH)libbpf.a
J
Jakub Kicinski 已提交
27

28
BPFTOOL_VERSION ?= $(shell make -rR --no-print-directory -sC ../../.. kernelversion)
29

J
Jakub Kicinski 已提交
30
$(LIBBPF): FORCE
31 32
	$(if $(LIBBPF_OUTPUT),@mkdir -p $(LIBBPF_OUTPUT))
	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a
J
Jakub Kicinski 已提交
33 34 35

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

38
prefix ?= /usr/local
39
bash_compdir ?= /usr/share/bash-completion/completions
J
Jakub Kicinski 已提交
40 41

CFLAGS += -O2
42
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
43
CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
44
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
45
	-I$(if $(OUTPUT),$(OUTPUT),.) \
46 47 48
	-I$(srctree)/kernel/bpf/ \
	-I$(srctree)/tools/include \
	-I$(srctree)/tools/include/uapi \
49
	-I$(srctree)/tools/lib \
50
	-I$(srctree)/tools/perf
51
CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
52 53 54
ifneq ($(EXTRA_CFLAGS),)
CFLAGS += $(EXTRA_CFLAGS)
endif
55 56 57
ifneq ($(EXTRA_LDFLAGS),)
LDFLAGS += $(EXTRA_LDFLAGS)
endif
58

59 60
INSTALL ?= install
RM ?= rm -f
61
CLANG ?= clang
62
LLVM_STRIP ?= llvm-strip
63

64
FEATURE_USER = .bpftool
65
FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
66
	clang-bpf-co-re
67
FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
68
	clang-bpf-co-re
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

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

90 91 92 93
ifeq ($(feature-reallocarray), 0)
CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
endif

94 95 96 97 98 99
LIBS = $(LIBBPF) -lelf -lz
ifeq ($(feature-libcap), 1)
CFLAGS += -DUSE_LIBCAP
LIBS += -lcap
endif

100
include $(wildcard $(OUTPUT)*.d)
J
Jakub Kicinski 已提交
101 102 103

all: $(OUTPUT)bpftool

S
Stanislav Fomichev 已提交
104 105 106 107 108
BFD_SRCS = jit_disasm.c

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

ifeq ($(feature-libbfd),1)
109 110 111 112 113 114 115 116
  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 已提交
117 118 119 120
CFLAGS += -DHAVE_LIBBFD_SUPPORT
SRCS += $(BFD_SRCS)
endif

121 122 123
BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)

BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)
124
OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
125

126
VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
127 128 129 130
		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
		     ../../../vmlinux					\
		     /sys/kernel/btf/vmlinux				\
		     /boot/vmlinux-$(shell uname -r)
131
VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
132

133
ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
134 135 136 137 138
ifeq ($(feature-clang-bpf-co-re),1)

BUILD_BPF_SKELS := 1

$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
139
ifeq ($(VMLINUX_H),)
140
	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
141 142 143
else
	$(Q)cp "$(VMLINUX_H)" $@
endif
144

145
$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
146
	$(QUIET_CLANG)$(CLANG) \
147
		-I$(if $(OUTPUT),$(OUTPUT),.) \
148
		-I$(srctree)/tools/include/uapi/ \
149 150
		-I$(LIBBPF_PATH) \
		-I$(srctree)/tools/lib \
151
		-g -O2 -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@
152

153 154 155 156 157
$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@

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

158 159
$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h

160 161
endif
endif
162

163
CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
164 165

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

168 169
$(OUTPUT)feature.o: | zdep

170 171 172 173 174
$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF)
	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) $(LIBS)

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

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

179 180 181 182 183
feature-detect-clean:
	$(call QUIET_CLEAN, feature-detect)
	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null

clean: $(LIBBPF)-clean feature-detect-clean
J
Jakub Kicinski 已提交
184
	$(call QUIET_CLEAN, bpftool)
185
	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
186
	$(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
187
	$(Q)$(RM) -r -- $(OUTPUT)libbpf/
188
	$(call QUIET_CLEAN, core-gen)
189 190
	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
	$(Q)$(RM) -r -- $(OUTPUT)feature/
J
Jakub Kicinski 已提交
191

192 193 194 195 196 197
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 已提交
198

199 200
uninstall:
	$(call QUIET_UNINST, bpftool)
201 202
	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
203

J
Jakub Kicinski 已提交
204
doc:
205 206 207 208
	$(call descend,Documentation)

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

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

213 214 215
doc-uninstall:
	$(call descend,Documentation,uninstall)

J
Jakub Kicinski 已提交
216 217
FORCE:

218 219 220
zdep:
	@if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi

221
.SECONDARY:
222
.PHONY: all FORCE clean install uninstall zdep
223
.PHONY: doc doc-clean doc-install doc-uninstall
J
Jakub Kicinski 已提交
224
.DEFAULT_GOAL := all