#===============================================================================
# Copyright 2005 Intel Corporation.
#
# This software and the related documents are Intel copyrighted  materials,  and
# your use of  them is  governed by the  express license  under which  they were
# provided to you (License).  Unless the License provides otherwise, you may not
# use, modify, copy, publish, distribute,  disclose or transmit this software or
# the related documents without Intel's prior written permission.
#
# This software and the related documents  are provided as  is,  with no express
# or implied  warranties,  other  than those  that are  expressly stated  in the
# License.
#===============================================================================

#  Content:
#      Intel(R) oneAPI Math Kernel Library BLAS95 library creation
#******************************************************************************

help:
	@echo
	@echo "Usage: make libintel64 INSTALL_DIR=<path> [<options>]"
	@echo
	@echo "Intel(R) Fortran Compiler is the default compiler."
	@echo
	@echo "  libintel64"
	@echo "      A makefile target to build Fortran 95 interfaces to BLAS for"
	@echo "      Intel(R) 64 architecture."
	@echo
	@echo "  INSTALL_DIR=<path>"
	@echo "      The built library and the .mod files will be installed in"
	@echo "      subdirectories of <path> the same way as if <path> were the"
	@echo "      Intel(R) oneMKL installation directory."
	@echo
	@echo "  <options>"
	@echo
	@echo "    FC={ifx|ifort|gfortran}"
	@echo "        The specified Fortran compiler will be used."
	@echo "        Default: ifx"
	@echo
	@echo "    interface={lp64|ilp64}"
	@echo "        The lp64 interface uses the 32-bit integer type, while the"
	@echo "        ilp64 interface uses the 64-bit integer type."
	@echo "        Default: lp64"
	@echo
	@echo "    MKLROOT=<path>"
	@echo "        Intel(R) oneMKL installation directory."
	@echo "        Default: ../../../.."
	@echo
	@echo
	@echo "Usage example:"
	@echo
	@echo "  make libintel64 INSTALL_DIR=lib95"
	@echo "      This command builds Fortran 95 interfaces to BLAS for"
	@echo "      Intel(R) 64 architecture using Intel(R) Fortran Compiler"
	@echo "      and installs the built files in the 'lib95' directory. The built"
	@echo "      library uses the 32-bit integer type."
	@echo

##------------------------------------------------------------------------------

ifndef FC
    FC = ifx
else
ifeq ($(FC),f77)
    FC = ifx
endif
endif

ifndef interface
    interface = lp64
endif

ifndef MKLROOT
    MKLROOT = ../../../..
endif

iface_95 = mkl_blas.f90
iface_77 = blas_interfaces.f90

opts0    =
optsc    = -c $(opts0)
sufobj   = o
suflib   = a
sufmod   = mod
src_path = source
LIBR     = ar -rs

obj_path = $(INSTALL_DIR)/lib/obj_blas95_intel64_$(interface)
obj77_path = $(obj_path)/obj77

inc_path = $(MKLROOT)/include

include blas95.lst
objs    = $(src_blas95:.f90=.$(sufobj))
objects = $(addprefix $(obj_path)/,$(objs))
mod77 = $(obj77_path)/$(iface_77:.f90=.obj)

ifeq ($(FC),gfortran)
    auto=-fautomatic
    # no space after J
    opt_mod = -J
else
    auto=-auto
    # space after module
    opt_mod = -module #
endif

FOPTS = -fPIC $(auto)
ifeq ($(interface),ilp64)
    FOPTS += $(if $(filter gfortran, $(FC)), -fdefault-integer-8, -i8)
endif

mod_path = $(INSTALL_DIR)/include/mkl/intel64/$(interface)
mkl_blas95_lib = libmkl_blas95_$(interface).$(suflib)

vpath %.f90       $(src_path)
vpath %.f90       $(inc_path)
vpath %.$(sufobj) $(obj_path)

$(obj_path)/%.$(sufobj): %.f90
	$(FC) $(SPEC_OPT) $(FOPTS) $(opt_mod)$(obj77_path) $(optsc) -o $(obj_path)/$(@F) $<

$(mod_path)/$(mod95): $(inc_path)/$(iface_95)
	mkdir -p $(obj77_path)
	mkdir -p $(mod_path)
	$(FC) $(SPEC_OPT) $(FOPTS) $(opt_mod)$(mod_path) $(optsc) -o $(obj77_path)/mkl_blas.$(sufobj) $<

.PHONY: build lib mod clean checkdir libem64t libintel64 objects clean_obj cleanintel64

libintel64 libem64t: checkdir
	make build interface=$(interface) MKLROOT=$(MKLROOT) INSTALL_DIR=$(INSTALL_DIR) FC=$(FC)

build: mod lib clean_obj
lib: $(INSTALL_DIR)/lib/$(mkl_blas95_lib)
mod: $(mod_path)/$(mod95)

$(INSTALL_DIR)/lib/$(mkl_blas95_lib):
	mkdir -p $(obj77_path)
	$(FC) $(SPEC_OPT) $(FOPTS) $(opt_mod)$(obj77_path) $(optsc) -o $(mod77) $(src_path)/$(iface_77)
	make objects
	$(LIBR) $(INSTALL_DIR)/lib/$(mkl_blas95_lib) $(obj_path)/*.$(sufobj)

objects:$(objects)

checkdir:
ifndef INSTALL_DIR
	$(error Not given INSTALL_DIR)
endif

clean cleanintel64: checkdir
	-rm -rf $(INSTALL_DIR)/lib/obj_blas95_intel64_*
	-rm -f $(INSTALL_DIR)/lib/libmkl_blas95_*lp64.$(suflib)

clean_obj:
	-rm -rf $(obj_path)
