#===============================================================================
# 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 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 FFTW3 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 "    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 "        'libfftw3xc_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 C interfaces to Intel(R) oneMKL for"
	@echo "      Intel(R) 64 architecture using Intel(R) oneAPI DPC++/C++ Compiler"
	@echo "      and installs the built library 'libfftw3xc_intel.a' in the"
	@echo "      directory that contains this makefile."
	@echo

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

MY_MAKEFILE := $(MAKEFILE_LIST)

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

include $(IFACE_DIR)/fftw3xc/fftw3xc.lst

install_to ?= .
INSTALL_DIR ?= $(install_to)
obj_path = $(INSTALL_DIR)/obj_$(compiler)
install_as ?= libfftw3xc_$(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_clang = clang
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

_cflags_clang_intel64 = -m64
_cflags_clang_diag = -Wall -Werror

# Define _cppflags
_cppflags = -I$(MKLROOT)/include -I$(MKLROOT)/include/fftw -I$(MKLROOT)/include/fftw/offload
_cppflags += $(CPPFLAGS) $(TARGET_ARCH)

_srcdir = $(IFACE_DIR)/fftw3xc/wrappers

vpath %.c $(_srcdir)
vpath %.c $(_srcdir)/offload

##-----------------------------------------------------------------------------
## 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
