prefix = /usr/local
exec_prefix = ${prefix}
bindir = ${exec_prefix}/bin
include= ${prefix}/include
libdir = ${exec_prefix}/lib

INSTALL= /usr/bin/install -c
KERNELDIR=/lib/modules/3.5.0-54-generic/build

###############################################################################
# Check kernel version.
# 2.4 is built outside kernel, 2.6 uses kernel build system
ifeq ($(shell uname -r | cut -d . -f 1-2),2.4)
#2.4.x

KCFLAGS= -Wall -Wimplicit -Wformat -O2 -DNDEBUG -DHAVE_CONFIG_H -pipe
KERNELCONFIG=/lib/modules/3.5.0-54-generic/build/.config

DRV_GCC=gcc

include $(KERNELCONFIG)

INCLUDE= -I$(KERNELDIR)/include

ifdef CONFIG_SMP
	CFLAGS += -D__SMP__ -DSMP
endif

# Voyager subarch support
mflags-$(CONFIG_X86_VOYAGER)    := -I$(KERNELDIR)/include/asm-i386/mach-voyager

# VISWS subarch support
mflags-$(CONFIG_X86_VISWS)      := -I$(KERNELDIR)/include/asm-i386/mach-visws

# NUMAQ subarch support
mflags-$(CONFIG_X86_NUMAQ)      := -I$(KERNELDIR)/include/asm-i386/mach-numaq

# BIGSMP subarch support
mflags-$(CONFIG_X86_BIGSMP)     := -I$(KERNELDIR)/include/asm-i386/mach-bigsmp

#Summit subarch support
mflags-$(CONFIG_X86_SUMMIT) := -I$(KERNELDIR)/include/asm-i386/mach-summit

# generic subarchitecture
mflags-$(CONFIG_X86_GENERICARCH) := -I$(KERNELDIR)/include/asm-i386/mach-generic

# ES7000 subarch support
mflags-$(CONFIG_X86_ES7000)     := -I$(KERNELDIR)/include/asm-i386/mach-es7000

# default subarch .h files
mflags-y += -I$(KERNELDIR)/include/asm-i386/mach-default

CFLAGS += $(mflags-y)

CFLAGS += $(INCLUDE) -D__KERNEL__ -DMODULE -fomit-frame-pointer
CFLAGS += -DDIAGNOSTIC
CFLAGS += $(DEBUGINC)
CFLAGS += $(KCFLAGS)

ACFILE = $(KERNELDIR)/include/linux/autoconf.h
# Detect modversions support
ifneq ($(strip $(shell grep 'define CONFIG_MODVERSIONS 1' $(ACFILE))),)
CFLAGS += -DMODVERSIONS -include $(KERNELDIR)/include/linux/modversions.h
endif

CC = ${DRV_GCC}

CSRC  = ./divdi3_exp.c ./divdi3_impl.c

DIVOBJ= divdi3_exp.o divdi3_impl.o
DRV   = divdi3

.PHONY: all install depend

all: $(DRV)

install: install-drv

depend: .depend

install-drv: $(DRV)
	$(INSTALL) -d /lib/modules/`uname -r`/extra
	$(INSTALL) $(DRV) /lib/modules/`uname -r`/extra
	depmod -a >/dev/null

divdi3: $(DIVOBJ)
	$(LD) -r $(DIVOBJ) -o divdi3

.depend: $(CSRC) Makefile
	@echo -n "rebuilding .depend ... "
	@$(CC) -M -MG $(CFLAGS) $(CSRC) | \
		sed -e '/\\$$/N' -e 's/\\\n//' | sed -e '/\\$$/N' -e 's/\\\n//' | \
		sed -e '/\\$$/N' -e 's/\\\n//' | sed -e '/\\$$/N' -e 's/\\\n//' | \
		sed -e '/\\$$/N' -e 's/\\\n//' | sed -e '/\\$$/N' -e 's/\\\n//' | \
		sed -e 's%/usr/[a-zA-Z0-9\._/-]*%%g' -e 's/  */ /g' >.depend
	@echo "done"

ifneq ($(MAKECMDGOALS),depend)
include .depend
endif

else
####################################################################################
# assume 2.6 or 3.0 kernel compile

ifneq ($(KERNELRELEASE),)
# Called by kbuild

obj-m += divdi3.o

divdi3-objs = ./divdi3_exp.o ./divdi3_impl.o

else

PWD  := $(shell pwd)

default:
	@echo '    Building divdi3 driver for Linux kernel.'
	$(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) modules

install:
	@echo '    Installing divdi3 driver for Linux kernel.'
	$(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) modules_install

endif  # End kbuild check

CSRC  = $(wildcard *.c)

.PHONY: all install

all: default

depend:

endif # kernel version check

####################################################################################
#common targets

distdir:
	cp divdi3_exp.c divdi3_impl.c Makefile.in COPYING README $(distdir)

clean:
	$(RM) *~ *.o $(DRV)

distclean: clean
	$(RM) .*o.cmd divdi3.mod.c *.ko Makefile .depend
	$(RM) -Rf .tmp_versions

.PHONY: dvi pdf ps info html tags ctags
dvi pdf ps info html tags ctags:
