#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# Copyright (c) 1999 - 2001, Intel Corporation 
#
# 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 Intel Corporation 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 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. 
#
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------

#   /***************************************************************************
#   **                                                                        **
#   ** INTEL CORPORATION                                                      **
#   **                                                                        **
#   ** This software is supplied under the terms of the license included      **
#   ** above.  All use of this software must be in accordance with the terms  **
#   ** of that license.                                                       **
#   **                                                                        **
#   **  Abstract:                                                             **
#   **    Makefile for the Advanced Networking Services (ANS) module.         **
#   **                                                                        **
#   **  Environment:                                                          **
#   **    Kernel Mode (linux 2.2.x, 2.4.x)                                    **
#   **                                                                        **
#   ***************************************************************************/

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

MAKEFLAGS += -s   #make it silent
NODEBUG = 1               
MODE = 3


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

# we need to know what platform the driver is being built on
ARCH := $(shell uname -m | sed 's/i.86/i386/')
ifneq ($(ARCH),i386)
  $(error Not an IA32 architecture)
endif

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

ifeq (,$(KSRC))
  $(error Linux kernel source not found)
endif

# files we get information from in KSRC
KVER_FILE := $(KSRC)/include/linux/version.h
KCFG_FILE := $(KSRC)/include/linux/config.h

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

ifeq (,$(wildcard $(KCFG_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) -E -dM $(KVER_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
ifneq (,$(wildcard /lib/modules/$(KVER)/kernel))
  MODULES_DIR := /lib/modules/$(KVER)/kernel/drivers/net
else
  MODULES_DIR := /lib/modules/$(KVER)/net
endif

# Adjust the following paths to match the appropriate directories
BIN_DIR = /usr/sbin
MAN_DIR = /usr/man/man1

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

# look for MODVERSIONS support in config.h
MODVERSIONS := $(shell $(CC) -E -dM $(KCFG_FILE) | grep CONFIG_MODVERSIONS | awk '{ print $$3 }')
ifneq ($(MODVERSIONS), 1)
  MODVERSIONS := 0
endif


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

TARGET = bin/ians.o


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

all: $(TARGET) 

$(TARGET): src/bin/ians_os.o
	ld -r $^ bin/ians_core.o -o bin/ians.o

src/bin/ians_os.o:  	 
	cd src && (make CC=$(CC) SMP=$(SMP) MODVERSIONS=$(MODVERSIONS) MODE=$(MODE) NODEBUG=$(NODEBUG))

depend:
	cd src && (make depend CC=$(CC) SMP=$(SMP) MODVERSIONS=$(MODVERSIONS) MODE=$(MODE) NODEBUG=$(NODEBUG))

clean:
	find . -name \*~ -exec rm -f {} \;
	cd src && (make clean)

veryclean: clean
	rm -f $(TARGET)
	cd src && (make veryclean)

install:	all
	mkdir -p $(MODULES_DIR)
	test -e $(MAN_DIR) || ln -s /usr/share/man /usr/man
	/usr/bin/install -b bin/ians.o	$(MODULES_DIR)
	/usr/bin/install -b bin/ianscfg	$(BIN_DIR)
	/usr/bin/install -b ianscfg.1		$(MAN_DIR)

	@echo "module installed under $(MODULES_DIR)"

	/sbin/depmod -a $(KVER)

uninstall:
	/bin/rm -f $(BIN_DIR)/ianscfg
	/bin/rm -f $(MODULES_DIR)/ians.o

	/sbin/depmod -a $(KVER)

options:
	@echo "SMP=[0/1]         compile with smp disabled/enabled"
	@echo "MODVERSIONS=[0/1] compile with modeversions disabled/enabled"
	@echo " (Default values are taken from the kernel headers)"

