
#
# Makefile.hipfort: Include Makefile to set values LINKOPTS, CXX, and FC 
#                   to support compilation with HIPFORT.
#
# This file is meant to be included by other Makefiles.  The includer must 
# set HIPFORT_HOME to the build or install location of HIPFORT.  For example:
#
#    HIPFORT_HOME ?= /opt/rocm/
#    include $(HIPFORT_HOME)/share/hipfort/Makefile.hipfort
#  
# If the caller does not set HIPFORT_ARCHGPU, this Makefile will call 
# a self-identification utility called myarchgpu.  Please create an issue
# in the hipfort repo if myarchgpu returns unknown.
#
# To avoid self identification set HIPFORT_ARCHGPU.  Examples:
#    HIPFORT_ARCHGPU = amdgcn-gfx906
#    HIPFORT_ARCHGPU = nvptx-sm_70
#

HIPFORT_ARCHGPU ?= $(shell $(HIPFORT_HOME)/libexec/hipfort/myarchgpu)
ARCH = $(firstword $(subst -, ,$(HIPFORT_ARCHGPU)))
HIPFORT_COMPILER ?= gfortran
CUDA_PATH=${CUDA_PATH:-/usr/local/cuda}
ROCM_PATH=${ROCM_PATH:-/opt/rocm}
DEVICE_LIB_PATH  ?= $(ROCM_PATH)/lib
HIP_CLANG_PATH   ?= $(ROCM_PATH)/llvm/bin
HIP_PLATFORM=${HIP_PLATFORM:-amd}

MOD_DIR  = $(HIPFORT_HOME)/include/hipfort/$(ARCH)
LIB_DIR  = $(HIPFORT_HOME)/lib

GPU = $(strip $(subst ., ,$(suffix $(subst -,.,$(HIPFORT_ARCHGPU)))))
ifeq (nvptx,$(findstring nvptx,$(HIPFORT_ARCHGPU)))
  UNAMEP     = $(shell uname -p)
  LINKOPTS   = -L$(LIB_DIR) -lhipfort-$(ARCH) -L$(CUDA_PATH)/targets/$(UNAMEP)-linux/lib $(LINKOPTS) -lcudart
  HIPCC_ENV  = HIP_PLATFORM=nvidia
  HIPCC_OPTS = "--gpu-architecture=$(GPU)"
else
  LINKOPTS   = -L$(LIB_DIR) -lhipfort-$(ARCH) -L$(ROCM_PATH)/lib $(HIPCC_LIBS) -lamdhip64 -Wl,-rpath=$(ROCM_PATH)/lib
  HIPCC_ENV  = HIP_PLATFORM=$(HIP_PLATFORM) DEVICE_LIB_PATH=$(DEVICE_LIB_PATH) HIP_CLANG_PATH=$(HIP_CLANG_PATH)
  HIPCC_OPTS = -fno-gpu-rdc -fPIC --offload-arch=$(GPU)
endif

LINKOPTS += -lstdc++
CXX      = $(HIPCC_ENV) $(ROCM_PATH)/bin/hipcc $(HIPCC_OPTS)
FC_OPTS  = "-DHIPFORT_ARCH=\"$(ARCH)\""
FC       = $(HIPFORT_COMPILER) -cpp -I$(MOD_DIR) $(FC_OPTS)
