#
# Check if some required definitions were made by the user.
#

ifeq ($(HOSTRTL),)
  $(error HOSTRTL is not set)
endif
ifeq ($(TARGETRTL),)
  $(error TARGETRTL is not set)
endif
ifeq ($(GLOMPRTL),)
  $(error GLOMPRTL is not set)
endif
ifeq ($(LLVMBIN),)
  $(error LLVMBIN is not set)
endif

#
# Compiler binaries 
#

CC=$(LLVMBIN)/clang
CXX=$(LLVMBIN)/clang++

ifeq ($(shell which $(CC)),)
  $(error $(CC) does not exist!)
endif
ifeq ($(shell which $(CXX)),)
  $(error $(CC) does not exist!)
endif

#
# Create default command for the target machine
#

machine := $(shell uname -m)

ifeq (HOST,1)
  machine := ppc64
endif

ifeq ($(machine),ppc64)
	cmdc += $(CC)
  cmdc += -I/usr/local/include
  cmdc += -I/usr/lib/gcc/ppc64-redhat-linux/4.4.7/include
  cmdc += -I/usr/include
  
  cmdx += $(CXX)
  cmdx += -I/usr/lib/gcc/ppc64-redhat-linux/4.4.7/../../../../include/c++/4.4.7
  cmdx += -I/usr/lib/gcc/ppc64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ppc64-redhat-linux
  cmdx += -I/usr/lib/gcc/ppc64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/backward
  cmdx += -I/usr/local/include
  cmdx += -I/usr/lib/gcc/ppc64-redhat-linux/4.4.7/include
  cmdx += -I/usr/include
  
  cmdx += -std=c++11
  
  cmdc += -L$(HOSTRTL) -L$(TARGETRTL) -target powerpc64-ibm-linux-gnu -mcpu=pwr7 -fopenmp=libomp -fopenmp-targets=powerpc64-ibm-linux-gnu
  cmdx += -L$(HOSTRTL) -L$(TARGETRTL) -target powerpc64-ibm-linux-gnu -mcpu=pwr7 -fopenmp=libomp -fopenmp-targets=powerpc64-ibm-linux-gnu
else ifeq ($(machine),ppc64le)
  cmdc += $(CC) -v 
  cmdc += -I$(HOSTRTL)
  cmdc += -I$(HOSTRTL)/../
  cmdc += -I$(HOSTRTL)/../projects/openmp/runtime/src/  

  cmdx += $(CXX)
  cmdx += -I$(HOSTRTL)
  cmdx += -I$(HOSTRTL)/../
  cmdx += -I$(HOSTRTL)/../projects/openmp/runtime/src/ 
  cmdx += -std=c++11 -v
  
  cmdc += -L$(HOSTRTL) -L$(TARGETRTL) -target powerpc64le-ibm-linux-gnu -mcpu=pwr8 -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda
  cmdx += -L$(HOSTRTL) -L$(TARGETRTL) -target powerpc64le-ibm-linux-gnu -mcpu=pwr8 -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda
else ifeq ($(machine),x86_64)
  cmdc += $(CC) -v
  cmdc += -I$(HOSTRTL)
  cmdc += -I$(HOSTRTL)/../
  cmdc += -I$(HOSTRTL)/../projects/openmp/runtime/src/
  cmdc += -I$(TARGETRTL)/../include

  cmdx += $(CXX)
  cmdx += -I$(HOSTRTL)
  cmdx += -I$(HOSTRTL)/../
  cmdx += -I$(HOSTRTL)/../projects/openmp/runtime/src/
  cmdx += -I$(TARGETRTL)/../include

  cmdx += -std=c++11 -v
  
  ifeq ($(TARGET),)
    TARGET  =  -fopenmp-targets=amdgcn--cuda -Xopenmp-target=amdgcn--cuda -march=gfx803
  endif
  cmdc += -L$(HOSTRTL) -L$(TARGETRTL) -L$(TARGETRTL)/libdevice -target x86_64-pc-linux-gnu -fopenmp=libomp $(TARGET)
  cmdx += -L$(HOSTRTL) -L$(TARGETRTL) -L$(TARGETRTL)/libdevice -target x86_64-pc-linux-gnu -fopenmp=libomp $(TARGET)

  extra_ld_lib_path := /home/compteam/opt/gcc/lib64
else
  $(error Support for $(machine) not implemented yet!)
endif

main_folder := $(shell pwd)
results_folder := $(main_folder)/results
error_log := $(results_folder)/error.log
compile_log := $(results_folder)/compile.log
compile_cmd := $(results_folder)/compile.cmd

#
# Make sure the libraries are visible 
#

LD_LIBRARY_PATH := $(HOSTRTL)/..:$(TARGETRTL):$(GLOMPRTL):/usr/local/cuda/lib64:$(results_folder):$(extra_ld_lib_path)
export LD_LIBRARY_PATH
LIBRARY_PATH := $(GLOMPRTL):/usr/local/cuda/lib64:/usr/local/cuda/nvvm/libdevice
export LIBRARY_PATH

