###############################################################################
# -*- Mode: makefile -*-
#
# Copyright (C) 2010-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.
#
###############################################################################

DRIVER_NAME := vtsspp

MARCH ?= $(shell uname -m | sed -e s/i.86/i386/)

# Check for external define from SEP Makefile
ifeq ($(KERNEL_VERSION),)
KVER  ?= $(shell uname -r)
else
KVER  ?= $(KERNEL_VERSION)
endif

KMAJ = $(shell echo $(KVER) | \sed -e 's/^\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*.*/\1/')

kmajver_less = $(shell \
echo test | awk '{if($(KMAJ) < $(1)) {print 1} else {print 0}}' \
)

# Check for external define from SEP Makefile
ifeq ($(KERNEL_SRC_DIR),)
KDIR  ?= /lib/modules/$(KVER)/build
else
KDIR  ?= $(KERNEL_SRC_DIR)
endif

PWD := $(shell pwd)
INSTALL ?= $(PWD)
PLATFORM := unknown
ifeq ($(MARCH),i386)
PLATFORM := x32
endif
ifeq ($(MARCH),x86_64)
PLATFORM := x32_64
endif
#ifneq ($(ARCH),)
#PLATFORM := $(ARCH)
#endif
#endif

###############################################################################
# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)

obj-m := $(DRIVER_NAME).o
$(DRIVER_NAME)-objs := module.o collector.o procfs.o transport.o record.o \
                       task_map.o globals.o cpuevents.o user_vm.o stack.o \
                       apic.o dsa.o bts.o pebs.o lbr.o nmiwd.o ipt.o
ifeq ($(PLATFORM),x32)
EXTRA_CFLAGS += -DVTSS_ARCH_32
$(DRIVER_NAME)-objs += sys32.o
endif
ifeq ($(PLATFORM),x32_64)
ifneq ($(ARCH),)
DARCH := $(shell echo $(ARCH) | sed -e s/i386/32/ -e s/x86_// -e s/x86/64/ -e s/[lk]1om/KNX/)
EXTRA_CFLAGS += -DVTSS_ARCH_$(DARCH)
$(DRIVER_NAME)-objs += sys$(DARCH).o
else
EXTRA_CFLAGS += -DVTSS_ARCH_64
$(DRIVER_NAME)-objs += sys64.o
endif
endif

EXTRA_CFLAGS  += -Wno-unused -Werror
VTSS_FEATURES := $(subst :, ,$(VTSS))
ifneq ($(filter trace, $(VTSS_FEATURES)),)
EXTRA_CFLAGS  += -DVTSS_DEBUG_TRACE
endif
ifneq ($(filter profile, $(VTSS_FEATURES)),)
EXTRA_CFLAGS  += -DVTSS_DEBUG_PROFILE
endif

ifeq ($(call kmajver_less,3),1)
EXTRA_CFLAGS += -DVTSS_USE_UEC
$(DRIVER_NAME)-objs += uec.o
else
ifneq ($(filter uec, $(VTSS_FEATURES)),)
EXTRA_CFLAGS += -DVTSS_USE_UEC
$(DRIVER_NAME)-objs += uec.o
else
VTSS_RING_BUFFER := 1
endif
endif
ifneq ($(filter syscall, $(VTSS_FEATURES)),)
EXTRA_CFLAGS += -DVTSS_SYSCALL_TRACE
endif
ifneq ($(filter vma_cache, $(VTSS_FEATURES)),)
EXTRA_CFLAGS += -DVTSS_VMA_CACHE
endif
ifneq ($(filter android, $(VTSS_FEATURES)),)
EXTRA_CFLAGS += -DVTSS_CHECK_IP_IN_MAP -DVTSS_STACK_LIMIT=0x10000
endif
EXTRA_CFLAGS += -DVTSS_VMA_SEARCH_BOOST
ifneq ($(filter mic, $(VTSS_FEATURES)),)
EXTRA_CFLAGS += -DVTSS_OVERFLOW_PAUSE -DVTSS_NO_BTS -DVTSS_VMA_SEARCH_BOOST
#EXTRA_CFLAGS += -DVTSS_OVERFLOW_PAUSE -DVTSS_NO_BTS
endif
#ifneq ($(filter kernel_switch, $(VTSS_FEATURES)),)
EXTRA_CFLAGS += -DVTSS_KERNEL_CONTEXT_SWITCH
#endif

#COPY_PUBLIC_HEADERS := $(cp ../include/* $obj)
 
ifeq ($(wildcard $(obj)/vtssrtcfg.h),)
COPY_PUBLIC_HEADERS := $(shell cp $(obj)/../include/* $(obj))
else
COPY_PUBLIC_HEADERS:=
endif 


$(DRIVER_NAME)-objs: COPY_PUBLIC_HEADERS $(obj)/vtss_autoconf.h



REQUIRED_SUBLEVEL := 32
CHECK_SUBLEVEL := $(shell if [ $(SUBLEVEL) -lt $(REQUIRED_SUBLEVEL) ] ; then echo less ; fi)
_KBUILD_CFLAGS := $(call flags,KBUILD_CFLAGS)
CHECK_BUILD    := $(CC) $(KBUILD_CPPFLAGS) $(CPPFLAGS) $(LINUXINCLUDE) \
                  $(_KBUILD_CFLAGS) $(CFLAGS_KERNEL) $(EXTRA_CFLAGS) $(CFLAGS) \
                  -DKBUILD_BASENAME=\"$(DRIVER_NAME)\" -Werror -S -o /dev/null -xc > /dev/null 2>&1

autoconf-srcs := $(sort $(wildcard $(M)/autoconf/*.c))
$(addprefix $(obj)/,$($(DRIVER_NAME)-objs)): $(obj)/vtss_autoconf.h
$(obj)/vtss_autoconf.h: $(autoconf-srcs) $(M)/Makefile
ifeq ($(VERSION).$(PATCHLEVEL),2.6)
	@$(if $(CHECK_SUBLEVEL),$(error Linux kernels prior to 2.6.$(REQUIRED_SUBLEVEL) are unsupported.))
else
ifneq ($(VERSION),$(filter $(VERSION),3 4))
	@$(error Linux kernels prior to 2.6 are unsupported.)
endif
endif
	@echo "/**" > $@
	@echo " * Automatically generated file; DO NOT EDIT." >> $@
	@echo " * $(DRIVER_NAME).ko Configuration for $(KERNELRELEASE) Kernel Configuration" >> $@
	@echo " */" >> $@
	@echo "#ifndef _VTSS_AUTOCONF_H_" >> $@
	@echo "#define _VTSS_AUTOCONF_H_" >> $@
	@echo "" >> $@
	@echo "#if !defined(__i386__) && !defined(__x86_64__)" >> $@
	@echo "#error \"Only i386 or x86_64 architecture is supported\"" >> $@
	@echo "#endif" >> $@
	@echo "" >> $@
	@echo "#ifndef CONFIG_MODULES" >> $@
	@echo "#error \"The kernel should be compiled with CONFIG_MODULES=y\"" >> $@
	@echo "#endif /* CONFIG_MODULES */" >> $@
	@echo "" >> $@
	@echo "#ifndef CONFIG_MODULE_UNLOAD" >> $@
	@echo "#error \"The kernel should be compiled with CONFIG_MODULE_UNLOAD=y\"" >> $@
	@echo "#endif /* CONFIG_MODULE_UNLOAD */" >> $@
	@echo "" >> $@
	@echo "#ifndef CONFIG_SMP" >> $@
	@echo "#error \"The kernel should be compiled with CONFIG_SMP=y\"" >> $@
	@echo "#endif /* CONFIG_SMP */" >> $@
	@echo "" >> $@
	@echo "#ifndef CONFIG_KPROBES" >> $@
	@echo "#error \"The kernel should be compiled with CONFIG_KPROBES=y\"" >> $@
	@echo "#endif /* CONFIG_KPROBES */" >> $@
	@echo "" >> $@
ifeq ($(VTSS_RING_BUFFER),1)
	@echo "#ifndef CONFIG_RING_BUFFER" >> $@
	@echo "#error \"The kernel should be compiled with CONFIG_RING_BUFFER=y\"" >> $@
	@echo "#endif /* CONFIG_RING_BUFFER */" >> $@
	@echo "" >> $@
endif
#	@$(foreach f, $(autoconf-srcs), if $(CHECK_BUILD) $(f); then (echo -n "#define "; echo -n $(subst $(obj)/autoconf/,VTSS_AUTOCONF_,$(basename $(f))) | tr '\-a-z' '_A-Z'; echo " 1") >> $@; fi;)
	@$(foreach f, $(autoconf-srcs), if $(CHECK_BUILD) $(f); then (echo -n "#define "; echo -n VTSS_AUTOCONF_$(notdir $(f:.c=)) | tr '\-a-z' '\_A-Z'; echo " 1") >> $@; fi;)
	@echo "" >> $@
	@if [ -f $(obj)/vtss_version.h ]; then \
		echo "#include \"vtss_version.h\""; \
		echo ""; \
	fi >> $@
	@echo "#ifndef VTSS_VERSION_MAJOR" >> $@
	@echo "#define VTSS_VERSION_MAJOR    1" >> $@
	@echo "#endif /* VTSS_VERSION_MAJOR */" >> $@
	@echo "#ifndef VTSS_VERSION_MINOR" >> $@
	@echo "#define VTSS_VERSION_MINOR    0" >> $@
	@echo "#endif /* VTSS_VERSION_MINOR */" >> $@
	@echo "#ifndef VTSS_VERSION_REVISION" >> $@
	@echo "#define VTSS_VERSION_REVISION 0" >> $@
	@echo "#endif /* VTSS_VERSION_REVISION */" >> $@
	@echo "#ifndef VTSS_VERSION_STRING" >> $@
	@echo "#define VTSS_VERSION_STRING   \"v1.0.0-custom\"" >> $@
	@echo "#endif /* VTSS_VERSION_STRING */" >> $@
	@echo "" >> $@
	@echo "#endif /* _VTSS_AUTOCONF_H_ */" >> $@


###############################################################################
# Otherwise, we were called directly from the command line;
# invoke the kernel build system.
else


# eventual filename of the driver
ifneq ($(ARCH),)
DRIVER_FILENAME := $(DRIVER_NAME)-$(ARCH)-$(KVER)smp.ko
else
DRIVER_FILENAME := $(DRIVER_NAME)-$(PLATFORM)-$(KVER)smp.ko
endif

.PHONY: default all clean distclean install

default: all

all:
	-@$(MAKE) -C $(KDIR) M=$(PWD) V=0 modules
	-cp -f $(DRIVER_NAME).ko $(DRIVER_FILENAME)

clean:
	-@if [ -d $(KDIR) ]; then \
		$(MAKE) -C $(KDIR) M=$(PWD) V=0 clean; \
	else \
		rm -f *.o .*.o.cmd .*.o.d .*.ko.cmd .*.ko.unsigned.cmd *.gcno; \
	fi
	-rm -f $(PWD)/vtss_autoconf.h

distclean: clean
	-rm -f $(DRIVER_NAME).ko $(DRIVER_FILENAME)

install:
	-@if [ -f $(DRIVER_NAME).ko ]; then \
		if [ "$(INSTALL)" != "." -a "$(INSTALL)" != "$(PWD)" -a "$(INSTALL)" != "$(PWD)/" ]; then \
			cp -f $(DRIVER_NAME).ko $(INSTALL)/$(DRIVER_FILENAME); \
			cp -f insmod-vtsspp $(INSTALL)/insmod-vtsspp; \
			cp -f rmmod-vtsspp $(INSTALL)/rmmod-vtsspp; \
			echo "Installed $(DRIVER_NAME) driver to $(INSTALL)/$(DRIVER_FILENAME)"; \
		fi; \
	else \
		echo "Error: Driver $(DRIVER_NAME) is not found"; exit 1; \
	fi
endif
