blob: 39bc6f0f4f0bb839ade8b6962bff515729f318ea [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001# SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002include ../../scripts/Makefile.include
3include ../../scripts/utilities.mak
4
5ifeq ($(srctree),)
6srctree := $(patsubst %/,%,$(dir $(CURDIR)))
7srctree := $(patsubst %/,%,$(dir $(srctree)))
8srctree := $(patsubst %/,%,$(dir $(srctree)))
9endif
10
11ifeq ($(V),1)
12 Q =
13else
14 Q = @
15endif
16
17BPF_DIR = $(srctree)/tools/lib/bpf/
18
19ifneq ($(OUTPUT),)
David Brazdil0f672f62019-12-10 10:32:29 +000020 LIBBPF_OUTPUT = $(OUTPUT)/libbpf/
21 LIBBPF_PATH = $(LIBBPF_OUTPUT)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022else
David Brazdil0f672f62019-12-10 10:32:29 +000023 LIBBPF_PATH = $(BPF_DIR)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024endif
25
David Brazdil0f672f62019-12-10 10:32:29 +000026LIBBPF = $(LIBBPF_PATH)libbpf.a
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027
David Brazdil0f672f62019-12-10 10:32:29 +000028BPFTOOL_VERSION := $(shell make -rR --no-print-directory -sC ../../.. kernelversion)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029
30$(LIBBPF): FORCE
David Brazdil0f672f62019-12-10 10:32:29 +000031 $(if $(LIBBPF_OUTPUT),@mkdir -p $(LIBBPF_OUTPUT))
32 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033
34$(LIBBPF)-clean:
35 $(call QUIET_CLEAN, libbpf)
David Brazdil0f672f62019-12-10 10:32:29 +000036 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037
38prefix ?= /usr/local
39bash_compdir ?= /usr/share/bash-completion/completions
40
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041CFLAGS += -O2
David Brazdil0f672f62019-12-10 10:32:29 +000042CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
43CFLAGS += $(filter-out -Wswitch-enum,$(EXTRA_WARNINGS))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
45 -I$(srctree)/kernel/bpf/ \
46 -I$(srctree)/tools/include \
47 -I$(srctree)/tools/include/uapi \
48 -I$(srctree)/tools/lib/bpf \
49 -I$(srctree)/tools/perf
50CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
David Brazdil0f672f62019-12-10 10:32:29 +000051ifneq ($(EXTRA_CFLAGS),)
52CFLAGS += $(EXTRA_CFLAGS)
53endif
54ifneq ($(EXTRA_LDFLAGS),)
55LDFLAGS += $(EXTRA_LDFLAGS)
56endif
57
58LIBS = $(LIBBPF) -lelf -lz
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059
60INSTALL ?= install
61RM ?= rm -f
62
63FEATURE_USER = .bpftool
David Brazdil0f672f62019-12-10 10:32:29 +000064FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib
65FEATURE_DISPLAY = libbfd disassembler-four-args zlib
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066
67check_feat := 1
68NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
69ifdef MAKECMDGOALS
70ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
71 check_feat := 0
72endif
73endif
74
75ifeq ($(check_feat),1)
76ifeq ($(FEATURES_DUMP),)
77include $(srctree)/tools/build/Makefile.feature
78else
79include $(FEATURES_DUMP)
80endif
81endif
82
83ifeq ($(feature-disassembler-four-args), 1)
84CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
85endif
86
87ifeq ($(feature-reallocarray), 0)
88CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
89endif
90
91include $(wildcard $(OUTPUT)*.d)
92
93all: $(OUTPUT)bpftool
94
David Brazdil0f672f62019-12-10 10:32:29 +000095BFD_SRCS = jit_disasm.c
96
97SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
98
99ifeq ($(feature-libbfd),1)
100 LIBS += -lbfd -ldl -lopcodes
101else ifeq ($(feature-libbfd-liberty),1)
102 LIBS += -lbfd -ldl -lopcodes -liberty
103else ifeq ($(feature-libbfd-liberty-z),1)
104 LIBS += -lbfd -ldl -lopcodes -liberty -lz
105endif
106
107ifneq ($(filter -lbfd,$(LIBS)),)
108CFLAGS += -DHAVE_LIBBFD_SUPPORT
109SRCS += $(BFD_SRCS)
110endif
111
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
113
114$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
115 $(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
116
David Brazdil0f672f62019-12-10 10:32:29 +0000117$(OUTPUT)feature.o: | zdep
118
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
David Brazdil0f672f62019-12-10 10:32:29 +0000120 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121
122$(OUTPUT)%.o: %.c
123 $(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
124
125clean: $(LIBBPF)-clean
126 $(call QUIET_CLEAN, bpftool)
David Brazdil0f672f62019-12-10 10:32:29 +0000127 $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
128 $(Q)$(RM) -r -- $(OUTPUT)libbpf/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129 $(call QUIET_CLEAN, core-gen)
David Brazdil0f672f62019-12-10 10:32:29 +0000130 $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
131 $(Q)$(RM) -r -- $(OUTPUT)feature/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132
133install: $(OUTPUT)bpftool
134 $(call QUIET_INSTALL, bpftool)
135 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
136 $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
137 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
138 $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
139
140uninstall:
141 $(call QUIET_UNINST, bpftool)
David Brazdil0f672f62019-12-10 10:32:29 +0000142 $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
143 $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000144
145doc:
146 $(call descend,Documentation)
147
148doc-clean:
149 $(call descend,Documentation,clean)
150
151doc-install:
152 $(call descend,Documentation,install)
153
154doc-uninstall:
155 $(call descend,Documentation,uninstall)
156
157FORCE:
158
David Brazdil0f672f62019-12-10 10:32:29 +0000159zdep:
160 @if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi
161
162.PHONY: all FORCE clean install uninstall zdep
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163.PHONY: doc doc-clean doc-install doc-uninstall
164.DEFAULT_GOAL := all