TEST_KMOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))

# Build the modules against the kernel tree these tests are part of, or
# its O= build directory, when that tree is configured and built; fall
# back to the running kernel's build tree.
KSRC_TREE := $(abspath $(TEST_KMOD_DIR)/../../../../..)
KOUT := $(or $(O),$(KBUILD_OUTPUT))
ifeq ($(KOUT),)
KDIR_TREE := $(KSRC_TREE)
else ifneq (,$(filter /%,$(KOUT)))
KDIR_TREE := $(abspath $(KOUT))
else
KDIR_TREE := $(abspath $(KSRC_TREE)/$(KOUT))
endif
ifneq (,$(wildcard $(KDIR_TREE)/include/config/auto.conf))
ifneq (,$(wildcard $(KDIR_TREE)/Module.symvers))
KDIR ?= $(KDIR_TREE)
endif
endif
KDIR ?= /lib/modules/$(shell uname -r)/build

ifeq ($(V),1)
Q =
else
Q = @
endif

MODULES = bpf_testmod.ko bpf_test_no_cfi.ko bpf_test_modorder_x.ko \
	bpf_test_modorder_y.ko

$(foreach m,$(MODULES),$(eval obj-m += $(m:.ko=.o)))

CFLAGS_bpf_testmod.o = -I$(src)

all:
	$(Q)$(MAKE) -C $(KDIR) M=$(TEST_KMOD_DIR) modules

clean:
	$(Q)$(MAKE) -C $(KDIR) M=$(TEST_KMOD_DIR) clean
