#
# Version: 1.6
#
# Copyright (C) 2008 Intel Corporation.  All Rights Reserved.
#
#     This file is part of SEP Development Kit
#
#     SEP Development Kit is free software; you can redistribute it
#     and/or modify it under the terms of the GNU General Public License
#     version 2 as published by the Free Software Foundation.
#
#     SEP Development Kit 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 SEP Development Kit; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#     As a special exception, you may use this file as part of a free software
#     library without restriction.  Specifically, if other files instantiate
#     templates or use macros or inline functions from this file, or you compile
#     this file and link it with other files to produce an executable, this
#     file does not by itself cause the resulting executable to be covered by
#     the GNU General Public License.  This exception does not however
#     invalidate any other reasons why the executable file might be covered by
#     the GNU General Public License.
#

# -------------------- user configurable options ------------------------

# base name of SEP driver
DRIVER_NAME = sep5

# location to install driver
ifeq ($(INSTALL),)
INSTALL = .
endif

# current working directory
DRV_CWD := $(shell pwd)

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
# Example flags are "-Werror", "-Wno-error", etc.
EXTRA_CFLAGS += -I$(LDDINCDIR) -I$(LDDINCDIR1)

# if ARCH variable is set, unset it to avoid conflicts with kbuild
unexport ARCH

# platform details
MACH ?= $(shell uname -m)
export MACH
ifeq ($(MACH),x86_64)
PLATFORM=x32_64
endif
ifeq ($(PLATFORM),)
PLATFORM=x32
endif

# eventual filename of SEP driver
DRIVER_MODE=$(DRIVER_NAME)
DRIVER_FILENAME=$(DRIVER_NAME).ko

# By default, build the EMON portions of the driver
ifeq ($(EMON),)
	EMON=YES
endif

# build options ...
ifneq ($(KERNELRELEASE),)
	obj-m := $(DRIVER_NAME).o

ifeq ($(PLATFORM),x32)
	arch-objs :=    core2.o                \
			perfver4.o             \
			sys32.o                \
			silvermont.o           \
			pci.o                  \
			apic.o                 \
			pebs.o                 \
			unc_gt.o               \
			unc_mmio.o             \
			unc_msr.o              \
			unc_common.o           \
			unc_pci.o              \
			ipt.o                  \
			sepdrv_p_state.o
endif
ifeq ($(PLATFORM),x32_64)
	arch-objs :=    core2.o                \
			perfver4.o             \
			sys64.o                \
			silvermont.o           \
			pci.o                  \
			apic.o                 \
			pebs.o                 \
			unc_gt.o               \
			unc_mmio.o             \
			unc_msr.o              \
			unc_common.o           \
			unc_pci.o              \
			ipt.o                  \
			sepdrv_p_state.o
endif

ifeq ($(EMON),YES)
    EXTRA_CFLAGS += -DEMON -DEMON_INTERNAL
endif

EXTRA_CFLAGS += -DDRV_ANDROID

EXTRA_CFLAGS += -DDRV_CPU_HOTPLUG

#** <private build only!>
ifeq ($(BUILD_PUBLIC),YES)
else
    EXTRA_CFLAGS += -DENABLE_CPUS
    private-objs :=	unc_sa.o
endif

EXTRA_CFLAGS += -DBUILD_SCOPE_BETA

#== </private build!>

	$(DRIVER_NAME)-objs :=            \
			lwpmudrv.o        \
			control.o         \
			cpumon.o          \
			eventmux.o        \
			linuxos.o         \
			output.o          \
			pmi.o             \
			sys_info.o        \
			utility.o         \
			valleyview_sochap.o   \
			unc_power.o       \
			pmu_list.o        \
			$(private-objs)   \
			$(arch-objs)

# targets ...

# Otherwise, we were called directly from the command
# line, so the kernel build system will be used.
else
	PWD  := $(shell pwd)
	PATH := $(ANDROID_TOOLCHAIN):$(PATH)
	export PATH

socperfdir=$(DRV_CWD)/src/socperf
ifeq ($(wildcard $(socperfdir)),)
    socperfdir=
endif

all: default

default:
	@echo "current dir: ${PWD}"
	@(cd $(socperfdir)/src && make all)
	@cp $(socperfdir)/src/Module*.symvers .
	$(MAKE) -C $(KERNEL_SRC_DIR) M=$(PWD) LDDINCDIR=$(PWD)/include LDDINCDIR1=$(PWD)/inc modules PWD=$(PWD)
	@(cd pax && make all)
	@if [ -d vtsspp ]; then          \
		(cd vtsspp && make all); \
	fi;

endif

install:
	@echo "Installing modules"
	@cp $(DRIVER_NAME).ko $(INSTALL)/$(DRIVER_FILENAME)
	@cp insmod-sep3 $(INSTALL)/insmod-sep3
	@cp rmmod-sep3 $(INSTALL)/rmmod-sep3
	@cp boot-script $(INSTALL)/boot-script
	@echo "Installed $(DRIVER_NAME) driver to $(INSTALL)/$(DRIVER_FILENAME) ."
	cd pax && mkdir -p $(INSTALL)/pax && make install INSTALL=$(INSTALL)/pax ; \
	cd $(socperfdir)/src && mkdir -p $(INSTALL)/socperf/src && make install INSTALL=$(INSTALL)/socperf/src ; \
	@echo "Installed $(DRIVER_NAME) driver to $(INSTALL)/$(DRIVER_FILENAME) ."
	
	@if [ -d vtsspp ]; then                                        \
		(cd vtsspp && mkdir -p $(INSTALL)/vtsspp && make install INSTALL=$(INSTALL)/vtsspp); \
	fi;

clean:
	@rm -f *.o .*.o.cmd .*.o.d .*.ko.cmd .*.ko.unsigned.cmd *.gcno *.mod .*.mod.cmd
	@rm -f $(DRIVER_NAME).ko $(DRIVER_NAME).ko.unsigned
	@rm -f $(DRIVER_MODE)*$(DRIVER_TYPE).ko
	@rm -f Module.symvers Modules.symvers modules.order Module.markers
	@rm -rf .tmp_versions
	@(cd pax && make clean)
	@(cd $(socperfdir)/src && make clean)
	@if [ -d vtsspp ]; then            \
		(cd vtsspp && make clean); \
	fi;	

distclean: clean
	@rm -f $(DRIVER_NAME)*.ko
	@(cd pax && make distclean)
	@(cd $(socperfdir)/src && make distclean)
	@if [ -d vtsspp ]; then                \
		(cd vtsspp && make distclean); \
	fi;
