netint_build := rel

ifneq ($(KERNELRELEASE),)
obj-m		+= netint.o

ccflags-y   += -I$(src)

ifeq ($(ARCH), arm64)
ifeq (/usr/lib/gcc/aarch64-linux-gnu/7/include, \
		$(wildcard /usr/lib/gcc/aarch64-linux-gnu/7/include))
ccflags-y   += -I/usr/lib/gcc/aarch64-linux-gnu/7/include
endif
endif

netint-y	+= netint_module.o netint_export.o netint_device.o \
			   netint_import.o netint_ioreq.o netint_fence.o \
			   netint_debugfs.o

else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

KERNEL_MAJ = $(shell uname -r | sed -e 's/^\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*.*/\1/')
KERNEL_MIN = $(shell uname -r | sed -e 's/^[0-9][0-9]*\.\([0-9][0-9]*\)\.[0-9][0-9]*.*/\1/')
KERNEL_REV  = $(shell uname -r | sed -e 's/^[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/')

kernel_less_than = $(shell echo test | awk '{ \
if ($(KERNEL_MAJ) < $(1)) { print 1 } else { \
if ($(KERNEL_MAJ) > $(1)) { print 0 } else { \
if ($(KERNEL_MIN) < $(2)) { print 1 } else { \
if ($(KERNEL_MIN) > $(2)) { print 0 } else { \
if ($(KERNEL_REV) < $(3)) { print 1 } else { print 0 } \
}}}}}' )

NETINT_CFLAGS = -DCONFIG_NETINT_DMABUF
ifeq ($(netint_build), eng)
NETINT_CFLAGS += -DNETINT_VERIFY_DMABUF
endif

ifeq ($(a1_build), eng)
NETINT_CFLAGS += -DA1_IMPORT_FUNCTION
endif

default:
ifeq ($(call kernel_less_than,5,10,0),1)
	@echo "Linux kernels older than 5.10.0 not supported"
else
	$(MAKE) -C $(KERNELDIR) M=$(PWD) \
		EXTRA_CFLAGS+="$(NETINT_CFLAGS)" \
		modules
endif

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) \
		EXTRA_CFLAGS+="$(NETINT_CFLAGS)" \
		modules_install
	depmod
endif

clean:
	rm -rf *.o *~ core *.o.ur-safe .depend .*.cmd *.ko *.mod.c *.mod \
		.tmp_versions Module.symvers modules.order .*.o.d
