#####################################################################
#                                                                   #
#             Copyright (c)2004, Matrox Graphics Inc.               #
#         Parhelia, P650/P750 Millenium kernel module Makefile      #
#                                                                   #
#####################################################################

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

KERNELVERSION   = "$(shell uname -r | cut -b 1,2,3)"

#####################################################################
#
#                          Directories
#
#####################################################################

PWD             := $(shell pwd)

ifeq ($(KERNELVERSION), "2.4")

# Kernel 2.4 directories
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 knl-conf.c 2>/dev/null | grep -s 'RELEASE = ' | cut -d' ' -f3)
ifeq ($(V),"$(VERSION)")
  LNXTREE := $A
else
  V := $(shell gcc -E -nostdinc -I$B knl-conf.c 2>/dev/null | grep -s 'RELEASE = ' | cut -d' ' -f3)
  ifeq ($(V),"$(VERSION)")
    LNXTREE := $B
  else
    V := $(shell gcc -E -nostdinc -I$C knl-conf.c 2>/dev/null | grep -s 'RELEASE = ' | cut -d' ' -f3)
    ifeq ($(V),"$(VERSION)")
      LNXTREE := $C
    else
      V := $(shell gcc -E -nostdinc -I$D knl-conf.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)
  $(error ERROR: Could not locate kernel tree in $A $B $C $D)
endif

KNLMODDIR = /lib/modules/$(VERSION)

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

  MTXMODDIR     := $(PWD)
  
else

# Kernel 2.6 directories
KERNELTOP ?= /lib/modules/$(shell uname -r)
KERNELDIR = $(KERNELTOP)/build

KERNELSRC := $(shell test -d $(KERNELTOP)/source && echo $(KERNELTOP)/source || echo $(KERNELTOP)/build)
LNXTREE         := $(KERNELSRC)/include
INSTALLDIR      := $(KERNELDIR)/../kernel/drivers/char/matrox

ifneq ($(KERNELRELEASE),)
  #MTXMODDIR is set by command line
else
  MTXMODDIR   := $(PWD)
endif

endif # ifeq ($(KERNELVERSION), "2.4")

# Common directories
CORE_DRIVERS    := parhelia
MTXVXD          := mtxvxd
CPPUTILS_SUBDIR := cpp_utils
MODULE_SUBDIRS  := $(CORE_DRIVERS) $(MTXVXD)

CPPUTILS_PATH       = $(MTXMODDIR)/$(CPPUTILS_SUBDIR)
MTXHALCORE_INCLUDE  = $(MTXMODDIR)/$(CORE_DRIVERS)
MTXCORE_INCLUDE     = $(MTXMODDIR)/$(CORE_DRIVERS)/Main

MTX_INCLUDES        := $(PARHELIUX)/include
ifeq ($(INSTALLERBUILD),1)
MTX_INCLUDES        := $(PARHELIUX)/../include
endif
CPPUTILS_INCLUDES   := $(MTX_INCLUDES)/$(CPPUTILS_SUBDIR)
MTL_INCLUDES        := $(MTX_INCLUDES)/$(CPPUTILS_SUBDIR)/mtl
SERVICES_INCLUDES   := $(MTX_INCLUDES)/$(MTXVXD)

export MTX_INCLUDES CPPUTILS_INCLUDES MTL_INCLUDES SERVICES_INCLUDES
export CPPUTILS_PATH

#####################################################################
#
#  Check kernel configuration and set compilation flags accordingly
#
#####################################################################

SMP             := $(shell gcc -E -nostdinc -D__KERNEL__ -I$(LNXTREE) knl-conf.c 2>/dev/null \
                   | grep -s 'SMP = ' | cut -d' ' -f3)
MODULES         := $(shell gcc -E -nostdinc -D__KERNEL__ -I$(LNXTREE) knl-conf.c 2>/dev/null \
                   | grep -s 'MODULES = ' | cut -d' ' -f3)
ifeq ($(KERNELVERSION), "2.4")
  MODVERSIONS     := $(shell gcc -E -nostdinc -D__KERNEL__ -I$(LNXTREE) knl-conf.c 2>/dev/null \
                     | grep -s 'MODVERSIONS = ' | cut -d' ' -f3)
endif                   
AGP             := $(shell gcc -E -nostdinc -D__KERNEL__ -I$(LNXTREE) knl-conf.c 2>/dev/null \
                   | grep -s 'AGP = ' | cut -d' ' -f3)
ifeq ($(AGP),0)
  AGP             := $(shell gcc -E -nostdinc -D__KERNEL__ -I$(LNXTREE) knl-conf.c 2>/dev/null \
                     | grep -s 'AGP_MODULE = ' | cut -d' ' -f3)
endif
REDHAT_KERNEL   := $(shell gcc -E -nostdinc -D__KERNEL__ -I$(LNXTREE) knl-conf.c 2>/dev/null \
                   | grep -s 'REDHAT_KERNEL = ' | cut -d' ' -f3)


# Kernel configuration

ifeq ($(KERNELVERSION), "2.4")
  ifeq ($(AGP),0)
    $(error ERROR: This version of kernel do not support AGP devices)
  endif
    
  ifeq ($(MODULES),0)
    $(error ERROR: This version of kernel do not support loadable modules)
  endif
  
  ifeq ($(MODVERSIONS),1)
    EXTRA_CFLAGS_CONFIG += -DMODVERSIONS -include $(LNXTREE)/linux/modversions.h
  endif
endif

ifeq ($(SMP),1)
  EXTRA_CFLAGS_CONFIG  += -D__SMP__
endif

ifeq ($(REDHAT_KERNEL),1)
  EXTRA_CFLAGS_CONFIG  += -DREDHAT_KERNEL
endif


# Module configuration

ifeq ($(PARHELIUX)/config.mk,$(wildcard $(PARHELIUX)/config.mk))
  include $(PARHELIUX)/config.mk
endif

CFLAGS_DEBUG           := -DDEBUG -g -O -fno-inline-functions
CFLAGS_RELEASE         := -O2 -fomit-frame-pointer -finline-functions

ifeq ($(CFG_MTX_DEBUG), YES)
  CFLAGS_MTX_CONFIG_DBGINFO    += $(CFLAGS_DEBUG)
  CPPFLAGS_MTX_CONFIG          += -fno-inline
else
  CFLAGS_MTX_CONFIG_DBGINFO    += $(CFLAGS_RELEASE)
endif

ifeq ($(CFG_MTX_MEMORY_STATS), YES)
  CFLAGS_MTX_CONFIG    += -DMEMORY_STATS=1
else
  CFLAGS_MTX_CONFIG    += -DMEMORY_STATS=0
endif

#TODO remove this when mtx.o opensource will exist
ifeq ($(CFG_OPENPARHL), YES)
  CFLAGS_MTX_CONFIG    += -DOPENPARHL
endif

# Compilation settings
INCLUDES        := -I$(MTX_INCLUDES) \
                   -I$(SERVICES_INCLUDES) \
                   -I$(MTXMODDIR) -I$(MTXHALCORE_INCLUDE) -I$(MTXCORE_INCLUDE) \
                   -I$(LNXTREE) -I$(LNXTREE)/asm/mach-default 

WARNINGS        :=  -Wpointer-arith -Wcast-align -Wno-missing-braces \
                    -Wno-sign-compare

CFLAGS_MTX      +=  $(CFLAGS_MTX_CONFIG) -D__KERNEL__ -DOS_LINUX $(WARNINGS)   
EXTRA_CFLAGS    :=  $(CFLAGS_MTX) $(EXTRA_CFLAGS_CONFIG) $(CFLAGS_MTX_CONFIG_DBGINFO) -DMODULE $(INCLUDES)

# Then we add anything we do not want in EXTRA_CFLAGS used by kbuild
CFLAGS_MTX      += -Wall -Wwrite-strings   
CPPFLAGS        := $(CPPFLAGS_MTX_CONFIG) $(CFLAGS_MTX_CONFIG_DBGINFO) $(CFLAGS_MTX)  \
                   -fno-rtti -fno-exceptions -fno-use-cxa-atexit

ifeq ($(KERNELVERSION), "2.4")
  CPPFLAGS3D    := $(CPPFLAGS)
else
  CPPFLAGS3D    := $(CFLAGS_RELEASE) $(CFLAGS_MTX) \
                    -fno-rtti -fno-exceptions -fno-use-cxa-atexit
endif

# Flags specific to C files
CFLAGS_MTX      += -Wnested-externs $(CFLAGS_MTX_CONFIG_DBGINFO)

export LNXTREE EXTRA_CFLAGS CPPFLAGS CPPFLAGS3D INCLUDES MTXMODDIR

#####################################################################
#
#                              Objects
#
#####################################################################

CORE_OSOBJS     := $(CORE_DRIVERS)/MtxParhlParms.o
CORE_MAINOBJS   := $(CORE_DRIVERS)/mtx_parhelia.o
MTXDRV          := mtx.o
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 mtx_irq.o \
                   mtx_client.o mtx_os.o mtx_strintf.o

#####################################################################
#
#                              Rules
#
#####################################################################

ifeq ($(KERNELVERSION), "2.4")
  include $(MTXMODDIR)/Makefile_2.4
else
  include $(MTXMODDIR)/Makefile_2.6  
endif
