#
# Version: 1.3
#
# Copyright (C) 2008-2014 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 = sep3_15

# location to install driver
INSTALL = .

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
EXTRA_CFLAGS += -I$(LDDINCDIR) -I$(LDDINCDIR1)  
EXTRA_CFLAGS += -I$(LDDINCDIR2)
EXTRA_CFLAGS += -DDRV_MIC


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

# platform details
MACH := $(shell uname -m)

KERNEL_VERSION ?= $(shell uname -r)

ifeq ($(MACH),x86_64)
PLATFORM=x32_64
endif
ifeq ($(ARCH),l1om)
PLATFORM=l1om
endif
ifeq ($(ARCH),k1om)
PLATFORM=k1om
endif
ifeq ($(PLATFORM),)
PLATFORM=x32
endif

ifeq ($(MODEL),knf)
EXTRA_CFLAGS += -DMIC_KNF
endif

ifeq ($(MODEL),knc)
EXTRA_CFLAGS += -DMIC_KNC
endif

SMP := $(shell uname -v | grep SMP)
ARITY=up
ifneq ($(SMP),)
ARITY=smp
endif

#ENABLE_CHIPSET=NO

# eventual filename of SEP driver
DRIVER_FILENAME=$(DRIVER_NAME)-$(PLATFORM)-$(KERNEL_VERSION)$(ARITY).ko
#DRIVER_FILENAME=$(DRIVER_NAME)-$(PLATFORM)-2.6.34-g9b6831a$(ARITY).ko

# By default, build the EMON portions of the driver
# unless building IPF, which EMON doesn't support currently
ifeq ($(EMON),)
	EMON=YES
endif

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

ifeq ($(MACH),x86_64)
	arch-objs :=    knx_core.o   \
			sys64.o   \
			kncunc_sbox.o \
			kncunc_gbox.o \
			kncunc_gbox_fbox.o \
			kncunc_gbox_mbox.o \
			apic.o    \
			pebs.o
endif


ifeq ($(SECURE_SAMPLING),YES)
    EXTRA_CFLAGS += -DSECURE_SEP
endif

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

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

# targets ...

# Otherwise, we were called directly from the command
# line; invoke the kernel build system.
else
#	KERNEL_SRC_DIR ?= /lib/modules/$(shell uname -r)/build
	PWD := $(shell pwd)
	KERNEL_SRC_DIR ?= $(PWD)/../mic_linux

all: default

default:  
	echo  $(arch-objs)
	$(MAKE) -C $(KERNEL_SRC_DIR) M=$(PWD) LDDINCDIR=$(PWD)/../include LDDINCDIR1=$(PWD)/inc LDDINCDIR2=$(PWD)/$(UTILS_INC) modules PWD=$(PWD)
	mv $(DRIVER_NAME).ko $(DRIVER_FILENAME)

endif

clean:
	rm -f *.o *.ko .*.o.cmd .*.o.d .*.ko.cmd .*.ko.unsigned.cmd *.cmd.c
	rm -f $(DRIVER_FILENAME).ko $(DRIVER_FILENAME).ko.unsigned $(DRIVER_FILENAME)
	rm -f Module.symvers Modules.symvers *.mod.c modules.order Module.markers
	rm -rf .tmp_versions
