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