##******************************************************************************
##  Copyright(C) 2011-2012 Intel Corporation. All Rights Reserved.
##
##  The source code, information  and  material ("Material") contained herein is
##  owned  by Intel Corporation or its suppliers or licensors, and title to such
##  Material remains  with Intel Corporation  or its suppliers or licensors. The
##  Material  contains proprietary information  of  Intel or  its  suppliers and
##  licensors. The  Material is protected by worldwide copyright laws and treaty
##  provisions. No  part  of  the  Material  may  be  used,  copied, reproduced,
##  modified, published, uploaded, posted, transmitted, distributed or disclosed
##  in any way  without Intel's  prior  express written  permission. No  license
##  under  any patent, copyright  or  other intellectual property rights  in the
##  Material  is  granted  to  or  conferred  upon  you,  either  expressly,  by
##  implication, inducement,  estoppel or  otherwise.  Any  license  under  such
##  intellectual  property  rights must  be express  and  approved  by  Intel in
##  writing.
##
##  *Third Party trademarks are the property of their respective owners.
##
##  Unless otherwise  agreed  by Intel  in writing, you may not remove  or alter
##  this  notice or  any other notice embedded  in Materials by Intel or Intel's
##  suppliers or licensors in any way.
##
##******************************************************************************
##  Content:
##      Makefile for SHOC
##******************************************************************************

ICC ?= icc
ICPC ?= icpc
TARGET_ICC ?= $(ICC)
TARGET_ICPC ?= $(ICPC)
INSTALL ?= install
VERSION ?= 0.0.0
prefix ?= /usr
exec_prefix ?= $(prefix)
libexecdir ?= $(exec_prefix)/libexec
datarootdir ?= $(prefix)/share
docdir ?= $(datarootdir)/doc/micperf
srcinstalldir ?= $(prefix)/src/micperf
card_libs ?= /opt/mpss/x200/minsdk/lib

TARGETS_x86_64 = BusSpeedDownload_pragma BusSpeedReadback_pragma BusSpeedDownload_scif BusSpeedReadback_scif
TARGETS_k1om = BusSpeed_mic
TARGETS = $(TARGETS_x86_64) $(TARGETS_k1om)

knc: TARGET_ICC += -mmic
knc: TARGET_ICPC += -mmic
knllb: TARGET_ICC += -xMIC-AVX512
knllb: TARGET_ICPC += -xMIC-AVX512

SOURCE = BusSpeedDownload_pragma.cpp BusSpeedDownload_scif.cpp BusSpeedReadback_pragma.cpp \
         BusSpeedReadback_scif.cpp main.cpp main_mic.c Option.cpp Option.h OptionParser.cpp \
         OptionParser.h ResultDatabase.cpp ResultDatabase.h \
         cross_timer.h Utility.h Makefile

all: knc
knc: $(TARGETS_x86_64) $(TARGETS_k1om)
knllb: $(TARGETS_x86_64) $(TARGETS_k1om)

install: install-knc

install-knc: install-x86_64 install-k1om install-license

install-knllb: install-x86_64 install-x86_64-avx512 install-license

$(TARGETS_x86_64) : CC = $(ICC)
$(TARGETS_x86_64) : CXX = $(ICPC)
$(TARGETS_k1om) : CC = $(TARGET_ICC)
$(TARGETS_k1om) : CXX = $(TARGET_ICPC)

COMMON_SRC = main.cpp Option.cpp OptionParser.cpp ResultDatabase.cpp

CFLAGS_k1om = $(shell echo $(CFLAGS) | sed -e 's/x86_64-linux-nativepkg/knightscorner/g')
LDFLAGS_k1om = $(shell echo $(LDFLAGS) | sed -e 's/x86_64-linux-nativepkg/knightscorner/g')

CXXFLAGS_pragma = -Wall -parallel -g -O3 -qopenmp -intel-extensions -I$(MKLROOT)/include -lrt
CFLAGS_scif = -Wall -g -O3 -DSCIF_SHOC -lrt -lscif
CXXFLAGS_scif = $(CFLAGS_scif)
knllb: KNLLB_FLAGS_pragma = -qoffload-arch=mic-avx512 -qoffload-option,mic,compiler,"-L$(card_libs)" -qoffload-option,mic,link,"-rpath-link=$(card_libs)"

BusSpeed%_pragma: BusSpeed%_pragma.cpp $(COMMON_SRC)
	$(CXX) $(KNLLB_FLAGS_pragma)  $(CPPFLAGS) $(CXXFLAGS) $(CXXFLAGS_pragma) $(LDFLAGS) $^ -o $@

BusSpeed%_scif: BusSpeed%_scif.cpp $(COMMON_SRC)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(CXXFLAGS_scif) $(LDFLAGS) $^ -o $@

BusSpeed_mic: main_mic.c
	$(CC) $(CPPFLAGS) $(CFLAGS_k1om) $(CFLAGS_scif) $(LDFLAGS_k1om) $^ -o $@

install-x86_64: $(TARGETS_x86_64)
	$(INSTALL) -d $(DESTDIR)$(libexecdir)/micperf/x86_64
	$(INSTALL) $^ $(DESTDIR)$(libexecdir)/micperf/x86_64

install-k1om: $(TARGETS_k1om)
	$(INSTALL) -d $(DESTDIR)$(libexecdir)/micperf/k1om
	$(INSTALL) $^ $(DESTDIR)$(libexecdir)/micperf/k1om

install-x86_64-avx512: $(TARGETS_k1om)
	$(INSTALL) -d $(DESTDIR)$(libexecdir)/micperf/x86_64_AVX512
	$(INSTALL) $^ $(DESTDIR)$(libexecdir)/micperf/x86_64_AVX512

install-license:
	$(INSTALL) -d $(DESTDIR)$(docdir)
	$(INSTALL) -m 644 license_shoc.txt $(DESTDIR)$(docdir)

clean:
	-rm -rf $(TARGETS)

.PHONY: all knc knllb install install-knc install-knllb clean install-x86_64 install-k1om

