xref: /linux/tools/bpf/Makefile (revision b40ba139371c2ba4beffe0533c6d85fda9bc932c)
1# SPDX-License-Identifier: GPL-2.0
2include ../scripts/Makefile.include
3
4prefix ?= /usr/local
5
6LEX = flex
7YACC = bison
8MAKE = make
9INSTALL ?= install
10
11CFLAGS += -Wall -O2
12CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi \
13	  -I$(srctree)/tools/include
14CFLAGS += $(EXTRA_CFLAGS)
15
16# This will work when bpf is built in tools env. where srctree
17# isn't set and when invoked from selftests build, where srctree
18# is set to ".". building_out_of_srctree is undefined for in srctree
19# builds
20ifeq ($(srctree),)
21update_srctree := 1
22endif
23ifndef building_out_of_srctree
24update_srctree := 1
25endif
26ifeq ($(update_srctree),1)
27srctree := $(patsubst %/,%,$(dir $(CURDIR)))
28srctree := $(patsubst %/,%,$(dir $(srctree)))
29endif
30
31FEATURE_USER = .bpf
32FEATURE_TESTS = libbfd disassembler-four-args disassembler-init-styled
33FEATURE_DISPLAY = libbfd
34
35check_feat := 1
36NON_CHECK_FEAT_TARGETS := clean bpftool_clean resolve_btfids_clean
37ifdef MAKECMDGOALS
38ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
39  check_feat := 0
40endif
41endif
42
43ifeq ($(check_feat),1)
44ifeq ($(FEATURES_DUMP),)
45include $(srctree)/tools/build/Makefile.feature
46else
47include $(FEATURES_DUMP)
48endif
49endif
50
51ifeq ($(feature-disassembler-four-args), 1)
52CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
53endif
54ifeq ($(feature-disassembler-init-styled), 1)
55CFLAGS += -DDISASM_INIT_STYLED
56endif
57
58$(OUTPUT)%.yacc.c: $(srctree)/tools/bpf/%.y
59	$(QUIET_BISON)$(YACC) -o $@ -d $<
60
61$(OUTPUT)%.lex.c: $(srctree)/tools/bpf/%.l
62	$(QUIET_FLEX)$(LEX) -o $@ $<
63
64$(OUTPUT)%.o: $(srctree)/tools/bpf/%.c
65	$(QUIET_CC)$(CC) $(CFLAGS) -c -o $@ $<
66
67$(OUTPUT)%.yacc.o: $(OUTPUT)%.yacc.c
68	$(QUIET_CC)$(CC) $(CFLAGS) -c -o $@ $<
69$(OUTPUT)%.lex.o: $(OUTPUT)%.lex.c
70	$(QUIET_CC)$(CC) $(CFLAGS) -c -o $@ $<
71
72PROGS = $(OUTPUT)bpf_jit_disasm $(OUTPUT)bpf_dbg $(OUTPUT)bpf_asm
73
74all: $(PROGS) bpftool
75
76$(OUTPUT)bpf_jit_disasm: CFLAGS += -DPACKAGE='bpf_jit_disasm'
77$(OUTPUT)bpf_jit_disasm: $(OUTPUT)bpf_jit_disasm.o
78	$(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $^ -lopcodes -lbfd -ldl
79
80$(OUTPUT)bpf_dbg: $(OUTPUT)bpf_dbg.o
81	$(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $^ -lreadline
82
83$(OUTPUT)bpf_asm: $(OUTPUT)bpf_asm.o $(OUTPUT)bpf_exp.yacc.o $(OUTPUT)bpf_exp.lex.o
84	$(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $^
85
86$(OUTPUT)bpf_exp.lex.c: $(OUTPUT)bpf_exp.yacc.c
87$(OUTPUT)bpf_exp.yacc.o: $(OUTPUT)bpf_exp.yacc.c
88$(OUTPUT)bpf_exp.lex.o: $(OUTPUT)bpf_exp.lex.c
89
90clean: bpftool_clean resolve_btfids_clean
91	$(call QUIET_CLEAN, bpf-progs)
92	$(Q)$(RM) -r -- $(OUTPUT)*.o $(OUTPUT)bpf_jit_disasm $(OUTPUT)bpf_dbg \
93	       $(OUTPUT)bpf_asm $(OUTPUT)bpf_exp.yacc.* $(OUTPUT)bpf_exp.lex.*
94	$(call QUIET_CLEAN, core-gen)
95	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpf
96	$(Q)$(RM) -r -- $(OUTPUT)feature
97
98install: $(PROGS) bpftool_install
99	$(call QUIET_INSTALL, bpf_jit_disasm)
100	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/bin
101	$(Q)$(INSTALL) $(OUTPUT)bpf_jit_disasm $(DESTDIR)$(prefix)/bin/bpf_jit_disasm
102	$(call QUIET_INSTALL, bpf_dbg)
103	$(Q)$(INSTALL) $(OUTPUT)bpf_dbg $(DESTDIR)$(prefix)/bin/bpf_dbg
104	$(call QUIET_INSTALL, bpf_asm)
105	$(Q)$(INSTALL) $(OUTPUT)bpf_asm $(DESTDIR)$(prefix)/bin/bpf_asm
106
107bpftool:
108	$(call descend,bpftool)
109
110bpftool_install:
111	$(call descend,bpftool,install)
112
113bpftool_clean:
114	$(call descend,bpftool,clean)
115
116resolve_btfids:
117	$(call descend,resolve_btfids)
118
119resolve_btfids_clean:
120	$(call descend,resolve_btfids,clean)
121
122.PHONY: all install clean bpftool bpftool_install bpftool_clean \
123	resolve_btfids resolve_btfids_clean
124