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