#===============================================================================
# Copyright 2006 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:
##      Build standalone library of FFTW3 Fortran wrappers to Intel(R) oneMKL.
##*****************************************************************************

help:
	@echo
	@echo "Usage: make libintel64 [<options>]"
	@echo
	@echo "Intel(R) oneAPI DPC++/C++ Compiler is the default compiler."
	@echo
	@echo "  libintel64"
	@echo "      A makefile target to build FFTW3 Fortran interfaces to"
	@echo "      Intel(R) oneMKL for Intel(R) 64 architecture."
	@echo
	@echo "  <options>"
	@echo
	@echo "    compiler={intel|gnu}"
	@echo "        The C compiler 'icx' or 'gcc' will be used."
	@echo "        Default: intel"
	@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 "    fname={a_name_|a_name|a_name__|A_NAME}"
	@echo "        The pattern for wrapper names, for example, we will have"
	@echo "            dfftw_execute_()  when specifying 'fname=a_name_';"
	@echo "            dfftw_execute()   when specifying 'fname=a_name';"
	@echo "            dfftw_execute__() when specifying 'fname=a_name__';"
	@echo "            DFFTW_EXECUTE()   when specifying 'fname=A_NAME'."
	@echo "        With no special options,"
	@echo "            g77 uses the pattern 'a_name__';"
	@echo "            ifx on Linux and gfortran use the pattern 'a_name_';"
	@echo "            ifx on Windows uses the pattern 'A_NAME'."
	@echo "        Default: a_name_"
	@echo
	@echo "    MKLROOT=<path>"
	@echo "        Intel(R) oneMKL installation directory."
	@echo "        Default: ../../../.."
	@echo
	@echo "    INSTALL_DIR=<path>"
	@echo "        The built library will be installed in <path>."
	@echo "        Default: ."
	@echo
	@echo "    INSTALL_LIBNAME=<name>"
	@echo "        The name of the built library."
	@echo "        The default value depends on 'compiler' in use, for example,"
	@echo "        'libfftw3xf_intel.a'."
	@echo
	@echo "    CFLAGS=<flags>"
	@echo "        Additional compilation flags."
	@echo "        This variable is not predefined."
	@echo
	@echo "    CPPFLAGS=<flags>"
	@echo "        Additional preprocessor flags."
	@echo "        This variable is not predefined."
	@echo
	@echo "    TARGET_ARCH=<flags>"
	@echo "        Additional architecture-specific flags."
	@echo "        This variable is not predefined."
	@echo
	@echo
	@echo "Usage example:"
	@echo
	@echo "  make libintel64"
	@echo "      This command builds FFTW3 Fortran interfaces to Intel(R) oneMKL"
	@echo "      for Intel(R) 64 architecture using Intel(R) oneAPI DPC++/C++"
	@echo "      Compiler and installs the built library 'libfftw3xf_intel.a' in"
	@echo "      the directory that contains this makefile. The names of the"
	@echo "      interfaces are of lowercase letters with a trailing underscore,"
	@echo "      and the built library uses the 32-bit integer type."
	@echo

##-----------------------------------------------------------------------------
## Default values

MY_MAKEFILE := $(MAKEFILE_LIST)

MKLROOT ?= ../../../..
IFACE_DIR = $(MKLROOT)/share/mkl/interfaces

include $(IFACE_DIR)/fftw3xf/fftw3xf.lst

compiler = intel
INTERFACE = lp64
fname = a_name_

install_to ?= .
INSTALL_DIR ?= $(install_to)
obj_path = $(INSTALL_DIR)/obj_$(compiler)
install_as ?= libfftw3xf_$(compiler).a
INSTALL_LIBNAME ?= $(install_as)

##-----------------------------------------------------------------------------
## Main targets

.PHONY: libintel64 libem64t

libintel64 libem64t:
	$(MAKE) -f $(MY_MAKEFILE) lib _IA=intel64

ifdef _IA
##-----------------------------------------------------------------------------
## Supporting _macros

_CC_intel = icx
_CC_gnu = gcc
CC = $(firstword $(_CC_$(compiler)) icx)

# Define _cflags
_cflags = $(_cflags_$(compiler)_$(_IA))
_cflags += $(_cflags_$(compiler)_diag)
_cflags += $(CFLAGS)

_cflags_intel_diag = -Wall -Werror

_cflags_gnu_intel64 = -m64
_cflags_gnu_diag = -Wall -Werror

# Define _cppflags
_cppflags = -I$(MKLROOT)/include -I$(MKLROOT)/include/fftw
_cppflags += $(_cppflags_interface_$(INTERFACE))
_cppflags += $(_cppflags_$(fname))
_cppflags += $(CPPFLAGS) $(TARGET_ARCH)

_cppflags_a_name   = -D_FNAME_NOUNDERSCORE
_cppflags_a_name_  =
_cppflags_a_name__ = -D_FNAME_SECOND_UNDERSCORE
_cppflags_A_NAME   = -D_FNAME_UPPERCASE

_cppflags_interface_ilp64   = -DMKL_ILP64

_srcdir = $(IFACE_DIR)/fftw3xf/wrappers

vpath %.c $(_srcdir)

##-----------------------------------------------------------------------------
## Rules

.PHONY: lib mkobjdir clean

.SUFFIXES:
.SUFFIXES: .c .o

lib: mkobjdir $(INSTALL_DIR)/$(INSTALL_LIBNAME)

$(INSTALL_DIR)/$(INSTALL_LIBNAME): $(WRAP:%=$(obj_path)/%.o)
	ar rs $@ $^
	rm -rf $(obj_path)

$(obj_path)/%.o: %.c
	$(CC) $(_cflags) $(_cppflags) -c $< -o $@

mkobjdir:
	mkdir -p $(obj_path)

clean:
	rm -rf $(obj_path)

##-----------------------------------------------------------------------------
endif
