# Makefile for the Linux iSCSI driver
# Copyright (C) 2001 Cisco Systems, Inc.
# maintained by linux-iscsi-devel@lists.sourceforge.net
#
# 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 of the License, 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.
#
# See the file COPYING included with this distribution for more details.

#
# 
ifneq ($(KERNELRELEASE),)
obj-m				:=	iscsi.o
iscsi-objs			:= 	iscsi-initiator.o 
iscsi-objs			+=	iscsi-attr.o \
					iscsi-debug-attr.o \
					iscsi-probe.o \
					iscsi-login.o \
					iscsiAuthClient.o \
					iscsiAuthClientGlue.o \
					iscsi-crc.o \
					iscsi-session.o \
					iscsi-timer.o \
					iscsi-task.o \
					iscsi-portal.o \
					iscsi-ioctl.o \
					iscsi-network.o \
					iscsi-recv-pdu.o \
					iscsi-xmit-pdu.o


ISCSI_MODFLAGS :=
#
# This helps with some of the portability ifdefs in the code.
# 
ISCSI_MODFLAGS += -DLINUX=1 -I$(obj) -Idrivers/scsi -Werror

ifeq ($(CONFIG_ISCSI_DEBUG),y)
ISCSI_MODFLAGS += -DDEBUG=1
endif

EXTRA_CFLAGS    += $(ISCSI_MODFLAGS)

## THE KERNEL MAKEFILE ENDS HERE!!!
else
## THE USER MAKEFILE BEGINS !!!

# for compiling the daemon and utils
OS:=$(shell uname)
ARCH:=$(shell uname -m)
OSTYPE:=linux

# Where to install files that must be relative to the root dir.
# This is a prefix prepended before the pathname, and is
# normally unset.  It would be set for such things as diskless
# clients, where the driver is being installed on the server
# for a client.
ROOT:=
export ROOT

# Where to install the relocatable files (daemon, utilities, and man pages)
BASEDIR:=/
export BASEDIR

# We need to have the kernel headers installed in order to compile.
# You can specify a TOPDIR for your Linux kernel here, or if you don't
# we'll look for it in a few likely places later.
TOPDIR:=

# We also need the pathname of the kernel .config file to use.
# You can specify a .config for your Linux kernel here, or if you don't
# we'll look for it in a few likely places later.
KERNEL_CONFIG:=

# The rest of this Makefile shouldn't need to be modified

# record the version of the running kernel
KERNEL_VPSE:=$(shell uname -r)
KERNEL_VPS:=$(shell uname -r | sed -e 's|^\([0-9]\+\.[0-9]\+\.[0-9]\+\).*|\1|')
KERNEL_VP:=$(shell uname -r | awk -F. '{ print $$1 "." $$2}')

# Try to find the top of the Linux kernel source tree for the running kernel
ifeq ($(TOPDIR),)
  ifeq ($(wildcard /usr/src/linux/.),/usr/src/linux/.)
    TOPDIR:=/usr/src/linux
  endif
  # linux-v.p
  ifeq ($(wildcard /usr/src/linux-$(KERNEL_VP)/.),/usr/src/linux-$(KERNEL_VP)/.)
    TOPDIR:=/usr/src/linux-$(KERNEL_VP)
  endif
  # linux-v.p.s
  ifeq ($(wildcard /usr/src/linux-$(KERNEL_VPS)/.),/usr/src/linux-$(KERNEL_VPS)/.)
    TOPDIR:=/usr/src/linux-$(KERNEL_VPS)
  endif
  # linux-v.p.s-e
  ifeq ($(wildcard /usr/src/linux-$(KERNEL_VPSE)/.),/usr/src/linux-$(KERNEL_VPSE)/.)
    TOPDIR:=/usr/src/linux-$(KERNEL_VPSE)
  endif
  # Linus has recommended a symlink /lib/modules/`uname -r`/build
  ifeq ($(wildcard /lib/modules/$(KERNEL_VPSE)/build/.),/lib/modules/$(KERNEL_VPSE)/build/.)
    TOPDIR:=/lib/modules/$(KERNEL_VPSE)/build
  endif
  ifeq ($(TOPDIR),)
    $(warning Linux kernel source must be installed to compile this code.)
    $(error Could not find the top of the linux kernel source tree.)
  endif
endif

# record the kernel source version
KERNEL_VERSION:=$(shell awk -F"[ \t]*=[ \t]*" '$$1 == "VERSION" { print $$2 }' $(TOPDIR)/Makefile | sed -e 's/[ \t\n]*//g')
KERNEL_PATCHLEVEL:=$(shell awk -F"[ \t]*=[ \t]*" '$$1 == "PATCHLEVEL" {print $$2}' $(TOPDIR)/Makefile | sed -e 's/[ \t\n]*//g')
KERNEL_SUBLEVEL:=$(shell awk -F"[ \t]*=[ \t]*" '$$1 == "SUBLEVEL" {print $$2 }' $(TOPDIR)/Makefile | sed -e 's/[ \t\n]*//g')
KERNEL_EXTRAVERSION:=$(shell awk -F"[ \t]*=[ \t]*" '$$1 == "EXTRAVERSION" {print $$2 }' $(TOPDIR)/Makefile | sed -e 's/[ \t\n]*//g')
KERNEL_SRC_VPS:=$(KERNEL_VERSION).$(KERNEL_PATCHLEVEL).$(KERNEL_SUBLEVEL)
KERNEL_SRC_VPSE:=$(KERNEL_VERSION).$(KERNEL_PATCHLEVEL).$(KERNEL_SUBLEVEL)$(KERNEL_EXTRAVERSION)

# by default, use the VPSE from the kernel source for warning comparisons.
SOURCE_KERNEL_VERSION:=$(KERNEL_SRC_VPSE)

# FIXME: would it be better to extract KERNELRELEASE from the kernel Makefile?
# Some distributions (e.g. SuSE) append other strings after EXTAVERSION, which
# can mess up some of our comparisions unless we compensate for it somehow.
# Extracting KERNELRELEASE via make is probably more reliable than scanning
# the Makefile with grep and awk.

# by default, use the whole `uname -r` string for comparisons
RUNNING_KERNEL_VERSION:=$(KERNEL_VPSE)

# SuSE may append some strings to the kernel version for SMP and HIGHMEM
# we want to strip these suffixes off of the `uname -r` string before
# doing comparisons against it.
SUSE_SUFFIXES:=$(shell grep '^[ \t]*SMPVERSION=[^ \t\n]' $(TOPDIR)/Makefile | awk -F= '{print $$2}') \
                 $(shell grep '^[ \t]*HIGHMEMVERSION=[^ \t\n]' $(TOPDIR)/Makefile | awk -F= '{print $$2}')

ifneq ($(SUSE_SUFFIXES),)
  # FIXME: if we could side-effect a variable within a foreach loop, that
  # would be preferable.  For now, hardcode the suffixes.

  # strip off the suffixes SuSE might have added to KERNELRELEASE
  ifeq ($(shell printf "$(RUNNING_KERNEL_VERSION)" | tail -c 4),-SMP)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(RUNNING_KERNEL_VERSION)" | sed -e 's/-SMP$$//')
  endif
  ifeq ($(shell printf "$(RUNNING_KERNEL_VERSION)" | tail -c 4),-4GB)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(RUNNING_KERNEL_VERSION)" | sed -e 's/-4GB$$//')
  endif
  ifeq ($(shell printf "$(RUNNING_KERNEL_VERSION)" | tail -c 5),-64GB)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(RUNNING_KERNEL_VERSION)" | sed -e 's/-64GB$$//')
  endif
  
  # SuSE tends to allow only one kernel binary to be installed,
  # and keeps the config in /boot as well.

  ifeq ($(wildcard /boot/vmlinuz.config),/boot/vmlinuz.config)
    KERNEL_CONFIG:=/boot/vmlinuz.config
  endif

  # FIXME: if /boot/vmlinuz.version.h and /boot/vmlinuz.autoconf.h don't match
  # what is in TOPDIR/include/linux/, the user may have forgotten to
  # copy the files.  Ask if we should copy the files for the user.
  # Copy by default, since that is what is most likely to work for
  # people who don't understand the question.  Users for whom that
  # is wrong hopefully know how to regenerate the files with
  # `make menuconfig`.

endif ## THE USER MAKEFILE ENDS !!!

# Try to find the kernel's config if the user didn't tell us where to find it.
# People who compile their own kernels will have a .config, as will some
# distributions that only ship one kernel binary, or that use separate
# kernel source for each kernel binary.  Redhat uses the same kernel source
# for multiple kernel binaries.

# special handling for Redhat, so that we can do better checking of kernel
# source mismatches with the running kernel, and can try to find the
# right kernel config file when there is no config file in /boot.
# Redhat Advanced Server kernel RPMs don't copy the appropriate config file
# to /boot/config-`uname -r`, so we have to try to guess which config file
# in $(TOPDIR)/configs might match the running kernel.  Sadly there's no
# direct mapping from the uname output to the filename, so this gets ugly.
# We assume that some part of the EXTRAVERSION suffix will let us pick the
# right config file name.  This breaks whenever Redhat adds a new kernel
# type, and may break if they change the way they encode their version
# in the kernel's EXTRAVERSION.  It also breaks if they ever get rid
# of rhconfig.h, which is how we currently detect Redhat kernel source.

ifeq ($(wildcard $(TOPDIR)/include/linux/rhconfig.h),$(TOPDIR)/include/linux/rhconfig.h)
  # try to strip the type suffix off of the kernel source VPSE, so that we
  # can check for version mismatches later without getting false positives
  # because of the kernel type suffix.
  # As of 12/17/2002, known Redhat kernel types are: (empty), debug, BOOT, smp,
  # enterprise, summit, bigmem, uml
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 6),custom)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/custom$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 5),cusom)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/cusom$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 5),debug)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/debug$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 4),BOOT)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/BOOT$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 3),smp)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/smp$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 10),enterprise)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/enterprise$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 9),enerprise)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/enerprise$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 6),summit)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/summit$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 5),summi)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/summi$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 6),bigmem)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/bigmem$$//')
  endif
  ifeq ($(shell printf "$(KERNEL_SRC_VPSE)" | tail -c 3),uml)
    SOURCE_KERNEL_VERSION:=$(shell printf "$(KERNEL_SRC_VPSE)" | sed -e 's/uml$$//')
  endif
  
 # determine the running Redhat kernel version (without the type suffix)
  REDHAT_KERNEL_TYPE:=
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 6),custom)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/custom$$//')
    REDHAT_KERNEL_TYPE:=-custom
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 5),cusom)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/cusom$$//')
    REDHAT_KERNEL_TYPE:=-custom
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 5),debug)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/debug$$//')
    REDHAT_KERNEL_TYPE:=-debug
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 4),BOOT)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/BOOT$$//')
    REDHAT_KERNEL_TYPE:=-BOOT
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 3),smp)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/smp$$//')
    REDHAT_KERNEL_TYPE:=-smp
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 10),enterprise)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/enterprise$$//')
    REDHAT_KERNEL_TYPE:=-enterprise
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 9),enerprise)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/enerprise$$//')
    REDHAT_KERNEL_TYPE:=-enterprise
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 6),summit)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/summit$$//')
    REDHAT_KERNEL_TYPE:=-summit
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 5),summi)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/summi$$//')
    REDHAT_KERNEL_TYPE:=-summit
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 6),bigmem)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/bigmem$$//')
    REDHAT_KERNEL_TYPE:=-bigmem
  endif
  ifeq ($(shell printf "$(KERNEL_VPSE)" | tail -c 3),uml)
    RUNNING_KERNEL_VERSION:=$(shell printf "$(KERNEL_VPSE)" | sed -e 's/uml$$//')
    REDHAT_KERNEL_TYPE:=-uml
  endif
  # Look for a Redhat config file that might be appropriate.
  ifeq ($(wildcard $(TOPDIR)/configs/.),$(TOPDIR)/configs/.)
    # we deliberately use the running kernel version next, so that we ignore
    # all of these config files if the running kernel version doesn't match
    # the source kernel version.
    POSSIBLE_CONFIG:=$(TOPDIR)/configs/kernel-$(KERNEL_VPS)-$(shell uname -m)$(REDHAT_KERNEL_TYPE).config
    ifeq ($(wildcard $(POSSIBLE_CONFIG)),$(POSSIBLE_CONFIG))
      KERNEL_CONFIG:=$(POSSIBLE_CONFIG)
    endif
  endif
endif


# some versions of Redhat make it easy, and put the file /boot
ifeq ($(wildcard /boot/config-$(KERNEL_VPSE)),/boot/config-$(KERNEL_VPSE))
    KERNEL_CONFIG:=/boot/config-$(KERNEL_VPSE)
endif


# the standard config file location is $(TOPDIR)/.config
ifeq ($(wildcard $(TOPDIR)/.config),$(TOPDIR)/.config)
  KERNEL_CONFIG:=$(TOPDIR)/.config
  # If there's a .config file, the user has configured a custom kernel,
  # so we want to ignore any other config files found earlier, and
  # check the entire EXTRAVERSION string for version mismatches.
  SOURCE_KERNEL_VERSION:=$(KERNEL_SRC_VPSE)
endif


# for compiling the daemon and utils 
CC:=gcc
CFLAGS:= -Wall -Wstrict-prototypes -O2 -DLINUX=1
LDFLAGS:= 
# Override path below if you have installed libsysfs on any other path.
SYSFS_LIB_PATH:= /usr/local/lib
LIBSYSFS:= -lsysfs
# the module gets most flags from the kernel Makefile, but we must add a few as well
ISCSIMODFLAGS:=-DLINUX=1 -I$(TOPDIR)/drivers/scsi

PWD         := $(shell pwd)

# assume object dirs should go under the current directory, unless told
# otherwise
OBJ:=$(CURDIR)

ifdef DEBUG
OBJDIR:=$(OBJ)/$(OS)-$(ARCH)/dbg-obj
KOBJDIR:=$(PWD)
CLEANDIRS:=$(OBJ)/$(OS)-$(ARCH)/dbg-obj 
CFLAGS += -g -DDEBUG
LDFLAGS += -g
ISCSIMODFLAGS += -DDEBUG=1
ifdef ATTR_DEBUG
CFLAGS += -g -DDEBUG -DATTR_DEBUG
ISCSIMODFLAGS += -DDEBUG=1 -DATTR_DEBUG=1
endif
else
KOBJDIR:=$(PWD)
OBJDIR:=$(OBJ)/$(OS)-$(ARCH)/obj
CLEANDIRS:=$(OBJ)/$(OS)-$(ARCH)/obj 
CFLAGS += -fomit-frame-pointer
endif

.PHONY: all module daemons utils tools kernel user permissions clean tar dist release \
         checklocal version newversion install remove uninstall dirs

# default target
all:: dirs module daemons utils tools permissions checkkernel checkscsi checkcrypto
module: dirs checkkernel 
daemons:  dirs $(OBJDIR)/iscsid
utils: dirs $(OBJDIR)/iscsi-device $(OBJDIR)/iscsi-iname $(OBJDIR)/iscsi-ls $(OBJDIR)/init $(OBJDIR)/iscsi-id
#
test: dirs $(OBJDIR)/bindings

tools: dirs $(OBJDIR)/iscsigt
	@cp -f $(OBJDIR)/iscsigt iscsigt

# distributors usually want these targets when building packages
user: dirs daemons utils permissions
kernel: dirs checkkernel checkscsi checkcrypto module

endif

export OBJDIR
export KOBJDIR

.SUFFIXES:

MODULE_SRCS:=iscsi-initiator.c iscsi-attr.c iscsi-debug-attr.c iscsi-probe.c iscsi-login.c iscsiAuthClient.c iscsiAuthClientGlue.c iscsi-crc.c iscsi-session.c iscsi-timer.c iscsi-task.c iscsi-portal.c iscsi-ioctl.c iscsi-network.c iscsi-recv-pdu.c iscsi-xmit-pdu.c
DAEMON_SRCS:=iscsid.c iscsi-linux.c iscsi-login.c iscsi-io.c iscsi-config.c iscsi-discovery.c iscsi-bindings.c \
               string-buffer.c iscsiAuthClient.c iscsiAuthClientGlue.c md5.c
#Install openslp release 1.1.4 and onwards on the system and uncomment the follo
#wing line to enable SLP discovery for iSCSI.
#ENABLE_SLP:= 1
ifeq ($(ENABLE_SLP),1)
CFLAGS+=-DSLP_ENABLE=1
LDFLAGS+=-lslp
DAEMON_SRCS += iscsi-slp-discovery.c
endif

UTIL_SRCS:=iscsi-iname.c md5.c iscsi-device.c iscsigt.c iscsi-ls.c init.c iscsi-network-boot.c iscsi-id.c
UTIL_OBJS:=$(UTIL_SRCS:%.c=$(OBJDIR)/%.o)
DAEMON_OBJS:=$(DAEMON_SRCS:%.c=$(OBJDIR)/%.o)
KDIR        := /lib/modules/$(shell uname -r)/build

export ISCSIMODFLAGS
# Make all objects depend on the Makefile
$(DAEMON_OBJS): Makefile
$(UTIL_OBJS): Makefile

install: module daemons utils
	@./install.sh

remove uninstall:
	@./remove.sh

echomodsrcs:
	@echo $(MODULE_SRCS)

# warn the user if the running kernel version
# doesn't match the $TOPDIR/Makefile version
checkkernel:
	@printf "\nNote: using kernel source from $(TOPDIR)\ncontaining kernel version $(KERNEL_SRC_VPSE)\n\n" | fmt -
	@if [ "$(KERNEL_CONFIG)" ] ; then \
                printf "Note: using kernel config from $(KERNEL_CONFIG)\n\n" | fmt - ; \
        else \
	   ( printf "Warning: failed to find a kernel config file for $(KERNEL_SRC_VPSE).\n" ; \
             printf "The iSCSI kernel module may fail to load or run properly.\n\n"; ) | fmt - ; \
        fi
        @if [ "$(KERNEL_VERSION).$(KERNEL_PATCHLEVEL)" = "2.4" ] ; then \
            ( printf "\nError: this iSCSI driver does not support 2.4 kernels.\n" ; \
              printf "You must upgrade your kernel to 2.6 or later\n" ; \
              printf "in order to use this driver.\n\n" ; ) | fmt - ; \
            exit 1; \
        fi
	@if [ "$(SOURCE_KERNEL_VERSION)" != "$(RUNNING_KERNEL_VERSION)" ] ; then\
            (   echo "Warning: $(TOPDIR) contains kernel version $(KERNEL_SRC_VPSE)," ; \
                echo "but kernel version $(KERNEL_VPSE) is currently running.";\
                echo "This mismatch may prevent the iSCSI module from loading onthe"; \
                echo "running kernel due to unresolved symbols, or may cause problems" ; \
                echo "during the operation of the iSCSI driver.  The iSCSI driver will" ; \
                echo "be built for kernel version $(KERNEL_SRC_VPSE)." ; \
                echo "If you wish to build the iSCSI driver for the kernel that is" ; \
                echo "currently running, you must install the appropriate kernelsource"; \
                echo "and rebuild the iSCSI driver."; \
                echo ; ) | fmt - ; \
        fi;

# Error out if there is no scsi support in the kernel
checkscsi:
	@if [ -n "$(KERNEL_CONFIG)" ] ; then \
	SCSI=`grep 'CONFIG_SCSI=' $(KERNEL_CONFIG)|awk -F'=' '{print $$2}'`; \
	if [ "$$SCSI" != "y" -a "$$SCSI" != "m" -a "$$SCSI" != "unknown" ] ; then \
		echo ; \
		echo "Error: your kernel does not appear to support SCSI." ; \
		echo "For iSCSI to function, you must rebuild your kernel with either" ; \
		echo "SCSI support compiled into the kernel, or with SCSI support as a module." ; \
		echo ; \
		exit 1;\
	fi \
	fi

checkcrypto:
	@if [ -n "$(KERNEL_CONFIG)" ] ; then \
	CRYPTO=`grep 'CONFIG_CRYPTO=' $(KERNEL_CONFIG)|awk -F'=' '{print $$2}'`; \
	MD5=`grep 'CONFIG_CRYPTO_MD5=' $(KERNEL_CONFIG)|awk -F'=' '{print $$2}'` ; \
	if [ "$$CRYPTO" != "y" -a "$$CRYPTO" != "unknown" ]; then \
		echo ; \
		echo "Error: your kernel does not appear to support CRYPTO API." ; \
		echo "For iSCSI to function, you must rebuild your kernel with" ; \
		echo "CRYPTO API support compiled into the kernel." ; \
		echo ; \
		exit 1;\
	elif [ "$$MD5" != "y" -a "$$MD5" != "m" -a "$$MD5" != "unknown" ]; then \
		echo; \
		echo "Error: your kernel does not appear to support MD5." ; \
		echo "For iSCSI to function, you must rebuild your kernel with" ; \
		echo "MD5 support compiled into the kernel." ; \
		echo ; \
		exit 1;\
	fi \
	fi

# compile daemon/utility code and auto-generate dependencies (and make missing prereqs cause a rebuild,
# not an error)
$(OBJDIR)/%.o : %.c
	@mkdir -p $(OBJDIR)
	$(CC) $(CFLAGS) -MMD -c -o $(OBJDIR)/$*.o $*.c
	@if [ ! -f $(OBJDIR)/$*.d -a -f $*.d ] ; then \
		printf "$(OBJDIR)/" > $(OBJDIR)/$*.d ; \
		cat $*.d >> $(OBJDIR)/$*.d && rm -f $*.d ; \
	fi
	@cp -f $(OBJDIR)/$*.d $(OBJDIR)/$*.dep
	@sed -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' -e 's/^ *//' -e '/^$$/ d' -e 's/$$/:/' < $(OBJDIR)/$*.d >> $(OBJDIR)/$*.dep


$(OBJDIR)/iscsid: $(DAEMON_OBJS)
	$(CC) $(LDFLAGS)  $(DAEMONFLAGS) -o $@ $^
# includes TOPDIR/include/scsi/scsi_ioctl.h
$(OBJDIR)/iscsi-device: iscsi-device.c Makefile
	$(CC) $(CFLAGS) -I$(TOPDIR)/include -o $@ $<

$(OBJDIR)/iscsi-id: $(OBJDIR)/iscsi-id.o
	$(CC) $(CFLAGS) -L SYSFS_LIB_PATH -o $@ -static $< $(LIBSYSFS)

$(OBJDIR)/iscsi-ls: iscsi-ls.c Makefile
	$(CC) $(CFLAGS) -I$(TOPDIR)/include -o $@ $<

# Link the iscsi-iname utility
$(OBJDIR)/iscsi-iname: $(OBJDIR)/iscsi-iname.o $(OBJDIR)/md5.o
	$(CC) $(CFLAGS) -o $@ $^

# Make the init utility. This needs to be statically compiled for initrd.
$(OBJDIR)/init: $(OBJDIR)/init.o $(OBJDIR)/iscsi-network-boot.o
	$(CC) $(CFLAGS) -static -o $@ $^

# development get trace utility
$(OBJDIR)/iscsigt: $(OBJDIR)/iscsigt.o
	$(CC) $(CFLAGS) -o $@ $<


dirs:
	@mkdir -p $(OBJDIR) 

permissions:
	@chmod 744 rc.iscsi
	@chmod 744 mkinitrd.iscsi
	@chmod 744 install.sh
	@chmod 744 remove.sh
	@chmod 744 iscsi-mountall
	@chmod 744 iscsi-umountall
clean:
	rm -rf $(OBJDIR) $(CLEANDIRS) *.o *.ko *.mod.c

module: 
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules;

