##******************************************************************************
##  Copyright(C) 2004-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:
##      Intel(R) Math Kernel Library Custom Shared Object builder
##
##******************************************************************************

help:
	@echo
	@echo "Custom Shared Object builder creates MKL custom library"
	@echo
	@echo "Usage: make <target> [<options>]"
	@echo
	@echo "  target"
	@echo "    (static builder: uses static Intel(R) MKL interface+threading+core libraries)"
	@echo "      libia32    - for IA-32 architecture"
	@echo "      libintel64 - for Intel(R) 64 architecture"
	@echo "    (dynamic builder: uses Intel(R) MKL single dynamic library libmkl_rt.so)"
	@echo "      soia32     - for IA-32 architecture"
	@echo "      sointel64  - for Intel(R) 64 architecture"
	@echo "    help         - prints this help"
	@echo
	@echo "    <options>"
	@echo "        interface={lp64|ilp64} (for libintel64 only)"
	@echo "            Specifies programming interface for libintel64."
	@echo "            Default: lp64."
	@echo "        export=<file_name>"
	@echo "            The name of the file that contains the list of entry points"
	@echo "            to be included to the shared object."
	@echo "            Default: user_example_list (no extension)."
	@echo "        name=<so_name>"
	@echo "            The name of the shared object to be created. The extension .so will be added."
	@echo "            Default: mkl_custom."
	@echo "        xerbla=<err_handlr>"
	@echo "            The name of the object file that contains the user's error handler."
	@echo "            By the default native Intel(R) MKL XERBLA is used."
	@echo "        threading={parallel|sequential} (for libia32 and libintel64)"
	@echo "            Specifies whether to use Intel(R) MKL in the threaded or sequential mode."
	@echo "            Default: parallel."
	@echo "        MKLROOT=<MKL_directory>"
	@echo "            Specifies the location of Intel(R) MKL libraries used to build the custom shared object."
	@echo "            Default: the Intel(R) MKL installation directory."
	@echo
	@echo "Usage examples:"
	@echo
	@echo "  make libia32"
	@echo "    Creates mkl_custom.so for IA-32 architecture."
	@echo "    Function list is taken from the predefined file user_example_list."
	@echo "    The static-parallel Intel(R) MKL libraries are used to build mkl_custom.so."
	@echo "    Native Intel(R) MKL error handler is used."
	@echo
	@echo "  make libintel64 export=my_blas_list interface=ilp64 name=my_blas"
	@echo "    Creates my_blas.so for Intel(R) 64 architecture."
	@echo "    Function list is taken from the user file my_blas_list."
	@echo "    The static-parallel-ilp64 Intel(R) MKL libraries are used to build my_blas.so."
	@echo
	@echo "  make sointel64 export=dft_example_list name=libmkldft"
	@echo "    Creates libmkldft.so for Intel(R) 64 architecture."
	@echo "    Function list is taken from the file dft_example_list."
	@echo "    Note: 'interface' and 'threading' options are not applicable to dynamic builder."
	@echo

##------------------------------------------------------------------------------

ifndef MKLROOT
MKLROOT = ../..
endif

mklia32_libpath=$(MKLROOT)/lib/ia32
mklintel64_libpath=$(MKLROOT)/lib/intel64
compileria32_libpath=$(MKLROOT)/../compiler/lib/ia32
compilerintel64_libpath=$(MKLROOT)/../compiler/lib/intel64

#ifndef export
export=user_example_list
#endif

#ifndef name
name=mkl_custom
#endif

ifdef xerbla
XERBLA="$(xerbla)"
else
XERBLA=
endif

ifndef interface
interface=lp64
endif

ifndef threading
threading=parallel
endif

IFACE_COMP_PART=intel
IFACE_THREADING_PART=intel

ifeq ($(interface),ilp64)
IFACE_LIB=libmkl_$(IFACE_COMP_PART)_ilp64.a
else
IFACE_LIB=libmkl_$(IFACE_COMP_PART)_lp64.a
endif

ifeq ($(threading),sequential)
THREADING_LIB=libmkl_sequential.a
OMP_LIB=
else
THREADING_LIB=libmkl_$(IFACE_THREADING_PART)_thread.a
OMP_LIB=-liomp5
endif

CORE_LIB=libmkl_core.a
PURE_IFACE_LIB=-lmkl_rt

LIBM=-lm

libia32 ia32: check_export_file_name
	export LIBRARY_PATH=$(LIBRARY_PATH):$(LD_LIBRARY_PATH); \
	gcc -m32 -shared -Bdynamic \
	$(XERBLA) $(addprefix -u ,$(shell grep -v '^[\#;]' "$(export)")) \
	-Wl,--start-group \
	"$(mklia32_libpath)/libmkl_intel.a" \
	"$(mklia32_libpath)/$(THREADING_LIB)" \
	"$(mklia32_libpath)/$(CORE_LIB)" \
	-Wl,--end-group \
	-L"$(compileria32_libpath)" $(OMP_LIB) $(LIBM) \
	-o "$(name).so"

libintel64 intel64 em64t: check_export_file_name
	export LIBRARY_PATH=$(LIBRARY_PATH):$(LD_LIBRARY_PATH); \
	gcc -shared -Bdynamic \
	$(XERBLA) $(addprefix -u ,$(shell grep -v '^[\#;]' "$(export)")) \
	-Wl,--start-group \
	"$(mklintel64_libpath)/$(IFACE_LIB)" \
	"$(mklintel64_libpath)/$(THREADING_LIB)" \
	"$(mklintel64_libpath)/$(CORE_LIB)" \
	-Wl,--end-group \
	-L"$(compilerintel64_libpath)" $(OMP_LIB) $(LIBM) \
	-o "$(name).so"

soia32: check_export_file_name
	gcc -m32 -shared -Bdynamic \
	$(XERBLA) $(addprefix -u ,$(shell grep -v '^[\#;]' "$(export)")) \
	-L"$(mklia32_libpath)" $(PURE_IFACE_LIB) \
	-L"$(compileria32_libpath)" $(OMP_LIB) $(LIBM) \
	-o "$(name).so"

sointel64: check_export_file_name
	gcc -shared -Bdynamic \
	$(XERBLA) $(addprefix -u ,$(shell grep -v '^[\#;]' "$(export)")) \
	-L"$(mklintel64_libpath)" $(PURE_IFACE_LIB) \
	-L"$(compilerintel64_libpath)" $(OMP_LIB) $(LIBM) \
	-o "$(name).so"

check_export_file_name: $(export)

