# This Makefile is automatically generated; do not edit
# Generated on 'builder101' on Thu Nov 4 21:16:44 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_gvi.o nv-vm.o os-agp.o os-interface.o os-registry.o nv-i2c.o nvacpi.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

obj-m := $(MODULE_NAME).o

#
# 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 -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 (e.g. for top-level passes).
#

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)
LD ?= ld

ifndef ARCH
 ARCH := $(shell uname -m | sed -e 's/i.86/i386/')
endif

CONFTEST := /bin/sh $(src)/conftest.sh "$(CC)" "$(HOST_CC)" $(ARCH) $(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)

#
# Linux 2.6 uses the .ko module extension.
#

MODULE_OBJECT := $(MODULE_NAME).ko

#
# NVIDIA specific CFLAGS and #define's.
#

EXTRA_CFLAGS += -D__KERNEL__ -DMODULE -DNVRM -DNV_VERSION_STRING=\"260.19.21\" -mcmodel=kernel -mno-red-zone -UDEBUG -U_DEBUG -DNDEBUG


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 \
	acpi_device_ops \
	acpi_device_id \
	acquire_console_sem \
	kmem_cache_create \
	on_each_cpu \
	smp_call_function \
	vmm_support \
	acpi_evaluate_integer \
	ioremap_wc \
	proc_dir_entry \
	INIT_WORK \
	acpi_walk_namespace \
	agp_memory \
	scatterlist \
	pci_domain_nr \
	pci_dma_mapping_error \
	file_operations \
	sg_init_table

#
# 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: $(src)/conftest.sh
	@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)

#
# KBUILD build parameters.
#

ifndef NV_VERBOSE
 ifneq ($(strip $(QUIET)),1)
 NV_VERBOSE=1
 else
 NV_VERBOSE=0
 endif
endif

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

#
# 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
	@echo "NVIDIA: calling KBUILD..."; \
	 $(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. Note the
# dependency on 'module', and not just the input object files. This is
# because we rely on KBUILD to produce the latter as part of the
# full kernel module build.
#

$(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)
