CC= gcc
RM = rm -f

MODS = R200DDK.o

DRMTEMPLATES =	drm_auth.h drm_bufs.h drm_context.h drm_dma.h drm_drawable.h \
		drm_drv.h drm_fops.h drm_init.h drm_ioctl.h drm_lists.h \
		drm_lock.h drm_memory.h drm_proc.h drm_stub.h drm_vm.h
DRMHEADERS =	drm.h drmP.h compat-pre24.h

R200OBJS=	R200_drv.o R200_cp.o

R200HEADERS= ati_drm.h R200_drv.h $(DRMHEADERS) $(DRMTEMPLATES)

INC=		/usr/include

CFLAGS=		-O2 $(WARNINGS)
WARNINGS=	-Wall -Wwrite-strings -Wpointer-arith -Wcast-align \
			-Wstrict-prototypes -Wnested-externs -Wpointer-arith

# -Wshadow -Winline -- make output too noisy
MODCFLAGS=      $(CFLAGS) -D__KERNEL__ -DMODULE -fomit-frame-pointer 

VERSION := $(shell uname -r)

INSTALLDIR= /lib/modules/$(VERSION)/kernel/drivers/char

# **** Start of SMP/MODVERSIONS detection

# First, locate correct tree for this kernel version.  If we find a
# matching tree, we assume that we can rely on that tree's autoconf.h.
# This may not be correct, but it is the best assumption we can make.

A := /usr/src/linux-$(VERSION)/include
B := /usr/src/linux/include
C := /usr/include

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

ifeq ($(TREE),0)
all:; @echo Error: Could not locate kernel tree in $A $B $C
else
SMP := $(shell gcc -E -nostdinc -I$(TREE) picker.c 2>/dev/null \
	| grep -s 'SMP = ' | cut -d' ' -f3)
MODULES := $(shell gcc -E -nostdinc -I$(TREE) picker.c 2>/dev/null \
	| grep -s 'MODULES = ' | cut -d' ' -f3)
MODVERSIONS := $(shell gcc -E -nostdinc -I$(TREE) picker.c 2>/dev/null \
	| grep -s 'MODVERSIONS = ' | cut -d' ' -f3)
AGP := $(shell gcc -E -nostdinc -I$(TREE) picker.c 2>/dev/null \
	| grep -s 'AGP = ' | cut -d' ' -f3)
PARAMS := $(shell if fgrep kill_fasync $(TREE)/linux/fs.h 2>/dev/null \
	| egrep -q '(band|int, int)'; then echo 3; else echo 2; fi)
MACHINE := $(shell echo `uname -m`)
ifeq ($(AGP),0)
AGP := $(shell gcc -E -nostdinc -I$(TREE) picker.c 2>/dev/null \
	| grep -s 'AGP_MODULE = ' | cut -d' ' -f3)
endif

ifeq ($(AGP),1)
MODCFLAGS += -DCONFIG_AGP -DCONFIG_AGP_MODULE
DRMOBJS += agpsupport.o
endif


all::;@echo === KERNEL HEADERS IN $(TREE)
all::;@echo === SMP=${SMP} MODULES=${MODULES} MODVERSIONS=${MODVERSIONS} AGP=${AGP}
all::;@echo === kill_fasync has $(PARAMS) parameters
all::;@echo === Compiling for machine $(MACHINE)
all::;@echo === AGP support: $(AGP)

ifeq ($(MODULES),0)	
all::;@echo
all::;@echo "*** Kernel modules must be configured.  Build aborted."
all::;@echo
endif
endif

# **** Handle SMP/MODVERSIONS
ifeq ($(SMP),1)
MODCFLAGS += -D__SMP__
endif
ifeq ($(MODVERSIONS),1)
MODCFLAGS += -DMODVERSIONS -include $(TREE)/linux/modversions.h
endif
ifeq ($(PARAMS),3)
MODCFLAGS += -DKILLFASYNCHASTHREEPARAMETERS
endif

all:: $(MODS)

# **** End of configuration
$(MODS): $(R200OBJS)
	$(LD) -r $^ -o $@

R200_drv.o: R200_drv.c
	$(CC) $(MODCFLAGS) -DEXPORT_SYMTAB -I$(TREE) -c $< -o $@

R200_cp.o: R200_cp.c
	$(CC) $(MODCFLAGS) -DEXPORT_SYMTAB -I$(TREE) -c $< -o $@

# .o files are used for modules
%.o: %.c
	$(CC) $(MODCFLAGS) -I$(TREE) -c $< -o $@

$(DRMOBJS): $(DRMHEADERS)
ifeq ($(AGP),1)
$(R200OBJS): $(R200HEADERS)
endif	

install::all
	@if [ -d $(INSTALLDIR) ]; \
	then echo " "; \
	else mkdir $(INSTALLDIR); \
	fi
	install $(MODS) $(INSTALLDIR)/$(MODS)
	@depmod

uninstall::
	$(RM) $(INSTALLDIR)/$(MODS)
	@depmod -q

clean::
	$(RM) *.CKP *.ln *.BAK *.bak *.o core errs ,* *~ *.a .emacs_* tags TAGS make.log MakeOut   "#"*












