%bcond_with debug
%bcond_with pthread

%global __cmake_in_source_build 1
%define _legacy_common_support 1
%define _lto_cflags %{nil}

%global blaslib openblas
%global blasvar o

%global with_hypre 1

%global with_petsc 0

%global with_superlumt 0

%global with_superludist 0

%global with_sercheck 1

%if %{?__isa_bits:%{__isa_bits}}%{!?__isa_bits:32} == 64
%global with_fortran 1
%else
%global with_fortran 0
%endif

Summary:    Suite of nonlinear solvers
Name:       sundials
Version:    5.3.0
Release:    1
License:    BSD
URL:        https://github.com/LLNL/%{name} 
Source0:    https://github.com/LLNL/%{name}/archive/v%{version}/%{name}-%{version}.tar.gz

Patch0:     %{name}-3.1.1-set_superlumt_name.patch

Patch1:     %{name}-3.1.1-set_superlumt64_name.patch

Patch2:     %{name}-change_petsc_variable.patch

BuildRequires: gcc-gfortran
BuildRequires: python%{python3_pkgversion}-devel
BuildRequires: %{?dts}gcc, %{?dts}gcc-c++
BuildRequires: cmake3 >= 3.10
BuildRequires: %{blaslib}-devel
%if 0%{?with_superlumt}
%ifarch s390x x86_64 %{power64} aarch64
BuildRequires: SuperLUMT64-devel
%endif
%ifarch %{arm} %{ix86}
BuildRequires: SuperLUMT-devel
%endif
%endif
BuildRequires: suitesparse-devel

Requires: gcc-gfortran%{?_isa}

%description
SUNDIALS is a SUite of Non-linear DIfferential/ALgebraic equation Solvers
for use in writing mathematical software.

SUNDIALS was implemented with the goal of providing robust time integrators
and nonlinear solvers that can easily be incorporated into existing simulation
codes. The primary design goals were to require minimal information from the
user, allow users to easily supply their own data structures underneath the
solvers, and allow for easy incorporation of user-supplied linear solvers and
preconditioners. 

%package devel
Summary:    Suite of nonlinear solvers (developer files)
Requires:   %{name}%{?_isa} = %{version}-%{release}
%description devel
SUNDIALS is a SUite of Non-linear DIfferential/ALgebraic equation Solvers
for use in writing mathematical software.
This package contains the developer files (.so file, header files).

%package doc
Summary:    Suite of nonlinear solvers (documentation)
BuildArch: noarch
%description doc
SUNDIALS is a SUite of Non-linear DIfferential/ALgebraic equation Solvers
for use in writing mathematical software.
This package contains the documentation files.

%prep
%setup -qc

pushd %{name}-%{version}

%ifarch s390x x86_64 %{power64} aarch64
%patch1 -p0 -b .set_superlumt64_name
%endif
%ifarch %{arm} %{ix86}
%patch0 -p0 -b .set_superlumt_name
%endif

##Set serial library's paths
sed -i 's|DESTINATION include/nvector|DESTINATION %{_includedir}/nvector|g' src/nvector/serial/CMakeLists.txt
sed -i 's|DESTINATION include/nvector|DESTINATION %{_includedir}/nvector|g' src/nvector/openmp/CMakeLists.txt
sed -i 's|DESTINATION include/nvector|DESTINATION %{_includedir}/nvector|g' src/nvector/pthreads/CMakeLists.txt

mv src/arkode/README.md src/README-arkode.md
mv src/cvode/README.md src/README-cvode.md
mv src/cvodes/README.md src/README-cvodes.md
mv src/ida/README.md src/README-ida.md
mv src/idas/README.md src/README.idas.md
mv src/kinsol/README.md src/README-kinsol.md
popd

%build
pushd sundials-%{version}

mkdir -p build && cd build

export LIBBLASLINK=-l%{blaslib}%{blasvar}
export INCBLAS=%{_includedir}/%{blaslib}

%if 0%{?with_superlumt}
%ifarch s390x x86_64 %{power64} aarch64
export LIBSUPERLUMTLINK=-lsuperlumt64_d
%endif
%ifarch %{arm} %{ix86}
export LIBSUPERLUMTLINK=-lsuperlumt_d
%endif
%endif

%if %{with debug}
%undefine _hardened_build
export CFLAGS=" "
export FFLAGS=" "
%global _cmake cmake3
%_cmake \
 -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
 -DCMAKE_BUILD_TYPE:STRING=Debug \
 -DCMAKE_C_FLAGS_DEBUG:STRING="-O0 -g %{__global_ldflags} -I$INCBLAS" \
 -DCMAKE_Fortran_FLAGS_DEBUG:STRING="-O0 -g %{__global_ldflags} -I$INCBLAS" \
 -DCMAKE_SHARED_LINKER_FLAGS_DEBUG:STRING="%{__global_ldflags} -lklu $LIBBLASLINK $LIBSUPERLUMTLINK" \
%else
export CFLAGS="%{build_cflags}"
export CFLAGS="%{build_fflags}"
%cmake3 \
%endif
%if %{?__isa_bits:%{__isa_bits}}%{!?__isa_bits:32} == 64
 -DSUNDIALS_INDEX_SIZE:STRING=64 \
 -DKLU_ENABLE=ON -DKLU_LIBRARY_DIR:PATH=%{_libdir} -DKLU_LIBRARY=%{_libdir}/libklu64.so \
 -DAMD_LIBRARY=%{_libdir}/libamd64.so -DAMD_LIBRARY_DIR:PATH=%{_libdir} \
 -DBTF_LIBRARY=%{_libdir}/libbtf64.so -DBTF_LIBRARY_DIR:PATH=%{_libdir} \
 -DCOLAMD_LIBRARY=%{_libdir}/libcolamd64.so -DCOLAMD_LIBRARY_DIR:PATH=%{_libdir} \
 -DKLU_INCLUDE_DIR:PATH=%{_includedir}/suitesparse \
%else
 -DSUNDIALS_INDEX_SIZE:STRING=32 \
 -DKLU_ENABLE=ON -DKLU_LIBRARY_DIR:PATH=%{_libdir} -DKLU_LIBRARY=%{_libdir}/libklu.so \
 -DAMD_LIBRARY=%{_libdir}/libamd.so -DAMD_LIBRARY_DIR:PATH=%{_libdir} \
 -DBTF_LIBRARY=%{_libdir}/libbtf.so -DBTF_LIBRARY_DIR:PATH=%{_libdir} \
 -DCOLAMD_LIBRARY=%{_libdir}/libcolamd.so -DCOLAMD_LIBRARY_DIR:PATH=%{_libdir} \
 -DKLU_INCLUDE_DIR:PATH=%{_includedir}/suitesparse \
%endif
 -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
 -DCMAKE_BUILD_TYPE:STRING=Release \
 -DCMAKE_C_FLAGS_RELEASE:STRING="%{optflags} -I$INCBLAS" \
 -DCMAKE_Fortran_FLAGS_RELEASE:STRING="%{optflags} -I$INCBLAS" \
 -DCMAKE_SHARED_LINKER_FLAGS_RELEASE:STRING="%{__global_ldflags} -lklu $LIBBLASLINK $LIBSUPERLUMTLINK" \
 -DLAPACK_ENABLE:BOOL=OFF \
 -DCMAKE_MODULE_LINKER_FLAGS:STRING="%{__global_ldflags}" \
 -DCMAKE_INSTALL_PREFIX:PATH=%{_prefix} -DCMAKE_INSTALL_LIBDIR:PATH=%{_lib} \
 -DPYTHON_EXECUTABLE:FILEPATH=%{__python3} \
 -DEXAMPLES_ENABLE_CXX:BOOL=ON -DEXAMPLES_ENABLE_C:BOOL=ON \
 -DCMAKE_SKIP_RPATH:BOOL=YES -DCMAKE_SKIP_INSTALL_RPATH:BOOL=YES \
 -DBUILD_SHARED_LIBS:BOOL=ON -DBUILD_STATIC_LIBS:BOOL=OFF \
 -DMPI_ENABLE:BOOL=OFF \
%if 0%{?with_fortran}
 -DF77_INTERFACE_ENABLE:BOOL=ON \
 -DEXAMPLES_ENABLE_F77:BOOL=ON \
 -DF2003_INTERFACE_ENABLE:BOOL=ON \
 -DEXAMPLES_ENABLE_F90:BOOL=ON \
 -DFortran_INSTALL_MODDIR:PATH=%{_fmoddir}/%{name} \
%endif
 -DUSE_GENERIC_MATH:BOOL=ON \
 -DOPENMP_ENABLE:BOOL=ON \
%if %{with pthread}
 -DPTHREAD_ENABLE:BOOL=ON \
%endif
 -DSUNDIALS_PRECISION:STRING=double \
%if 0%{?with_superlumt}
 -DSUPERLUMT_ENABLE:BOOL=ON \
 -DSUPERLUMT_INCLUDE_DIR:PATH=%{_includedir}/SuperLUMT \
 -DSUPERLUMT_LIBRARY_DIR:PATH=%{_libdir} \
 -DSUPERLUMT_THREAD_TYPE:STRING=OpenMP \
%endif
 -DSUPERLUDIST_ENABLE:BOOL=OFF \
 -DHYPRE_ENABLE:BOOL=OFF \
 -DEXAMPLES_INSTALL:BOOL=OFF \
 -DSUNDIALS_BUILD_WITH_MONITORING:BOOL=ON -Wno-dev ..

%make_build V=1
cd ..
popd

%install
%make_install -C sundials-%{version}/build

# Remove files in bad position
rm -f %{buildroot}%{_prefix}/LICENSE
rm -f %{buildroot}%{_includedir}/sundials/LICENSE
rm -f %{buildroot}%{_includedir}/sundials/NOTICE

# Remove static files
find %{buildroot} -name '*.a' -exec rm -f {} ';'

%ldconfig_scriptlets

%check
%if 0%{?with_sercheck}
pushd sundials-%{version}/build
%if %{with debug}
export LD_LIBRARY_PATH=%{buildroot}%{_libdir}:%{_libdir}
export OMPI_MCA_rmaps_base_oversubscribe=yes
ctest3 --force-new-ctest-process -VV -j1 --output-on-failure --debug
%else
export LD_LIBRARY_PATH=%{buildroot}%{_libdir}:%{_libdir}
export OMPI_MCA_rmaps_base_oversubscribe=yes
ctest3 --force-new-ctest-process -j1
%endif
popd
%endif
## if with_sercheck

%files
%license sundials-%{version}/LICENSE
%doc sundials-%{version}/README.md
%doc sundials-%{version}/src/README-arkode.md
%doc sundials-%{version}/src/README-cvode.md
%doc sundials-%{version}/src/README-cvodes.md
%doc sundials-%{version}/src/README-ida.md
%doc sundials-%{version}/src/README.idas.md
%doc sundials-%{version}/src/README-kinsol.md
%doc sundials-%{version}/NOTICE
%{_libdir}/libsundials_ida*.so.*
%{_libdir}/libsundials_cvode*.so.*
%{_libdir}/libsundials_arkode*.so.*
%{_libdir}/libsundials_kinsol.so.*
%{_libdir}/libsundials_nvecserial.so.*
%{_libdir}/libsundials_nvecopenmp.so.*
%{_libdir}/libsundials_nvecmanyvector.so.*
%if %{with pthread}
%{_libdir}/libsundials_nvecpthreads.so.*
%endif
%{_libdir}/libsundials_sunmatrix*.so.*
%{_libdir}/libsundials_sunlinsol*.so.*
%{_libdir}/libsundials_sunnonlinsol*.so.*
%if 0%{?with_fortran}
%{_libdir}/libsundials_f*_mod.so.*
%{_libdir}/libsundials_fnvecserial.so.*
%{_libdir}/libsundials_fnvecopenmp.so.*
%if %{with pthread}
%{_libdir}/libsundials_fnvecpthreads.so.*
%endif
%{_libdir}/libsundials_fsunlinsolband.so.*
%{_libdir}/libsundials_fsunlinsoldense.so.*
#%{_libdir}/libsundials_fsunlinsolklu.so.*
%{_libdir}/libsundials_fsunlinsolpcg.so.*
%{_libdir}/libsundials_fsunlinsolspbcgs.so.*
%{_libdir}/libsundials_fsunlinsolspfgmr.so.*
%{_libdir}/libsundials_fsunlinsolspgmr.so.*
%{_libdir}/libsundials_fsunlinsolsptfqmr.so.*
%if 0%{?with_superlumt}
%{_libdir}/libsundials_fsunlinsolsuperlumt.so.*
%endif
%{_libdir}/libsundials_fsunmatrixband.so.*
%{_libdir}/libsundials_fsunmatrixdense.so.*
%{_libdir}/libsundials_fsunmatrixsparse.so.*
%{_libdir}/libsundials_fsunnonlinsolfixedpoint.so.*
%{_libdir}/libsundials_fsunnonlinsolnewton.so.*
%endif

%files devel
%{_libdir}/libsundials_ida*.so
%{_libdir}/libsundials_cvode*.so
%{_libdir}/libsundials_arkode*.so
%{_libdir}/libsundials_kinsol.so
%{_libdir}/libsundials_nvecserial.so
%{_libdir}/libsundials_nvecopenmp.so
%{_libdir}/libsundials_nvecmanyvector.so
%if %{with pthread}
%{_libdir}/libsundials_nvecpthreads.so
%endif
%{_libdir}/libsundials_sunmatrix*.so
%{_libdir}/libsundials_sunlinsol*.so
%{_libdir}/libsundials_sunnonlinsol*.so
%if 0%{?with_fortran}
%{_includedir}/sundials/sundials_fconfig.h
%{_libdir}/libsundials_f*_mod.so
%{_fmoddir}/%{name}/
%{_libdir}/libsundials_fnvecserial.so
%{_libdir}/libsundials_fnvecopenmp.so
%if %{with pthread}
%{_libdir}/libsundials_fnvecpthreads.so
%endif
%{_libdir}/libsundials_fsunlinsolband.so
%{_libdir}/libsundials_fsunlinsoldense.so
#%{_libdir}/libsundials_fsunlinsolklu.so
%{_libdir}/libsundials_fsunlinsolpcg.so
%{_libdir}/libsundials_fsunlinsolspbcgs.so
%{_libdir}/libsundials_fsunlinsolspfgmr.so
%{_libdir}/libsundials_fsunlinsolspgmr.so
%{_libdir}/libsundials_fsunlinsolsptfqmr.so
%if 0%{?with_superlumt}
%{_libdir}/libsundials_fsunlinsolsuperlumt.so
%endif
%{_libdir}/libsundials_fsunmatrixband.so
%{_libdir}/libsundials_fsunmatrixdense.so
%{_libdir}/libsundials_fsunmatrixsparse.so
%{_libdir}/libsundials_fsunnonlinsolfixedpoint.so
%{_libdir}/libsundials_fsunnonlinsolnewton.so
%endif
%{_includedir}/nvector/
%{_includedir}/sunmatrix/
%{_includedir}/sunlinsol/
%{_includedir}/sunnonlinsol/
%{_includedir}/arkode/
%{_includedir}/cvode/
%{_includedir}/cvodes/
%{_includedir}/ida/
%{_includedir}/idas/
%{_includedir}/kinsol/
%{_includedir}/sundials/

%files doc
%license sundials-%{version}/LICENSE
%doc sundials-%{version}/README.md
%doc sundials-%{version}/NOTICE
%doc sundials-%{version}/doc/cvode/cv_guide.pdf
%doc sundials-%{version}/doc/kinsol/kin_guide.pdf
%doc sundials-%{version}/doc/cvodes/cvs_guide.pdf
%doc sundials-%{version}/doc/ida/ida_guide.pdf
%doc sundials-%{version}/doc/arkode/*

%changelog
* Wed May 12 2021 caodongxia <caodongxia@huawei.com> - 5.3.0-1
- Package init
