#!/usr/bin/make -f
##########################################################
# Copyright (C) 2007,2014 VMware, Inc. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation version 2 and no later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
##########################################################

CC_OPTS += -DVMCI

INCLUDE += -I$(SRCROOT)/shared -I$(SRCROOT)/common -I$(SRCROOT)/linux

EXTRA_CFLAGS := $(CC_OPTS) $(INCLUDE)

obj-m += $(DRIVER).o

$(DRIVER)-y := $(subst $(SRCROOT)/, , $(patsubst %.c, %.o, \
                $(wildcard $(SRCROOT)/linux/*.c $(SRCROOT)/common/*.c)))

#
# In open-vm-tools, need to compile the common sources from the shared directory.
#
DRIVERLOG   := driverLog.o
$(DRIVER)-y += $(DRIVERLOG)

VMCI_PATH  := $(shell cd $(SRCROOT) && pwd)
ifdef OVT_SOURCE_DIR
DRIVERLOG_PATH := $(OVT_SOURCE_DIR)/modules/linux/shared
else
DRIVERLOG_PATH := $(VMCI_PATH)/shared
endif

$(addprefix $(VMCI_PATH)/,$(DRIVERLOG)): $(VMCI_PATH)/%.o: $(DRIVERLOG_PATH)/%.c
	$(Q)$(rule_cc_o_c)

clean:
	rm -rf $(wildcard $(DRIVER).mod.c $(DRIVER).ko .tmp_versions \
	        Module.symvers Modules.symvers Module.markers modules.order \
		$(foreach dir,linux/ common/ \
		./,$(addprefix $(dir),.*.cmd .*.o.flags *.o)))
ifneq ($(MODULEBUILDDIR),)
	rm -f $(MODULEBUILDDIR)/VMwareVMCIModule.symvers
endif

#
# If this build generated a Module.symvers, copy it to a public place where
# the vSockets build will be able to find it.
#
postbuild::
ifeq ($(call vm_check_file,$(SRCROOT)/Module.symvers), yes)
ifneq ($(MODULEBUILDDIR),)
	cp -f $(SRCROOT)/Module.symvers $(MODULEBUILDDIR)/VMwareVMCIModule.symvers
endif
endif
