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