#!/usr/bin/make all
# Makefile for building updated Linux PCI network device drivers as modules.
# Distributed for use with the Scyld driver set.
#
# By default this Makefile tries to build and install for the running kernel.
# You may override this with command line parameters
#   make KERN_INCLUDE=/lib/modules/2.4.20-12smp/build/include
#   make KERNVER=2.4.20-12smp
#
# You may specify a kernel with the following variables.
#  KERN_INCLUDE  The include directory for the target kernel.
#  MODULEDIR     The directory where netdriver modules should be installed.
#  PCMCIA        The include file for the PCMCIA subsystem.
#
# Setting KERNVER may allow this Makefile to guess the above.
# $id$

# PREFIX may be set by the RPM build to set the effective root.
PREFIX=
# Set KERNVER on the command line to build modules for a kernel other than
# the version that is currently running.

PCMCIA?=/usr/src/pcmcia/include
CC?=gcc
CFLAGS=-DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -I$(KERN_INCLUDE) $(EXTRACFLAGS)

# We go to great effort to work with the myriad ways that distributions
# have gotten confused about where kernel include files should be put.
# There have been over twenty broken "solutions", which is far more painful
# to deal with than just leaving the problem alone.

# The path for kernel "include" header files is
#   /lib/modules/$(KERNVER)/build/include/
#   /usr/src/linux-$(KERNVER)/include
#   /usr/src/linux/include
# The path for destination modules is
#   /lib/modules/$(KERNVER)/kernel/drivers/
#   /lib/modules/$(KERNVER)/
# however PCMCIA modules use the latter directory in Red Hat.

define newline


endef
ifndef KERNVER
  KERNVER=$(shell uname -r)
  $(warning No kernel version has been specified.$(newline)\
  Assuming $(KERNVER).$(newline)\
  Set the KERNVER variable to specify a different kernel.)
endif

# Do not use PREFIX for include files or when checking if a path exists!
ifndef KERN_INCLUDE
   KERN_INCLUDE=$(firstword $(wildcard /lib/modules/$(KERNVER)/build/include/ /usr/src/linux-$(KERNVER)/include /usr/src/linux/include))
else
   $(warning Using $(KERN_INCLUDE) for the kernel header files.)
endif

ifeq "$(KERN_INCLUDE)" ""
   $(warning The kernel header files do not appear to be installed on)
   $(warning this machine.  You likely need to install the kernel source)
   $(warning or the kernel-headers package.)
endif

ifneq "$(wildcard $(KERN_INCLUDE)/pcmcia/driver_ops.h)" ""
    PCMCIA=$(KERN_INCLUDE)
endif

MODULEDIR=$(PREFIX)$(firstword $(wildcard /lib/modules/$(KERNVER)/kernel/drivers/ /lib/modules/$(KERNVER)/))


# Some architectures require magic gcc flags for kernel compiles.
#  First canoicalize the name:  *86 becomes i386.
ARCH:=$(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
ifneq "$(wildcard $(KERN_INCLUDE)/../make.inc)" ""
   include $(KERN_INCLUDE)/../make.inc
   CFLAGS+=KERN_CFLAGS
else 
  ifeq ($(ARCH),i386)
    CFLAGS+=-pipe -fno-strength-reduce
  endif
  ifeq ($(ARCH),alpha)
    CFLAGS+=-pipe -mno-fp-regs -ffixed-8 -mcpu=ev5
  endif
  ifeq ($(ARCH),x86_64)
    CFLAGS+=-pipe -fno-strength-reduce -fno-strict-aliasing -fomit-frame-pointer -mno-red-zone -mcmodel=kernel -fno-reorder-blocks -finline-limit=2000 -fno-strength-reduce -nostdinc -iwithprefix include
  endif
endif

# Work around Red Hat 7.0 GCC version lossage.
ifneq ($(wildcard /usr/bin/kgcc),)
  CC=kgcc
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 = $(KERN_INCLUDE)/linux/modversions.h
ifneq ($(wildcard $(MODVER_H)),"")
  CFLAGS += -DMODVERSIONS
endif

DRV_OFILES = 3c59x.o eepro100.o epic100.o hamachi.o intel-gige.o \
        myson803.o natsemi.o ns820.o rtl8139.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 (,$(filter 2.4.%, $(KERNVER)))
# This is a 2.4 kernel, do not build ne2k-pci.o
else
# This is a 2.2 or earlier kernel, check that we have 8390.h
ifneq (,$(wildcard $(KERN_INCLUDE)/../drivers/net/8390.h))
  DRV_OFILES += ne2k-pci.o
  PATH_8390H=$(KERN_INCLUDE)/../drivers/net/
endif
endif

OFILES= $(DRV_OFILES) pci-scan.o

ifeq ($(wildcard $(PCMCIA)/pcmcia/driver_ops.h),"")
   $(warning The PCMCIA header files could not be found.$(newline)\
 No CardBus support will be built.)
else
  OFILES += cb_shim.o
  CARDBUS= 3c575_cb.o epic_cb.o tulip_cb.o realtek_cb.o
  CARDBUS-FLAGS = -DCARDBUS -I$(KERN_INCLUDE) -I$(PCMCIA) -I/usr/include
  CB_SHIM = cb_shim.o
endif

all: pci-skeleton.o pci-scan.o $(DRV_OFILES) $(CB_SHIM)
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 $(PATH_8390H)/8390.h
	$(CC) $(CPPFLAGS) $(CFLAGS) -I$(PATH_8390H)/ -c $<

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

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

netdrivers.tgz: Makefile *.[ch] netdrivers.spec
	tar cfvz $@ $^

rpm: netdrivers.tgz
	rpm -ta $^

# 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 $@ 

.DELETE_ON_ERROR: ChangeLog
ChangeLog: Makefile *.[ch] netdrivers.spec
	rcs2log -h scyld.com -u "`echo -en "becker\tDonald Becker\tbecker@scyld.com"`" -i 5 | sed -e 's@/home/repository/@@g' -e 's@^     \* netdrivers/@  \* netdrivers/@g' > $@

.PHONEY: all clean install cardbus tar

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