#===============================================================================
# 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|so|libintel64|sointel64|help}"
	@echo "lib is alias for libintel64, so is alias for sointel64"
	@echo "[example=name] [compiler=compiler_name] [mode=mode_name]"
	@echo
	@echo "compiler_name       - intel or gnu"
	@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 sointel64                - build by Intel(R) C++ Compiler
##                                 and run all samples for Intel(R)64 processor
##                                 family  applications, dynamic linking
##
## make sointel64 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

ifndef compiler
    compiler = intel
endif

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

ifndef TBBROOT
    TBBROOT = ./../../../../../../tbb/latest
endif

OLD_TBB_LAYOUT := $(if $(wildcard $(TBBROOT)/lib/intel64/),yes,no)

ifeq ($(OLD_TBB_LAYOUT),no)
	TBB_PATH = "$(TBBROOT)/lib"
else
	TBB_PATH = "$(TBBROOT)/lib/intel64/gcc4.8"
endif

ifndef sample
    sample = $(MYSQL)
endif

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

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

EXT_LIB := $(addprefix -L,$(TBB_PATH)) -ltbb -ltbbmalloc -lodbc -lpthread -ldl

COPTS := -Wall -w
LOPTS := -Wl,--start-group $(DAAL_LIB) $(EXT_LIB) -Wl,--end-group

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



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

$(RES_DIR)/%.res: $(RES_DIR)/%.exe
	$< "$(DAAL_MYSQL_SAMPLE_CONNECTION_STRING)" > $@

_make_ex: $(RES)

%/.:; mkdir -p $*

lib libintel64:
	$(MAKE) _make_ex _IA=intel64 RES_EXT=a
so sointel64:
	$(MAKE) _make_ex _IA=intel64 RES_EXT=so
