# ViennaRNA.spec
#
# Copyright (c) 1994-2024 Ivo Hofacker, Peter Stadler, Ronny Lorenz rna@tbi.univie.ac.at
#

%global with_rnalocmin 1

%{!?_pkgdocdir: %global _pkgdocdir %%{_docdir}/%{name}}

Name:           viennarna
Version:        @PACKAGE_VERSION@
Release:        1%{?dist}
Summary:        RNA Secondary Structure Prediction and Comparison
Provides:       ViennaRNA = %{version}-%{release}

Vendor:         Ivo Hofacker, TBI - University of Vienna
Packager:       Ronny Lorenz <ronny@tbi.univie.ac.at>
Distribution:   viennarna-package

Group:          Productivity/Scientific/Chemistry
License:        Free for non commercial use.
URL:            @PACKAGE_URL@
Source0:        https://www.tbi.univie.ac.at/RNA/packages/source/ViennaRNA-%{version}.tar.gz
BuildRoot:      %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)

BuildRequires:  autoconf
BuildRequires:  automake
BuildRequires:  libtool
BuildRequires:  rpm-devel

BuildRequires:  libstdc++-devel
BuildRequires:  gcc gcc-c++ glibc-devel info
BuildRequires:  gsl-devel
BuildRequires:  mpfr-devel
BuildRequires:  perl perl(ExtUtils::Embed)
BuildRequires:  perl-Test-Simple

BuildRequires:  perl(FileHandle)

Requires:       perl glibc info gsl lapack
%if 0%{?suse_version}
BuildRequires:  gcc-fortran
BuildRequires:  lapack-devel lapacke-devel
%if 0%{?suse_version} >= 1500
Requires:       libmpfr6
%else
Requires:       libmpfr4
%endif
%else
BuildRequires:	gcc-gfortran
BuildRequires:  lapack-devel
Requires:       mpfr
%endif

Provides:       Kinfold
Provides:       kinwalker

# work-arround, maybe removed later
%undefine __brp_remove_la_files

BuildRequires:  python3-devel python3


%{!?__python3: %global __python3 /usr/bin/python3}

%{!?python3_sitelib: %global python3_sitelib %(%{__python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}
%{!?python3_sitearch: %global python3_sitearch %(%{__python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
%global python3_modext %(%{__python3} -c "from distutils.sysconfig import get_config_vars; print(get_config_vars('SO')[[0]])")

%global __provides_exclude_from ^%{python3_sitearch}/.*\\.so$

%endif
%description
The ViennaRNA package consists of a library and several standalone
programs for RNA secondary structure analysis. It includes algorithms
for predicting optimal and suboptimal secondary structures, base pair
probabilities and partition functions, for comparing secondary
structures, and the design of RNA sequences with a desired structure.

%package devel
Summary:  Library and header files for ViennaRNA RNAlib
Group:    Development/Libraries/C and C++
Provides: libRNA.a = %{version}-%{release}
Requires: %{name} = %{version}-%{release}
Requires: libstdc++-devel
Requires: pkgconfig
Requires: gsl-devel
Requires: mpfr-devel

%description devel
Header files for ViennaRNA.


%package -n perl-rna
Summary:  Perl binding for ViennaRNA RNAlib
Group:    Development/Libraries/Perl
Requires: %{name} = %{version}-%{release}
Requires: perl
%description -n perl-rna
Perl binding for ViennaRNA RNAlib.


%package -n python3-rna
Summary:  Python 3 bindings for ViennaRNA RNAlib
Group:    Development/Libraries/Python
Requires: %{name} = %{version}-%{release}
Requires: python3
%description -n python3-rna
Python 3 bindings for ViennaRNA RNAlib.


%prep
%setup -n ViennaRNA-%{version} -q

%build

%if 0%{?with_rnalocmin}

%if 0%{?suse_version}
%configure \
    --disable-lto \
    --with-cluster \
    --without-forester \
    --with-kinwalker \
    --docdir=%{_pkgdocdir} \
    INSTALLDIRS=vendor \
    PERL=/usr/bin/perl
%else
%configure \
    --disable-lto \
    --with-cluster \
    --without-forester \
    --with-kinwalker \
    --docdir=%{_pkgdocdir} \
    INSTALLDIRS=vendor \
    PERL=/usr/bin/perl
%endif

%else

%{build_cflags} -fno-lto
%{build_cxxflags} -fno-lto
%{build_ldflags} -fno-lto

%configure \
    --disable-lto \
    --with-cluster \
    --without-forester \
    --without-rnalocmin \
    --with-kinwalker \
    --docdir=%{_pkgdocdir} \
    INSTALLDIRS=vendor \
    PERL=/usr/bin/perl
%endif

make %{?_smp_mflags} V=1


########################################
# Perl5 test-RNA-Design.t fails on
# RedHat/CentOS/Scientific Linux < 7 due
# to old perl-Test-Simple package
########################################
%if 0%{?centos_version} >= 700 || %{defined el7} || 0%{?fedora} || 0%{?is_opensuse}
%check
make %{?_smp_mflags} check
%endif


%install
rm -rf $RPM_BUILD_ROOT

%{__make} install DESTDIR=$RPM_BUILD_ROOT

# crude bugfix for info clash
rm -f $RPM_BUILD_ROOT/usr/share/info/dir

########################################
# Stripping of our static LTO libRNA.a
# totally destroys the library, i.e.
# linking against libRNA fails with lots
# of 'undefined reference' errors.
# To avoid this, we can either turn off
# LTO at compile time, or disable
# stripping of the static library itself
#
# We do the latter here!
########################################

# Extract %%__os_install_post into os_install_post~
cat << \EOF > os_install_post~
%__os_install_post
EOF

# remove stripping of static libraries
sed -i -e 's!/usr/lib/rpm[^[:space:]]*/brp-strip-static-archive[[:space:]]%{__strip}!!g' os_install_post~
sed -i -e 's!/usr/lib/rpm[^[:space:]]*/brp-strip-static-archive!!g' os_install_post~

# openSUSE does not call 'brp-strip-static-archive' but
# calls its own brp_suse script, which by default strips
# debug symbols from binary files. Luckily, it provides an
# environment variable to turn off stripping debug symbols
%if 0%{?suse_version}
# get rid of /usr/lib/rpm/brp-strip-debug
export NO_BRP_STRIP_DEBUG=true
%endif

%define __os_install_post . ./os_install_post~

########################################
# The above should ensure not to
# accidentally strip libRNA.a
########################################

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root,-)
%{_bindir}/*
%dir %{_datadir}/ViennaRNA
%{_datadir}/ViennaRNA/bin
%{_datadir}/ViennaRNA/*.par
%{_datadir}/ViennaRNA/*.json
%{_datadir}/ViennaRNA/*.R
%{_datadir}/ViennaRNA/*.gri
%{_mandir}/man1/RNAplfold.1.gz
%{_mandir}/man1/AnalyseSeqs.1.gz
%{_mandir}/man1/Kinfold.1.gz
%{_mandir}/man1/RNApdist.1.gz
%{_mandir}/man1/RNAduplex.1.gz
%{_mandir}/man1/RNAdos.1.gz
%{_mandir}/man1/RNAup.1.gz
%{_mandir}/man1/RNAheat.1.gz
%{_mandir}/man1/RNAeval.1.gz
%{_mandir}/man1/RNALalifold.1.gz
%{_mandir}/man1/RNAaliduplex.1.gz
%{_mandir}/man1/RNAsubopt.1.gz
%{_mandir}/man1/RNALfold.1.gz
%{_mandir}/man1/RNAfold.1.gz
%{_mandir}/man1/RNApvmin.1.gz
%{_mandir}/man1/AnalyseDists.1.gz
%{_mandir}/man1/RNAsnoop.1.gz
%{_mandir}/man1/RNAplot.1.gz
%{_mandir}/man1/RNAalifold.1.gz
%{_mandir}/man1/RNAinverse.1.gz
%{_mandir}/man1/RNAPKplex.1.gz
%{_mandir}/man1/RNAparconv.1.gz
%{_mandir}/man1/RNAcofold.1.gz
%{_mandir}/man1/RNA2Dfold.1.gz
%{_mandir}/man1/RNApaln.1.gz
%{_mandir}/man1/RNAdistance.1.gz
%{_mandir}/man1/RNAplex.1.gz
%{_mandir}/man1/RNAmultifold.1.gz
%{_mandir}/man1/ct2db.1.gz
%{_mandir}/man1/kinwalker.1.gz
%{_mandir}/man1/RNAxplorer.1.gz
%if 0%{?with_rnalocmin}
%{_mandir}/man1/RNAlocmin.1.gz
%endif

%files devel
%defattr(-,root,root,-)
%{_libdir}/libRNA.a
%{_libdir}/libRNA.la
%{_includedir}/ViennaRNA
%{_libdir}/pkgconfig/RNAlib2.pc
%{_datadir}/ViennaRNA/viennarna.bib
%{_datadir}/ViennaRNA/DoxygenLayout.xml
%{_datadir}/ViennaRNA/examples
%doc %{_pkgdocdir}
%doc /usr/share/info/RNAlib.info.gz

%files -n perl-rna
%defattr(644,root,root,755)
%{perl_vendorlib}/RNA.pm
%{perl_vendorlib}/RNA
%dir %{perl_vendorarch}/auto/RNA
%attr(755,root,root) %{perl_vendorarch}/auto/RNA/RNA.so


%files -n python3-rna
%defattr(-,root,root,-)
%dir %{python3_sitearch}/RNA
%dir %{python3_sitearch}/ViennaRNA
%{python3_sitearch}/RNA/*
%{python3_sitearch}/ViennaRNA/*
%if 0%{?suse_version}
%pycache_only %{python3_sitearch}/RNA/__pycache__
%endif
