TARGET=cmhp
TARGETLIB=.
SRCS=cmhp_wrap.c
LIBS=cmhp_lib.o

OS_VERSION=$(shell uname -r)
OS_MARCH=$(shell uname -m)
OS_PREP=
OS_UP=
OS_SMP=
OS_ENT=

# check if 2.4 kernel or 2.5+ kernel
OS_KERNEL=$(shell uname -r | cut -c1-3 | sed 's/2\.[56]/2\.6/')

EFS_VER=

RM  = /bin/rm -f
CC = gcc
CXX = g++
LD = ld

OBJS_ = ${SRCS:.c=.o}
OBJS__ = ${OBJS_:.s=.o}

ifneq "$(OS_UP)$(OS_SMP)$(OS_ENT)" ""
	REDHAT_DFLAGS = -D__BOOT_KERNEL_H -D__MODULE_KERNEL_$(OS_MARCH)=1 -D__BOOT_KERNEL_UP=$(OS_UP) -D__BOOT_KERNEL_SMP=$(OS_SMP) -D__BOOT_KERNEL_ENTERPRISE=$(OS_ENT) -D__BOOT_KERNEL_BIGMEM=$(OS_ENT) -D__BOOT_KERNEL_HUGEMEM=$(OS_ENT)
endif


ifeq "$(OS_KERNEL)" "2.6"

KBUILD_VERBOSE=1

ifneq "$(KERNELRELEASE)" ""

obj-m := _$(TARGET).o
_$(TARGET)-objs := cmhp_wrap.o 
EXTRA_CFLAGS = -D__NO_VERSION__ -DEFS_VER=\"$(EFS_VER)\" $(DFLAGS) $(IFLAGS) 
# EXTRA_CFLAGS += $(REDHAT_DFLAGS)
else

_$(TARGET).ko:
	make -C /lib/modules/$(OS_VERSION)/build V=1 SUBDIRS=$(shell pwd) modules
endif

$(TARGET).ko: _$(TARGET).ko $(LIBS)
	$(LD) -i $^ -o $@

all: allex $(TARGET).ko

clean: commonclean cleanex
ifneq "$(TARGETLIB)" "."
	${RM} -rf ${TARGETLIB}
endif

install: installex
ifneq "$(TARGET)" ""
ifneq "$(TARGETLIB)" ""
	mkdir -p $(TARGETLIB)
	cp -af ${TARGET}.ko ${TARGETLIB}
endif
endif

else

CFLAGS = $(REDHAT_DFLAGS) -DEFS_VER=\"$(EFS_VER)\" -DMODULE -D__KERNEL__ -D__NO_VERSION__ -Wall -Wstrict-prototypes -I/lib/modules/$(OS_VERSION)/build/include -include /lib/modules/$(OS_VERSION)/build/include/linux/autoconf.h -include /lib/modules/$(OS_VERSION)/build/include/linux/modversions.h $(DFLAGS) $(WFLAGS) $(IFLAGS) 

CFLAGS += -fno-strict-aliasing -fno-common -fomit-frame-pointer -fno-builtin

ifeq "$(OS_MARCH)" "x86_64"
	CFLAGS+=-mno-red-zone -mcmodel=kernel -pipe -finline-limit=2000
endif

$(TARGET).o: $(OBJS__)  $(LIBS)
	$(LD) -i $^ -o $@

all: allex $(TARGET).o

clean: commonclean cleanex
ifneq "$(TARGETLIB)" "."
	${RM} -rf ${TARGETLIB}
endif

install: installex
ifneq "$(TARGET)" ""
ifneq "$(TARGETLIB)" ""
	mkdir -p $(TARGETLIB)
	cp -af ${TARGET}.o ${TARGETLIB}
endif
endif

endif

commonclean:
	rm -rf .tmp_versions
	${RM} .*.cmd ${TARGET}.o ${TARGET}.ko ${OBJS__} _${TARGET}.* 

allex:

cleanex:

installex:
