##*****************************************************************************
## Copyright(C) 2006-2014 Intel Corporation. All Rights Reserved.
## 
## The source code, information  and  material ("Material") contained herein is
## owned  by Intel Corporation or its suppliers or licensors, and title to such
## Material remains  with Intel Corporation  or its suppliers or licensors. The
## Material  contains proprietary information  of  Intel or  its  suppliers and
## licensors. The  Material is protected by worldwide copyright laws and treaty
## provisions. No  part  of  the  Material  may  be  used,  copied, reproduced,
## modified, published, uploaded, posted, transmitted, distributed or disclosed
## in any way  without Intel's  prior  express written  permission. No  license
## under  any patent, copyright  or  other intellectual property rights  in the
## Material  is  granted  to  or  conferred  upon  you,  either  expressly,  by
## implication, inducement,  estoppel or  otherwise.  Any  license  under  such
## intellectual  property  rights must  be express  and  approved  by  Intel in
## writing.
## 
## *Third Party trademarks are the property of their respective owners.
## 
## Unless otherwise  agreed  by Intel  in writing, you may not remove  or alter
## this  notice or  any other notice embedded  in Materials by Intel or Intel's
## suppliers or licensors in any way.
##
##*****************************************************************************
## Content:
##      Build standalone library of FFTW3 C wrappers to Intel MKL.
##*****************************************************************************

## Usage example:
##
## make libintel64 compiler=gnu
##      Build the library for Intel(R) 64 based applications
##      using GNU C compiler, for use with GNU C compiler.

help:
	@echo "Usage: make libia32|libintel64|libmic [option...]"
	@echo
	@echo "Options:"
	@echo "  compiler=gnu|pgi|intel"
	@echo "        Build the library using GNU gcc, PGI pgcc, or"
	@echo "        Intel(R) C compiler icc."
	@echo "        Default value: intel"
	@echo
	@echo "Additional macros:"
	@echo "  MKLROOT=<path>"
	@echo "        Path to MKL root directory with header files and libraries."
	@echo "        Default value: ../.."
	@echo
	@echo "  INSTALL_DIR=<path>"
	@echo "        Install the library to the specified location."
	@echo "        Default value:  . (that is, the current directory)"
	@echo
	@echo "  INSTALL_LIBNAME=<name>"
	@echo "        Specify the name of the library."
	@echo "        Default value depends on PRECISION and compiler used:"
	@echo "          libfftw3xc_$(compiler).a"
	@echo

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

MY_MAKEFILE := $(MAKEFILE_LIST)

MKLROOT ?= ../..
compiler = intel

include $(MKLROOT)/interfaces/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: libia32 lib32 libintel64 libem64t libmic

libia32 lib32:
	$(MAKE) -f $(MY_MAKEFILE) lib _IA=ia32

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

libmic:
	$(MAKE) -f $(MY_MAKEFILE) lib _IA=mic

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

_CC_intel = icc
_CC_gnu = gcc
_CC_pgi = pgcc
CC = $(firstword $(_CC_$(compiler)) icc)

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

_cflags_intel = -vec-report0

## This option is required by Intel(R) compiler to produce
## Pentium(R) III compatible executables.
_cflags_intel_ia32 = -mia32

_cflags_gnu_ia32 = -m32
_cflags_gnu_intel64 = -m64

_cflags_pgi_ia32 = -tp px
_cflags_pgi_intel64 = -tp x64

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

_srcdir = $(MKLROOT)/interfaces/fftw3xc/wrappers

vpath %.c $(_srcdir)

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

.PHONY: lib mkresdir clean cleanup

ifeq ($(_IA),mic) 
    TARGET_ARCH += -mmic 
endif

.SUFFIXES:
.SUFFIXES: .c .o

lib: clean mkresdir $(INSTALL_DIR)/$(INSTALL_LIBNAME) cleanup

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

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

mkresdir:
	mkdir -p $(obj_path)

clean:
	-rm -rf $(obj_path)

cleanup:
	-rm -rf $(obj_path)

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