##
##  kaldi.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         kaldi
Summary:      Speech Recognition Toolkit
URL:          http://kaldi-asr.org/
Vendor:       Yanmin Qian et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Audio
License:      Apache
Version:      20180901
Release:      20180901

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/kaldi/kaldi-%{version}.tar.xz
Source1:      kaldi.sh
Patch0:       kaldi.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes, gcc::with_fortran = yes
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  openfst, openblas, speex, libexecinfo
PreReq:       openfst, openblas, speex, libexecinfo

%description
    Kaldi is an advanced toolkit for speech recognition. Important
    features include: Code-level integration with Finite State
    Transducers (FSTs); Extensive linear algebra support; Extensible
    design and Complete recipes.

%track
    prog kaldi = {
        version   = %{version}
        url       = http://download.openpkg.org/components/versioned/kaldi/
        regex     = kaldi-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n kaldi
    %patch -p0

%build
    #   adjust paths to GNU bash
    %{l_shtool} subst \
        -e 's;/bin/bash;%{l_bash};g' \
        src/Makefile \
        src/configure \
        tools/extras/*.sh

    #   adjust for linking on non-Linux platforms
    case "%{l_platform -t}" in
        linux* ) ;;
        * ) %{l_shtool} subst -e 's;-ldl;;g' src/configure src/makefiles/*.mk ;;
    esac

    #   adjust for special compiler/linker flags
    %{l_shtool} subst \
        -e 's; -g ;;g' \
        -e 's;-Wall;;g' \
        -e 's;-lpthread;-lpthread -lexecinfo;g' \
        -e 's;-lgfortran;-lgfortran -lquadmath;g' \
        src/configure \
        src/makefiles/*.mk
    %{l_shtool} subst \
        -e 's;-std=c++0x;-std=c++14;' \
        src/configure

    #   configure package
    cd src
    CC="%{l_cc}" \
    CXX="%{l_cxx}" \
    CFLAGS="%{l_cflags -O}" \
    CXXFLAGS="%{l_cxxflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    %{l_bash} ./configure \
        --static \
        --mathlib=OPENBLAS \
        --openblas-root=%{l_prefix} \
        --static-fst=yes \
        --fst-root=%{l_prefix} \
        --static-math=yes \
        --threaded-math=no \
        --use-cuda=no

    #   build package
    %{l_make} %{l_mflags -O} \
        EXTRA_CXXFLAGS="%{l_cxxflags -O} -std=c++14 -DHAVE_OPENFST_GE_10400 -I%{l_prefix}/lib/openfst-kaldi/include %{l_cppflags}"

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/kaldi

    #   install executables
    for dir in \
        bin fstbin gmmbin fgmmbin sgmmbin sgmm2bin featbin nnetbin \
        nnet2bin nnet3bin latbin ivectorbin lmbin kwsbin online2bin; do
        for file in src/$dir/*; do
            case "$file" in
                *.cc ) ;;
                * )
                    if [ ! -x $file ]; then
                        continue
                    fi
                    %{l_shtool} install -c -s -m 755 \
                        $file $RPM_BUILD_ROOT%{l_prefix}/libexec/kaldi/
                    ;;
            esac
        done
    done

    #   install wrapper command
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE kaldi.sh} $RPM_BUILD_ROOT%{l_prefix}/bin/kaldi

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

