-include ../version

DISTFILES=divdi3.c hal.c iw_ndis.c iw_ndis.h loader.c loader.h \
		longlong.h Makefile misc_funcs.c ndis.c ndis.h \
		ndiswrapper.h ntoskernel.c ntoskernel.h \
		pe_linker.c pe_linker.h proc.c usb.c usb.h winnt_types.h \
		wrapper.c wrapper.h x86_64_stubs.S

KVERS ?= $(shell uname -r)
KSRC  ?= /lib/modules/$(KVERS)/build

KPSUB := $(shell echo $(KVERS) |  sed -e 's/\([^\.]*\)\.\([^\.]*\)\..*/\1\2/')

DESTDIR =
INST_DIR := $(DESTDIR)/lib/modules/$(KVERS)/misc

SRC_DIR=$(shell pwd)

-include $(KSRC)/.config

CFLAGS += $(shell [ -f $(KSRC)/include/linux/modversions.h ] && \
		  echo -DEXPORT_SYMTAB -DMODVERSIONS \
		  -include $(KSRC)/include/linux/modversions.h)

ifdef DEBUG
CFLAGS += -DDEBUG=$(DEBUG) -g -DDEBUG_TIMER 
endif

ifdef DEBUG_TIMER
CFLAGS += -DDEBUG_TIMER
endif

ifdef NDISWRAPPER_VERSION
CFLAGS += -DNDISWRAPPER_VERSION=\"${NDISWRAPPER_VERSION}\" \
		  -DEXTRA_VERSION=\"${EXTRA_VERSION}\"
endif

.PHONY: prereq_check gen_exports clean dist_clean \
		 x86_64_stubs install stack_check

all : prereq_check x86_64_stubs gen_exports default stack_check

OBJS := hal.o iw_ndis.o loader.o misc_funcs.o ndis.o ntoskernel.o \
		pe_linker.o proc.o wrapper.o

ifeq ($(CONFIG_USB),y)
OBJS += usb.o
endif
ifeq ($(CONFIG_USB),m)
OBJS += usb.o
endif

#ifeq ($(CONFIG_DEBUG_SPINLOCK),y)
#$(warning CONFIG_DEBUG_SPINLOCK conflicts with spinlocks used by \
#  Windows driver. There is a workaround for it in ndiswrapper, but it \
#  may not work correctly. Don't complain about kernel crashes until \
#  CONFIG_DEBUG_SPINLOCK is disabled.)
#endif

divdi3.o: divdi3.c longlong.h

hal.o: hal.c ntoskernel.h

iw_ndis..o:	iw_ndis.c iw_ndis.h wrapper.h

loader.o: loader.c loader.h ndis.h wrapper.h

misc_funcs.o: misc_funcs.c ndis.h

ndis.o: ndis.c ndis.h iw_ndis.h wrapper.h

ntoskernel.o: ntoskernel.c ndis.h usb.h

pe_linker.o: pe_linker.c

proc.o: proc.c ndis.h iw_ndis.h wrapper.h

usb.o: usb.c usb.h ndis.h

wrapper.o: wrapper.c wrapper.h iw_ndis.h ntoskernel.h loader.h

iw_ndis.h: ndis.h
loader.h: ndiswrapper.h
ndis.h: ntoskernel.h
ntoskernel.h: ndiswrapper.h winnt_types.h pe_linker.h
usb.h: ntoskernel.h
wrapper.h: ndis.h

hal_exports.h: hal.c
misc_funcs_exports.h: misc_funcs.c
ndis_exports.h: ndis.c
ntoskerlen_exports.h: ntoskernel.c
usb_exports.h: usb.c

ifeq ($(CONFIG_X86_64),y)
OBJS += x86_64_stubs.o
x86_64_stubs.o: x86_64_stubs.h x86_64_stubs.S

x86_64_stubs: x86_64_stubs.h

x86_64_stubs.h: hal.c misc_funcs.c ndis.c ntoskernel.c usb.c wrapper.c
	@if :; then \
		echo "# Do not edit this file; \
			 it is automatically generated"; \
		for file in $^; do \
			echo; \
			echo "# generated from $$file"; \
			sed -n \
				-e 's/.*WRAP_EXPORT(\([^)]\+\)).*/   \
					  win_to_lin_stub \1/p'   \
				-e 's/.*WRAP_FUNC_PTR(\([^)]\+\)).*/   \
					  win_to_lin_stub \1/p'   \
					   $$file | sort -u; \
		done; \
	fi > $@
else
x86_64_stubs:

OBJS += divdi3.o
endif

# generate exports symbol table from C files
%_exports.h: %.c
	@if :; then \
		echo "/* Do not edit this file; \
			 it is automatically generated from $< */"; \
		echo "#ifdef CONFIG_X86_64"; \
		sed -n \
			-e 's/.*WRAP_EXPORT(\([^)]\+\)).*/   \
				   extern void x86_64_\1(void);/p' \
			-e 's/.*WRAP_FUNC_PTR(\([^)]\+\)).*/   \
				   extern void x86_64_\1(void);/p' \
			   $< | sort; \
		echo "#endif"; \
		echo "struct wrap_export $(basename $<)_exports[] = {";\
		sed -n \
			-e 's/.*WRAP_EXPORT(\(_win_\)\([^)]\+\)).*/     \
				   WRAP_EXPORT_WIN_FUNC(\2),/p' \
			-e 's/.*WRAP_EXPORT(\([^)]\+\)).*/   \
				   WRAP_EXPORT_SYMBOL(\1),/p' \
			-e 's/.*WRAP_EXPORT_MAP(\("[^"]\+"\)[ ,\n]\+\([^)]\+\)).*/   \
				   {\1, (WRAP_EXPORT_FUNC)\2},/p' $< | sort; \
		echo "    {NULL, NULL}"; \
		echo "};"; \
	fi > $@

gen_exports: ndis_exports.h hal_exports.h ntoskernel_exports.h \
			 misc_funcs_exports.h usb_exports.h wrapper_exports.h

prereq_check:
	@ if [ ! -f $(KSRC)/include/linux/version.h ]; then \
		echo "Can't find kernel sources in $(KSRC);"; \
		echo "  give the path to kernel sources with KSRC=<path>\
		       argument to make";\
		exit 1;\
	  fi

clean:
	rm -rf $(MODULE) ndiswrapper.o $(OBJS) usb.o x86_64_stubs.o \
	   divdi3.o .*.ko.cmd .*.o.cmd ndiswrapper.mod.[oc] *~ .tmp_versions

distclean: clean
	rm -f *_exports.h .\#* x86_64_stubs.h

ifeq ($(KPSUB),24)
MODULE := ndiswrapper.o
CFLAGS  += -DLINUX -D__KERNEL__ -DMODULE -I$(KSRC)/include \
		  -Wall -Wstrict-prototypes -fomit-frame-pointer    \
		  -fno-strict-aliasing -pipe -O2

ifneq ($(CONFIG_X86_64),y)
CFLAGS += -mpreferred-stack-boundary=2 
endif
default: $(OBJS)
	$(LD) -r -o $(MODULE) $(OBJS)
else

MODULE := ndiswrapper.ko
obj-m := ndiswrapper.o 

ndiswrapper-objs := $(OBJS)

default:
	$(MAKE) -C $(KSRC) SUBDIRS=$(SRC_DIR) \
		NDISWRAPPER_VERSION=$(NDISWRAPPER_VERSION) \
		EXTRA_VERSION=$(EXTRA_VERSION) modules

endif

stack_check:
	  @ if [ "x$(CONFIG_X86_64)" = "x" -a $(KPSUB) -eq 26 ]; then \
		   if grep -q CONFIG_4KSTACKS $(KSRC)/.config; then \
			  if grep -q "CONFIG_4KSTACKS=y" $(KSRC)/.config; then \
				echo; echo; \
			  	echo "*** WARNING: "\
					 "Kernel is compiled with 4K stack size option"\
					 "(CONFIG_4KSTACKS); many Windows drivers will"\
					 "not work with this option enabled. Disable"\
					 "CONFIG_4KSTACKS option, recompile and install"\
					 "kernel";\
				echo; echo; \
			  fi;\
			else \
				echo; echo; \
			   echo "*** WARNING: Kernel seems to have 4K size stack option" \
			     "(CONFIG_4KSTACKS) removed; many Windows drivers will"\
			     "need at least 8K size stacks."\
			     "You should read wiki about 4K size stack issue."\
				 "Don't complain about crashes until you resolve this."; \
				echo; echo; \
			fi;\
		fi

install: prereq_check x86_64_stubs gen_exports default stack_check
	mkdir -p $(INST_DIR)
	install -m 0644 $(MODULE) $(INST_DIR)
	-/sbin/depmod -a

dist:
	@for file in $(DISTFILES); do \
	  cp  $$file $(distdir)/$$file; \
	done

