NDISWRAPPER_VERSION=0.12
EXTRA_VERSION=

DISTFILES=Makefile ndiswrapper.h pe_loader.c pe_loader.h  ndis.c ndis.h \
				   hal.c ntoskernel.c ntoskernel.h  iw_ndis.c iw_ndis.h \
				   wrapper.c wrapper.h  proc.c misc_funcs.c \
				   divdi3.c longlong.h usb.c usb.h

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

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

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

SRC_DIR=$(shell pwd)

.PHONY: prereq_check gen_exports clean dist_clean install

all : prereq_check gen_exports default

wrapper.o: wrapper.c pe_loader.h ndis.h

iw_ndis..o:	iw_ndis.c iw_ndis.h

pe_loader.o: pe_loader.c pe_loader.h

misc_funcs.o: misc_funcs.c ndis.h misc_funcs_exports.h

hal.o: hal.c ndis.h hal_exports.h

ntoskernel.o: ntoskernel.c ntoskernel.h ntoskernel_exports.h

ndis.o: ndis.c ndis.h ndis_exports.h

proc.o: proc.c ndis.h

divdi3.o: divdi3.c longlong.h

iw_ndis.h: ndis.h

ndis.h : ntoskernel.h

ntoskernel.h : ndiswrapper.h

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

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

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

OBJS := wrapper.o iw_ndis.o pe_loader.o ndis.o ntoskernel.o \
		  misc_funcs.o proc.o divdi3.o hal.o

ifeq ($(KPSUB),24)
-include $(KSRC)/.config
endif

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

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

ifdef DEBUG_TIMER
CFLAGS += -DDEBUG_TIMER
endif

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
	  @ if [ $(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

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

distclean: clean
	rm -f *_exports.h

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

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

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

default: $(OBJS)
	$(LD) -r -o $(MODULE) $(OBJS)
else

MODULE := ndiswrapper.ko
obj-$(CONFIG_NDISWRAPPER) := ndiswrapper.o 
ndiswrapper-objs := $(OBJS)

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


endif

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

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

