ifndef EFX_UPSTREAM

# The EFX_UPSTREAM variable is never defined.  Sections of the Makefile
# that are only needed for an out-of-tree build are guarded by
# "ifndef EFX_UPSTREAM" and removed using sed when it is exported to a
# kernel tree.

# The KERNELRELEASE variable is defined by kbuild.  Some parts of the
# Makefile should only be included when this is the top-level Makefile
# and not when it is included by kbuild; these are guarded by
# "ifndef KERNELRELEASE".

ifndef KERNELRELEASE

# Configuration
export CONFIG_SFC := m
export CONFIG_SFC_DEBUGFS := y
export CONFIG_SFC_MTD := m

export CONFIG_SFC_TUNE := m

ifeq ($(MAKECMDGOALS),export-srpm)

ifndef KSRCDIR
$(error KSRCDIR not defined)
endif

else # !export-srpm

# Get kernel version and source directory.  Either may be specified and
# we work out the other automatically.  If neither is specified then we
# assume the current kernel version.
ifdef KSRCDIR
ifndef KVER
KVER := $(shell sed -r 's/^\#define UTS_RELEASE "(.*)"/\1/; t; d' $(KSRCDIR)/include/linux/utsrelease.h $(KSRCDIR)/include/linux/version.h 2>/dev/null)
ifeq ($(KVER),)
$(error Failed to find kernel version for $(KSRCDIR))
endif
endif
else # !KSRCDIR
ifndef KVER
KVER := $(shell uname -r)
endif
KSRCDIR := /lib/modules/$(KVER)/build
endif

KVERPARTS = $(subst -, ,$(subst ., ,$(KVER)))
ifneq ($(wordlist 1,2,$(KVERPARTS)),2 6)
$(error Kernel version $(KVER) is not supported\; minimum version is 2.6.5)
endif
ifneq ($(filter 0 1 2 3 4,$(word 3,$(KVERPARTS))),)
$(error Kernel version $(KVER) is not supported\; minimum version is 2.6.5)
endif

# RedHat ships with the MTD header files in the wrong place...
KERNEL_MTD_HEADERS := $(wildcard $(KSRCDIR)/include/mtd/*.h)
GLIBC_MTD_HEADERS := $(wildcard /usr/include/mtd/*.h)
DUMMY := $(shell rm -f ./mtd)
ifndef KERNEL_MTD_HEADERS
ifdef GLIBC_MTD_HEADERS
$(warning "MTD headers in the wrong place (probably RedHat)")
DUMMY := $(shell ln -sf /usr/include/mtd ./mtd)
else
$(warning "MTD headers missing.  Try installing glibc-kernheaders")
CONFIG_SFC_MTD := n
endif
endif

endif # export-srpm

endif # !KERNELRELEASE
endif # !EFX_UPSTREAM

# Final objects
sfc_o = sfc.o
sfc_mtd_o = sfc_mtd.o

# Constituent objects
sfc_elements_o :=
sfc_elements_o += efx.o
sfc_elements_o += falcon.o
sfc_elements_o += tx.o
sfc_elements_o += rx.o
sfc_elements_o += mentormac.o
sfc_elements_o += falcon_gmac.o
sfc_elements_o += falcon_xmac.o
sfc_elements_o += alaska.o
sfc_elements_o += i2c-direct.o
sfc_elements_o += selftest.o
sfc_elements_o += driverlink.o
ifeq ($(CONFIG_SFC_DEBUGFS),y)
sfc_elements_o += debugfs.o
endif
sfc_elements_o += ethtool.o
sfc_elements_o += xfp_phy.o
sfc_elements_o += mdio_10g.o
sfc_elements_o += txc43128_phy.o
sfc_elements_o += tenxpress.o
sfc_elements_o += lm87_support.o
sfc_elements_o += boards.o
sfc_elements_o += sfe4001.o
sfc_elements_o += pm8358_phy.o
sfc_elements_o += null_phy.o
ifndef EFX_UPSTREAM
ifneq ($(MAKECMDGOALS),export)
sfc_elements_o += generic_lro.o
sfc_elements_o += kernel_compat.o
sfc_elements_o += workarounds.o
endif
endif # !EFX_UPSTREAM

sfc_mtd_elements_o := mtd.o

obj-$(CONFIG_SFC) += $(sfc_o)
obj-$(CONFIG_SFC_MTD) += $(sfc_mtd_o)

sfc-objs = $(sfc_elements_o)
sfc_mtd-objs = $(sfc_mtd_elements_o)

ifndef EFX_UPSTREAM

sfc_tune_o = sfc_tune.o

sfc_tune_elements_o = tweak.o
sfc_tune_elements_o += idle.o


# Compiler flags
EXTRA_CFLAGS += -I${PWD} -Wall -DEFX_NOT_UPSTREAM=1 -DEFX_USE_KCOMPAT=1

ifndef NOWERROR
EXTRA_CFLAGS += -Werror
endif

# Debugging-enabled builds
ifndef NDEBUG
EXTRA_CFLAGS += -DEFX_ENABLE_DEBUG -g
endif

obj-$(CONFIG_SFC_TUNE) += $(sfc_tune_o)
sfc_tune-objs = $(sfc_tune_elements_o)


all_sfc_o = $(obj-m) $(obj-y)
all_sfc_ko = $(subst .o,.ko,$(all_sfc_o))

ifndef KERNELRELEASE

default : modules

all : modules

config.h : FORCE
	@echo >config.latest.h
ifeq ($(CONFIG_SFC_DEBUGFS),y)
	@echo >>config.latest.h "#define CONFIG_SFC_DEBUGFS 1"
endif
	@if [ -f config.h ] && diff -q config.h config.latest.h; then	\
		rm config.latest.h;					\
	else								\
		mv config.latest.h config.h;				\
		echo Updated config.h;					\
	fi

extraversion.h : FORCE
	@./extraversion.sh $(KSRCDIR) >extraversion.latest.h
	@if [ -f extraversion.h ] && diff -q extraversion.h extraversion.latest.h; then \
		rm extraversion.latest.h;				     \
	else								     \
		mv extraversion.latest.h extraversion.h;		     \
		echo Updated extraversion.h;				     \
	fi

modules : config.h extraversion.h
	$(MAKE) -C $(KSRCDIR) M=$$(pwd)
# strip objects
ifndef DEBUG
	@strip --strip-debug $(all_sfc_ko)
endif

# Work around for undefined KERNELRELEASE in FC4 2.6.16 kernels
ifneq ($(filter 2.6.16-%_FC4 2.6.16-%_FC4smp,$(KVER)),)
INSTALL_FLAGS := KERNELRELEASE=$(KVER)
else
INSTALL_FLAGS :=
endif

modules_install :
	echo INSTALL_MOD_PATH=$(INSTALL_MOD_PATH)
	$(MAKE) -C $(KSRCDIR) M=$$(pwd) $(INSTALL_FLAGS) modules_install

clean : clean_modules

clean_modules :
	rm -f *.o *.s *.ko *.mod.c *.symvers config.h extraversion.h
	$(MAKE) -C $(KSRCDIR) M=$$(pwd) clean

.PHONY : all modules modules_install clean clean_modules FORCE


endif # !KERELRELEASE
endif # !EFX_UPSTREAM
