#
# Copyright (c) nVidia Corporation
#
# Makefile to build nForce audio  driver
#

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

ifeq "$(TARGETARCH)" "AMD64"
# Cflags section for 64bit linux
CFLAGS = -g -D__KERNEL__ -Wall -Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing -c \
	 -fno-common -fomit-frame-pointer -mno-red-zone -mcmodel=kernel -pipe \
	 -fno-reorder-blocks -finline-limit=2000 -fno-strength-reduce -DMODULE \
	 -DKBUILD_BASENAME=nvaudio -I$(SYSINCLUDE) $(ARCHDEFS)
else
# Cflags section for 32bit linux
CFLAGS= -D__KERNEL__ $(ARCHDEFS) -I$(SYSINCLUDE) -Wall -Wstrict-prototypes \
        -O -fno-strict-aliasing -mpreferred-stack-boundary=2 \
        -march=$(TARGET_CPU) $(ALIGN) -DMODULE
endif


# Add the Driver version which got defined from build script.
NVVERSION ?=9999
CFLAGS  += -DDRIVERVER=\"$(NVVERSION)\"

#Test to see how many parameters remap_page_range takes
BUILDCHECK_RPR =if test -f trpr.o; then echo "good"; fi;
TESTBUILD_RPR = rm -f trpr.o; $(CC) $(CFLAGS) -c nvcrush.c -o trpr.o >/dev/null 2>&1;
TESTPROG_RPR = $(shell $(TESTBUILD_RPR) $(BUILDCHECK_RPR))
ifneq ($(TESTPROG_RPR), good)
CFLAGS += -DREMAP_NEW
endif

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)
	mkdir -p $(INSTROOT)/usr/include
ifeq  "$(UID)" "0"
	install -b -m 755 -o root $(MODULE) $(INSTROOT)/$(TARGETDIR)
	install -b -m 444 -o root nvioctl.h $(INSTROOT)/usr/include/nvaudio.h
	/sbin/depmod -a
else
	install -b -m 755 $(MODULE) $(INSTROOT)/$(TARGETDIR)
	install -b -m 444 nvioctl.h $(INSTROOT)/usr/include/nvaudio.h
endif # uid is root
endif # module

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

clean:
	rm -f *.o *~ core
