#
# Copyright (C) 2007 InterOperability Lab (IOL)
# University of New Hampshire (UNH)
# Durham, NH 03824
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
# The name of IOL and/or UNH may not be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# ----
#
# This Makefile builds the UNH user-space iscsi applications.
#
# Created 04/19/2007 -- Ethan Burns <eaburns@iol.unh.edu>
#

# For some reason ld doesn't seem to work
LD=gcc

#
# Uncomment the following line only if compiling in iSER/RDMA Support
#
SUBDIRS+=datamover

#
# Debugging support
#
CFLAGS+=-g

#
# Turn on debugging support.
#
CFLAGS+=-DCONFIG_ISCSI_DEBUG
CFLAGS+=-DDEBUG_TARGET

#
# Standard CFLAGS
#
CFLAGS+=-Wall -Werror -pipe -fsigned-char
CFLAGS+=-I/usr/local/include -L/usr/local/lib

#
# Select either OSC or OFA for the verbs abstraction to use.
# This can also be set on the command line.
#
ifndef VERBS
#VERBS=osc
VERBS=ofa
endif

#
# List of subdirs that contain Make.inc files
#
SUBDIRS+=common security target initiator cmd

#
# These are the various object variables.  These are filled in by the SUBDIRS
# Make.inc files with the object files that are built from source in each sub
# directory.
#
target_obj=
initiator_obj=
common_obj=
user_obj=
security_obj=
cmd_obj=
datamover_obj=

#
# Populate a list of all source files.
#
src=$(shell find . -name \*.[chs])

#
# This file is used to store all dependency information
#
DEPEND_FILE=make.dep

#
# Build all subdirectory targets.
#
all:	make.dep \
	target/unh_main_scsi_target \
	initiator/unh_main_scsi_initiator

#
# Include the Make.inc file in each subdirectory.
#
$(foreach DIR, $(SUBDIRS), $(eval include $(DIR)/Make.inc))

#
# The make target for the unh_main_scsi_target program.
#
TARGET_CFLAGS+=-DDEBUG_TARGET
TARGET_LIBS+=-lpthread -lrt

target/unh_main_scsi_target: $(target_obj) $(security_obj) $(common_obj) \
				$(user_obj) $(datamover_obj)
	$(LD) $(LDFLAGS) $(TARGET_LIBS) $^ \
	-o target/unh_main_scsi_target

initiator/version.h:
	$(shell /bin/echo "#define OUR_NAME \"UNH-IOL iSCSI Initiator.  \
			Kernel: `uname -r`. Built on `date`. \"" > $@)

#
# The make target for the unh_main_scsi_initiator program.
#
INITIATOR_LIBS+=-lpthread -lrt
initiator/unh_main_scsi_initiator: initiator/version.h \
					$(initiator_obj) $(security_obj) \
					$(common_obj) $(user_obj) \
					$(datamover_obj)
	$(LD) $(LDFLAGS) $(INITIATOR_LIBS) $(initiator_obj) $(security_obj) \
					$(common_obj) $(user_obj) \
					$(datamover_obj) \
	-o initiator/unh_main_scsi_initiator

#
# Builds the dependencies table.
#
make.dep:
	rm -f $(DEPEND_FILE)
	touch $(DEPEND_FILE)
	makedepend `find . -name \*.[ch]` -f$(DEPEND_FILE) >& /dev/null
	rm $(DEPEND_FILE).bak

#
# Generate the cscope databse.
#
cscope.out: $(src)
	find . -name \*.[csh] > cscope.files
	cscope -b
	rm -f cscope.files

#
# Clean up all object files.
#
clean:
	rm -f target/unh_main_scsi_target
	rm -f initiator/unh_main_scsi_initiator
	rm -f initiator/version.h
	rm -f $(target_obj)
	rm -f $(initiator_obj)
	rm -f $(common_obj)
	rm -f $(user_obj)
	rm -f $(security_obj)
	rm -f $(cmd_obj)
	rm -f $(datamover_obj)
	rm -f $(DEPEND_FILE)
	rm -f cscope.out

.PHONY: clean

-include make.dep
