#===============================================================================
# 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 FFTW2 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 FFTW2 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 "    PRECISION={MKL_DOUBLE|MKL_SINGLE}"
	@echo "        The library will be built for double- or single-precision"
	@echo "        floating-point data type."
	@echo "        Default: MKL_DOUBLE"
	@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 "    INSTALL_DIR=<path>"
	@echo "        The built library will be installed in <path>."
	@echo "        Default: \$$(MKLROOT)/lib"
	@echo
	@echo "    INSTALL_LIBNAME=<name>"
	@echo "        The name of the built library."
	@echo "        The default value depends on 'PRECISION' and 'compiler' in use,"
	@echo "        for example, 'libfftw2xf_double_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 double-precision FFTW2 Fortran interfaces to"
	@echo "      Intel(R) oneMKL for Intel(R) 64 architecture using"
	@echo "      Intel(R) oneAPI DPC++/C++ Compiler and installs the built"
	@echo "      library 'libfftw2xf_double_intel.a' in ../../../../lib. The built"
	@echo "      library uses the 32-bit integer type."
	@echo

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

MY_MAKEFILE := $(MAKEFILE_LIST)

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

include $(IFACE_DIR)/fftw2xf/fftw2xf.lst

compiler = intel

ifndef INSTALL_DIR
   INSTALL_DIR = $(MKLROOT)/lib
   obj_path = obj_$(prec)_$(compiler)
else
   obj_path = $(INSTALL_DIR)/obj_$(prec)_$(compiler)
endif

ifneq ($(PRECISION),MKL_SINGLE)
   override PRECISION = MKL_DOUBLE
   prec = double
else
   prec = single
endif

INSTALL_LIBNAME ?= libfftw2xf_$(prec)_$(compiler).a

ifeq ($(compiler),gnu)
   CC = gcc -D_GNU
   COPTS.intel64 = -m64
   COPTS.DIAG = -Wall -Werror
else
   override compiler = intel
   CC = icx
   COPTS.DIAG = -Wall -Werror
endif

COPTS_interface_ilp64 = -DMKL_ILP64

COPTS = $(COPTS.$(_IA)) $(COPTS.DIAG) $(COPTS_interface_$(interface))

objs    = $(addsuffix .o ,$(WRAP))
objects = $(addprefix $(obj_path)/,$(objs))

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

.PHONY: libintel64 libem64t

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

ifdef _IA
##-----------------------------------------------------------------------------
## Rules

.PHONY: lib mkobjdir library clean clean_obj

.SUFFIXES:
.SUFFIXES: .c .o

vpath %.c $(IFACE_DIR)/fftw2xf/wrappers

lib: mkobjdir library

library: $(objects)
	-mkdir -p $(INSTALL_DIR)
	ar rs $(INSTALL_DIR)/$(INSTALL_LIBNAME) $^

$(obj_path)/%.o: %.c
	$(CC) $(COPTS) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c \
	  -D$(PRECISION) -I$(MKLROOT)/include -I$(MKLROOT)/include/fftw \
	  $< -o $@

mkobjdir:
	mkdir -p $(obj_path)

clean: clean_obj
	-rm -f $(INSTALL_DIR)/$(INSTALL_LIBNAME)

clean_obj:
	-rm -rf $(obj_path)

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