#===============================================================================
# Copyright 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================

##  Content:
##     Intel(R) oneAPI Data Analytics Library samples creation and run
##******************************************************************************

help:
	@echo "Usage: make {lib|dylib|libintel64|dylibintel64|help}"
	@echo "[compiler=compiler_name] [mode=mode_name] [sample=sample_name]"
	@echo
	@echo "compiler_name       - intel, gnu or clang"
	@echo "                      Intel(R) C++ Compiler as default"
	@echo
	@echo "mode_name           - can be build or run. Default is run"
	@echo "sample_name         - sample name. Please see daal.lst file"

##------------------------------------------------------------------------------
## samples of using:
##
## make dylibintel64             - build by Intel(R) C++ Compiler
##                                 and run all samples for Intel(R)64 processor
##                                 family  applications, dynamic linking
##
## make dylibintel64 mode=build  - build only (not run) by Intel(R) C++ Compiler
##                                 (as default) all samples for Intel(R)64
##                                 processor family  applications, dynamic linking
##
## make help                     - show help
##
##------------------------------------------------------------------------------

include daal.lst

ifeq (,$(filter gnu clang,$(compiler)))
    override compiler = intel
endif

ifndef DALROOT
    DALROOT = ../../../../..
endif

ifndef sample
    sample = $(MYSQL)
endif

ODBC_PATH = /opt/local

DAAL_PATH = "$(DALROOT)/lib"
TBB_PATH = "$(TBBROOT)/lib"
DAAL_LIB := $(DAAL_PATH)/libonedal_core.$(RES_EXT) $(DAAL_PATH)/libonedal_thread.$(RES_EXT)

ifneq ($(mode),build)
    override mode = run
endif

EXT_LIB := -ltbb -ltbbmalloc -L$(ODBC_PATH)/lib -lodbc -ldl

COPTS := -Wall -w -stdlib=libc++ -I$(ODBC_PATH)/include
LOPTS := $(DAAL_LIB) $(EXT_LIB)

RES_DIR=_results/$(compiler)_intel64_$(RES_EXT)
RES = $(addprefix $(RES_DIR)/, $(if $(filter run, $(mode)), $(addsuffix .res ,$(sample)), $(addsuffix .exe,$(sample))))


ifeq ($(compiler),intel)
    CC = icc
endif

ifeq ($(compiler),gnu)
    CC = g++
    COPTS += -m64
endif

ifeq ($(compiler),clang)
    CC = clang++
    COPTS += -m64
endif


.SECONDARY:
$(RES_DIR)/%.exe: ./sources/%.cpp | $(RES_DIR)/.
	$(CC) $(COPTS) $< -o $@ $(LOPTS)

$(RES_DIR)/%.res: $(RES_DIR)/%.exe
	export DYLD_LIBRARY_PATH=$(DAAL_PATH):$(TBB_PATH):$(DYLD_LIBRARY_PATH); $< "$(DAAL_MYSQL_SAMPLE_CONNECTION_STRING)" > $@

_make_ex: $(RES)

%/.:; mkdir -p $*

lib libintel64:
	$(MAKE) _make_ex RES_EXT=a
dylib dylibintel64:
	$(MAKE) _make_ex RES_EXT=dylib
