#!/usr/bin/make -f
#############################################################
# Copyright 1998 VMware, Inc.  All rights reserved. -- VMware Confidential
#############################################################

####
####  VMware kernel module Makefile to be distributed externally
####

####
#### SRCROOT _must_ be a relative path.
####
SRCROOT = .

VM_UNAME = $(shell uname -r)

# Header directory for the running kernel
HEADER_DIR = /lib/modules/$(VM_UNAME)/build/include

BUILD_DIR = $(HEADER_DIR)/..

DRIVER := vmnet

ifeq ($(DRIVER), vmnet)
ifndef VMWARE_VER
VMWARE_VER := $(shell $(SRCROOT)/getversion.pl)
export VMWARE_VER
endif
endif

# Grep program
GREP = /bin/grep

vm_check_gcc = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \
        > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi)
vm_check_gxx = $(shell if $(CC) $(1) -S -o /dev/null -xc++ /dev/null \
        > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi)
vm_check_file = $(shell if test -f $(1); then echo "yes"; else echo "no"; fi)

ifndef VM_KBUILD
VM_KBUILD := no
VM_KBUILD_OUTPUT := $(KBUILD_OUTPUT)
ifdef O
  ifeq ("$(origin O)", "command line")
    VM_KBUILD_OUTPUT := $(O)
  endif
endif
ifeq ($(VM_KBUILD_OUTPUT),)
VM_KBUILD_OUTPUT := $(BUILD_DIR)
endif
ifeq ($(call vm_check_file,$(BUILD_DIR)/Makefile), yes)
ifeq ($(call vm_check_file,$(VM_KBUILD_OUTPUT)/.config), no)
VM_DUMMY := $(shell echo >&2 '*** Your sources installation is broken:')
VM_DUMMY := $(shell echo >&2 '*** $(BUILD_DIR) does not contain .config file')
VM_DUMMY := $(shell echo >&2 '*** vmmon/vmnet build may fail, or built vmmon/vmnet may crash.')
ifeq ($(call vm_check_file,/boot/vmlinuz.config), yes)
VM_DUMMY := $(shell echo >&2 '*** You are apparently use SuSE system. You should copy')
VM_DUMMY := $(shell echo >&2 '*** /boot/vmlinuz.config to the $(BUILD_DIR)/.config.')
endif
VM_DUMMY := $(shell echo >&2)
VM_DUMMY := $(shell echo >&2 '*** Hit ENTER to continue, or ^C to stop build.')
VM_DUMMY := $(shell read dummy)
else
ifeq ($(call vm_check_file,$(BUILD_DIR)/Rules.make), yes)
ifeq ($(shell if $(GREP) "^PATCHLEVEL = 4$$" $(BUILD_DIR)/Makefile > /dev/null 2>&1; then echo "yes"; fi), yes)
VM_KBUILD := 24
endif
else
VM_KBUILD := 26
endif
endif
endif
export VM_KBUILD
endif

ifndef VM_KBUILD_SHOWN
ifeq ($(VM_KBUILD), no)
VM_DUMMY := $(shell echo >&2 "Using standalone build system.")
else
ifeq ($(VM_KBUILD), 24)
VM_DUMMY := $(shell echo >&2 "Using 2.4.x kernel build system.")
else
VM_DUMMY := $(shell echo >&2 "Using 2.6.x kernel build system.")
endif
endif
VM_KBUILD_SHOWN := yes
export VM_KBUILD_SHOWN
endif

ifneq ($(VM_KBUILD), no)

VMCCVER := $(shell $(CC) -dumpversion)

# If there is no version defined, we are in toplevel pass, not yet in kernel makefiles...
ifeq ($(VERSION),)

ifeq ($(VM_KBUILD), 24)
DRIVER_KO := $(DRIVER).o
else
DRIVER_KO := $(DRIVER).ko
endif

.PHONY: $(DRIVER_KO)

auto-build: $(DRIVER_KO)
	cp -f $< $(SRCROOT)/../$(DRIVER).o

# $(DRIVER_KO) is a phony target, so compare file times explicitly
$(DRIVER): $(DRIVER_KO)
	if [ $< -nt $@ ]; then cp -f $< $@; fi

# Pass gcc version down the chain, so we can detect if kernel attempts to use unapproved compiler
VM_CCVER := $(VMCCVER)
export VM_CCVER
VM_CC := $(CC)
export VM_CC

MAKEOVERRIDES := $(filter-out CC=%,$(MAKEOVERRIDES))

$(DRIVER_KO):
	make -C $(BUILD_DIR) SUBDIRS=$$PWD SRCROOT=$$PWD/$(SRCROOT) modules

else

ifneq ($(VM_CCVER), $(VMCCVER))
$(warning *** Inappropriate build environment: you wanted to use gcc \
 version $(VM_CCVER) while kernel attempts to use gcc version $(VMCCVER).)
$(error For proper build you'll have to replace $(CC) with symbolic \
 link to $(VM_CC))
endif

endif

VMWARE_DEBUG_REDIRECT := > /dev/null 2>&1
ifeq ($(VMWARE_DEBUG_AUTOCONF),y)
ifneq ($(VERSION),)
VMWARE_DEBUG_REDIRECT := >&2
endif
endif
vm_check_build = $(shell if $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) \
	$(CPPFLAGS) $(CFLAGS) $(CFLAGS_KERNEL) \
	$(EXTRA_CFLAGS) -Iinclude2/asm/mach-default \
	-DKBUILD_BASENAME=\"$(DRIVER)\" \
	-Werror -S -o /dev/null -xc $(1) \
	$(VMWARE_DEBUG_REDIRECT); then echo "$(2)"; else echo "$(3)"; fi)

CC_WARNINGS := -Wall -Wstrict-prototypes
CC_OPTS := $(GLOBAL_DEFS) $(CC_WARNINGS) -DVMW_USING_KBUILD
ifdef VMX86_DEVEL
CC_OPTS += -DVMX86_DEVEL
endif
ifdef VMX86_DEBUG
CC_OPTS += -DVMX86_DEBUG
endif
CC_OPTS += -DVME_DEFAULT=$(VMWARE_VER)


include $(SRCROOT)/Makefile.kernel

ifdef TOPDIR
ifeq ($(VM_KBUILD), 24)

O_TARGET := $(DRIVER).o

obj-y := $($(DRIVER)-y)

include $(TOPDIR)/Rules.make
endif
endif

else

include $(SRCROOT)/Makefile.normal

endif

#.SILENT:
