%global pkgvers 9
%global scdate0 20200213
%global schash0 9b891540183ddc834a02b2bd81b31afae71b2153
%global branch0 master
%global source0 https://github.com/BVLC/caffe.git

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

%global vcu_maj 12
%global vcu_min 3

Name:           caffe
Version:        1.0
Release:        %{scdate0}.%{pkgvers}.git%{sshort0}.cu%{vcu_maj}_%{vcu_min}%{?dist}
Summary:        Caffe: a Fast framework for deep learning
Epoch:          1
License:        BSD

URL:            http://caffe.berkeleyvision.org

Source1:        http://dl.caffe.berkeleyvision.org/caffe-presentation.pdf
Source2:        http://ucb-icsi-vision-group.github.io/caffe-paper/caffe.pdf

Patch0:         caffe-opencv.patch
Patch1:         caffe-logging.patch
Patch2:         caffe-boost-python.patch
Patch3:         https://github.com/BVLC/caffe/pull/7000.patch
Patch4:         caffe-protobuf.patch
Patch5:         caffe-python.patch

BuildRequires:  cmake gcc-c++ git doxygen glog-devel gflags-devel python3-devel
BuildRequires:  protobuf-compiler protobuf-c protobuf-devel opencv-devel boost-devel
BuildRequires:  openblas-devel lmdb-devel snappy-devel leveldb-devel hdf5-devel
BuildRequires:  python3-numpy boost-python3
%if 0%{?rhel}
BuildRequires:  boost-python3-devel
%endif

%define have_cuda 1
%define have_cuda_gcc 1

%ifarch aarch64
%define gpu_target_arch "5.0;5.2;5.3;6.0;6.1;6.2;7.0;7.5;8.0;8.6;9.0"
%else
%define gpu_target_arch "5.0;5.2;6.0;6.1;7.0;7.5;8.0;8.6;9.0"
%endif

%if %{have_cuda}
%if 0%{?fedora}
%if %{have_cuda_gcc}
BuildRequires:  cuda-gcc-c++
%endif
%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:  cuda-profiler-api-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcublas-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcurand-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libnccl-devel
BuildRequires:  libcudnn8-devel
Requires:  cuda-cudart-%{vcu_maj}-%{vcu_min}
Requires:  cuda-nvrtc-%{vcu_maj}-%{vcu_min}
Requires:  libcublas-%{vcu_maj}-%{vcu_min}
Requires:  libcurand-%{vcu_maj}-%{vcu_min}
%endif

%global __cmake_in_source_build 1

%description
Implementation of some deep learning algorithms.

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

%description    devel
This package contains development files for Caffe.

%package        python3
Summary:        Python bindings for Caffe
Requires:       scikit-image-tools
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description    python3
Python bindings for Caffe


%prep
%setup -T -c -n %{name}
find %{_builddir} -name SPECPARTS -exec rm -rf {} +
git clone --depth 1 -n -b %{branch0} %{source0} .
git fetch --depth 1 origin %{schash0}
git reset --hard %{schash0}
git log --format=fuller
%patch0 -p1 -b .ocv~
%patch1 -p0 -b .log~
%patch2 -p0 -b .boost~
%patch3 -p1 -b .cudnn8~
%patch4 -p0 -b .proto~
%patch5 -p0 -b .py~

%build
# openblaso
sed -i -e 's|NAMES openblas|NAMES openblaso openblas|' cmake/Modules/FindOpenBLAS.cmake
# hdf5
sed -i -e 's|HDF5_LIBRARIES}|HDF5_LIBRARIES} hdf5_hl|g' cmake/Dependencies.cmake
# python3
sed -i -e 's|python_version "2"|python_version "3"|g' CMakeLists.txt
# cuda > 11
sed -i -e 's|<math_functions.h>|<cuda_runtime.h>|' src/caffe/util/math_functions.cu

mkdir build
pushd build
export CXXFLAGS=`echo %{optflags} -std=c++17`
%cmake .. -Wno-dev \
       -DCMAKE_SKIP_RPATH=ON \
       -DCMAKE_BUILD_TYPE=RelWithDebInfo \
       -DCMAKE_VERBOSE_MAKEFILE=OFF \
       -DBUILD_SHARED_LIBS=ON \
       -DBLAS=open \
%if %{have_cuda}
       -DCPU_ONLY=OFF \
       -DUSE_NCCL=ON \
       -DUSE_CUDNN=ON \
       -DCUDA_ARCH_NAME="Manual" \
       -DCUDA_ARCH_BIN=%{gpu_target_arch} \
       -DCUDA_PROPAGATE_HOST_FLAGS=OFF \
       -DCUDA_NVCC_FLAGS="-Wno-deprecated-gpu-targets -allow-unsupported-compiler" \
%if 0%{?fedora}
%if %{have_cuda_gcc}
       -DCUDA_HOST_COMPILER="%{_bindir}/cuda-gcc" \
%endif
%endif
%else
       -DCPU_ONLY=ON \
%endif
       -DBUILD_docs=ON \
       -DBUILD_matlab=OFF \
       -DBUILD_python=ON \
       -DBUILD_python_layer=ON
make %{?_smp_mflags}
make docs
popd


%install
rm -rf %{buildroot}
pushd build
make install DESTDIR=%{buildroot}
popd

# fix wrong path
mkdir -p %{buildroot}%{python3_sitearch}
mkdir -p %{buildroot}%{_libdir}/cmake/%{name}
mv -f %{buildroot}/usr/python/*.py %{buildroot}%{_bindir}/
mv -f %{buildroot}/usr/python/* %{buildroot}%{python3_sitearch}/
mv -f %{buildroot}/usr/share/Caffe/* %{buildroot}%{_libdir}/cmake/%{name}/

# fix wrong attr
chmod +x %{buildroot}%{_bindir}/*.py

# add docs
install -p -m 0644 %{SOURCE1} .
install -p -m 0644 %{SOURCE2} .

# remove junk
rm -rf %{buildroot}%{_libdir}/lib*.a
rm -rf %{buildroot}%{_includedir}/%{name}/util/*.hpp.cudnn8~
rm -rf %{buildroot}%{_includedir}/%{name}/layers/*.hpp.cudnn8~
rm -rf %{buildroot}/%{python3_sitearch}/requirements.txt

# shbag
for f in `find %{buildroot} -name '*.py'`;
do
  sed -i -e 's|#!/usr/bin/env python|#!/usr/bin/python3|' $f
done
sed -i -e '/#!\/usr\/bin\/python/d' %{buildroot}/%{python3_sitearch}/%{name}/detector.py
sed -i -e '/#!\/usr\/bin\/python/d' %{buildroot}/%{python3_sitearch}/%{name}/classifier.py


%files
%doc caffe.pdf
%doc caffe-presentation.pdf
%doc examples docs data
%doc CONTRIBUTORS.md README.md
%license LICENSE
%exclude %{_bindir}/*.py
%{_bindir}/*
%{_libdir}/*.so.*

%files devel
%dir %{_includedir}/%{name}
%{_includedir}/%{name}/*
%dir %{_libdir}/cmake/%{name}
%{_libdir}/cmake/%{name}/*
%{_libdir}/*.so

%files python3
%{_bindir}/*.py
%{python3_sitearch}/*


%changelog
* Wed Aug 13 2014 Balint Cristian <cristian.balint@gmail.com>
- github usptream releases