#
# Makefile for building the agpgart module standalone.
#
# Modelled closely on the drm kernel Makefile.linux in the XFree86 source
# tree.
#
# $TG: agpgart/GNUmakefile,v 1.6.2.10 2003/04/14 15:46:11 dawes Exp $
#

.SUFFIXES:

MODS = agpgart.o

PROGS = testgart # readgtt

AGPHEADERS = agp.h

AGPLINHEADERS = agp_backend.h

AGPOBJS = agpgart_be.o agpgart_fe.o

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

MODCFLAGS = $(CFLAGS) -D__KERNEL__ -DMODULE -fomit-frame-pointer

# Get the current kernel version.  For Red Hat, append "custom" for matching
# against the version in linux/version.h.

VERSION := $(shell uname -r)

# For Red Hat...
RHVERS := $(shell uname -r)custom

# If SRCDIR is specified, use that source tree, and don't do any version
# checking.  This is useful for building modules for a specific kernel
# source tree regardless of what kernel is running.
#
# Otherwise, look in the "standard" place for the running kernel's source
# tree.

ifndef SRCDIR
TREE := /lib/modules/$(VERSION)/build/include
else
TREE := $(SRCDIR)/include
endif

INCLUDES := -I. -I$(TREE)

all::

ifndef SRCDIR

# SuSE has the version.h and autoconf.h headers for the current kernel
# in /boot as /boot/vmlinuz.version.h and /boot/vmlinuz.autoconf.h.
# Check these first to see if they match the running kernel.

BOOTVERSION_PREFIX = /boot/vmlinuz.

V := $(shell if [ -f $(BOOTVERSION_PREFIX)version.h ]; then \
	gcc -E -nostdinc -include $(BOOTVERSION_PREFIX)version.h \
		$(INCLUDES) picker.c 2>/dev/null \
	| grep -s 'RELEASE = ' | cut -d' ' -f3; fi)

ifeq ($(V),"$(VERSION)")
VERSIONOK := 1
HEADERFROMBOOT := 1
else
ifeq ($(V),"$(RHVERS)")
  VERSIONOK := 1
  HEADERFROMBOOT := 1
else
# Check the version in the standard header location.
  V := $(shell gcc -E -nostdinc $(INCLUDES) picker.c 2>/dev/null \
	| grep -s 'RELEASE = ' | cut -d' ' -f3)
ifeq ($(V),"$(VERSION)")
    VERSIONOK := 1
else
ifeq ($(V),"$(RHVERS)")
      VERSIONOK := 1
else
# Version doesn't match the running kernel.
      VERSIONOK := 0
endif
endif
endif
endif

else
# Override the version check with $(SRCDIR) is specified.
VERSIONOK := 1
endif

# Make the /boot versions of the version.h and autoconf.h files available
# when needed.

ifeq ($(HEADERFROMBOOT),1)
PICKERINCLUDES := -include $(BOOTVERSION_PREFIX)version.h \
		  -include $(BOOTVERSION_PREFIX)autoconf.h $(INCLUDES)

includes:: version.h autoconf.h

version.h: $(BOOTVERSION_PREFIX)version.h
	rm -f $@
	ln -s $< $@

autoconf.h: $(BOOTVERSION_PREFIX)autoconf.h
	rm -f $@
	ln -s $< $@

else
PICKERINCLUDES := $(INCLUDES)
endif


ifeq ($(VERSIONOK),0)
info::; @echo Error: cannot find header matching kernel version $(VERSION)
else
SMP := $(shell gcc -E -nostdinc $(PICKERINCLUDES) picker.c 2>/dev/null \
        | grep -s 'SMP = ' | cut -d' ' -f3)
MODULES := $(shell gcc -E -nostdinc $(PICKERINCLUDES) picker.c 2>/dev/null \
        | grep -s 'MODULES = ' | cut -d' ' -f3)
MODVERSIONS := $(shell gcc -E -nostdinc $(PICKERINCLUDES) picker.c 2>/dev/null \
        | grep -s 'MODVERSIONS = ' | cut -d' ' -f3)
MACHINE := $(shell echo `uname -m`)
endif

ifndef SRCDIR
info::;@echo === Running kernel version: $(VERSION)
endif
info::;@echo === Kernel headers in $(TREE)
ifeq ($(HEADERFROMBOOT),1)
info::;@echo === Using kernel version.h and autoconf.h headers from /boot
endif
info::;@echo === SMP=${SMP} MODULES=${MODULES} MODVERSIONS=${MODVERSIONS}
info::;@echo === Compiling for machine $(MACHINE)

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

ifeq ($(SMP),1)
MODCFLAGS += -D__SMP__
endif
ifeq ($(MODVERSIONS),1)
MODCFLAGS += -DMODVERSIONS -include $(TREE)/linux/modversions.h
endif

$(MODS):: info includes

agpgart.o:: $(AGPOBJS)
	$(LD) -r $(AGPOBJS) -o $@

testgart: testgart.c
	$(CC) -I. -o $@ $<

readgtt: readgtt.c
	$(CC) -I. -o $@ $<

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

$(AGPOBJS): $(AGPHEADERS)

includes:: linux

linux:
	rm -f linux
	ln -s . linux

clean:
	rm -f *.o
	rm -f linux
	rm -f version.h
	rm -f autoconf.h
	rm -f $(PROGS)

