#
# Copyright (c) nVidia Corporation
#
# Patch i810 audio driver for use with nForce hardware
#

MODULE = nvaudio.o

# SYSINCLUDE can be overridden by environment variable or command line argument
# TARGET_KERNEL can be overridden by build system
TARGET_KERNEL ?= $(shell uname -r)
LINUXDIR = $(shell uname -r | awk '{sub(/-.*/,"",$$0); print $$0}')
TARGET_CPU ?= $(shell uname -m)

# Find kernel sources, if they're installed
ifeq ($(filter build, $(notdir $(wildcard /lib/modules/$(TARGET_KERNEL)/*))), build)
	KERNSRC = /lib/modules/$(TARGET_KERNEL)/build
else
	KERNSRC = /usr/src/linux-$(LINUXDIR)
endif

SYSINCLUDE ?= $(KERNSRC)/include

#Test to see which version of -{m,f}align-functions to use
OLDALIGN = -malign-functions=4
NEWALIGN = -falign-functions=4
BUILDCHECK = rm -f tp.c; if test -x tp; then echo "good"; fi;
TESTBUILD = rm -f tp; $(CC) -falign-functions=4 tp.c -o tp >/dev/null 2>&1;
MAKEPROG = echo "main() {}" > tp.c;
TESTPROG = $(shell $(MAKEPROG) $(TESTBUILD) $(BUILDCHECK))
ifeq ($(TESTPROG), good)
ALIGN = $(NEWALIGN)
else
ALIGN = $(OLDALIGN)
endif

CFLAGS= -D__KERNEL__ $(ARCHDEFS) -I$(SYSINCLUDE) -Wall -Wstrict-prototypes \
        -O -fno-strict-aliasing -mpreferred-stack-boundary=2 \
        -march=$(TARGET_CPU) $(ALIGN) -DMODULE

OBJS = \
	nvhw.o \
	nvrec.o \
	nvwavout.o \
	nvspdif.o \
	nvcrush.o \
	nvmain.o \
	$(NULL)

#
# Modversion settings
#  
CFLAGS += $(if $(wildcard $(SYSINCLUDE)/linux/modversions.h), -DMODVERSIONS -include $(SYSINCLUDE)/linux/modversions.h)

all: 
	$(MAKE) $(MODULE)

$(MODULE):	$(OBJS)
	$(LD) -r $^ -o $@

$(OBJS): chaninfo.h nvhw.h
nvrec.o: nvrec.h
nvwavout.o: nvwavout.h
nvspdif.o: nvspdif.h
nvcrush.o nvmain.o: nvrec.h nvwavout.h nvspdif.h

# Installation
TARGETDIR = /lib/modules/$(TARGET_KERNEL)/kernel/drivers/sound


# Do fake install if not root (needed for building rpms)
install:
ifeq	($(wildcard $(MODULE)), $(MODULE))
	mkdir -p $(INSTROOT)/$(TARGETDIR)
ifeq  "$(UID)" "0"
	install -b -m 644 -o root $(MODULE) $(INSTROOT)/$(TARGETDIR)
	/sbin/depmod -a
else
	install -b -m 644 $(MODULE) $(INSTROOT)/$(TARGETDIR)
endif # uid is root
endif # module

uninstall:
	rm -f $(TARGETDIR)/$(MODULE)
	/sbin/depmod -a

clean:
	rm -f *.o *~ core
