#!/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

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

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
dpcpp -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 -fopenmp -std=gnu++14 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/regression_obj_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/regression_obj_oneapi.cc
dpcpp -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 -fopenmp -std=gnu++14 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/multiclass_obj_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/multiclass_obj_oneapi.cc
dpcpp -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 -fopenmp -std=gnu++14 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/predictor_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/predictor_oneapi.cc
dpcpp -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 -fopenmp -std=gnu++14 -o CMakeFiles/oneapi_plugin.dir/updater_oneapi/hist_util_oneapi.cc.o -c ${XGBOOSTROOT}/plugin/updater_oneapi/hist_util_oneapi.cc
dpcpp -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 -fopenmp -std=gnu++14 -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/
dpcpp -v -O3 -DNDEBUG -rpath ${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/charconv.cc.o src/CMakeFiles/objxgboost.dir/common/common.cc.o src/CMakeFiles/objxgboost.dir/common/hist_util.cc.o src/CMakeFiles/objxgboost.dir/common/host_device_vector.cc.o src/CMakeFiles/objxgboost.dir/common/io.cc.o src/CMakeFiles/objxgboost.dir/common/json.cc.o src/CMakeFiles/objxgboost.dir/common/quantile.cc.o src/CMakeFiles/objxgboost.dir/common/random.cc.o src/CMakeFiles/objxgboost.dir/common/survival_util.cc.o src/CMakeFiles/objxgboost.dir/common/timer.cc.o src/CMakeFiles/objxgboost.dir/common/version.cc.o src/CMakeFiles/objxgboost.dir/data/data.cc.o src/CMakeFiles/objxgboost.dir/data/ellpack_page.cc.o src/CMakeFiles/objxgboost.dir/data/ellpack_page_source.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/data/sparse_page_source.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/aft_obj.cc.o src/CMakeFiles/objxgboost.dir/objective/hinge.cc.o src/CMakeFiles/objxgboost.dir/objective/multiclass_obj.cc.o src/CMakeFiles/objxgboost.dir/objective/objective.cc.o src/CMakeFiles/objxgboost.dir/objective/rank_obj.cc.o src/CMakeFiles/objxgboost.dir/objective/regression_obj.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_colmaker.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_histmaker.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/c_api.cc.o src/CMakeFiles/objxgboost.dir/__/rabit/src/engine.cc.o -o ../xgboost  -lpthread dmlc-core/libdmlc.a -lrt ${CONDA_PREFIX}/lib/libiomp5.so -fsycl -fsycl-device-code-split=per_kernel
dpcpp -v -fPIC -O3 -DNDEBUG -rpath ${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/charconv.cc.o src/CMakeFiles/objxgboost.dir/common/common.cc.o src/CMakeFiles/objxgboost.dir/common/hist_util.cc.o src/CMakeFiles/objxgboost.dir/common/host_device_vector.cc.o src/CMakeFiles/objxgboost.dir/common/io.cc.o src/CMakeFiles/objxgboost.dir/common/json.cc.o src/CMakeFiles/objxgboost.dir/common/quantile.cc.o src/CMakeFiles/objxgboost.dir/common/random.cc.o src/CMakeFiles/objxgboost.dir/common/survival_util.cc.o src/CMakeFiles/objxgboost.dir/common/timer.cc.o src/CMakeFiles/objxgboost.dir/common/version.cc.o src/CMakeFiles/objxgboost.dir/data/data.cc.o src/CMakeFiles/objxgboost.dir/data/ellpack_page.cc.o src/CMakeFiles/objxgboost.dir/data/ellpack_page_source.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/data/sparse_page_source.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/aft_obj.cc.o src/CMakeFiles/objxgboost.dir/objective/hinge.cc.o src/CMakeFiles/objxgboost.dir/objective/multiclass_obj.cc.o src/CMakeFiles/objxgboost.dir/objective/objective.cc.o src/CMakeFiles/objxgboost.dir/objective/rank_obj.cc.o src/CMakeFiles/objxgboost.dir/objective/regression_obj.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_colmaker.cc.o src/CMakeFiles/objxgboost.dir/tree/updater_histmaker.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/c_api.cc.o src/CMakeFiles/objxgboost.dir/__/rabit/src/engine.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
