#
# Copyright (c) 2011-2018, Emulex
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, 
#    this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright notice, 
#    this list of conditions and the following disclaimer in the documentation 
#    and/or other materials provided with the distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
#

KMOD = ocs_fc_lio
OS		= ../common
OCS		= ../../common/ocs
HAL		= ../../common/hal
XPORT		= ../../common/ocs_fc
TARGET		= ../common/ocs_lio
INITIATOR	= ../common/ocs_ml_fc

.PHONY: all clean install remoteinstall installdev distclean

CWD := $(shell pwd)

EXTRA_CFLAGS += -Wno-declaration-after-statement -Wno-unused-function -Wno-unused-variable
EXTRA_CFLAGS += -fdiagnostics-color=always

KBUILD_EXTRA_SYMBOLS += $(wildcard ${SysRootPrefix}/kernel/lio-4.x/Module.symvers)

obj-m := $(KMOD).o

ccflags-y := -I$(src) -I$(src)/$(OCS) -I$(src)/$(HAL) -I$(src)/$(XPORT) -I$(src)/$(OS) -I$(src)/$(INITIATOR) -I$(src)/${TARGET}

$(KMOD)-y := \
	$(OS)/ocs_os.o \
	$(OS)/ocs_lnx.o \
	$(XPORT)/ocs_driver.o \
	$(XPORT)/ocs_debug.o \
	$(XPORT)/ocs_domain.o \
	$(XPORT)/ocs_sport.o \
	$(XPORT)/ocs_node.o \
	$(XPORT)/ocs_elxu.o \
	$(XPORT)/ocs_unsol.o \
	$(XPORT)/ocs_io.o \
	$(XPORT)/ocs_scsi.o \
	$(XPORT)/ocs_els.o \
	$(XPORT)/ocs_fabric.o \
	$(XPORT)/ocs_device.o \
	$(XPORT)/ocs_ddump.o \
	$(XPORT)/ocs_xport.o \
	$(XPORT)/ocs_mgmt.o \
	$(XPORT)/ocs_nvme_stub.o \
	$(XPORT)/spv.o \
	$(HAL)/sli4.o \
	$(HAL)/sli4_fc.o \
	$(HAL)/ocs_sm.o \
	$(HAL)/ocs_hal.o \
	$(HAL)/ocs_hal_rqpair.o \
	$(HAL)/ocs_hal_workaround.o \
	$(HAL)/ocs_hal_queues.o \
	$(OCS)/ocs_textbuf.o \
	$(OCS)/ocs_mgmt_common.o \
	$(OCS)/ocs_ramlog.o \
	$(OCS)/ocs_list.o \
	$(OCS)/ocs_ddumplib.o \
	$(OCS)/ocs_ioctl.o \
	$(OCS)/ocs_ioctl_fc.o \
	$(OCS)/ocs_gendump.o \
	$(OCS)/ocs_ras.o \
	$(OCS)/ocs_recovery.o \
	$(OCS)/ocs_dif.o \
	$(OCS)/t10crc16.o \
	$(OCS)/ocs_pm.o \
	$(OCS)/ocs_cbuf.o \
	$(OCS)/ocs_array.o \
	$(OCS)/ocs_pool.o \
	$(TARGET)/ocs_lio.o \
	$(INITIATOR)/ocs_ml_fc.o \
	$(INITIATOR)/ocs_lnx_xport_fc.o


clean-files := $($(KMOD)-y)

all: kernel

kernel:
	@echo "Building emulex ocs kernel modules"
	mount --bind ${SysRootInclude}/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 ocs_fc_lio.ko $(DESTDIR)/lib/modules

installdev:
	[ -d $(DESTDIR)/usr/kernel/emulex_ocs ] || install -d $(DESTDIR)/usr/kernel/emulex_ocs
	cp Module.symvers $(DESTDIR)/usr/kernel/emulex_ocs/

