OUTPUT := .output
JQ ?= jq
SED ?= sed

# ---------------------------------------------------------------------------
# LLVM tools from system PATH
# ---------------------------------------------------------------------------
CLANG        ?= clang
OPT          ?= opt
LLC          ?= llc
LLVM_PROFDATA ?= llvm-profdata
LLVM_COV     ?= llvm-cov

# ---------------------------------------------------------------------------
# Locate libBPFCov.so: explicit > system lib > build tree
# ---------------------------------------------------------------------------
ifndef BPFCOVLIB_DIR
  ifneq ($(wildcard /usr/lib64/libBPFCov.so),)
    BPFCOVLIB_DIR := /usr/lib64
  else ifneq ($(wildcard /usr/lib/libBPFCov.so),)
    BPFCOVLIB_DIR := /usr/lib
  else
    BPFCOVLIB_DIR := $(abspath ../../build/lib)
  endif
endif

ARCH := $(shell uname -m | $(SED) 's/x86_64/x86/' | $(SED) 's/aarch64/arm64/' | $(SED) 's/ppc64le/powerpc/' | $(SED) 's/mips.*/mips/' | $(SED) 's/loongarch64/loongarch/')
BPFTOOL ?= $(shell command -v bpftool)
ifeq ($(BPFTOOL),)
$(error "bpftool not found in PATH; install bpftool (with skeletons support)")
endif
BPFT_CHECK := $(OUTPUT)/.bpftool-checked
BPFCOV_CLI ?= $(or $(shell command -v kybpfcov 2>/dev/null),$(abspath ../../cli/kybpfcov))
USE_SYSTEM_LIBBPF ?= 1
LIBBPF_DIR := $(abspath ../libbpf)
LIBBPF_SRC := $(abspath $(LIBBPF_DIR)/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
VMLINUX := $(abspath $(OUTPUT)/vmlinux/vmlinux.h)
INCLUDES := -I$(LIBBPF_DIR)/include/uapi -I$(dir $(VMLINUX))
CLANG_BPF_SYS_INCLUDES = $(shell $(CLANG) -v -E - </dev/null 2>&1 \
	| $(SED) -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')

# List your examples here
EXAMPLES = raw_enter fentry lsm \
	kprobe_write kretprobe_read tp_openat tp_sched \
	fentry_mkdir fexit_unlink tp_btf_exec raw_tp_exec \
	xdp_pass fentry_rename

ifeq ($(V),1)
	Q =
	msg =
else
	Q = @
	msg = @printf '  %-8s %s%s\n'					\
		      "$(1)"						\
		      "$(patsubst $(abspath $(OUTPUT))/%,%,$(2))"	\
		      "$(if $(3), $(3))";
	MAKEFLAGS += --no-print-directory
endif

.PHONY: all
all: $(EXAMPLES)

.PHONY: cov
cov: $(patsubst %,cov/%,$(EXAMPLES))

.PHONY: distclean
distclean:
	$(call msg,DISTCLEAN)
	$(Q)rm -rf $(OUTPUT)

.PHONY: clean
clean:
	$(call msg,CLEAN)
	$(Q)rm -rf $(OUTPUT)/*.{o,bpf.o,skel.h}
	$(Q)rm -rf $(OUTPUT)/cov
	$(Q)rm -rf $(patsubst %,$(OUTPUT)/%,$(EXAMPLES))

# Create output directory
$(OUTPUT) $(OUTPUT)/cov $(OUTPUT)/libbpf:
	$(call msg,MKDIR,$@)
	$(Q)mkdir -p $@

ifeq ($(USE_SYSTEM_LIBBPF),1)
LIBBPF_PKGCONF ?= libbpf
LIBBPF_CFLAGS := $(shell pkg-config --cflags $(LIBBPF_PKGCONF))
LIBBPF_LDFLAGS := $(shell pkg-config --libs $(LIBBPF_PKGCONF))
# Fallback if pkg-config is missing or returns nothing
ifeq ($(strip $(LIBBPF_LDFLAGS)),)
LIBBPF_LDFLAGS := -lbpf
endif
LIBBPF_OBJ :=
INCLUDES := $(INCLUDES) $(LIBBPF_CFLAGS)
else
# Build bundled libbpf
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
	$(call msg,LIB,$@)
	$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
		OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \
		INCLUDEDIR= LIBDIR= UAPIDIR= \
		install
INCLUDES := $(INCLUDES) -I$(LIBBPF_SRC)
endif

# Check bpftool skeletons capability once and memoize
$(BPFT_CHECK): | $(OUTPUT)
	$(call msg,BPFTOOL,$(BPFTOOL))
	$(Q)if command -v $(JQ) >/dev/null 2>&1; then \
		$(BPFTOOL) version -j | $(JQ) -e '.features.skeletons' >/dev/null; \
	else \
		$(BPFTOOL) version | grep -qi skeletons || \
			(echo "bpftool with skeletons feature is required; install jq for exact check or rebuild bpftool (tools/bpf/bpftool) with libbpf support" && false); \
	fi
	$(Q)touch $@

# Generate vmlinux.h
$(VMLINUX): $(BPFT_CHECK) /sys/kernel/btf/vmlinux
	$(call msg,MKDIR,$(dir $@))
	$(Q)mkdir -p $(dir $@)
	$(call msg,VMLINUX,$@)
	$(Q)$(BPFTOOL) btf dump file $(word 2,$^) format c > $@

# Compile the eBPF example as is
$(OUTPUT)/%.bpf.o: %.bpf.c $(wildcard %.h) $(VMLINUX) $(LIBBPF_OBJ) | $(OUTPUT)
	$(call msg,OBJ,$@)
	$(Q)$(CLANG) -g -O2 \
		-target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \
		-c $(filter %.c,$^) \
		-o $@

# Obtain the LLVM IR instrumenting profiling and coverage mappings
$(OUTPUT)/cov/%.bpf.ll: %.bpf.c $(wildcard %.h) $(VMLINUX) $(LIBBPF_OBJ) | $(OUTPUT)/cov
	$(call msg,LL,$@)
	$(Q)$(CLANG) -g -O2 \
		-target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT)/cov $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \
		-fprofile-instr-generate -fcoverage-mapping \
		-emit-llvm -S \
		-c $(filter %.c,$^) \
		-o $@

# Create the object file for coverage (not for loading, only for llvm-cov)
$(OUTPUT)/cov/%.bpf.obj: $(OUTPUT)/cov/%.bpf.ll | $(OUTPUT)/cov
	$(call msg,ARCHIVE,$@)
	$(Q)$(OPT) \
		-load-pass-plugin $(BPFCOVLIB_DIR)/libBPFCov.so -passes="bpf-cov-strip-only" $< \
		| $(LLC) -march=bpf -filetype=obj -o $@

# Make the LLVM IR valid for eBPF
$(OUTPUT)/cov/%.bpf.cov.ll: $(OUTPUT)/cov/%.bpf.ll | $(OUTPUT)/cov
	$(call msg,COV,$@)
	$(Q)$(OPT) \
		-load-pass-plugin $(BPFCOVLIB_DIR)/libBPFCov.so -passes="bpf-cov" \
		-S $< -o $@

# Build the instrumented ELF
$(patsubst %,$(OUTPUT)/cov/%.bpf.o,$(EXAMPLES)): %.bpf.o: %.bpf.cov.ll %.bpf.obj
$(OUTPUT)/cov/%.bpf.o: $(OUTPUT)/cov/%.bpf.cov.ll | $(OUTPUT)/cov
	$(call msg,OBJ,$@)
	$(Q)$(LLC) -march=bpf -filetype=obj -o $@ $<

# Generate the skeleton for the eBPF example as is
$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o $(BPFT_CHECK) | $(OUTPUT)
	$(call msg,SKEL,$@)
	$(Q)$(BPFTOOL) gen skeleton $< name $* > $@

# Generate the skeleton for the instrumented ELF
$(OUTPUT)/cov/%.skel.h: $(OUTPUT)/cov/%.bpf.o $(BPFT_CHECK) | $(OUTPUT)/cov
	$(call msg,SKEL,$@)
	$(Q)$(BPFTOOL) gen skeleton $< name $* > $@

# Build userspace code
$(patsubst %,$(OUTPUT)/%.o,$(EXAMPLES)): %.o: %.skel.h

# Build the eBPF application as is
$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT)
	$(call msg,CC,$@)
	$(Q)$(CC) -g -Wall -I$(OUTPUT) $(INCLUDES) -c $(filter %.c,$^) -o $@

# Build userspace code using the instrumented skeleton
$(patsubst %,$(OUTPUT)/cov/%.o,$(EXAMPLES)): %.o: %.skel.h

# Build the instrumented eBPF application
$(OUTPUT)/cov/%.o: %.c $(wildcard %.h) | $(OUTPUT)/cov
	$(call msg,CC,$@)
	$(Q)$(CC) -g -Wall -I$(OUTPUT)/cov $(INCLUDES) -c $(filter %.c,$^) -o $@

# Build the binary as is

LINK_LIBS := -lelf -lz $(LIBBPF_LDFLAGS)
ifeq ($(USE_SYSTEM_LIBBPF),0)
LINK_LIBS := -lelf -lz
endif

%: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)
	$(call msg,BIN,$(OUTPUT)/$@)
	$(Q)$(CC) -g -Wall $^ $(LINK_LIBS) -o $(OUTPUT)/$@

# Build the instrumented eBPF binary
cov/%: $(OUTPUT)/cov/%.o $(LIBBPF_OBJ) | $(OUTPUT)/cov
	$(call msg,BIN,$(OUTPUT)/$@)
	$(Q)$(CC) -g -Wall $^ $(LINK_LIBS) -o $(OUTPUT)/$@

# ---------------------------------------------------------------------------
# make test - run all examples through the full coverage pipeline and validate
# ---------------------------------------------------------------------------
.PHONY: test
test: cov
	@echo "=== kybpfcov coverage test suite ==="
	@pass=0; fail=0; skip=0; \
	for ex in $(EXAMPLES); do \
	    printf "--- %-20s " "$$ex"; \
	    $(BPFCOV_CLI) run $(OUTPUT)/cov/$$ex 2>/dev/null; \
	    rc=$$?; \
	    if [ $$rc -ne 0 ]; then \
	        echo "SKIP (run failed, rc=$$rc)"; skip=$$((skip+1)); \
	        continue; \
	    fi; \
	    $(BPFCOV_CLI) gen $(OUTPUT)/cov/$$ex 2>/dev/null; \
	    if [ ! -f $(OUTPUT)/cov/$$ex.profraw ]; then \
	        echo "SKIP (no profraw)"; skip=$$((skip+1)); \
	        continue; \
	    fi; \
	    $(LLVM_PROFDATA) merge -sparse $(OUTPUT)/cov/$$ex.profraw \
	        -o $(OUTPUT)/cov/$$ex.profdata 2>/dev/null; \
	    if [ ! -f $(OUTPUT)/cov/$$ex.profdata ]; then \
	        echo "FAIL (profdata merge failed)"; fail=$$((fail+1)); \
	        continue; \
	    fi; \
	    report=$$($(LLVM_COV) report \
	        -instr-profile=$(OUTPUT)/cov/$$ex.profdata \
	        $(OUTPUT)/cov/$$ex.bpf.obj 2>/dev/null | tail -1); \
	    regions=$$(echo "$$report" | awk '{print $$2}'); \
	    pct=$$(echo "$$report" | awk '{print $$4}'); \
	    if [ -n "$$regions" ] && [ "$$regions" -gt 0 ] 2>/dev/null; then \
	        echo "PASS (regions=$$regions, coverage=$$pct)"; pass=$$((pass+1)); \
	    else \
	        echo "FAIL (no coverage regions hit)"; fail=$$((fail+1)); \
	    fi; \
	done; \
	echo "=== Results: $$pass passed, $$fail failed, $$skip skipped ==="; \
	[ $$fail -eq 0 ]

# Delete failed targets
.DELETE_ON_ERROR:

# Keep intermediate (.bpf.o, etc.) targets
.SECONDARY:
