#!/bin/bash
set -ex

# Copy the [de]activate scripts to $PREFIX/etc/conda/[de]activate.d.
# This will allow them to be run on environment activation.
for CHANGE in "activate" "deactivate"
do
    mkdir -p "${PREFIX}/etc/conda/${CHANGE}.d"
    cp "${RECIPE_DIR}/${CHANGE}.sh" "${PREFIX}/etc/conda/${CHANGE}.d/${PKG_NAME}_${CHANGE}.sh"
done

# Intel LLVM must cooperate with compiler and sysroot from conda
echo "--gcc-toolchain=${BUILD_PREFIX} --sysroot=${BUILD_PREFIX}/${HOST}/sysroot -target ${HOST}" > icpx_for_conda.cfg
export ICPXCFG="$(pwd)/icpx_for_conda.cfg"
export ICXCFG="$(pwd)/icpx_for_conda.cfg"

# source ${DPCPPROOT}/env/vars.sh
# echo "DPCPPROOT = ${DPCPPROOT}"
icpx -fsycl --version

export CPATH="${CPATH}:${CONDA_PREFIX}/include"
export XGBOOSTROOT="$(pwd)"

mkdir _build
cd _build
cmake -DPLUGIN_UPDATER_ONEAPI=ON -DUSE_OPENMP=OFF -DINTEL_OMP_PATH="${CONDA_PREFIX}/lib" ..
make -j8
cd plugin
mkdir CMakeFiles/oneapi_plugin.dir
mkdir CMakeFiles/oneapi_plugin.dir/updater_oneapi
icpx -fsycl -DXGBOOST_USE_ONEAPI=1 -I${XGBOOSTROOT}/include -I${XGBOOSTROOT}/dmlc-core/include -I${XGBOOSTROOT}/rabit/include -O3 -DNDEBUG -fPIC -fsycl -fsycl-device-code-split=per_kernel -qopenmp -std=gnu++17 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/regression_obj_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/regression_obj_oneapi.cc
icpx -fsycl -DXGBOOST_USE_ONEAPI=1 -I${XGBOOSTROOT}/include -I${XGBOOSTROOT}/dmlc-core/include -I${XGBOOSTROOT}/rabit/include -O3 -DNDEBUG -fPIC -fsycl -fsycl-device-code-split=per_kernel -qopenmp -std=gnu++17 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/multiclass_obj_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/multiclass_obj_oneapi.cc
icpx -fsycl -DXGBOOST_USE_ONEAPI=1 -I${XGBOOSTROOT}/include -I${XGBOOSTROOT}/dmlc-core/include -I${XGBOOSTROOT}/rabit/include -O3 -DNDEBUG -fPIC -fsycl -fsycl-device-code-split=per_kernel -qopenmp -std=gnu++17 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/predictor_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/predictor_oneapi.cc
icpx -fsycl -DXGBOOST_USE_ONEAPI=1 -I${XGBOOSTROOT}/include -I${XGBOOSTROOT}/dmlc-core/include -I${XGBOOSTROOT}/rabit/include -O3 -DNDEBUG -fPIC -fsycl -fsycl-device-code-split=per_kernel -qopenmp -std=gnu++17 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/hist_util_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/hist_util_oneapi.cc
icpx -fsycl -DXGBOOST_USE_ONEAPI=1 -I${XGBOOSTROOT}/include -I${XGBOOSTROOT}/dmlc-core/include -I${XGBOOSTROOT}/rabit/include -O3 -DNDEBUG -fPIC -fsycl -fsycl-device-code-split=per_kernel -qopenmp -std=gnu++17 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/updater_quantile_hist_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/updater_quantile_hist_oneapi.cc
cd ..
ls -l ${CONDA_PREFIX}/lib/
export LD_LIBRARY_PATH=${CONDA_PREFIX}/lib/:$LD_LIBRARY_PATH
icpx -O3 -DNDEBUG -rpath -L${CONDA_PREFIX}/lib/ CMakeFiles/runxgboost.dir/src/cli_main.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/hist_util_oneapi.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/regression_obj_oneapi.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/multiclass_obj_oneapi.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/updater_quantile_hist_oneapi.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/predictor_oneapi.cc.o src/CMakeFiles/objxgboost.dir/c_api/c_api.cc.o src/CMakeFiles/objxgboost.dir/c_api/c_api_error.cc.o src/CMakeFiles/objxgboost.dir/common/*.cc.o src/CMakeFiles/objxgboost.dir/data/data.cc.o src/CMakeFiles/objxgboost.dir/data/ellpack_page.cc.o src/CMakeFiles/objxgboost.dir/data/gradient_index_format.cc.o src/CMakeFiles/objxgboost.dir/data/gradient_index_page_source.cc.o src/CMakeFiles/objxgboost.dir/data/gradient_index.cc.o src/CMakeFiles/objxgboost.dir/data/iterative_dmatrix.cc.o src/CMakeFiles/objxgboost.dir/data/proxy_dmatrix.cc.o src/CMakeFiles/objxgboost.dir/data/simple_dmatrix.cc.o src/CMakeFiles/objxgboost.dir/data/sparse_page_dmatrix.cc.o src/CMakeFiles/objxgboost.dir/data/sparse_page_raw_format.cc.o src/CMakeFiles/objxgboost.dir/gbm/gblinear.cc.o src/CMakeFiles/objxgboost.dir/gbm/gblinear_model.cc.o src/CMakeFiles/objxgboost.dir/gbm/gbm.cc.o src/CMakeFiles/objxgboost.dir/gbm/gbtree.cc.o src/CMakeFiles/objxgboost.dir/gbm/gbtree_model.cc.o src/CMakeFiles/objxgboost.dir/global_config.cc.o src/CMakeFiles/objxgboost.dir/learner.cc.o src/CMakeFiles/objxgboost.dir/linear/linear_updater.cc.o src/CMakeFiles/objxgboost.dir/linear/updater_coordinate.cc.o src/CMakeFiles/objxgboost.dir/linear/updater_shotgun.cc.o src/CMakeFiles/objxgboost.dir/logging.cc.o src/CMakeFiles/objxgboost.dir/metric/auc.cc.o src/CMakeFiles/objxgboost.dir/metric/elementwise_metric.cc.o src/CMakeFiles/objxgboost.dir/metric/metric.cc.o src/CMakeFiles/objxgboost.dir/metric/multiclass_metric.cc.o src/CMakeFiles/objxgboost.dir/metric/rank_metric.cc.o src/CMakeFiles/objxgboost.dir/metric/survival_metric.cc.o src/CMakeFiles/objxgboost.dir/objective/*.cc.o src/CMakeFiles/objxgboost.dir/predictor/cpu_predictor.cc.o src/CMakeFiles/objxgboost.dir/predictor/predictor.cc.o src/CMakeFiles/objxgboost.dir/tree/constraints.cc.o src/CMakeFiles/objxgboost.dir/tree/param.cc.o src/CMakeFiles/objxgboost.dir/tree/tree_model.cc.o src/CMakeFiles/objxgboost.dir/tree/tree_updater.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_approx.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_colmaker.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_prune.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_quantile_hist.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_refresh.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_sync.cc.o src/CMakeFiles/objxgboost.dir/__/rabit/src/allreduce_base.cc.o src/CMakeFiles/objxgboost.dir/__/rabit/src/rabit_c_api.cc.o src/CMakeFiles/objxgboost.dir/__/rabit/src/engine.cc.o src/CMakeFiles/objxgboost.dir/collective/communicator.cc.o src/CMakeFiles/objxgboost.dir/collective/socket.cc.o -o ../xgboost  -lpthread dmlc-core/libdmlc.a -lrt ${CONDA_PREFIX}/lib/libiomp5.so -fsycl -fsycl-device-code-split=per_kernel
icpx -fPIC -O3 -DNDEBUG -rpath -L${CONDA_PREFIX}/lib/ -shared -Wl,-soname,libxgboost.so -o ../lib/libxgboost.so plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/hist_util_oneapi.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/regression_obj_oneapi.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/multiclass_obj_oneapi.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/updater_quantile_hist_oneapi.cc.o plugin/CMakeFiles/oneapi_plugin.dir/updater_oneapi/predictor_oneapi.cc.o src/CMakeFiles/objxgboost.dir/c_api/c_api.cc.o src/CMakeFiles/objxgboost.dir/c_api/c_api_error.cc.o src/CMakeFiles/objxgboost.dir/common/*.cc.o src/CMakeFiles/objxgboost.dir/data/data.cc.o src/CMakeFiles/objxgboost.dir/data/ellpack_page.cc.o  src/CMakeFiles/objxgboost.dir/data/gradient_index_format.cc.o src/CMakeFiles/objxgboost.dir/data/gradient_index_page_source.cc.o src/CMakeFiles/objxgboost.dir/data/gradient_index.cc.o src/CMakeFiles/objxgboost.dir/data/iterative_dmatrix.cc.o src/CMakeFiles/objxgboost.dir/data/proxy_dmatrix.cc.o src/CMakeFiles/objxgboost.dir/data/simple_dmatrix.cc.o src/CMakeFiles/objxgboost.dir/data/sparse_page_dmatrix.cc.o src/CMakeFiles/objxgboost.dir/data/sparse_page_raw_format.cc.o src/CMakeFiles/objxgboost.dir/gbm/gblinear.cc.o src/CMakeFiles/objxgboost.dir/gbm/gblinear_model.cc.o src/CMakeFiles/objxgboost.dir/gbm/gbm.cc.o src/CMakeFiles/objxgboost.dir/gbm/gbtree.cc.o src/CMakeFiles/objxgboost.dir/gbm/gbtree_model.cc.o src/CMakeFiles/objxgboost.dir/global_config.cc.o src/CMakeFiles/objxgboost.dir/learner.cc.o src/CMakeFiles/objxgboost.dir/linear/linear_updater.cc.o src/CMakeFiles/objxgboost.dir/linear/updater_coordinate.cc.o src/CMakeFiles/objxgboost.dir/linear/updater_shotgun.cc.o src/CMakeFiles/objxgboost.dir/logging.cc.o src/CMakeFiles/objxgboost.dir/metric/auc.cc.o src/CMakeFiles/objxgboost.dir/metric/elementwise_metric.cc.o src/CMakeFiles/objxgboost.dir/metric/metric.cc.o src/CMakeFiles/objxgboost.dir/metric/multiclass_metric.cc.o src/CMakeFiles/objxgboost.dir/metric/rank_metric.cc.o src/CMakeFiles/objxgboost.dir/metric/survival_metric.cc.o src/CMakeFiles/objxgboost.dir/objective/*.cc.o src/CMakeFiles/objxgboost.dir/predictor/cpu_predictor.cc.o src/CMakeFiles/objxgboost.dir/predictor/predictor.cc.o src/CMakeFiles/objxgboost.dir/tree/constraints.cc.o src/CMakeFiles/objxgboost.dir/tree/param.cc.o src/CMakeFiles/objxgboost.dir/tree/tree_model.cc.o src/CMakeFiles/objxgboost.dir/tree/tree_updater.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_approx.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_colmaker.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_prune.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_quantile_hist.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_refresh.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_sync.cc.o src/CMakeFiles/objxgboost.dir/__/rabit/src/allreduce_base.cc.o src/CMakeFiles/objxgboost.dir/__/rabit/src/rabit_c_api.cc.o src/CMakeFiles/objxgboost.dir/__/rabit/src/engine.cc.o src/CMakeFiles/objxgboost.dir/collective/communicator.cc.o src/CMakeFiles/objxgboost.dir/collective/socket.cc.o -lpthread dmlc-core/libdmlc.a -lrt ${CONDA_PREFIX}/lib/libiomp5.so -fsycl -fsycl-device-code-split=per_kernel
cd ..

cd python-package
$PYTHON setup.py install --single-version-externally-managed --record record.txt
