#!/usr/bin/make -f
##########################################################
# Copyright (C) 1998-2016 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
#
##########################################################

####
####  VMware vmhgfs Makefile to be distributed externally
####

INCLUDE += -I.

EXTRA_CFLAGS := $(CC_OPTS) $(INCLUDE)

EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/cachector.c, -DVMW_KMEMCR_CTOR_HAS_3_ARGS, )
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/cachector1.c, -DVMW_KMEMCR_CTOR_HAS_2_ARGS, )
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/file_operations_fsync.c, -DVMW_FSYNC_31, )
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/file_operations_flush.c, -DVMW_FLUSH_HAS_1_ARG, )

# Note: These tests are inverted
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/getsb1.c,, -DVMW_GETSB_2618)
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/statfs1.c,, -DVMW_STATFS_2618)
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/inode1.c,, -DVMW_INODE_2618)
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/dcount.c,, -DVMW_DCOUNT_311)
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/dalias.c,, -DVMW_DALIAS_319)
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/dalias1.c,, -DVMW_DALIAS_319)
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/truncate_pagecache.c,, -DVMW_PAGECACHE_312)
EXTRA_CFLAGS += $(call vm_check_build, $(AUTOCONF_DIR)/wait_on_bit.c,, -DVMW_WAITONBIT_317)

obj-m += $(DRIVER).o

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

#
# In open-vm-tools, need to compile the common sources from the lib directory.
#
VMHGFS_PATH      := $(shell cd $(SRCROOT) && pwd)

ifdef OVT_SOURCE_DIR
LIBBACKDOOR_PATH := $(call VMLIB_PATH,backdoor)
LIBHGFS_PATH     := $(call VMLIB_PATH,hgfs)
LIBHGFSBD_PATH   := $(call VMLIB_PATH,hgfsBd)
LIBMESSAGE_PATH  := $(call VMLIB_PATH,message)
LIBRPCOUT_PATH   := $(call VMLIB_PATH,rpcOut)
STUBS_PATH       := $(OVT_SOURCE_DIR)/modules/linux/shared

INCLUDE          += -I$(LIBBACKDOOR_PATH)
INCLUDE          += -I$(LIBHGFS_PATH)

LIBBACKDOOR      := backdoor.o
LIBBACKDOOR      += backdoorGcc32.o
LIBBACKDOOR      += backdoorGcc64.o

LIBHGFS          := cpName.o
LIBHGFS          += cpNameLinux.o
LIBHGFS          += cpNameLite.o
LIBHGFS          += hgfsEscape.o
LIBHGFS          += hgfsUtil.o

LIBHGFSBD        := hgfsBd.o

LIBMESSAGE       := message.o

LIBRPCOUT        := rpcout.o

$(addprefix $(VMHGFS_PATH)/,$(LIBBACKDOOR)): $(VMHGFS_PATH)/%.o: $(LIBBACKDOOR_PATH)/%.c
	$(Q)$(rule_cc_o_c)

$(addprefix $(VMHGFS_PATH)/,$(LIBHGFS)): $(VMHGFS_PATH)/%.o: $(LIBHGFS_PATH)/%.c
	$(Q)$(rule_cc_o_c)

$(addprefix $(VMHGFS_PATH)/,$(LIBHGFSBD)): $(VMHGFS_PATH)/%.o: $(LIBHGFSBD_PATH)/%.c
	$(Q)$(rule_cc_o_c)

$(addprefix $(VMHGFS_PATH)/,$(LIBMESSAGE)): $(VMHGFS_PATH)/%.o: $(LIBMESSAGE_PATH)/%.c
	$(Q)$(rule_cc_o_c)

$(addprefix $(VMHGFS_PATH)/,$(LIBRPCOUT)): $(VMHGFS_PATH)/%.o: $(LIBRPCOUT_PATH)/%.c
	$(Q)$(rule_cc_o_c)


$(DRIVER)-y += $(LIBBACKDOOR)
$(DRIVER)-y += $(LIBHGFS)
$(DRIVER)-y += $(LIBHGFSBD)
$(DRIVER)-y += $(LIBMESSAGE)
$(DRIVER)-y += $(LIBRPCOUT)
else
STUBS_PATH  := $(VMHGFS_PATH)/shared
endif

STUBS       := kernelStubsLinux.o
$(DRIVER)-y += $(STUBS)
$(addprefix $(VMHGFS_PATH)/,$(STUBS)): $(VMHGFS_PATH)/%.o: $(STUBS_PATH)/%.c
	$(Q)$(rule_cc_o_c)

#
# On a 32-bit machine, strip out 64-bit backdoor code, and vice versa.
#
ifeq ($(CONFIG_X86_64),y)
$(DRIVER)-y := $(filter-out backdoorGcc32.o, $($(DRIVER)-y))
else
$(DRIVER)-y := $(filter-out backdoorGcc64.o, $($(DRIVER)-y))
endif

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