##
##  cntk.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:         cntk
Summary:      Computational Network ToolKit (CNTK)
URL:          https://github.com/Microsoft/CNTK
Vendor:       Microsoft
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Algorithm
License:      BSD-style
Version:      20170114
Release:      20170114

#   package options
%option       with_opencv  no

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

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  openmpi, openmpi::with_cxx = yes, openblas, libzip, boost, protobuf
PreReq:       openmpi, openmpi::with_cxx = yes, openblas, libzip, boost, protobuf
%if "%{with_opencv}" == "yes"
BuildPreReq:  opencv
PreReq:       opencv
%endif

%description
    CNTK, the Computational Network Toolkit by Microsoft Research, is a
    unified deep-learning toolkit that describes neural networks as a
    series of computational steps via a directed graph. In this directed
    graph, leaf nodes represent input values or network parameters,
    while other nodes represent matrix operations upon their inputs.
    CNTK allows to easily realize and combine popular model types such
    as feed-forward DNNs, convolutional nets (CNNs), and recurrent
    networks (RNNs/LSTMs). It implements stochastic gradient descent
    (SGD, error backpropagation) learning with automatic differentiation
    and parallelization across multiple GPUs and servers.

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

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

%build
    #   patch the build tools
    %{l_shtool} subst \
        -e 's;/bin/bash;%{l_bash};g' \
        Tools/build-and-test \
        Tools/generate_build_info \
        Tools/make_binary_drop_linux

    #   patch the build procedure
    case "%{l_platform -t}" in
        *-linux* ) ;;
        *        ) %{l_shtool} subst -e 's; -ldl;;g' Makefile ;;
    esac

    #   configure the toolkit
    CC="%{l_cc}" \
    CXX="%{l_cxx}" \
    CFLAGS="%{l_cflags -O}" \
    CXXFLAGS="%{l_cxxflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    %{l_bash} ./configure \
        --with-buildtype=release \
        --with-openblas=%{l_prefix} \
%if "%{with_opencv}" == "yes"
        --with-opencv=%{l_prefix} \
%endif
        --with-libzip \
        --with-code-coverage=no \
        --asgd=no \
        --cuda=no

    #   build the toolkit
    %{l_make} %{l_mflags -O}

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

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

    #   install the toolkit binaries
    %{l_shtool} install -c -m 755 \
        bin/cntk $RPM_BUILD_ROOT%{l_prefix}/libexec/cntk/
    %{l_shtool} install -c -m 644 \
        bin/cntk.core.bs $RPM_BUILD_ROOT%{l_prefix}/libexec/cntk/

    #   install the toolkit libraries
    %{l_shtool} install -c -m 644 \
        lib/* $RPM_BUILD_ROOT%{l_prefix}/lib/cntk/

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

%files -f files

%clean

