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# Overrides for the prepare step libraries.
21HOST_OVERRIDES := AR="$(HOSTAR)" CC="$(HOSTCC)" LD="$(HOSTLD)" ARCH="$(HOSTARCH)" \
22		  CROSS_COMPILE="" EXTRA_CFLAGS="$(HOSTCFLAGS)"
23
24RM      ?= rm
25HOSTCC  ?= gcc
26HOSTLD  ?= ld
27HOSTAR  ?= ar
28CROSS_COMPILE =
29
30OUTPUT ?= $(srctree)/tools/bpf/resolve_btfids/
31
32LIBBPF_SRC := $(srctree)/tools/lib/bpf/
33SUBCMD_SRC := $(srctree)/tools/lib/subcmd/
34
35BPFOBJ     := $(OUTPUT)/libbpf/libbpf.a
36LIBBPF_OUT := $(abspath $(dir $(BPFOBJ)))/
37SUBCMDOBJ  := $(OUTPUT)/libsubcmd/libsubcmd.a
38SUBCMD_OUT := $(abspath $(dir $(SUBCMDOBJ)))/
39
40LIBBPF_DESTDIR := $(LIBBPF_OUT)
41LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)include
42
43SUBCMD_DESTDIR := $(SUBCMD_OUT)
44SUBCMD_INCLUDE := $(SUBCMD_DESTDIR)include
45
46BINARY     := $(OUTPUT)/resolve_btfids
47BINARY_IN  := $(BINARY)-in.o
48
49all: $(BINARY)
50
51prepare: $(BPFOBJ) $(SUBCMDOBJ)
52
53$(OUTPUT) $(OUTPUT)/libsubcmd $(LIBBPF_OUT):
54	$(call msg,MKDIR,,$@)
55	$(Q)mkdir -p $(@)
56
57$(SUBCMDOBJ): fixdep FORCE | $(OUTPUT)/libsubcmd
58	$(Q)$(MAKE) -C $(SUBCMD_SRC) OUTPUT=$(SUBCMD_OUT) \
59		    DESTDIR=$(SUBCMD_DESTDIR) $(HOST_OVERRIDES) prefix= subdir= \
60		    $(abspath $@) install_headers
61
62$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUT)
63	$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC) OUTPUT=$(LIBBPF_OUT)    \
64		    DESTDIR=$(LIBBPF_DESTDIR) $(HOST_OVERRIDES) prefix= subdir= \
65		    $(abspath $@) install_headers
66
67LIBELF_FLAGS := $(shell $(HOSTPKG_CONFIG) libelf --cflags 2>/dev/null)
68LIBELF_LIBS  := $(shell $(HOSTPKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
69
70HOSTCFLAGS_resolve_btfids += -g \
71          -I$(srctree)/tools/include \
72          -I$(srctree)/tools/include/uapi \
73          -I$(LIBBPF_INCLUDE) \
74          -I$(SUBCMD_INCLUDE) \
75          $(LIBELF_FLAGS)
76
77LIBS = $(LIBELF_LIBS) -lz
78
79export srctree OUTPUT HOSTCFLAGS_resolve_btfids Q HOSTCC HOSTLD HOSTAR
80include $(srctree)/tools/build/Makefile.include
81
82$(BINARY_IN): fixdep FORCE prepare | $(OUTPUT)
83	$(Q)$(MAKE) $(build)=resolve_btfids
84
85$(BINARY): $(BPFOBJ) $(SUBCMDOBJ) $(BINARY_IN)
86	$(call msg,LINK,$@)
87	$(Q)$(HOSTCC) $(BINARY_IN) $(KBUILD_HOSTLDFLAGS) -o $@ $(BPFOBJ) $(SUBCMDOBJ) $(LIBS)
88
89clean_objects := $(wildcard $(OUTPUT)/*.o                \
90                            $(OUTPUT)/.*.o.cmd           \
91                            $(OUTPUT)/.*.o.d             \
92                            $(LIBBPF_OUT)                \
93                            $(LIBBPF_DESTDIR)            \
94                            $(SUBCMD_OUT)                \
95                            $(SUBCMD_DESTDIR)            \
96                            $(OUTPUT)/resolve_btfids)
97
98ifneq ($(clean_objects),)
99clean: fixdep-clean
100	$(call msg,CLEAN,$(BINARY))
101	$(Q)$(RM) -rf $(clean_objects)
102else
103clean:
104endif
105
106tags:
107	$(call msg,GEN,,tags)
108	$(Q)ctags -R . $(LIBBPF_SRC) $(SUBCMD_SRC)
109
110FORCE:
111
112.PHONY: all FORCE clean tags prepare
113