#!/usr/bin/make all
# Makefile for building updated Linux PCI network device drivers as modules.
# $id$

# PREFIX may be set by the RPM build to set the effective root.
PREFIX=
PCMCIA=/usr/src/pcmcia/include
LINUX=/usr/src/linux
CC=gcc
CFLAGS=-DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -I$(LINUX)/include

# Some architectures require magic gcc flags for kernel files.
ARCH:=$(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
ifeq ($(ARCH),i386)
    CFLAGS+=-pipe -fno-strength-reduce
endif
ifeq ($(ARCH),alpha)
    CFLAGS+=-pipe -mno-fp-regs -ffixed-8 -mcpu=ev5
endif

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

# If the modversion.h file exists we should pass a flag.
# Most of my drivers have been updated to not need this, but not the other
# kernel source files.
MODVER_H = $(LINUX)/include/linux/modversions.h
ifneq ($(wildcard $(MODVER_H)),"")
  CFLAGS += -DMODVERSIONS
endif

DRV_OFILES = 3c59x.o eepro100.o epic100.o hamachi.o natsemi.o\
        rtl8139.o starfire.o starfire.o sundance.o tulip.o via-rhine.o\
        winbond-840.o yellowfin.o

# We can only build the ne2k-pci. driver if we have a 8390.h file available.
ifneq ($(wildcard $(LINUX)/drivers/net/8390.h),)
  DRV_OFILES += ne2k-pci.o
endif

OFILES= $(DRV_OFILES) pci-scan.o
ifeq ($(wildcard $(PCMCIA)/pcmcia/driver_ops.h),$(PCMCIA)/pcmcia/driver_ops.h)
  OFILES += cb_shim.o
  CARDBUS= 3c575_cb.o epic_cb.o tulip_cb.o realtek_cb.o
  CARDBUS-FLAGS = -DCARDBUS -I$(LINUX)/include -I/usr/include -I$(PCMCIA)
  CB_SHIM = cb_shim.o
endif

all: pci-skeleton.o pci-scan.o $(DRV_OFILES)
tar: netdrivers.tgz

$(OFILES) pci-scan.o pci-skeleton.o: pci-scan.h kern_compat.h

pci-scan.o: pci-scan.h
# We need to pick up 8390.h
ne2k-pci.o: ne2k-pci.c $(LINUX)/drivers/net/8390.h
	$(CC) $(CPPFLAGS) $(CFLAGS) -I$(LINUX)/drivers/net/ -c $<

cb_shim.o: cb_shim.c
	 $(CC) $(CARDBUS-FLAGS) $(CFLAGS) -c $< -o $@ 

install: $(OFILES)
	install -m 444 pci-scan.o $(DRV_OFILES) $(PREFIX)/lib/modules/`uname -r`/net/
	@if [ -r cb_shim.o ]; then install -m 444 cb_shim.o $(PREFIX)/lib/modules/`uname -r`/pcmcia/; fi
	@if [ "$(PREFIX)" = "" ]; then /sbin/depmod -a ;\
	else echo " *** Run '/sbin/depmod -a' to update the module database.";\
	fi

netdrivers.tgz: Makefile *.[ch]
	tar cfvz $@ $^
rpm: netdrivers.tgz netdriver.spec
	cp -p $< ${rpmdir}/SOURCES/
	rpm -ba netdriver.spec

# These are old-style CardBus drivers that don't need pci-scan and cb_shim.
cardbus: $(CARDBUS)
3c575_cb.o: 3c59x.c
	 $(CC) $(CARDBUS-FLAGS) $(CFLAGS) -c $< -o $@ 
epic_cb.o: epic100.c
	 $(CC) $(CARDBUS-FLAGS) $(CFLAGS) -c $< -o $@ 
realtek_cb.o: rtl8139.c
	 $(CC) $(CARDBUS-FLAGS) $(CFLAGS) -c $< -o $@ 
tulip_cb.o: tulip.c
	 $(CC) $(CARDBUS-FLAGS) $(CFLAGS) -c $< -o $@ 

.PHONEY: all clean install cardbus tar

clean:
	-rm -f *.o netdrivers.tgz
