1# SPDX-License-Identifier: GPL-2.0-only
2include ../../scripts/Makefile.include
3include ../../scripts/Makefile.arch
4
5srctree := $(abspath $(CURDIR)/../../../)
6
7ifeq ($(V),1)
8  Q =
9  msg =
10else
11  Q = @
12  ifeq ($(silent),1)
13    msg =
14  else
15    msg = @printf '  %-8s %s%s\n' "$(1)" "$(notdir $(2))" "$(if $(3), $(3))";
16  endif
17  MAKEFLAGS=--no-print-directory
18endif
19
20# always use the host compiler
21AR       = $(HOSTAR)
22CC       = $(HOSTCC)
23LD       = $(HOSTLD)
24ARCH     = $(HOSTARCH)
25RM      ?= rm
26CROSS_COMPILE =
27
28OUTPUT ?= $(srctree)/tools/bpf/resolve_btfids/
29
30LIBBPF_SRC := $(srctree)/tools/lib/bpf/
31SUBCMD_SRC := $(srctree)/tools/lib/subcmd/
32
33BPFOBJ     := $(OUTPUT)/libbpf/libbpf.a
34LIBBPF_OUT := $(abspath $(dir $(BPFOBJ)))/
35SUBCMDOBJ  := $(OUTPUT)/libsubcmd/libsubcmd.a
36
37LIBBPF_DESTDIR := $(LIBBPF_OUT)
38LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)include
39
40BINARY     := $(OUTPUT)/resolve_btfids
41BINARY_IN  := $(BINARY)-in.o
42
43all: $(BINARY)
44
45$(OUTPUT) $(OUTPUT)/libsubcmd $(LIBBPF_OUT):
46	$(call msg,MKDIR,,$@)
47	$(Q)mkdir -p $(@)
48
49$(SUBCMDOBJ): fixdep FORCE | $(OUTPUT)/libsubcmd
50	$(Q)$(MAKE) -C $(SUBCMD_SRC) OUTPUT=$(abspath $(dir $@))/ $(abspath $@)
51
52$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUT)
53	$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC) OUTPUT=$(LIBBPF_OUT)    \
54		    DESTDIR=$(LIBBPF_DESTDIR) prefix=			       \
55		    $(abspath $@) install_headers
56
57CFLAGS := -g \
58          -I$(srctree)/tools/include \
59          -I$(srctree)/tools/include/uapi \
60          -I$(LIBBPF_INCLUDE) \
61          -I$(SUBCMD_SRC)
62
63LIBS = -lelf -lz
64
65export srctree OUTPUT CFLAGS Q
66include $(srctree)/tools/build/Makefile.include
67
68$(BINARY_IN): $(BPFOBJ) fixdep FORCE | $(OUTPUT)
69	$(Q)$(MAKE) $(build)=resolve_btfids
70
71$(BINARY): $(BPFOBJ) $(SUBCMDOBJ) $(BINARY_IN)
72	$(call msg,LINK,$@)
73	$(Q)$(CC) $(BINARY_IN) $(LDFLAGS) -o $@ $(BPFOBJ) $(SUBCMDOBJ) $(LIBS)
74
75clean_objects := $(wildcard $(OUTPUT)/*.o                \
76                            $(OUTPUT)/.*.o.cmd           \
77                            $(OUTPUT)/.*.o.d             \
78                            $(LIBBPF_OUT)                \
79                            $(LIBBPF_DESTDIR)            \
80                            $(OUTPUT)/libsubcmd          \
81                            $(OUTPUT)/resolve_btfids)
82
83ifneq ($(clean_objects),)
84clean: fixdep-clean
85	$(call msg,CLEAN,$(BINARY))
86	$(Q)$(RM) -rf $(clean_objects)
87else
88clean:
89endif
90
91tags:
92	$(call msg,GEN,,tags)
93	$(Q)ctags -R . $(LIBBPF_SRC) $(SUBCMD_SRC)
94
95FORCE:
96
97.PHONY: all FORCE clean tags
98