CWD := $(shell pwd)

EXTRA_CFLAGS += -Wno-declaration-after-statement -Wno-unused-function -Wno-unused-variable
EXTRA_CFLAGS += -fdiagnostics-color=always
EXTRA_CFLAGS += -I$(CWD)/include -D${PLATFORM_FAMILY}

all: kernel

kernel:
	@echo "Building kernel modules"
	mount --bind ./include/target $(KSRC)/include/target; \
	$(MAKE) EXTRA_CFLAGS="${EXTRA_CFLAGS}" ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -C $(KSRC) M=$(CWD) modules; \
	status=$$?; \
	umount $(KSRC)/include/target; \
	exit $$status

distclean:
	rm -f `find . -name \*.o`; rm -f `find . -name .\*.o.cmd`;   rm -f `find . -name .\*.o.flags`; rm -f Module.symvers;

remoteinstall:
	[ -d $(DESTDIR)/lib/modules ] || install -d $(DESTDIR)/lib/modules
	install -m600 target_core_mod.ko $(DESTDIR)/lib/modules
	install -m600 target_core_file.ko $(DESTDIR)/lib/modules
	install -m600 target_core_multi_file.ko $(DESTDIR)/lib/modules
	install -m600 target_core_iblock.ko $(DESTDIR)/lib/modules
	install -m600 target_core_ep.ko $(DESTDIR)/lib/modules
ifeq ($(SYNO_KERNEL_VERSION), $(filter $(SYNO_KERNEL_VERSION), linux-4.4.x linux-4.4.x-glk))
	install -m600 target_core_user.ko $(DESTDIR)/lib/modules
endif
	install -m600 lio-extent/syno_extent_pool.ko $(DESTDIR)/lib/modules
	install -m600 lio-rodsp/rodsp_ep.ko $(DESTDIR)/lib/modules
	install -m600 vhost/vhost.ko $(DESTDIR)/lib/modules
ifeq ($(SYNO_KERNEL_VERSION), $(filter $(SYNO_KERNEL_VERSION), linux-4.4.x linux-4.4.x-glk))
	install -m600 vhost/vhost_net.ko $(DESTDIR)/lib/modules
endif
	install -m600 vhost/vhost_scsi.ko $(DESTDIR)/lib/modules
	install -m600 iscsi/iscsi_target_mod.ko $(DESTDIR)/lib/modules
	install -m600 loopback/tcm_loop.ko $(DESTDIR)/lib/modules
ifeq ($(SYNO_KERNEL_VERSION), $(filter $(SYNO_KERNEL_VERSION), linux-4.4.x linux-4.4.x-glk))
	install -m600 tcm_fc/tcm_fc.ko $(DESTDIR)/lib/modules
endif
	[ -d ${DESTDIR}/lib/udev/rules.d ] || install -d ${DESTDIR}/lib/udev/rules.d
	install -m 755 udev/50-iscsi.rules ${DESTDIR}/lib/udev/rules.d
	[ -d ${DESTDIR}/lib/udev/script ] || install -d ${DESTDIR}/lib/udev/script
	install -m 755 udev/iscsi-add.sh ${DESTDIR}/lib/udev/script
	install -m 755 udev/iscsi-remove.sh ${DESTDIR}/lib/udev/script

installdev:
	[ -d $(DESTDIR)/usr/include ] || install -d $(DESTDIR)/usr/include
	cp -rf include/target $(DESTDIR)/usr/include/
	[ -d $(DESTDIR)/usr/include/lio-extent ] || install -d $(DESTDIR)/usr/include/lio-extent
	cp lio-extent/extent_file_format.h $(DESTDIR)/usr/include/lio-extent/
ifneq ($(wildcard Module.symvers),)
		[ -d $(DESTDIR)/usr/kernel/lio-4.x ] || install -d $(DESTDIR)/usr/kernel/lio-4.x
		cp Module.symvers $(DESTDIR)/usr/kernel/lio-4.x/
endif

CONFIG_TARGET_CORE=m
CONFIG_TCM_IBLOCK=m
CONFIG_TCM_FILEIO=m
CONFIG_TCM_MULTI_FILEIO=m
CONFIG_TCM_EPIO=m
ifeq ($(SYNO_KERNEL_VERSION), $(filter $(SYNO_KERNEL_VERSION), linux-4.4.x linux-4.4.x-glk))
CONFIG_TCM_USER=m
endif
export CONFIG_ISCSI_TARGET=m
export CONFIG_LOOPBACK_TARGET=m
export CONFIG_VHOST=m
export CONFIG_VHOST_NET=m
export CONFIG_VHOST_SCSI=m
ifeq ($(SYNO_KERNEL_VERSION), $(filter $(SYNO_KERNEL_VERSION), linux-4.4.x linux-4.4.x-glk))
export CONFIG_TCM_FC=m
endif

obj-m += lio-extent/
obj-m += lio-rodsp/

target_core_mod-y		:= target_core_configfs.o \
	target_core_device.o \
	target_core_fabric_configfs.o \
	target_core_fabric_lib.o \
	target_core_hba.o \
	target_core_pr.o \
	target_core_alua.o \
	target_core_tmr.o \
	target_core_tpg.o \
	target_core_transport.o \
	target_core_sbc.o \
	target_core_spc.o \
	target_core_ua.o \
	target_core_rd.o \
	target_core_stat.o \
	target_core_xcopy.o \
	target_core_lunbackup.o \
	target_core_segment_lock.o \
	target_core_cache_lock.o \
	target_core_odx.o \
	target_core_iostat_update.o \
	percpu_ida.o \
	percpu-refcount.o \
	syno_backport.o \
	syno_target_file.o \
	target_core_pseudo.o

obj-$(CONFIG_TARGET_CORE)	+= target_core_mod.o

# Subsystem modules
obj-$(CONFIG_TCM_IBLOCK) += target_core_iblock.o
obj-$(CONFIG_TCM_FILEIO) += target_core_file.o
obj-$(CONFIG_TCM_MULTI_FILEIO) += target_core_multi_file.o
obj-$(CONFIG_TCM_EPIO) += target_core_ep.o
ifeq ($(SYNO_KERNEL_VERSION), $(filter $(SYNO_KERNEL_VERSION), linux-4.4.x linux-4.4.x-glk))
obj-$(CONFIG_TCM_USER) += target_core_user.o
endif

# Fabric modules
obj-$(CONFIG_ISCSI_TARGET) += iscsi/
obj-$(CONFIG_VHOST)	+= vhost/
obj-$(CONFIG_LOOPBACK_TARGET)	+= loopback/
obj-$(CONFIG_TCM_FC) += tcm_fc/
