##############################################################################
# Recipient has requested a license and Intel Corporation ("Intel") is willing
# to grant a license for the software entitled Linux Base Driver for the 
# Intel(R) PRO/1000 Family of Adapters (e1000) (the "Software") being provided 
# by Intel Corporation. 
# 
# "Licensed Patents" means patent claims licensable by Intel Corporation which
#  are necessarily infringed by the use or sale of the Software alone or when
#  combined with the operating system referred to below.
# "Recipient" means the party to whom Intel delivers this Software.
# "Licensee" means Recipient and those third parties that receive a license to
#  any operating system available under the GNU Public License version 2.0 or
#  later.
# 
# Copyright (c) 1996-2001 Intel Corporation 
# All rights reserved. 
# 
# The license is provided to Recipient and Recipient's Licensees under the 
# following terms.  
# 
# Redistribution and use in source and binary forms of the Software, with or 
# without modification, are permitted provided that the following conditions are
# met: 
#
# Redistributions of source code of the Software may retain the above copyright
# notice, this list of conditions and the following disclaimer. 
# Redistributions in binary form of the Software may reproduce the above
# copyright notice, this list of conditions and the following disclaimer in the 
# documentation and/or other materials provided with the distribution. 
# Neither the name of Intel Corporation nor the names of its contributors shall 
# be used to endorse or promote products derived from this Software without 
# specific prior written permission.
#
# Intel hereby grants Recipient and Licensees a non-exclusive, worldwide, 
# royalty-free patent license under Licensed Patents to make, use, sell, 
# offer to sell, import and otherwise transfer the Software, if any, in source 
# code and object code form. This license shall include changes to the Software 
# that are error corrections or other minor changes to the Software that do not 
# add functionality or features when the Software is incorporated in any version
# of a operating system that has been distributed under the GNU General Public 
# License 2.0 or later.  This patent license shall apply to the combination of 
# the Software and any operating system licensed under the GNU Public License
# version 2.0 or later if, at the time Intel provides the Software to Recipient,
# such addition of the Software to the then publicly available versions of such 
# operating system available under the GNU Public License version 2.0 or later 
# (whether in gold, beta or alpha form) causes such combination to be covered by
# the Licensed Patents. The patent license shall not apply to any other 
# combinations which include the Software. No hardware per se is licensed 
# hereunder. 
#  
# 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 INTEL OR ITS 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.
###############################################################################

###########################################################################
# Configuration Section

# Optional features - set to 'y' for on, anything else for off
# Intel(R) Advanced Network Services
IANS := y
# Intel(R) PRO Diagnostics
IDIAG := y

# debug
CFLAGS += -Wall 

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

# core driver files
TARGET = e1000.o
CFILES = e1000_main.c e1000_fxhw.c e1000_phy.c e1000_proc.c
HFILES = e1000.h e1000_kcompat.h e1000_fxhw.h e1000_phy.h e1000_proc.h \
		 e1000_fx_osdep.h

# man page
MANSECTION = 7
MANFILE = $(TARGET:.o=.$(MANSECTION))

# iANS
IANS_CFLAGS = -DIANS -DIANS_BASE_ADAPTER_TEAMING \
              -DIANS_BASE_VLAN_TAGGING -DIANS_BASE_VLAN_ID
IANS_CFILES = ans.c ans_hw.c ans_os.c ans_driver.c
IANS_HFILES = base_comm.h ans_interface.h ans.h ans_hw.h ans_os.h ans_driver.h

# Diagnostics
IDIAG_CFLAGS = -DIDIAG
IDIAG_CFILES = e1000_idiag.c
IDIAG_HFILES = idiag_pro.h idiag_e1000.h e1000_idiag.h

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

# Kernel Search Path
# All the places we look for kernel source
KSP :=  /lib/modules/$(shell uname -r)/build \
        /usr/src/linux-$(shell uname -r) \
        /usr/src/linux-$(shell uname -r | sed 's/-.*//') \
        /usr/src/kernel-headers-$(shell uname -r) \
        /usr/src/kernel-source-$(shell uname -r) \
        /usr/src/linux-$(shell uname -r | 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

# check for version.h and autoconf.h for running kernel in /boot (SuSE)
ifneq (,$(wildcard /boot/vmlinuz.version.h))
	VERSION_FILE := /boot/vmlinuz.version.h
	CONFIG_FILE  := /boot/vmlinuz.autoconf.h
	KVER := $(shell $(CC) $(CFLAGS) -E -dM $(VERSION_FILE) | \
                  grep UTS_RELEASE | awk '{ print $$3 }' | sed 's/\"//g')
	ifeq ($(KVER),$(shell uname -r))
		# set up include path to override headers from kernel source
		x:=$(shell rm -rf include)
		x:=$(shell mkdir -p include/linux)
		x:=$(shell cp /boot/vmlinuz.version.h include/linux/version.h)
		x:=$(shell cp /boot/vmlinuz.autoconf.h include/linux/autoconf.h)
		CFLAGS += -I./include
	else
		VERSION_FILE := $(KSRC)/include/linux/version.h
		CONFIG_FILE  := $(KSRC)/include/linux/config.h
	endif
else
	VERSION_FILE := $(KSRC)/include/linux/version.h
	CONFIG_FILE  := $(KSRC)/include/linux/config.h
endif

ifeq (,$(wildcard $(VERSION_FILE)))
  $(error Linux kernel source not configured - missing version.h)
endif

ifeq (,$(wildcard $(CONFIG_FILE)))
  $(error Linux kernel source not configured - missing config.h)
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 which $(cc) > /dev/null 2>&1 && echo $(cc))
CC := $(foreach cc, $(CC), $(test_cc))
CC := $(firstword $(CC))

# get the kernel version - we use this to find the correct install path
KVER := $(shell $(CC) $(CFLAGS) -E -dM $(VERSION_FILE) | grep UTS_RELEASE | \
          awk '{ print $$3 }' | sed 's/\"//g')

ifneq ($(KVER),$(shell uname -r))
  $(warning ***)
  $(warning *** Warning: kernel source version ($(KVER)))
  $(warning *** does not match running kernel  ($(shell uname -r)))
  $(warning *** Continuing with build,)
  $(warning *** resulting driver may not be what you want)
  $(warning ***)
endif

# pick an appropriate install path
INSTDIR := $(shell find /lib/modules/$(KVER) -name $(TARGET) -printf %h)
ifeq (,$(INSTDIR))
  ifneq (,$(wildcard /lib/modules/$(KVER)/kernel))
    INSTDIR := /lib/modules/$(KVER)/kernel/drivers/net
  else
    INSTDIR := /lib/modules/$(KVER)/net
  endif
endif

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

# look for SMP in config.h
SMP := $(shell $(CC) $(CFLAGS) -E -dM $(CONFIG_FILE) | \
         grep CONFIG_SMP | awk '{ print $$3 }')
ifneq ($(SMP),1)
  SMP := 0
endif

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

ifeq (,$(MANDIR))
  # find the best place to install the man page
  MANPATH := $(shell (manpath 2>/dev/null || echo $MANPATH) | sed 's/:/ /g')
  ifneq (,$(MANPATH))
    # test based on inclusion in MANPATH
    test_dir = $(findstring $(dir), $(MANPATH))
  else
    # no MANPATH, test based on directory existence
    test_dir = $(shell [ -e $(dir) ] && echo $(dir))
  endif
  # our preferred install path
  # should /usr/local/man be in here ?
  MANDIR := /usr/share/man /usr/man
  MANDIR := $(foreach dir, $(MANDIR), $(test_dir))
  MANDIR := $(firstword $(MANDIR))
endif
ifeq (,$(MANDIR))
  # fallback to /usr/man
  MANDIR := /usr/man
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 (,$(findstring $(ARCH),i386 ia64))
  IANS := n
endif

# depmod version for rpm builds
DEPVER := $(shell /sbin/depmod -V 2>/dev/null | awk 'BEGIN {FS="."} NR==1 {print $$2}') 

# iANS
ifeq ($(IANS),y)
  CFLAGS += $(IANS_CFLAGS)
  CFILES += $(IANS_CFILES)
  HFILES += $(IANS_HFILES)
endif

# Diagnostic features
ifeq ($(IDIAG),y)
  CFLAGS += $(IDIAG_CFLAGS)
  CFILES += $(IDIAG_CFILES)
  HFILES += $(IDIAG_HFILES)
endif

###########################################################################
# Build rules

.SILENT: $(TARGET)
$(TARGET): $(filter-out $(TARGET), $(CFILES:.c=.o))
	$(LD) -r $^ -o $@
	echo; echo
	echo "**************************************************"
	echo "** $(TARGET) built for $(KVER)"
	echo -n "** SMP               "
	if [ "$(SMP)" = "1" ]; \
		then echo "Enabled"; else echo "Disabled"; fi
	echo -n "** iANS hooks        "
	if [ "$(IANS)" = "y" ]; \
		then echo "Enabled"; else echo "Disabled"; fi
	echo -n "** Pro Diagnostics   "
	if [ "$(IDIAG)" = "y" ]; \
		then echo "Enabled"; else echo "Disabled"; fi
	echo "**************************************************"
	echo

$(CFILES:.c=.o): $(HFILES) Makefile

$(MANFILE).gz: $(MANFILE)
	gzip -c $< > $@

install: $(TARGET) $(MANFILE).gz
	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 || true
  endif
endif
	install -D -m 644 $(MANFILE).gz $(INSTALL_MOD_PATH)$(MANDIR)/man$(MANSECTION)/$(MANFILE).gz
	man -c -P'cat > /dev/null' $(MANFILE:.$(MANSECTION)=) || true

uninstall:
	if [ -e $(INSTDIR)/$(TARGET) ] ; then \
	    rm -f $(INSTDIR)/$(TARGET) ; \
	fi
	/sbin/depmod -a
	if [ -e $(MANDIR)/man$(MANSECTION)/$(MANFILE).gz ] ; then \
		rm -f $(MANDIR)/man$(MANSECTION)/$(MANFILE).gz ; \
	fi

clean:
	rm -f $(TARGET) $(CFILES:.c=.o) $(MANFILE).gz

