xref: /linux/tools/bpf/bpftool/Makefile (revision ec8f24b7faaf3d4799a7c3f4c1b87f6b02778ad1)
1# SPDX-License-Identifier: GPL-2.0-only
2include ../../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),)
20  BPF_PATH = $(OUTPUT)
21else
22  BPF_PATH = $(BPF_DIR)
23endif
24
25LIBBPF = $(BPF_PATH)libbpf.a
26
27BPFTOOL_VERSION := $(shell make --no-print-directory -sC ../../.. kernelversion)
28
29$(LIBBPF): FORCE
30	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) $(OUTPUT)libbpf.a
31
32$(LIBBPF)-clean:
33	$(call QUIET_CLEAN, libbpf)
34	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) clean >/dev/null
35
36prefix ?= /usr/local
37bash_compdir ?= /usr/share/bash-completion/completions
38
39CFLAGS += -O2
40CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers
41CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
42	-I$(srctree)/kernel/bpf/ \
43	-I$(srctree)/tools/include \
44	-I$(srctree)/tools/include/uapi \
45	-I$(srctree)/tools/lib/bpf \
46	-I$(srctree)/tools/perf
47CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
48ifneq ($(EXTRA_CFLAGS),)
49CFLAGS += $(EXTRA_CFLAGS)
50endif
51ifneq ($(EXTRA_LDFLAGS),)
52LDFLAGS += $(EXTRA_LDFLAGS)
53endif
54
55LIBS = -lelf $(LIBBPF)
56
57INSTALL ?= install
58RM ?= rm -f
59
60FEATURE_USER = .bpftool
61FEATURE_TESTS = libbfd disassembler-four-args reallocarray
62FEATURE_DISPLAY = libbfd disassembler-four-args
63
64check_feat := 1
65NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
66ifdef MAKECMDGOALS
67ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
68  check_feat := 0
69endif
70endif
71
72ifeq ($(check_feat),1)
73ifeq ($(FEATURES_DUMP),)
74include $(srctree)/tools/build/Makefile.feature
75else
76include $(FEATURES_DUMP)
77endif
78endif
79
80ifeq ($(feature-disassembler-four-args), 1)
81CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
82endif
83
84ifeq ($(feature-reallocarray), 0)
85CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
86endif
87
88include $(wildcard $(OUTPUT)*.d)
89
90all: $(OUTPUT)bpftool
91
92BFD_SRCS = jit_disasm.c
93
94SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
95
96ifeq ($(feature-libbfd),1)
97  LIBS += -lbfd -ldl -lopcodes
98else ifeq ($(feature-libbfd-liberty),1)
99  LIBS += -lbfd -ldl -lopcodes -liberty
100else ifeq ($(feature-libbfd-liberty-z),1)
101  LIBS += -lbfd -ldl -lopcodes -liberty -lz
102endif
103
104ifneq ($(filter -lbfd,$(LIBS)),)
105CFLAGS += -DHAVE_LIBBFD_SUPPORT
106SRCS += $(BFD_SRCS)
107endif
108
109OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
110
111$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
112	$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
113
114$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
115	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
116
117$(OUTPUT)%.o: %.c
118	$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
119
120clean: $(LIBBPF)-clean
121	$(call QUIET_CLEAN, bpftool)
122	$(Q)$(RM) $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
123	$(call QUIET_CLEAN, core-gen)
124	$(Q)$(RM) $(OUTPUT)FEATURE-DUMP.bpftool
125
126install: $(OUTPUT)bpftool
127	$(call QUIET_INSTALL, bpftool)
128	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
129	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
130	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
131	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
132
133uninstall:
134	$(call QUIET_UNINST, bpftool)
135	$(Q)$(RM) $(DESTDIR)$(prefix)/sbin/bpftool
136	$(Q)$(RM) $(DESTDIR)$(bash_compdir)/bpftool
137
138doc:
139	$(call descend,Documentation)
140
141doc-clean:
142	$(call descend,Documentation,clean)
143
144doc-install:
145	$(call descend,Documentation,install)
146
147doc-uninstall:
148	$(call descend,Documentation,uninstall)
149
150FORCE:
151
152.PHONY: all FORCE clean install uninstall
153.PHONY: doc doc-clean doc-install doc-uninstall
154.DEFAULT_GOAL := all
155