#!/usr/bin/make -f
#############################################################
# Copyright 1998 VMware, Inc.  All rights reserved. -- VMware Confidential
#############################################################
####
####  VMware vmmon Makefile to be distributed externally
####

####
####  If you have an SMP kernel, you _must_ uncomment the line below.  If
#### you don't, and you're lucky, the driver won't compile.  Otherwise
#### it'll compile but just be potentially rather broken.
####
#SUPPORT_SMP=1

# Header directory for the running kernel
HEADER_DIR = /usr/src/linux/include

# Default to use gcc 2.x options
IS_GCC_3 = no

# Grep program
GREP = /bin/grep

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

####
####  DESTDIR is where the module, object files, and dependencies are built
####
DESTDIR = driver-`uname -r`

####
####  DRIVERNAME should be untouched unless you have a good reason to change 
#### it.  The form below is how the scripts expect it.
####
ifdef SUPPORT_SMP
DRIVERNAME = vmmon-smp-`uname -r`
else
DRIVERNAME = vmmon-up-`uname -r`
endif

ifdef QUIET
ECHO = @true
else
ECHO = @echo
endif

####
#### You must compile with at least -O level of optimization
#### or the module won't load.
#### If desparate, I think that bringing in <linux/bitops.h> might
#### suffice.
####
CC_WARNINGS = -Wall -Wstrict-prototypes 
# Don't use -pipe or egcs-2.91.66 (shipped with RedHat) will die
CC_KFLAGS = -D__KERNEL__ -fno-strength-reduce -fno-omit-frame-pointer \
            -DCPU=586 -fno-common -DKBUILD_MODNAME=vmmon
# Gcc 3.0 deprecates -m486 --hpreg
ifeq ($(IS_GCC_3),yes)
CC_KFLAGS += -march=i586 -falign-loops=2 -falign-jumps=2 -falign-functions=2
else
CC_KFLAGS += -m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2
endif
CC_OPTS = -O2 -DMODULE $(GLOBAL_DEFS) $(CC_KFLAGS) $(CC_WARNINGS) 

ifdef SUPPORT_SMP
CC_OPTS += -DSUPPORT_SMP
endif

ifeq ($(shell $(GREP) 'FIX_IO_APIC_BASE_0' $(HEADER_DIR)/asm/fixmap.h 2>/dev/null),)
CC_OPTS += -DVMWARE__FIX_IO_APIC_BASE=FIX_IO_APIC_BASE
else
CC_OPTS += -DVMWARE__FIX_IO_APIC_BASE=FIX_IO_APIC_BASE_0
endif

INCLUDE = -I$(SRCROOT)/include -I$(SRCROOT)/common -I$(SRCROOT)/linux -I$(SRCROOT)/export/include -I$(HEADER_DIR)

C_TARGETS_LINUX  = driver.o hostif.o
C_TARGETS_COMMON = vmx86.o task.o memtrack.o phystrack.o cpuid.o
C_TARGETS_LINUX_D = ${C_TARGETS_LINUX:.o=.d}
C_TARGETS_COMMON_D = ${C_TARGETS_COMMON:.o=.d}
C_TARGETS =  $(C_TARGETS_LINUX) $(C_TARGETS_COMMON)

####
#### Make Targets are beneath here.
####

driver: setup deps
	$(MAKE) -C $(DESTDIR) -f ../Makefile SRCROOT=../$(SRCROOT) vmmon INCLUDE_DEPS=1

setup:
	@if [ -d $(DESTDIR) ] ; then true ; else mkdir $(DESTDIR); chmod 755 $(DESTDIR) ; fi

vmmon: $(C_TARGETS)
	$(ECHO) "Building $(DRIVERNAME)"   
	ld -r -o $(DRIVERNAME) $(C_TARGETS)

auto-build: 
	$(MAKE) driver QUIET=1
	cp -f $(DESTDIR)/$(DRIVERNAME) $(SRCROOT)/../vmmon.o

$(C_TARGETS_LINUX): %.o: $(SRCROOT)/linux/%.c
	$(ECHO) "Compiling  linux/$(<F)"
	$(CC) $(CC_OPTS) $(INCLUDE) -c $<

$(C_TARGETS_COMMON): %.o: $(SRCROOT)/common/%.c
	$(ECHO) "Compiling  common/$(<F)"
	$(CC) $(CC_OPTS) $(INCLUDE) -c $<

clean:
	rm -rf $(DESTDIR)/

$(C_TARGETS_COMMON_D): %.d: $(SRCROOT)/common/%.c
	$(ECHO) "Dependencies for $(<F)"
	$(CC) -MM $(CC_OPTS) $(INCLUDE) $< > $@

$(C_TARGETS_LINUX_D): %.d: $(SRCROOT)/linux/%.c
	$(ECHO) "Dependencies for $(<F)"
	$(CC) -MM $(CC_OPTS) $(INCLUDE) $< > $@

deps: setup 
	$(MAKE) -C $(DESTDIR) -f ../Makefile SRCROOT=../$(SRCROOT) driver_deps 

driver_deps: ${C_TARGETS:.o=.d}

ifdef INCLUDE_DEPS
include ${C_TARGETS:.o=.d}
endif

.SILENT:
