################################################################################
#
# 
# Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
# 
# 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.
# 
# 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 full GNU General Public License is included in this distribution in the
# file called LICENSE.
# 
# Contact Information:
# Linux NICS <linux.nics@intel.com>
# Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
#
################################################################################

###########################################################################
# Driver files

# core driver files
CFILES = heci_init.c interrupt.c heci_interface.c io_heci.c heci_main.c
HFILES = heci.h heci_interface.h
ifeq (,$(BUILD_KERNEL))
BUILD_KERNEL = $(shell uname -r)
endif

###########################################################################
# Environment tests

# Kernel Search Path
# All the places we look for kernel source
KSP :=  /lib/modules/$(BUILD_KERNEL)/source \
        /usr/src/linux-$(BUILD_KERNEL) \
        /usr/src/linux-$($(BUILD_KERNEL) | sed 's/-.*//') \
        /usr/src/kernel-headers-$(BUILD_KERNEL) \
        /usr/src/kernel-source-$(BUILD_KERNEL) \
        /usr/src/linux-$($(BUILD_KERNEL) | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/') \
        /usr/src/linux

# prune the list down to only values that exist
# and have an include/linux sub-directory
test_dir = $(shell [ -e $(dir)/include/linux ] && echo $(dir))
KSP := $(foreach dir, $(KSP), $(test_dir))

# we will use this first valid entry in the search path
ifeq (,$(KSRC))
  KSRC := $(firstword $(KSP))
endif
ifeq (,$(KSRC))
  $(error Linux kernel source not found)
endif

# Point to a build area for this kernel in order to do
# module compiling.  If not found settle for a source area.
ifneq (,$(wildcard /lib/modules/$(BUILD_KERNEL)/build))
  KOBJ := /lib/modules/$(BUILD_KERNEL)/build
  CONFIG_FILE := $(KOBJ)/.config
  ifeq (,$(wildcard $(CONFIG_FILE)))
    $(error Linux kernel build not configured - missing .config)
  endif
else
  KOBJ := $(KSRC)
  ifneq (,$(wildcard $(KSRC)/include/linux/config.h))
    CONFIG_FILE := $(KSRC)/include/linux/config.h
  else
    ifneq (,$(wildcard $(KSRC)/include/linux/autoconf.h))
      CONFIG_FILE := $(KSRC)/include/linux/autoconf.h
    endif
  endif
  ifeq (,$(wildcard $(CONFIG_FILE)))
    $(error Linux kernel source not configured - missing config.h and autoconf.h)
  endif
endif

# pick a compiler
ifneq (,$(findstring egcs-2.91.66, $(shell cat /proc/version)))
  CC := kgcc gcc cc
else
  CC := gcc cc
endif
test_cc = $(shell $(cc) --version > /dev/null 2>&1 && echo $(cc))
CC := $(foreach cc, $(CC), $(test_cc))
CC := $(firstword $(CC))
ifeq (,$(CC))
  $(error Compiler not found)
endif

# we need to know what platform the driver is being built on
# some additional features are only built on Intel platforms
ARCH := $(shell uname -m | sed 's/i.86/i386/')
ifeq ($(ARCH),alpha)
  CFLAGS += -ffixed-8 -mno-fp-regs
endif
ifeq ($(ARCH),x86_64)
  CFLAGS += -mcmodel=kernel -mno-red-zone
endif
ifeq ($(ARCH),ppc)
  CFLAGS += -msoft-float
endif
ifeq ($(ARCH),ppc64)
  CFLAGS += -m64 -msoft-float
  LDFLAGS += -melf64ppc
endif

# standard flags for module builds
CFLAGS += -DLINUX -D__KERNEL__ -DMODULE -O2 -pipe -Wall 
CFLAGS += -I$(KSRC)/include -I.
CFLAGS += $(shell [ -f $(KSRC)/include/linux/modversions.h ] && \
            echo "-DMODVERSIONS -DEXPORT_SYMTAB \
                  -include $(KSRC)/include/linux/modversions.h")

CFLAGS += $(CFLAGS_EXTRA)

RHC := $(KSRC)/include/linux/rhconfig.h
ifneq (,$(wildcard $(RHC)))
  # 7.3 typo in rhconfig.h
  ifneq (,$(shell $(CC) $(CFLAGS) -E -dM $(RHC) | grep __module__bigmem))
	CFLAGS += -D__module_bigmem
  endif
endif

# get the kernel version - we use this to find the correct install path
KVER := $(shell uname -r)

KKVER := $(shell echo $(KVER) | \
         awk '{ if ($$0 ~ /2\.[4-9]\./) print "1"; else print "0"}')
ifeq ($(KKVER), 0)
  $(error *** Aborting the build. \
          *** This driver is not supported on kernel versions older than 2.4.0)
endif

# set the install path
INSTDIR := /lib/modules/$(KVER)/kernel/drivers/char

# determine SMP setting of build area
ifeq ($(KOBJ),$(KSRC))
  SMP := $(shell $(CC) $(CFLAGS) -E -dM $(CONFIG_FILE) | \
           grep "CONFIG_SMP " | awk '{ print $$3 }')
  ifneq ($(SMP),1)
    SMP := 0
  endif
else
  SMP := $(shell grep 'CONFIG_SMP=y' $(CONFIG_FILE) > \
           /dev/null 2>&1 && echo 1 || echo 0)
endif

# compare SMP setting for running kernel and build area
ifneq ($(SMP),$(shell uname -a | grep SMP > /dev/null 2>&1 && echo 1 || echo 0))
  $(warning ***)
  ifeq ($(SMP),1)
    $(warning *** Warning: kernel source configuration (SMP))
    $(warning *** does not match running kernel (UP))
  else
    $(warning *** Warning: kernel source configuration (UP))
    $(warning *** does not match running kernel (SMP))
  endif
  $(warning *** Continuing with build,)
  $(warning *** resulting driver may not be what you want)
  $(warning ***)
endif

ifeq ($(SMP),1)
  CFLAGS += -D__SMP__
endif


###########################################################################
# 2.4.x & 2.6.x Specific rules

K_VERSION:=$(shell uname -r | cut -c1-3 | sed 's/2\.[56]/2\.6/')

ifeq ($(K_VERSION), 2.6)

# Makefile for 2.6.x kernel
TARGET = heci.ko

ifneq ($(PATCHLEVEL),)
EXTRA_CFLAGS += $(CFLAGS_EXTRA)
obj-m += heci.o
heci-objs := $(CFILES:.c=.o)
else
default: 
ifeq ($(KOBJ),$(KSRC))
	make -C $(KSRC) SUBDIRS=$(shell pwd) modules
else
	make -C $(KSRC) O=$(KOBJ) SUBDIRS=$(shell pwd) modules
endif
endif

endif # ifeq ($(K_VERSION),2.6)

install: default
	# remove all old versions of the driver
	find $(INSTALL_MOD_PATH)/lib/modules/$(KVER) -name $(TARGET) -exec rm -f {} \; || true
	find $(INSTALL_MOD_PATH)/lib/modules/$(KVER) -name $(TARGET).gz -exec rm -f {} \; || true
	install -D -m 644 $(TARGET) $(INSTALL_MOD_PATH)$(INSTDIR)/$(TARGET)
ifeq (,$(INSTALL_MOD_PATH))
	/sbin/depmod -a || true
else
  ifeq ($(DEPVER),1 )
	/sbin/depmod -r $(INSTALL_MOD_PATH) -a || true
  else
	/sbin/depmod -b $(INSTALL_MOD_PATH) -a -n > /dev/null || true
  endif
endif

.PHONY: clean install

clean:
	rm -rf $(TARGET) $(TARGET:.ko=.o) $(TARGET:.ko=.mod.o) $(TARGET:.ko=.mod.c) $(CFILES:.c=.o) .*cmd .tmp_versions Module.symvers
	
test:
	@echo $(KVER)
