# This Makefile is automatically generated; do not edit
# Generated on 'builder75' on Wed Oct 27 19:01:35 PDT 2010

# to use this file, rename or delete the default Makefile, then rename or
# link this file to Makefile. then type 'make'

#
# KBUILD Makefile for the NVIDIA Linux kernel module.
#
# The motivation for replacing the original Makefile is the hope that this
# version will simplify the build and installation process. In the past,
# many architectural and cosmetic changes to the Linux kernel have made it
# difficult to maintain compatibility or required duplication of logic.
#
# Linux 2.6 introduces numerous such changes, many of which impact modules.
# Relying on KBUILD, some aspects of the build system otherwise difficult
# to support (for example, module versioning) are abstracted away and dealt
# with elsewhere, making life significantly easier here.
#
# The new approach currently has its own share of problems, some of which
# are architectural difficulties with KBUILD, others minor annoyances. For
# this reason, an improved version of the NVIDIA Makefile is available to
# those experiencing difficulties.
#
# kbuild Makefile originally developed by:
#
# Alistair J Strachan (alistair@devzero.co.uk) (first pass, enhancements)
# Christian Zander (zander@mail.minion.de) (enhancements)
#

all: install
install: package-install

#
# The NVIDIA kernel module base name and static file names. KBUILD will go
# ahead and append ".o" or ".ko" to form the final module name.
#

MODULE_NAME := nvidia
VERSION_HEADER := nv_compiler.h

#
# List of object files to link into NVIDIA kernel module; make sure KBUILD
# understands that we want a module.
#

CORE_OBJS := nv-kernel.o
RESMAN_GLUE_OBJS := nv.o nv-vm.o os-agp.o os-interface.o os-registry.o nv-i2c.o

$(MODULE_NAME)-objs := $(CORE_OBJS) $(RESMAN_GLUE_OBJS)

#
# The precompiled kernel module build process requires a separation of the
# closed source and open source object files.
#

KERNEL_GLUE_NAME := nv-linux.o
KERNEL_GLUE_OBJS := $(RESMAN_GLUE_OBJS) $(MODULE_NAME).mod.o

#
# A bug in KBUILD 2.4 means that leaving obj-m set in top-level context
# will cause Rules.make to call pathdown.sh, which is wrong. So, we only
# set this conditional of a kernel-level instance.
#

TOPDIR ?= $(srctree)

ifdef TOPDIR
obj-m := $(MODULE_NAME).o
endif

#
# Include local source directory in $(CC)'s include path and set disable any
# warning types that are of little interest to us.
#

EXTRA_CFLAGS += -I$(src)
EXTRA_CFLAGS += -Wall -Wimplicit -Wreturn-type -Wswitch -Wformat -Wchar-subscripts -Wparentheses -Wpointer-arith -Wno-multichar -Werror -MD $(DEFINES) $(INCLUDES) -Wsign-compare -Wno-cast-qual -Wno-error

#
# We rely on these two definitions below; if they aren't set, we set them to
# reasonable defaults (Linux 2.4's KBUILD, and top-level passes will not set
# these).
#

src ?= .
obj ?= .

#
# Determine location of the Linux kernel source tree. Allow users to override
# the default (i.e. automatically determined) kernel source location with the
# SYSSRC directive; this new directive replaces NVIDIA's SYSINCLUDE.
#

KERNEL_MODLIB := /lib/modules/$(shell uname -r)

ifdef SYSSRC
 KERNEL_SOURCES := $(SYSSRC)
 KERNEL_HEADERS := $(KERNEL_SOURCES)/include
else
 KERNEL_UNAME := $(shell uname -r)
 KERNEL_SOURCES := $(shell test -d $(KERNEL_MODLIB)/source && echo $(KERNEL_MODLIB)/source || echo $(KERNEL_MODLIB)/build)
 KERNEL_HEADERS := $(KERNEL_SOURCES)/include
endif

KERNEL_OUTPUT := $(KERNEL_SOURCES)
KBUILD_PARAMS :=

ifdef SYSOUT
 ifneq ($(SYSOUT), $(KERNEL_SOURCES))
 KERNEL_OUTPUT := $(SYSOUT)
 KBUILD_PARAMS := KBUILD_OUTPUT=$(KERNEL_OUTPUT)
 endif
else
 ifeq ($(KERNEL_SOURCES), $(KERNEL_MODLIB)/source)
 KERNEL_OUTPUT := $(KERNEL_MODLIB)/build
 KBUILD_PARAMS := KBUILD_OUTPUT=$(KERNEL_OUTPUT)
 endif
endif

CC ?= cc
HOST_CC ?= $(CC)
OBJDUMP ?= objdump
CONFTEST := /bin/sh $(src)/conftest.sh "$(CC)" "$(HOST_CC)" $(KERNEL_SOURCES) $(KERNEL_OUTPUT)

KERNEL_UNAME ?= $(shell $(CONFTEST) get_uname)
MODULE_ROOT := /lib/modules/$(KERNEL_UNAME)/kernel/drivers

#
# Sets any internal variables left unset by KBUILD (e.g. this happens during
# a top-level run).
#

TOPDIR ?= $(KERNEL_SOURCES)
PATCHLEVEL ?= $(shell $(CONFTEST) kernel_patch_level)

#
# Linux 2.4 uses the .o module extension. Linux 2.6, however, uses the .ko
# module extension. Handle these gracefully.
#

ifeq ($(PATCHLEVEL), 4)
 MODULE_OBJECT := $(MODULE_NAME).o
else
 MODULE_OBJECT := $(MODULE_NAME).ko
endif

#
# NVIDIA specific CFLAGS and #define's. The remap_page_range check has become
# necessary with the introduction of the five argument version to Linux 2.4
# distribution kernels; this conflicting change cannot be detected at compile
# time.
#

EXTRA_CFLAGS += -D_LOOSE_KERNEL_NAMES -D__KERNEL__ -DMODULE -DNVRM -DNV_VERSION_STRING=\"96.43.19\" -UDEBUG -U_DEBUG -DNDEBUG

ifeq ($(shell echo $(NVDEBUG)),1)
 ifeq ($(shell test -z $(RMDEBUG) && echo yes),yes)
 RMDEBUG=1
 endif
endif

ifeq ($(shell echo $(RMDEBUG)),1)
 EXTRA_CFLAGS += -DDEBUG -g 
endif

ifneq ($(PATCHLEVEL), 4)
 COMPILE_TESTS = \
	remap_page_range \
	remap_pfn_range \
	vmap \
	agp_backend_acquire \
	set_pages_uc \
	change_page_attr \
	i2c_adapter \
	pci_get_class \
	pm_message_t \
	irq_handler_t \
	pci_choose_state \
	vm_insert_page \
	acquire_console_sem \
	kmem_cache_create \
	on_each_cpu \
	smp_call_function \
	ioremap_wc \
	proc_dir_entry \
	acpi_walk_namespace \
	agp_memory \
	scatterlist \
	file_operations \
	sg_init_table
else
 COMPILE_TESTS = \
	remap_page_range \
	vmap \
	change_page_attr \
	i2c_adapter \
	smp_call_function \
	proc_dir_entry \
	scatterlist
endif

#
# Miscellaneous NVIDIA kernel module build support targets. They are needed
# to satisfy KBUILD requirements and to support NVIDIA specifics.
#

$(obj)/$(CORE_OBJS):
	cp $(src)/$(CORE_OBJS) $(obj)/$(CORE_OBJS)

$(obj)/$(VERSION_HEADER):
	@ echo \#define NV_COMPILER \"`$(CC) -v 2>&1 | tail -n 1`\" > $@

$(obj)/conftest.h:
	@if ! $(CONFTEST) compile_tests $(COMPILE_TESTS); then exit 1; fi

$(obj)/patches.h:
	@if ! $(CONFTEST) patch_check; then exit 1; fi

RESMAN_GLUE_TARGETS = $(addprefix $(obj)/,$(RESMAN_GLUE_OBJS))

$(RESMAN_GLUE_TARGETS): $(obj)/conftest.h $(obj)/patches.h

$(obj)/nv.o: $(obj)/$(VERSION_HEADER)

#
# More quirks for Linux 2.4 KBUILD, which doesn't link automatically.
#

ifeq ($(PATCHLEVEL), 4)
$(obj)/$(MODULE_NAME).o: $($(MODULE_NAME)-objs)
	$(LD) $(EXTRA_LDFLAGS) -r -o $@ $($(MODULE_NAME)-objs)
endif

#
# KBUILD build parameters.
#

KBUILD_PARAMS += KBUILD_VERBOSE=1 -C $(KERNEL_SOURCES) SUBDIRS=$(PWD)

#
# NVIDIA sanity checks.
#

.PHONY: suser-sanity-check rmmod-sanity-check build-sanity-checks

suser-sanity-check:
	@if ! $(CONFTEST) suser_sanity_check; then exit 1; fi

rmmod-sanity-check:
	@if ! $(CONFTEST) rmmod_sanity_check; then exit 1; fi

build-sanity-checks:
	@if ! $(CONFTEST) cc_version_check 		full_output; then exit 1; fi
	@if ! $(CONFTEST) rivafb_sanity_check 	full_output; then exit 1; fi
	@if ! $(CONFTEST) nvidiafb_sanity_check full_output; then exit 1; fi
	@if ! $(CONFTEST) xen_sanity_check 		full_output; then exit 1; fi

#
# Build the NVIDIA kernel module using Linux KBUILD. This target is used by
# the "package-install" target below.
#

module: build-sanity-checks
	@if [ -z "$(PATCHLEVEL)" ]; then \
	 echo "failed to determine PATCHLEVEL!"; \
	 exit 1; \
	fi; \
	echo "NVIDIA: calling KBUILD..."; \
	echo "make CC=$(CC) $(KBUILD_PARAMS) modules"; \
	make "CC=$(CC)" $(KBUILD_PARAMS) modules; \
	echo "NVIDIA: left KBUILD."; \
	if ! [ -f $(MODULE_OBJECT) ]; then \
	 echo "$(MODULE_OBJECT) failed to build!"; \
	 exit 1; \
	fi

#
# Build the NVIDIA kernel module with KBUILD. Verify that the user posesses
# sufficient privileges. Rebuild the module dependency file.
#

module-install: suser-sanity-check module
	@mkdir -p $(MODULE_ROOT)/video; \
	install -m 0664 -o root -g root $(MODULE_OBJECT) $(MODULE_ROOT)/video; \
	PATH="$(PATH):/bin:/sbin" depmod -ae;

#
# This target builds, then installs, then creates device nodes and inserts
# the module, if successful.
#

package-install: module-install rmmod-sanity-check
	PATH="$(PATH):/bin:/sbin" modprobe $(MODULE_NAME) && \
	echo "$(MODULE_OBJECT) installed successfully.";

#
# Build an object file suitable for further processing by the installer and
# inclusion as a precompiled kernel interface file.
#

$(KERNEL_GLUE_NAME): module
	$(LD) $(EXTRA_LDFLAGS) -r -o $(KERNEL_GLUE_NAME) $(KERNEL_GLUE_OBJS)

#
# Support hack, KBUILD isn't prepared to clean up after external modules.
#

clean:
	@ $(RM) -f $(RESMAN_GLUE_OBJS) $(KERNEL_GLUE_OBJS)
	@ $(RM) -f build-in.o nv-linux.o *.d .*.{cmd,flags}
	@ $(RM) -f $(MODULE_NAME).{o,ko,mod.{o,c}} $(VERSION_HEADER) *~
	@ $(RM) -f patches.h
	@ $(RM) -f conftest*.c conftest.h
	@ $(RM) -rf Modules.symvers .tmp_versions

#
# This target just prints the kernel module filename (for use by the
# installer)
#

print-module-filename:
	@ echo $(MODULE_OBJECT)

#
# Linux 2.4 KBUILD requires the inclusion of Rules.make; Linux 2.6's KBUILD
# includes dependencies automatically.
#

ifeq ($(PATCHLEVEL), 4)
include $(KERNEL_SOURCES)/Rules.make
endif
