#===============================================================================
# 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 C 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 C interfaces to Intel(R) oneMKL"
	@echo "      for Intel(R) 64 architecture."
	@echo
	@echo "  <options>"
	@echo
	@echo "    compiler={intel|gnu|clang}"
	@echo "        The C compiler 'icx', 'gcc', or 'clang' 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 "    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, 'libfftw2xc_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 C 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 'libfftw2xc_double_intel.a' in ../../../../lib."
	@echo

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

MY_MAKEFILE := $(MAKEFILE_LIST)

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

include $(IFACE_DIR)/fftw2xc/fftw2xc.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 ?= libfftw2xc_$(prec)_$(compiler).a

gnu_or_clang = $(if $(filter gnu,$(compiler)),gcc,$(filter clang,$(compiler)))
ifneq ($(gnu_or_clang),)
   CC = $(gnu_or_clang)
   COPTS.intel64 = -m64
   COPTS.DIAG = -Wall -Werror
else
   override compiler = intel
   CC = icx
   COPTS.DIAG = -Wall -Werror
endif

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

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

.PHONY: libintel64 libem64t

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

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

.PHONY: lib mkobjdir clean

.SUFFIXES:
.SUFFIXES: .c .o

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

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) $(COPTS) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c \
	  -D$(PRECISION) -I$(MKLROOT)/include -I$(MKLROOT)/include/fftw \
	  $< -o $@

mkobjdir:
	mkdir -p $(obj_path)

clean:
	rm -rf $(obj_path)

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