#!/usr/bin/make
# Makefile for building Linux Broadcom 5706 kernel diags driver as a module.
# $id$

# PREFIX may be set by the RPM build to set the effective root.
PREFIX=
ifeq ($(shell ls /lib/modules/$(shell uname -r)/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/$(shell uname -r)/build
endif

b06kdiag-objs := b06_diag.o diag_pal.o diag_quiet.o

# list of internal source files
DIAGCOREOBJ= diag_core.o

DIAGEXTOBJ= b06_diag.o diag_pal.o

ifeq ($(shell uname -p), ppc64)
  LDFLAGS= -r -m elf64ppc
else
  LDFLAGS= -r #-m elf64ppc
endif

BCM_KVER_MAJOR=$(shell uname -r | cut -c1)
BCM_KVER=older_kernel

ifeq ($(BCM_KVER_MAJOR), 3)
BCM_KVER=newer_kernel
endif

ifeq ($(BCM_KVER_MAJOR), 2)
# check if 2.4 kernel or 2.5+ kernel
BCM_KVER_2DIGITS:=$(shell uname -r | cut -c1-3 | sed 's/2\.[56]/2\.6/')
ifeq ($(BCM_KVER_2DIGITS), 2.6)
BCM_KVER=newer_kernel
endif
endif

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

BCM_DRV = b06kdiag.ko

EXTRA_CFLAGS= -DLINUX -DKDIAG

ifneq ($(KERNELRELEASE),)

obj-m += b06kdiag.o

else

default:
	make -C $(LINUX) SUBDIRS=$(shell pwd) modules
	rm b06kdiag.ko b06kdiag.ko.unsigned -f
	$(LD) $(LDFLAGS) -o b06kdiag.o $(DIAGEXTOBJ) $(DIAGCOREOBJ)
	make -C $(LINUX) SUBDIRS=$(shell pwd) modules

endif

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

BCM_DRV = b06kdiag.o

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

LD = ld
CC = gcc

CFLAGS=-DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -I$(LINUX)/include \
	-DLINUX -DKDIAG

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

  ifneq ($(shell ls /opt/cross/bin/powerpc64-linux-ld > /dev/null 2>&1 && echo ppcld),)
    LD=/opt/cross/bin/powerpc64-linux-ld
  endif

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

ifdef SMALL
  CFLAGS += -Os
else
  CFLAGS += -O2
endif

all: b06kdiag.o

b06kdiag.o: $(DIAGEXTOBJ) $(DIAGCOREOBJ)
	$(LD) $(LDFLAGS) -i $^ -o $@

endif # ifeq ($(BCM_KVER),newer_kernel)

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

ifeq ($(BCM_KVER), newer_kernel)
install: default
else
install: $(BCM_DRV)
endif
	mkdir -p $(PREFIX)/lib/modules/$(shell uname -r)/kernel/drivers/net;
	install -m 444 $(BCM_DRV) $(PREFIX)/lib/modules/`uname -r`/kernel/drivers/net;
	@if [ "$(PREFIX)" = "" ]; then /sbin/depmod -a ;\
	else echo " *** Run '/sbin/depmod -a' to update the module database.";\
	fi

.PHONEY: all clean install tar

small:
	@make SMALL=1

clean:
	-rm -f b06kdiag.o b06kdiag.ko b06kdiag.mod.c b06kdiag.mod.o $(b06kdiag-objs)

