######################################################################
#
# Copyright (c) nVidia Corporation
#
# Abstract:
#
#    Makefile for Linux driver for nForce MCP Ethernet controller
#
# Revision History:
#
#######################################################################

#
# Sources 
# 
MODULE_NAME = nvnet
SRC = nvenet.c
OBJ = $(addsuffix .o, $(basename $(SRC)))
TEMP = temp.o

#
# Target
#
TARGET = $(MODULE_NAME).o

#
# Networking library
#
NVENETLIB = nvenetlib.o

#
# MCP Include
#
MCPINCLUDE = basetype.h os.h nvenet.h adapter.h

# SYSINCLUDE can be overridden by environment variable or command line argument
# TARGET_KERNEL can be overridden by build system
TARGET_KERNEL ?= $(shell uname -r)
LINUXDIR = $(shell uname -r | awk '{sub(/-.*/,"",$$0); print $$0}')


# Find kernel sources, if they're installed
ifeq ($(filter build, $(notdir $(wildcard /lib/modules/$(TARGET_KERNEL)/*))), build)
	KERNSRC = /lib/modules/$(TARGET_KERNEL)/build
else
	KERNSRC = /usr/src/linux-$(LINUXDIR)
endif


SYSINCLUDE ?= $(KERNSRC)/include

#
# Configuration files
#
VERSION_FILE = $(SYSINCLUDE)/linux/version.h
CONFIG_FILE  = $(SYSINCLUDE)/linux/config.h


#Test to see which version of -{m,f}align-functions to use
OLDALIGN = -malign-functions=4
NEWALIGN = -falign-functions=4
BUILDCHECK = rm -f tp.c; if test -x tp; then echo "good"; fi;
TESTBUILD = rm -f tp; $(CC) -falign-functions=4 tp.c -o tp >/dev/null 2>&1;
MAKEPROG = echo "main() {}" > tp.c;
TESTPROG = $(shell $(MAKEPROG) $(TESTBUILD) $(BUILDCHECK))
ifeq ($(TESTPROG), good)
ALIGN = $(NEWALIGN)
else
ALIGN = $(OLDALIGN)
endif

# determine target architecture if not set
ifeq "$(TARGETARCH)" ""
ifeq (x86_64,$(findstring x86_64,$(shell $(CC) -dumpmachine)))
export TARGETARCH=AMD64
endif
endif

#
# CFlags
#
ifeq "$(TARGETARCH)" "AMD64"
CFLAGS = -D__KERNEL__ -Wall -Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing -c \
	-fno-common -fomit-frame-pointer -mno-red-zone -mcmodel=kernel -pipe \
	-fno-reorder-blocks -finline-limit=2000 -fno-strength-reduce -DEXPORT_SYMTAB -DMODULE -DCONFIG_PM \
	-DKBUILD_BASENAME=nvnet $(INCLDIR) -I$(SYSINCLUDE) $(ARCHDEFS)
else
CFLAGS = -c -Wall -DLINUX -DMODULE -DEXPORT_SYMTAB -D__KERNEL__ -O2 \
		-Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common \
        -fomit-frame-pointer -pipe -mpreferred-stack-boundary=2 -march=i686 -DMODULE -DCONFIG_PM \
        $(ALIGN) -I$(SYSINCLUDE) $(ARCHDEFS)
endif

#
# Kernel version
#
# Make sure build script can override this value
TARGET_KERNEL ?= $(shell $(CC) $(CFLAGS) -E -dM $(VERSION_FILE) | \
           grep UTS_RELEASE | awk '{ print $$3 }' | sed 's/\"//g')

#
# Modversion settings
#  
CFLAGS += $(if $(wildcard $(SYSINCLUDE)/linux/modversions.h), -DMODVERSIONS -include $(SYSINCLUDE)/linux/modversions.h)

#
# Module installation directory
#
INSTALL_DIR = /lib/modules/$(TARGET_KERNEL)/kernel/drivers/net


#
# Objcopy
#
OBJCOPY=objcopy

all: $(TARGET) 

$(TARGET): $(SRC) $(NVENETLIB) $(MCPINCLUDE)
	$(CC) $(CFLAGS) $(SRC)
	ld -r -o $(TEMP) $(OBJ) $(NVENETLIB)
	$(OBJCOPY) --strip-symbol="gcc2_compiled." $(TEMP)
	cp $(TEMP) $(TARGET)
	rm $(TEMP)
	rm -f nvenet.o

#
# Install the NVIDIA module
#

# Double install; try first without setting root, then try again with
# so we can build rpm files
install:
	mkdir -p $(INSTROOT)/$(INSTALL_DIR)
ifeq  "$(UID)" "0"
	install -b -m 644 -o root $(TARGET) $(INSTROOT)/$(INSTALL_DIR)
	/sbin/depmod -a 
else
	install -b -m 644 $(TARGET) $(INSTROOT)/$(INSTALL_DIR)
endif


#
# Uninstall the NVIDIA module
#
uninstall:
	@if [ -f $(INSTALL_DIR)/$(TARGET) ]; then \
		rm -f $(INSTALL_DIR)/$(TARGET); \
		/sbin/depmod -a ; \
	fi;

#
# Delete generated files
#
clean:
	rm -f $(TARGET)
	rm -f nvenet.o
