#===============================================================================
# Copyright 2020 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 examples build and run
##******************************************************************************

help:
	@echo "Usage: make {lib|so|help}"
	@echo "[example=name] [compiler=compiler_name] [mode=mode_name]"
	@echo
	@echo "name              - example name. Please see onedal.lst file"
	@echo
	@echo "compiler_name     - currently can be dpcpp only that stands
	@echo "                    for Intel(R) oneAPI DPC++ Compiler."
	@echo
	@echo "mode_name         - can be build or run. Default is run"

##------------------------------------------------------------------------------
## examples of using:
##
## make lib example=pca_cor_dense_batch   - build by Intel(R) oneAPI DPC++ Compiler (as default)
##                                          and run pca_cor_dense_batch example, static linking
##
## make so                                - build by Intel(R) oneAPI DPC++ Compiler (as default)
##                                          and run all examples, dynamic linking
##
## make so mode=build                     - build only (not run) by Intel(R) oneAPI DPC++ Compiler
##                                          (as default) all examples, dynamic linking
##
## make help                              - show help
##
##------------------------------------------------------------------------------

include onedal.lst

ifndef example
    example = $(ONEDAL)
endif

ifneq ($(compiler),dpcpp)
    override compiler = dpcpp
endif

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

ifndef DAALROOT
    DAALROOT = ./../../..
endif
DAAL_PATH = $(DAALROOT)/lib/intel64

ifndef TBBROOT
    TBBROOT = ./../../../../../tbb/latest
endif
TBB_PATH = $(TBBROOT)/lib/intel64/gcc4.8

ifeq ($(threading),sequential)
    $(error Sequential version is not supported. To run sequential version please refer to 'examples/oneapi/cpp')
else
    override threading = parallel
    THREADING_LIB := onedal_thread
endif

ifeq ($(RES_EXT),so)
    ONEDAL_LIBS := -lonedal_dpc \
                   -lonedal_core \
                   -l$(THREADING_LIB) \
                   "$(DAAL_PATH)"/libonedal_sycl.a
else
    ONEDAL_LIBS := "$(DAAL_PATH)"/libonedal_dpc.a \
                   "$(DAAL_PATH)"/libonedal_core.a \
                   "$(DAAL_PATH)"/lib$(THREADING_LIB).a \
                   "$(DAAL_PATH)"/libonedal_sycl.a
endif

COPTS := -std=c++17 \
         -pedantic \
         -Wall \
         -Wextra \
         -Werror \
         -Wno-unused-parameter \
         -fsycl-device-code-split=per_kernel \
         -I./source \
         -I"$(DAALROOT)/include"

LIBS := $(ONEDAL_LIBS) \
        -ltbb \
        -ltbbmalloc \
        -lpthread \
        -lOpenCL \
        -ldl

LOPTS := -L"$(DAAL_PATH)" \
         -L"$(TBB_PATH)" \
         $(LIBS)

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

ifeq ($(compiler),dpcpp)
    gcc_toolchain := $(realpath $(dir $(shell which gcc))/..)
    CC = dpcpp --gcc-toolchain=$(gcc_toolchain)
endif

CC := $(if $(COVFILE), cov01 -1; covc -i  $(CC),$(CC))

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

_make_ex: $(RES)

vpath
vpath %.cpp $(addprefix ./source/,decision_forest dbscan kmeans kmeans_init knn linear_kernel linear_regression pca rbf_kernel svm table covariance basic_statistics)

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

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

%/.:; mkdir -p $*
