CC  = gcc
CPP = g++
INSTALL = install
DEPEND = mkdep
MV = mv
LD = ld

WARNINGS    =	-Wall -Wwrite-strings -Wpointer-arith -Wcast-align \
    		    -Wstrict-prototypes -Wnested-externs \
	        	-Wpointer-arith

CFLAGS      =  $(WARNINGS)
MODCFLAGS   =	$(CFLAGS) -D__KERNEL__ -DMODULE

SUBDIRS =

RULES = build

# **** Parse config file

include config.mk

ifeq ($(CFG_DEBUG), YES)
CFLAGS += -DDEBUG -g -O
else
CFLAGS += -O3 -fomit-frame-pointer
endif

ifeq ($(CFG_MEMORY_STATS), YES)
CFLAGS += -DMEMORY_STATS
endif

# **** Get right version of kernel headers

VERSION := $(shell uname -r)
A       := /lib/modules/$(VERSION)/build/include
B       := /usr/src/linux-$(VERSION)/include
C       := /usr/src/linux/include
D       := /usr/include

V := $(shell gcc -E -nostdinc -I$A picker.c 2>/dev/null \
      | grep -s 'RELEASE = ' | cut -d' ' -f3)
ifeq ($(V),"$(VERSION)")
LNXTREE := $A
else
  V := $(shell gcc -E -nostdinc -I$B picker.c 2>/dev/null \
		| grep -s 'RELEASE = ' | cut -d' ' -f3)
ifeq ($(V),"$(VERSION)")
LNXTREE := $B
else
    V := $(shell gcc -E -nostdinc -I$C picker.c 2>/dev/null \
          | grep -s 'RELEASE = ' | cut -d' ' -f3)
ifeq ($(V),"$(VERSION)")
LNXTREE := $C
else
    V := $(shell gcc -E -nostdinc -I$D picker.c 2>/dev/null \
          | grep -s 'RELEASE = ' | cut -d' ' -f3)
ifeq ($(V),"$(VERSION)")
LNXTREE := $D
else
LNXTREE := 0
endif
endif
endif
endif

ifeq ($(LNXTREE),0)
all:; @echo Error: Could not locate kernel tree in $A $B $C $D
else

KNLMODDIR = /lib/modules/$(VERSION)

ifeq ($(shell if test -d $(KNLMODDIR)/kernel; then echo yes; fi),yes)
INSTALLDIR=$(KNLMODDIR)/kernel/drivers/video
else
INSTALLDIR=$(KNLMODDIR)/video
endif

# **** Detect kernel settings

INCLUDES = -I$(LNXTREE) -I.
SMP := $(shell gcc -E -nostdinc -I$(LNXTREE) picker.c 2>/dev/null \
		| grep -s 'SMP = ' | cut -d' ' -f3)
MODULES := $(shell gcc -E -nostdinc -I$(LNXTREE) picker.c 2>/dev/null \
			| grep -s 'MODULES = ' | cut -d' ' -f3)
MODVERSIONS := $(shell gcc -E -nostdinc -I$(LNXTREE) picker.c 2>/dev/null \
				| grep -s 'MODVERSIONS = ' | cut -d' ' -f3)
SIS := $(shell gcc -E -nostdinc -I$(LNXTREE) picker.c 2>/dev/null \
		| grep -s 'SIS = ' | cut -d' ' -f3)
MACHINE := $(shell echo `uname -m`)
AGP := $(shell gcc -E -nostdinc -I$(LNXTREE) picker.c 2>/dev/null \
		| grep -s 'AGP = ' | cut -d' ' -f3)
ifeq ($(AGP),0)
AGP := $(shell gcc -E -nostdinc -I$(LNXTREE) picker.c 2>/dev/null \
	| grep -s 'AGP_MODULE = ' | cut -d' ' -f3)
endif
endif
ifeq ($(AGP),0)
all:; @echo Error: This version of kernel do not support AGP devices
endif

# **** Additionnal flags on kernel settings

ifeq ($(MACHINE),alpha)
MODCFLAGS+= -ffixed-8 -mno-fp-regs -mcpu=ev56 -Wa,-mev6 
endif
ifeq ($(SMP),1)
MODCFLAGS += -D__SMP__
endif
ifeq ($(MODVERSIONS),1)
MODCFLAGS += -DMODVERSIONS -include $(LNXTREE)/linux/modversions.h
endif

export LNXTREE MODCFLAGS CFLAGS

# **** Sources and rules

INCLUDES   =  -I. -I$(LNXTREE)

CORE_DRIVERS =  parhelia #winparhelia #mtxsys
CORE_OBJS    =  parhelia/mtx_parhelia_core.o

SUBDIRS    +=  $(CORE_DRIVERS)

OBJS       =  mtx_drv.o mtx_dev.o mtx_ctx.o mtx_mem.o mtx_fops.o mtx_ioctl.o mtx_vm.o mtx_agp.o
HEADERS    =  mtx.h mtx_drv.h

# XXX For now, memory heaps are implemented in root MTX
CFLAGS += -DMTX_WITH_MEMORY_HEAPS
OBJS  += mtx_heap.o

OBJS += $(CORE_OBJS)

all: $(OBJS) $(RULES); @echo Kernel headers from $(LNXTREE) were used.

.PHONY : build
build:
		@for i in $(SUBDIRS) ; \
		do \
			echo "making" all "in $(CURRENT_DIR)/$$i..."; \
			set -e; $(MAKE) -C $$i $(MFLAGS) all; \
		done;
		$(RM) mtx.o mtx.o
		$(LD) -r -o mtx.o $(OBJS) 

reset:
	$(CC) -o reset reset.c $(INCLUDES)

%.o: %.c Makefile
	$(CC) $(MODCFLAGS) $(INCLUDES) -c $< -o $@

$(OBJS): $(HEADERS)
    
clean:
		@for i in $(SUBDIRS) ; \
    	do \
			echo "making" clean "in $(CURRENT_DIR)/$$i..."; \
			$(MAKE) -C $$i $(MFLAGS) clean; \
		done;
		$(RM) -f *.o *.a *~ core

depend:
	@for i in $(SUBDIRS) ; \
	do \
		$(MAKE) -C $$i $(MFLAGS) depend; \
	done;
	$(DEPEND) $(INCLUDES) $(MODCFLAGS) $(wildcard *.c)

install:
	$(INSTALL) -m 0644 -o root -g root mtx.o $(INSTALLDIR)/mtx.o
