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