# Makefile for airo_mpi to implement building for both 2.4 and 2.6 kernels
# Lots of this using the work from the madwifi Makefile. Thanks to them.
#
# Peter Johanson <latexer@gentoo.org>

#
# Set KERNEL_VERSION and KERNEL_PATH variables here or in the environment
# to build for a different kernel than the one currently running
#
#KERNEL_VERSION=	
#KERNEL_PATH=	
#CC=gcc32

#
#
# Path to the Linux kernel, include files, and where to install the modules.
#
ifndef KERNEL_VERSION
KERNEL_VERSION=	$(shell uname -r)
endif
ifndef KERNEL_PATH
KERNEL_PATH=	/lib/modules/${KERNEL_VERSION}/build
endif
LINUX=		${KERNEL_PATH}/include
MODULE_PATH=	/lib/modules/${KERNEL_VERSION}/kernel/drivers/net/wireless
#
# A numeric version of the target system's version string, suitable
# for doing if checks in make files.
#
LINUX_VERSION=	$(shell echo $(KERNEL_VERSION) | \
		awk '{ split($$0,a,"."); print a[1]*100+a[2]; }')
#

RECALC_SIGPENDING := $(shell grep recalc_sigpending \
	$(KERNEL_PATH)/include/linux/sched.h | \
	grep -v recalc_sigpending_tsk | grep -c task_)

SIGMASK_LOCK := $(shell grep -c sigmask_lock $(KERNEL_PATH)/include/linux/sched.h)

PDE := $(shell grep -c PDE $(KERNEL_PATH)/include/linux/proc_fs.h)

# On Red Hat we need to check if there is a .config file in the kernel
# source directory.  If there isn't, we need to check if there's a
# matching file in the configs subdirectory.

ifneq ($(HAVECONFIG),y)
HAVECONFIG := $(shell if [ -e $(KERNEL_PATH)/.config ]; then echo y; fi)
endif

ifneq ($(HAVECONFIG),y)
REL_BASE := $(shell echo $(KERNEL_VERSION) | sed 's/-.*//')
REL_TYPE := $(shell echo $(KERNEL_VERSION) | sed 's/[0-9.-]//g' | sed 's/nptl//')
MACHINE  := $(shell uname -m)
ifeq ($(REL_TYPE),)
RHCONFIG := configs/kernel-$(REL_BASE)-$(MACHINE).config
else
RHCONFIG := configs/kernel-$(REL_BASE)-$(MACHINE)-$(REL_TYPE).config
endif
HAVECONFIG := $(shell if [ -e $(KERNEL_PATH)/$(RHCONFIG) ]; then echo y; fi)
ifneq ($(HAVECONFIG),y)
RHCONFIG :=
endif
endif

ifneq ($(HAVECONFIG),y)
$(error Cannot find a kernel config file)
endif


#
# Basic build components.
#
CLEANFILES=	.depend

ifneq ($(RHCONFIG),)
include ${KERNEL_PATH}/$(RHCONFIG)
else
include ${KERNEL_PATH}/.config
endif

COPTS+= -Wall

ifdef CONFIG_SMP
COPTS+=	-D__SMP__ -DSMP
endif

ifndef CONFIG_FRAME_POINTER
COPTS+=	-fomit-frame-pointer
endif

ifdef CONFIG_MODVERSIONS
ifneq (${LINUX_VERSION}, 204)
MODOPTS=-DMODVERSIONS -include $(KERNEL_PATH)/include/linux/modsetver.h
else
MODOPTS=-DMODVERSIONS -include $(KERNEL_PATH)/include/linux/modversions.h
endif

GENKSYMS=	/sbin/genksyms
ifdef CONFIG_SMP
GENKSYMS+=	-p smp_
endif
endif

ifneq ($(RECALC_SIGPENDING),0)
COPTS+= -DRECALC_SIGPENDING_1_ARG
endif

ifneq ($(SIGMASK_LOCK),0)
COPTS+= -DHAVE_SIGMASK_LOCK
endif

ifneq ($(PDE),0)
COPTS+= -DHAVE_PDE
endif

KMODNAME=	airo_mpi
#
# 2.4, 2.5, 2.6 handling.
# Wish we had proper conditionals to use here..
#
ifneq (${LINUX_VERSION}, 204)
KMODSUF=	ko
INCS+=		-I${KERNEL_PATH}/include/asm/mach-default
MODOPTS+=	-DKBUILD_BASENAME=${KMODNAME} -DKBUILD_MODNAME=${KMODNAME}
CLEANFILES+=	*.mod.c
else
KMODSUF=	o
endif
#

SRCS=	airo_mpi.c
OBJS=	airo_mpi.o

INCS+=	-I. -I${LINUX}

ifneq (${LINUX_VERSION}, 204)
OBJS+=		airo_mpi.mod.o
endif

ALL=	${KMODNAME}.${KMODSUF}
CFLAGS=	${INCS} -D__KERNEL__ -Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing -fno-common -pipe -mpreferred-stack-boundary=2 -DMODULE ${MODOPTS} ${COPTS}
LDFLAGS=

all:	${ALL}

install: all
	test -d ${DESTDIR}/${MODULE_PATH} || mkdir -p ${DESTDIR}/${MODULE_PATH}
	cp ${ALL} ${DESTDIR}/${MODULE_PATH}

${KMODNAME}.${KMODSUF}: ${OBJS} Makefile
ifneq (${LINUX_VERSION}, 204)
	${LD} -o $@ -r ${LDFLAGS} ${OBJS}
endif

ifneq (${LINUX_VERSION}, 204)
airo_mpi.mod.c: airo_mpi.o
	${KERNEL_PATH}/scripts/modpost airo_mpi.o
airo_mpi.mod.o:	airo_mpi.mod.c
endif

clean:
	rm -f ${OBJS} ${ALL} ${CLEANFILES}

depend: beforedepend .depend afterdepend
.depend: ${SRCS} Makefile
	rm -f .depend
	-${CC} -M ${CFLAGS} ${SRCS} > .depend
beforedepend:
afterdepend:
-include .depend
