Makefile 4.9 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 43
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
CFLAGS += $(filter-out -Wswitch-enum,$(EXTRA_WARNINGS))
44 45 46 47
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
	-I$(srctree)/kernel/bpf/ \
	-I$(srctree)/tools/include \
	-I$(srctree)/tools/include/uapi \
48
	-I$(srctree)/tools/lib \
49
	-I$(srctree)/tools/perf
50
CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
51 52 53
ifneq ($(EXTRA_CFLAGS),)
CFLAGS += $(EXTRA_CFLAGS)
endif
54 55 56
ifneq ($(EXTRA_LDFLAGS),)
LDFLAGS += $(EXTRA_LDFLAGS)
endif
57

58
LIBS = $(LIBBPF) -lelf -lz
J
Jakub Kicinski 已提交
59

60 61
INSTALL ?= install
RM ?= rm -f
62
CLANG ?= clang
63

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

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

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

93
include $(wildcard $(OUTPUT)*.d)
J
Jakub Kicinski 已提交
94 95 96

all: $(OUTPUT)bpftool

S
Stanislav Fomichev 已提交
97 98 99 100 101
BFD_SRCS = jit_disasm.c

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

ifeq ($(feature-libbfd),1)
102 103 104 105 106 107 108 109
  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 已提交
110 111 112 113
CFLAGS += -DHAVE_LIBBFD_SUPPORT
SRCS += $(BFD_SRCS)
endif

114
OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
115 116
_OBJS = $(filter-out $(OUTPUT)prog.o,$(OBJS)) $(OUTPUT)_prog.o

117 118 119 120 121 122
ifeq ($(feature-clang-bpf-global-var),1)
	__OBJS = $(OBJS)
else
	__OBJS = $(_OBJS)
endif

123 124 125 126 127 128 129
$(OUTPUT)_prog.o: prog.c
	$(QUIET_CC)$(COMPILE.c) -MMD -DBPFTOOL_WITHOUT_SKELETONS -o $@ $<

$(OUTPUT)_bpftool: $(_OBJS) $(LIBBPF)
	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(_OBJS) $(LIBS)

skeleton/profiler.bpf.o: skeleton/profiler.bpf.c
130 131 132 133
	$(QUIET_CLANG)$(CLANG) \
		-I$(srctree)/tools/include/uapi/ \
		-I$(srctree)/tools/testing/selftests/bpf/include/uapi \
		-I$(srctree)/tools/lib -g -O2 -target bpf -c $< -o $@
134 135 136 137 138 139

profiler.skel.h: $(OUTPUT)_bpftool skeleton/profiler.bpf.o
	$(QUIET_GEN)$(OUTPUT)./_bpftool gen skeleton skeleton/profiler.bpf.o > $@

$(OUTPUT)prog.o: prog.c profiler.skel.h
	$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
140 141 142

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

144 145
$(OUTPUT)feature.o: | zdep

146 147
$(OUTPUT)bpftool: $(__OBJS) $(LIBBPF)
	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(__OBJS) $(LIBS)
J
Jakub Kicinski 已提交
148 149 150 151 152 153

$(OUTPUT)%.o: %.c
	$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<

clean: $(LIBBPF)-clean
	$(call QUIET_CLEAN, bpftool)
154
	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
155
	$(Q)$(RM) -- $(OUTPUT)_bpftool profiler.skel.h skeleton/profiler.bpf.o
156
	$(Q)$(RM) -r -- $(OUTPUT)libbpf/
157
	$(call QUIET_CLEAN, core-gen)
158 159
	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
	$(Q)$(RM) -r -- $(OUTPUT)feature/
J
Jakub Kicinski 已提交
160

161 162 163 164 165 166
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 已提交
167

168 169
uninstall:
	$(call QUIET_UNINST, bpftool)
170 171
	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
172

J
Jakub Kicinski 已提交
173
doc:
174 175 176 177
	$(call descend,Documentation)

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

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

182 183 184
doc-uninstall:
	$(call descend,Documentation,uninstall)

J
Jakub Kicinski 已提交
185 186
FORCE:

187 188 189 190
zdep:
	@if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi

.PHONY: all FORCE clean install uninstall zdep
191
.PHONY: doc doc-clean doc-install doc-uninstall
J
Jakub Kicinski 已提交
192
.DEFAULT_GOAL := all