#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# 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 open portion of 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
KVER := $(shell $(CC) -E -dM $(KVER_FILE) | grep UTS_RELEASE | awk '{ print $$3 }' | sed 's/\"//g')

# standard flags for module builds
INCLUDES = -I$(KSRC)/include -I. -Ilib 
DEFINES  = -DLINUX -D__KERNEL__ -DMODULE -pipe
MODE_CONFIG = "module built for kernel $(KVER)"

# 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

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

# If SMP is supported, add the necessary flags
ifeq ($(SMP),1)
  DEFINES+=-D__SMP__ -DCONFIG_SMP
  MODE_CONFIG+="\nSMP         Enabled"
else
  MODE_CONFIG+="\nSMP         Disabled"
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

# If MODVERSIONS is supported, add the necessary flags
ifeq ($(MODVERSIONS),1)
  DEFINES+=-DMODVERSIONS -include $(KSRC)/include/linux/modversions.h
  MODE_CONFIG+="\nMODVERSIONS Enabled"
else
  MODE_CONFIG+="\nMODVERSIONS Disabled"
endif

# If possible, use multiprocessor for compiling
SMP_STATUS = $(findstring SMP, $(shell uname -v))
ifeq ($(SMP_STATUS), SMP) 
  MAKEFLAGS+=-j
  MODE_CONFIG+="\nusing SMP"
endif


###########################################################################
# Features and Diagnostics

DEFINES += -DINPD_LINUX

TEAMING_DEFS = -DIANS_ADAPTER_TEAMING  -DIANS_BASE_ADAPTER_TEAMING
IBVLANID_DEFS = -DIANS_TAGGING -DIANS_BINDING_BASED_VLAN  -DIANS_BASE_VLAN_ID -DIANS_BASE_VLAN_TAGGING

ifeq ($(MODE),1)                     # teaming only
DEFINES+=$(TEAMING_DEFS)
endif

ifeq ($(MODE),2)                     # ibvlanid only
DEFINES+=$(IBVLANID_DEFS)
endif

ifeq ($(MODE),3)                     # teaming and ibvlanid
DEFINES+=$(TEAMING_DEFS) $(IBVLANID_DEFS)
endif

ifeq ($(NODEBUG),0)
  DEFINES += -DDEBUG
  OPTIMIZE_FLAGS = -O1 -fno-omit-frame-pointer
  WARNING_FLAGS  = -Wall -Wstrict-prototypes -Winline
# DEFINES += -g
else
  OPTIMIZE_FLAGS = -O3 -fomit-frame-pointer 
  WARNING_FLAGS  = -Wstrict-prototypes
endif


###########################################################################
# Sum it all up

CFLAGS = $(DEFINES) $(INCLUDES) $(OPTIMIZE_FLAGS) $(WARNING_FLAGS)


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

IANS_OS_OBJ =	ians_base.o ians_kernel.o   

LIB_OBJ     =	lib/incg_message.o lib/incg_gp_mem.o \
		lib/incg_log.o lib/incg_flow.o  lib/incg_timer.o \
		lib/incg_utils.o lib/incg_pci.o  

LINK_OBJ    =	$(IANS_OS_OBJ) $(LIB_OBJ)
SOURCES     =	$(LINK_OBJ:.o=.c)
TARGET      =	bin/ians_os.o


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

all: $(TARGET)

$(TARGET): pre_ians_os.o lib/lib.o
	ld -r $^ -o $@
	@echo
	@echo
	@echo "**************************************************"
	@echo -e $(MODE_CONFIG)
	@echo "**************************************************"
	@echo


pre_ians_os.o: $(IANS_OS_OBJ)
	ld -r  $^ -o $@

lib/lib.o: $(LIB_OBJ)
	ld -r $^ -o $@

%.o: %.c
	@echo "Compiling $< ..."
	$(CC) $(CFLAGS) -c $< -o $@

depend:
	makedepend -f .depend -- $(CFLAGS) -- $(SOURCES)

clean:
	find . -name \*.o -exec rm -f {} \;
	find . -name \*~ -exec rm -f {} \;

veryclean: clean
	echo -n > .depend
	@echo "you should run make depend"

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)"

include .depend
