# Makefile

ifneq (,$(findstring 2.6,$(KERNELRELEASE)))
EXTRA_CFLAGS += -D KERNEL26
obj-m := kaodv.o
else
SRC =	main.c list.c debug.c timer_queue.c \
	libipq.c  packet_input.c

OBJS =	$(SRC:%.c=%.o)
OBJS_ARM = $(SRC:%.c=%-arm.o)
OBJS_MIPS = $(SRC:%.c=%-mips.o)

KERNEL=$(shell uname -r)
# Change to compile against different kernel (can be overridden):
KERNEL_DIR=/lib/modules/$(KERNEL)/build
KERNEL_INC=$(KERNEL_DIR)/include

# Some shell scripting to find out Linux kernel version
VERSION=$(shell if [ ! -d $(KERNEL_DIR) ]; then echo "No linux source found!!! Check your setup..."; exit; fi; grep ^VERSION $(KERNEL_DIR)/Makefile | cut -d' ' -f 3)
PATCHLEVEL=$(shell grep ^PATCHLEVEL $(KERNEL_DIR)/Makefile | cut -d' ' -f 3)
SUBLEVEL=$(shell grep ^SUBLEVEL $(KERNEL_DIR)/Makefile | cut -d' ' -f 3)

# Compiler and options:
CC=gcc
# You might want to use gcc32 for the kernel module on Fedora core 1
KCC=gcc
#ARM_CC=Arm-linux-gcc
ARM_CC=/opt/Embedix/tools/arm-linux/bin/gcc
MIPS_CC=mipsel-uclibc-gcc
CPP=g++
OPTS=-Wall -O3
CPP_OPTS=-Wall

# Comment out to disable debug operation...
DEBUG=-g -DDEBUG
# Add extra functionality. Uncomment or use "make DEFS=-D<feature>" on 
# the command line.
DEFS=-DCONFIG_GATEWAY#-DLLFEEDBACK
CFLAGS=$(OPTS) $(DEBUG) $(DEFS)
LD=

# ARM specific configuration goes here:
#=====================================
ARM_INC=

# Archiver and options
AR=ar
AR_FLAGS=rc

# These are the options for the kernel module with kernel 2.4.x:
#==============================================
KINC=-nostdinc $(shell $(CC) -print-search-dirs | sed -ne 's/install: \(.*\)/-I \1include/gp') -I$(KERNEL_INC)
KDEFS=-D__KERNEL__ -DMODULE
KCFLAGS=-Wall -O2 $(KDEFS) $(KINC)
KCFLAGS_ARM=-Wall -O2 -D__KERNEL__ -DMODULE -nostdinc $(shell $(ARM_CC) -print-search-dirs | sed -ne 's/install: \(.*\)/-I \1include/gp') -I$(KERNEL_INC)
KCFLAGS_MIPS=-Wall -mips2 -O2 -fno-pic -mno-abicalls -mlong-calls -G0 -msoft-float -D__KERNEL__ -DMODULE -nostdinc $(shell $(MIPS_CC) -print-search-dirs | sed -ne 's/install: \(.*\)/-I \1include/gp') -I$(KERNEL_INC)

.PHONY: default clean install uninstall depend tags mdtmrd-arm docs

# Check for kernel version
ifeq ($(PATCHLEVEL),6)
default: mdtmrd kaodv.ko
else 
# Assume kernel 2.4
default: mdtmrd kaodv.o
endif

arm: mdtmrd-arm kaodv-arm.o

$(OBJS): %.o: %.c Makefile
	$(CC) $(CFLAGS) -c -o $@ $<

$(OBJS_ARM): %-arm.o: %.c Makefile
	$(ARM_CC) $(CFLAGS) -DARM $(ARM_INC) -c -o $@ $<

$(OBJS_MIPS): %-mips.o: %.c Makefile
	$(MIPS_CC) $(CFLAGS) -DMIPS $(MIPS_INC) -c -o $@ $<

mdtmrd: $(OBJS) Makefile
	$(CC) $(CFLAGS) $(LD) -o $@ $(OBJS)

mdtmrd-arm: $(OBJS_ARM) Makefile
	$(ARM_CC) $(CFLAGS) -DARM -o $(@:%-arm=%) $(OBJS_ARM)

mdtmrd-mips: $(OBJS_MIPS) Makefile
	$(MIPS_CC) $(CFLAGS) -DMIPS -o $(@:%-mips=%) $(OBJS_MIPS)

mdtmr-uu.o: $(OBJS_NS_CPP) $(OBJS_NS)
	$(AR) $(AR_FLAGS) libmdtmr-uu.a $(OBJS_NS_CPP) $(OBJS_NS) > /dev/null

# Kernel module:
kaodv.o: kaodv.c
	$(KCC) $(KCFLAGS) -c -o $@ $<

kaodv.ko: kaodv.c
	$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules

kaodv-arm.o: kaodv.c
	$(ARM_CC) $(KCFLAGS_ARM) -c -o $(@:%-arm.o=%.o) $<

kaodv-mips.o: kaodv.c
	$(MIPS_CC) $(KCFLAGS_MIPS) -c -o $(@:%-mips.o=%.o) $<
tags:
	etags *.c *.h
indent:
	indent -kr -l 80 *.c \
	$(filter-out $(SRC_NS_CPP:%.cc=%.h),$(wildcard *.h))
depend:
	@echo "Updating Makefile dependencies..."
	@makedepend -Y./ -- $(DEFS) -- $(SRC) &>/dev/null
	@makedepend -a -Y./ -- $(KDEFS) kaodv.c &>/dev/null

install: default
	install -s -m 755 mdtmrd /usr/sbin/mdtmrd
	@if [ ! -d /lib/modules/$(KERNEL)/aodv ]; then \
		mkdir /lib/modules/$(KERNEL)/aodv; \
	fi

	@echo "Installing kernel module in /lib/modules/$(KERNEL)/aodv/";
	@if [ -f ./kaodv.ko ]; then \
		install -m 644 kaodv.ko /lib/modules/$(KERNEL)/aodv/kaodv.ko; \
	else \
		install -m 644 kaodv.o /lib/modules/$(KERNEL)/aodv/kaodv.o; \
	fi
	/sbin/depmod -a
uninstall:
	rm -f /usr/sbin/mdtmrd
	rm -rf /lib/modules/$(KERNEL)/aodv

docs:
	cd docs && $(MAKE) all
clean: 
	rm -f mdtmrd *~ *.o core *.log *.ko *.mod.[co] .*.cmd *.ver *.mod .*.d
#cd docs && $(MAKE) clean

endif

# DO NOT DELETE

main.o: defs.h timer_queue.h list.h debug.h packet_input.h
list.o: list.h
debug.o: defs.h timer_queue.h list.h debug.h
timer_queue.o: timer_queue.h defs.h list.h debug.h
packet_input.o: defs.h timer_queue.h list.h debug.h
packet_input.o: libipq.h packet_input.h
libipq.o: libipq.h
