#!/usr/bin/make
# Makefile for building Linux Broadcom Gigabit ethernet driver as a module.
# $id$
KVER=
ifeq ($(KVER),)
  KVER=$(shell uname -r)
endif

# PREFIX may be set by the RPM build to set the effective root.
PREFIX=
ifeq ($(shell ls /lib/modules/$(KVER)/build > /dev/null 2>&1 && echo build),)
  ifeq ($(shell ls /usr/src/linux > /dev/null 2>&1 && echo linux),)
    LINUX=
  else
    LINUX=/usr/src/linux
  endif
else
  LINUX=/lib/modules/$(KVER)/build
  ifeq ($(shell ls /lib/modules/$(KVER)/source > /dev/null 2>&1 && echo source),)
    LINUXSRC=$(LINUX)
  else
    LINUXSRC=/lib/modules/$(KVER)/source
  endif
endif

ifneq ($(shell grep netdump_mode $(LINUXSRC)/include/linux/kernel.h > /dev/null 2>&1 && echo rh),)
  NETDUMP_CFLAG = -DRED_HAT_LINUX_KERNEL
else
  NETDUMP_CFLAG =
endif

# check if 2.4 kernel or 2.5+ kernel
BCM_KVER:=$(shell echo $(KVER) | cut -c1-3 | sed 's/2\.[56]/2\.6/')

ifeq ($(BCM_KVER), 2.6)
# Makefile for 2.5+ kernel

BCM_DRV = tg3.ko

ifneq ($(KERNELRELEASE),)

obj-m += tg3.o

EXTRA_CFLAGS = $(NETDUMP_CFLAG)

else

default:
	make -C $(LINUX) SUBDIRS=$(shell pwd) modules

endif

else # ifeq ($(BCM_KVER),2.6)
# Makefile for 2.4 kernel

BCM_DRV = tg3.o

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

CC = gcc

CFLAGS=-DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -I$(LINUX)/include $(NETDUMP_CFLAG)

ifeq ($(shell grep netif_poll_disable $(LINUXSRC)/include/linux/netdevice.h > /dev/null 2>&1 && echo newnetif),)
  CFLAGS+=-DOLD_NETIF
endif

ifeq ($(wildcard ~/rpmdir),)
  rpmdir = /usr/src/redhat
else
  rpmdir = $(wildcard ~/rpmdir)
endif

ARCH:=$(shell uname -m)

ifeq ($(ARCH),x86_64)
  CFLAGS+=-mno-red-zone -mcmodel=kernel -pipe -finline-limit=2000
endif

ifeq ($(ARCH),ia64)
  CFLAGS+=-pipe -ffixed-r13 -mfixed-range=f10-f15,f32-f127 -falign-functions=32
endif

ifeq ($(ARCH),ppc64)
  ifneq ($(shell ls /opt/cross/bin/powerpc64-linux-gcc > /dev/null 2>&1 && echo ppcgcc),)
    CC=/opt/cross/bin/powerpc64-linux-gcc
  endif

  CFLAGS+=-fno-strict-aliasing -fno-common -fomit-frame-pointer -msoft-float -pipe -mminimal-toc -fno-builtin
endif

CFLAGS += -O2

all: tg3.o

endif # ifeq ($(BCM_KVER),2.6)

tg3.4.gz:
	gzip -c tg3.4 > tg3.4.gz


ifeq ($(BCM_KVER), 2.6)
install: default tg3.4.gz
else
install: $(BCM_DRV) tg3.4.gz
endif
	mkdir -p $(PREFIX)/lib/modules/$(KVER)/kernel/drivers/net;
	install -m 444 $(BCM_DRV) $(PREFIX)/lib/modules/$(KVER)/kernel/drivers/net;
	@if [ "$(PREFIX)" = "" ]; then /sbin/depmod -a ;\
	else echo " *** Run '/sbin/depmod -a' to update the module database.";\
	fi
	install -m 444 tg3.4.gz $(PREFIX)/usr/share/man/man4;\

.PHONEY: all clean install tar

clean:
	-rm -f tg3.o tg3.ko tg3.mod.c tg3.mod.o tg3.4.gz

