##*****************************************************************************
##                              INTEL CONFIDENTIAL
## Copyright(C) 2006-2010 Intel Corporation. All Rights Reserved.
## The source code contained  or  described herein and all documents related to
## the source code ("Material") are owned by Intel Corporation or its suppliers
## or licensors.  Title to the  Material remains with  Intel Corporation or its
## suppliers and licensors. The Material contains trade secrets and proprietary
## and  confidential  information of  Intel or its suppliers and licensors. The
## Material  is  protected  by  worldwide  copyright  and trade secret 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, trade secret or other intellectual
## property right is granted to or conferred upon you by disclosure or delivery
## of the Materials,  either expressly, by implication, inducement, estoppel or
## otherwise.  Any  license  under  such  intellectual property  rights must be
## express and approved by Intel in writing.
##
##*****************************************************************************
## Content:
##      Build standalone library of FFTW3 Fortran wrappers to Intel MKL.
##*****************************************************************************

## Usage example:
##
## make lib64 compiler=gnu
##        Build the library for Intel(R) Itanium(R)-based applications
##        using GNU gcc compiler, for use with gfortran compiler.

help:
	@echo "Usage: make {lib32|lib64|libem64t} [option...]"
	@echo "Options:"
	@echo ""
	@echo " compiler=gnu|pgi|intel"
	@echo "        Build examples using GNU gcc, PGI pgcc, or"
	@echo "        Intel(R) C compiler icc. Default value: intel."
	@echo ""
	@echo " MKLROOT=<path>"
	@echo "        Locate MKL header files relative to <path>."
	@echo "        Default value: ../.., unless defined in environment"
	@echo ""
	@echo " i8=yes|no"
	@echo "        Target default INTEGER size 8 bytes."
	@echo "        Default value: no, that is assume INTEGER is 4 bytes."
	@echo ""
	@echo " fname=a_name|a_name_|a_name__|A_NAME"
	@echo "        Select the pattern to decorate wrapper names for"
	@echo "        Fortran. For example, with no special options"
	@echo "            g77 uses pattern a_name__"
	@echo "            ifort and gfortran use pattern a_name_"
	@echo "            ifort on Windows uses pattern A_NAME"
	@echo "        Default value: a_name_."
	@echo ""
	@echo " install_to=<path>"
	@echo "        Install the library to the specified location,"
	@echo "        which must exist. Default value: ."
	@echo ""
	@echo " install_as=<name>"
	@echo "        Specify the name of the library."
	@echo "        Default value depends on compiler used."

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

include fftw3xf.lst
compiler = intel
MKLROOT ?= ../..
i8 = no
fname = a_name_
install_to = .
install_as = libfftw3xf_$(compiler).a

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

lib32:
	$(MAKE) lib _IA=32

libem64t:
	$(MAKE) lib _IA=em64t

lib64:
	$(MAKE) lib _IA=64

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

## Option -O0 is required by Intel(R) compiler to produce
## Pentium(R) III compatible executables.
_cflags_intel_32 = -O0

# Define _cppflags
_cppflags  = -I$(MKLROOT)/include -I$(MKLROOT)/include/fftw
_cppflags += $(_cppflags_i8_$(i8))
_cppflags += $(_cppflags_$(fname))
_cppflags += $(CPPFLAGS)

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

_cppflags_i8_yes   = -DMKL_ILP64

_srcdir = wrappers
vpath %.c $(_srcdir)

_tmpdir := $(shell mktemp -d)

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

lib: clean mkresdir $(install_to)/$(install_as) cleanup

$(install_to)/$(install_as): $(WRAP:%=$(_tmpdir)/%.o)
	ar rs $@ $^

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


clean:
	-rm -rf $(_tmpdir)

mkresdir:
	mkdir -p $(_tmpdir)

cleanup:
	-rm -rf $(_tmpdir)

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