#===============================================================================
# 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:
##     oneAPI Data Analytics Library samples creation and run
##******************************************************************************

help:
	@echo "Usage: make {libintel64|sointel64|help}"
	@echo "[sample=name] [compiler=compiler_name] [mode=mode_name]"
	@echo
	@echo "name                - sample name. Please see daal.lst file"
	@echo
	@echo "compiler_name       - can be intel, icx or gnu. Default value is intel."
	@echo
	@echo "mode_name           - can be build or run. Default is run"

##------------------------------------------------------------------------------
## 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
##                                 all samples for Intel(R)64
##                                 processor family  applications, dynamic linking
##
## make help                     - show help
##
##------------------------------------------------------------------------------

include daal.lst

ifneq ($(compiler),gnu)
	ifneq ($(compiler),icx)
		override compiler = intel
	endif
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

DAAL_PATH = "$(DALROOT)/lib/intel64"
DAAL_LIB := $(DAAL_PATH)/libonedal_core.$(RES_EXT) $(DAAL_PATH)/libonedal_thread.$(RES_EXT)
COPTS := -Wall -w
RES_DIR=_results/$(compiler)_intel64_$(RES_EXT)

ifeq ($(compiler),intel)
    override COPTS += -diag-disable=10441
endif

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

ifndef sample
    sample = $(MPI)
endif

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

override proc = 4

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


ifeq ($(compiler),intel)
	override CC = mpiicc
endif

ifeq ($(compiler),icx)
	override CC = mpiicpx
endif

ifeq ($(compiler),gnu)
	override CC = mpigxx
endif

CRUN = mpirun
RUNOPTS = -n $(proc) -ppn 1


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

$(RES_DIR)/%.res: $(RES_DIR)/%.exe
	$(CRUN) $(RUNOPTS) $< > $@

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

_make_ex: $(RES)

%/.:; mkdir -p $*

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