%global pkgvers 0
%global scdate0 20250317
%global schash0 257b87ca949b3d622886e5cdbf23c66eaa9784d4
%global branch0 master
%global source0 https://github.com/dmlc/xgboost.git

%global sshort0 %{expand:%%{lua:print(('%{schash0}'):sub(1,8))}}

%global vcu_maj 12
%global vcu_min 8

%define ext_dmlc 1

Name:           xgboost
Version:        %(curl -s https://raw.githubusercontent.com/dmlc/xgboost/%{schash0}/CMakeLists.txt | grep "project.*xgboost.*VERSION" | awk '{print $6}' | sed 's|[^[:digit:].]||g')
Release:        %{scdate0}.%{pkgvers}.git%{sshort0}.cu%{vcu_maj}_%{vcu_min}%{?dist}
Summary:        Scalable, Portable and Distributed Gradient Boosting
License:        Apache

URL:            https://github.com/dmlc/xgboost

Patch0:          https://patch-diff.githubusercontent.com/raw/dmlc/xgboost/pull/10797.patch

BuildRequires:  doxygen cmake gcc-c++ git python3-setuptools
BuildRequires:  python3-devel gtest-devel
%if %{ext_dmlc}
BuildRequires:  dmlc-core-devel
%endif

%define have_cuda 1
%define have_cuda_gcc 1
# last is always +PTX
%define gpu_target_arch "61;75;86;89;90;50"

%bcond_without cuda
%if %{without cuda}
%define have_cuda 0
%endif

%if %{have_cuda}
%if %{have_cuda_gcc}
BuildRequires:  cuda-gcc-c++
%endif
BuildRequires:  cuda-nvcc-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvtx-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-cudart-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvml-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvrtc-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-driver-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libnccl-devel libnccl
Requires:       cuda-cudart-%{vcu_maj}-%{vcu_min}
Requires:       cuda-nvrtc-%{vcu_maj}-%{vcu_min}
%endif

%undefine _hardened_build
%undefine _annotated_build
%undefine _strict_symbol_defs_build
%global _lto_cflags %{nil}
%global __cmake_in_source_build 1

%description
Scalable, Portable and Distributed Gradient Boosting

%package        devel
Summary:        Development files for %{name}
Requires:       %{name} = %{version}-%{release}

%description    devel
This package contains development files for %{name}.

%package        python3
Summary:        Python files for %{name}
Requires:       python3-cffi python3-psutil
Requires:       %{name} = %{version}-%{release}

%description    python3
This package contains python files for %{name}.


%prep
%setup -T -c -n %{name}
git clone --depth 1 -n -b %{branch0} %{source0} .
git fetch --depth 1 origin %{schash0}
git reset --hard %{schash0}
%if ! %{ext_dmlc}
git submodule update --init --depth 1 dmlc-core
%endif
git submodule update --init --depth 1 gputreeshap
git log --format=fuller
%patch -P 0 -p 1 -b .cu126~
# force PTX last arch
sed -i -e 's|VERSION_GREATER_EQUAL "3.18")|VERSION_LESS_EQUAL "3.18")|' cmake/Utils.cmake
%if %{ext_dmlc}
# external dmlc-core
sed -i '/add_subdirectory.*dmlc-core/d'  CMakeLists.txt
sed -i 's|target xgboost objxgboost dmlc|target xgboost objxgboost|' CMakeLists.txt
%else
sed -i 's|add_library(dmlc |add_library(dmlc STATIC |g' dmlc-core/CMakeLists.txt
%endif
# build fixes
%if 0%{?rhel} == 8
sed -i -e 's|runxgboost PRIVATE|runxgboost PRIVATE %{_libdir}/libstdc++.so.6 -lstdc++fs|g' CMakeLists.txt
sed -i -e 's|xgboost PRIVATE objxgboost|xgboost PRIVATE objxgboost %{_libdir}/libstdc++.so.6 -lstdc++fs|g' CMakeLists.txt
%else
sed -i -e 's|runxgboost PRIVATE|runxgboost PRIVATE %{_libdir}/libstdc++.so.6|g' CMakeLists.txt
sed -i -e 's|xgboost PRIVATE objxgboost|xgboost PRIVATE objxgboost %{_libdir}/libstdc++.so.6|g' CMakeLists.txt
%endif
sed -i -e 's|__CUDACC_VER_MINOR__ == 1|__CUDACC_VER_MINOR__ == 0|g' src/common/device_helpers.cuh
sed -i -e 's|set(CMAKE_CUDA_HOST_COMPILER \${CMAKE_CXX_COMPILER})|set(CMAKE_CUDA_HOST_COMPILER ${CUDA_HOST_COMPILER})|' CMakeLists.txt
# gcc13
sed -i -e '1i #include <cstdint>' include/xgboost/base.h


%build
rm -rf cub
%if %{ext_dmlc}
rm -rf dmlc-core
%endif
mkdir build
pushd build
%cmake .. -Wno-dev \
       -DCMAKE_SKIP_RPATH=ON \
       -DCMAKE_VERBOSE_MAKEFILE=OFF \
       -DCMAKE_BUILD_TYPE=RelWithDebInfo \
       -DCMAKE_CXX_STANDARD=17 \
       -DDMLC_FORCE_SHARED_CRT=OFF \
%if %{have_cuda}
       -DUSE_CUDA=ON \
       -DUSE_NCCL=ON \
       -DBUILD_WITH_SHARED_NCCL=ON \
%if %{have_cuda_gcc}
%if 0%{?fedora} || (0%{?rhel} > 8)
       -DCUDA_HOST_COMPILER="%{_bindir}/cuda-g++" \
       -DCMAKE_CUDA_HOST_COMPILER="%{_bindir}/cuda-g++" \
%endif
%endif
       -DGPU_COMPUTE_VER=%{gpu_target_arch} \
       -DCMAKE_CUDA_COMPILER="/usr/local/cuda-%{vcu_maj}.%{vcu_min}/bin/nvcc" \
%ifarch aarch64
       -DCUDA_NVCC_FLAGS="--compiler-options;-fPIC;-Wno-deprecated-gpu-targets;-allow-unsupported-compiler;--fatbin-options;-compress-all;-Xcompiler;-march=armv8.1-a" \
       -DCMAKE_CUDA_FLAGS="--compiler-options -fPIC -Wno-deprecated-gpu-targets -allow-unsupported-compiler --fatbin-options -compress-all -Xcompiler -march=armv8.1-a" \
%else
       -DCMAKE_CUDA_FLAGS="--compiler-options -fPIC -Wno-deprecated-gpu-targets -allow-unsupported-compiler" \
%endif
%else
       -DUSE_CUDA=OFF \
%endif
       -DGOOGLE_TEST=OFF

make %{?_smp_mflags}
popd

pushd python-package
echo "import setuptools; setuptools.setup(packages=['xgboost'])" > setup.py
%{__python3} setup.py build
popd


%install
pushd build
make install DESTDIR=%{buildroot}
popd

pushd python-package
%{__python3} setup.py install --root %{buildroot} --install-lib=%{python3_sitearch}
popd

# clean spurious files
rm -rf %{buildroot}/usr/doc
rm -rf %{buildroot}/usr/xgboost
rm -rf %{buildroot}/%{python3_sitearch}/xgboost/rabit/test
rm -rf %{buildroot}/%{python3_sitearch}/xgboost/rabit/guide
rm -rf %{buildroot}/%{python3_sitearch}/xgboost/dmlc-core/scripts
find %{buildroot}/%{python3_sitearch}/xgboost/ \
    -type f \
    -not -name '*.py' \
    -not -name '*.pyc' \
    -not -name '*.so' \
    -not -name 'VERSION' \
    -exec rm -rf {} \;
# erase empty folders
for d in `find %{buildroot}/%{python3_sitearch}/xgboost/ -type d -empty`
do
  rm -rf $d | true
done

# python3
for f in `find %{buildroot} -name '*.py'`;
do
  sed -i -e '/env python/d' $f
done
# versioning
echo %{version} > %{buildroot}%{python3_sitearch}/%{name}/VERSION
# de-duplicate libraries
mkdir -p %{buildroot}%{python3_sitearch}/%{name}/lib
rm -rf %{buildroot}%{python3_sitearch}/%{name}/lib/*.so
ln -sf %{_libdir}/libxgboost.so %{buildroot}%{python3_sitearch}/%{name}/lib/libxgboost.so
sed -i '/nvidia-nccl-cu12/d' %{buildroot}/%{python3_sitearch}/*egg-info/PKG-INFO || true
sed -i '/nvidia-nccl-cu12/d' %{buildroot}/%{python3_sitearch}/*egg-info/requires.txt || true


%files
%license LICENSE
%doc README.md
%doc CONTRIBUTORS.md
%{_libdir}/lib*.so*

%files devel
%{_includedir}/*
%{_datadir}/*
%{_libdir}/cmake/*
%{_libdir}/pkgconfig/*

%files python3
%{python3_sitearch}/*


%changelog
* Wed Mar 27 2019 Balint Cristian <cristian.balint@gmail.com>
- github update releases