##*****************************************************************************
## 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 FFTW2 C wrappers to Intel MKL.
##*****************************************************************************

help:
	@echo "Usage: make libia32|libintel64|libmic"
	@echo "            [PRECISION={MKL_DOUBLE|MKL_SINGLE}]"
	@echo "            [compiler=compiler_name]"
	@echo
	@echo "  compiler_name - {pgi|gnu|intel}"
	@echo "        Default value: intel"
	@echo "        Intel(R) C Compiler as default"
	@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: $$(MKLROOT)/lib/<arch>'
	@echo
	@echo "  INSTALL_LIBNAME=<name>"
	@echo "        Specify the name of the library."
	@echo "        Default value depends on PRECISION and compiler used:"
	@echo '          libfftw2xc_$(prec)_$(compiler).a'
	@echo

##-----------------------------------------------------------------------------
## examples of using:
##
## make libia32 PRECISION=MKL_SINGLE compiler=gnu
##      - compile by GNU C compiler for single precision data
##        and build library for Intel IA32 based applications
##
## make libintel64
##      - compile by Intel(R) C Compiler and build library
##        for Intel(R) 64 processor family applications
##-----------------------------------------------------------------------------
## Default values

MY_MAKEFILE := $(MAKEFILE_LIST)

MKLROOT ?= ../..

include $(MKLROOT)/interfaces/fftw2xc/fftw2xc.lst

compiler = intel

ifndef INSTALL_DIR
   INSTALL_DIR = $(MKLROOT)/lib/$(_IA)
   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

ifeq ($(compiler),gnu)
   CC = gcc
   COPTS.ia32 = -m32
   COPTS.intel64 = -m64
else
   ifeq ($(compiler),pgi)
      CC = pgcc
      COPTS.ia32 = -tp px
      COPTS.intel64 = -tp x64
   else
      override compiler = intel
      CC = icc
   endif
endif

COPTS = $(COPTS.$(_IA))

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

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

.PHONY: lib mkobjdir library clean clean_obj

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

.SUFFIXES:
.SUFFIXES: .c .o

vpath %.c $(MKLROOT)/interfaces/fftw2xc/wrappers

lib: mkobjdir library

library: $(objects)
	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
